Mercurial > repos > devteam > tabular_to_fasta
annotate tabular_to_fasta.py @ 1:7f7a1bea4653 draft default tip
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
author | devteam |
---|---|
date | Wed, 05 Feb 2020 15:51:26 +0000 |
parents | db61adc12770 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 Input: fasta, minimal length, maximal length | |
4 Output: fasta | |
5 Return sequences whose lengths are within the range. | |
6 """ | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
7 import os |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
8 import sys |
0 | 9 |
10 | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
11 def stop_err(msg): |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
12 sys.exit(msg) |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
13 |
0 | 14 |
15 def __main__(): | |
16 infile = sys.argv[1] | |
17 title_col = sys.argv[2] | |
18 seq_col = sys.argv[3] | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
19 outfile = sys.argv[4] |
0 | 20 |
21 if title_col == None or title_col == 'None' or seq_col == None or seq_col == 'None': | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
22 stop_err("Columns not specified.") |
0 | 23 try: |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
24 seq_col = int(seq_col) - 1 |
0 | 25 except: |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
26 stop_err("Invalid Sequence Column: %s." % str(seq_col)) |
0 | 27 |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
28 title_col_list = title_col.split(',') |
0 | 29 skipped_lines = 0 |
30 first_invalid_line = 0 | |
31 invalid_line = "" | |
32 i = 0 | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
33 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
34 with open(outfile, 'w') as out: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
35 for i, line in enumerate(open(infile)): |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
36 error = False |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
37 line = line.rstrip('\r\n') |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
38 if line and not line.startswith('#'): |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
39 fields = line.split('\t') |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
40 fasta_title = [] |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
41 for j in title_col_list: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
42 try: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
43 j = int(j) - 1 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
44 fasta_title.append(fields[j]) |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
45 except: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
46 skipped_lines += 1 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
47 if not invalid_line: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
48 first_invalid_line = i + 1 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
49 invalid_line = line |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
50 error = True |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
51 break |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
52 if not error: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
53 try: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
54 fasta_seq = fields[seq_col] |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
55 if fasta_title[0].startswith(">"): |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
56 fasta_title[0] = fasta_title[0][1:] |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
57 print(">%s\n%s" % ("_".join(fasta_title), fasta_seq), file=out) |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
58 except: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
59 skipped_lines += 1 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
60 if not invalid_line: |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
61 first_invalid_line = i + 1 |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
62 invalid_line = line |
0 | 63 |
64 if skipped_lines > 0: | |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
65 print('Data issue: skipped %d blank or invalid lines starting at #%d: "%s"' % (skipped_lines, first_invalid_line, invalid_line)) |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
66 |
0 | 67 |
1
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
68 if __name__ == "__main__": |
7f7a1bea4653
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/tabular_to_fasta commit 01140c0ac1a926856c55853a0028e5d44935d9e6"
devteam
parents:
0
diff
changeset
|
69 __main__() |