Mercurial > repos > devteam > tabular_to_fasta
comparison tabular_to_fasta.py @ 1:0a7799698fe5 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 10:51:52 -0500 |
parents | 0b4e36026794 |
children |
comparison
equal
deleted
inserted
replaced
0:0b4e36026794 | 1:0a7799698fe5 |
---|---|
2 """ | 2 """ |
3 Input: fasta, minimal length, maximal length | 3 Input: fasta, minimal length, maximal length |
4 Output: fasta | 4 Output: fasta |
5 Return sequences whose lengths are within the range. | 5 Return sequences whose lengths are within the range. |
6 """ | 6 """ |
7 import sys, os | 7 import os |
8 import sys | |
8 | 9 |
9 assert sys.version_info[:2] >= ( 2, 4 ) | |
10 | 10 |
11 def stop_err( msg ): | 11 def stop_err(msg): |
12 sys.stderr.write( msg ) | 12 sys.exit(msg) |
13 sys.exit() | 13 |
14 | 14 |
15 def __main__(): | 15 def __main__(): |
16 infile = sys.argv[1] | 16 infile = sys.argv[1] |
17 title_col = sys.argv[2] | 17 title_col = sys.argv[2] |
18 seq_col = sys.argv[3] | 18 seq_col = sys.argv[3] |
19 outfile = sys.argv[4] | 19 outfile = sys.argv[4] |
20 | 20 |
21 if title_col == None or title_col == 'None' or seq_col == None or seq_col == 'None': | 21 if title_col == None or title_col == 'None' or seq_col == None or seq_col == 'None': |
22 stop_err( "Columns not specified." ) | 22 stop_err("Columns not specified.") |
23 try: | 23 try: |
24 seq_col = int( seq_col ) - 1 | 24 seq_col = int(seq_col) - 1 |
25 except: | 25 except: |
26 stop_err( "Invalid Sequence Column: %s." %str( seq_col ) ) | 26 stop_err("Invalid Sequence Column: %s." % str(seq_col)) |
27 | 27 |
28 title_col_list = title_col.split( ',' ) | 28 title_col_list = title_col.split(',') |
29 out = open( outfile, 'w' ) | |
30 skipped_lines = 0 | 29 skipped_lines = 0 |
31 first_invalid_line = 0 | 30 first_invalid_line = 0 |
32 invalid_line = "" | 31 invalid_line = "" |
33 i = 0 | 32 i = 0 |
34 | 33 |
35 for i, line in enumerate( open( infile ) ): | 34 with open(outfile, 'w') as out: |
36 error = False | 35 for i, line in enumerate(open(infile)): |
37 line = line.rstrip( '\r\n' ) | 36 error = False |
38 if line and not line.startswith( '#' ): | 37 line = line.rstrip('\r\n') |
39 fields = line.split( '\t' ) | 38 if line and not line.startswith('#'): |
40 fasta_title = [] | 39 fields = line.split('\t') |
41 for j in title_col_list: | 40 fasta_title = [] |
42 try: | 41 for j in title_col_list: |
43 j = int( j ) - 1 | 42 try: |
44 fasta_title.append( fields[j] ) | 43 j = int(j) - 1 |
45 except: | 44 fasta_title.append(fields[j]) |
46 skipped_lines += 1 | 45 except: |
47 if not invalid_line: | 46 skipped_lines += 1 |
48 first_invalid_line = i + 1 | 47 if not invalid_line: |
49 invalid_line = line | 48 first_invalid_line = i + 1 |
50 error = True | 49 invalid_line = line |
51 break | 50 error = True |
52 if not error: | 51 break |
53 try: | 52 if not error: |
54 fasta_seq = fields[seq_col] | 53 try: |
55 if fasta_title[0].startswith( ">" ): | 54 fasta_seq = fields[seq_col] |
56 fasta_title[0] = fasta_title[0][1:] | 55 if fasta_title[0].startswith(">"): |
57 print >> out, ">%s\n%s" % ( "_".join( fasta_title ), fasta_seq ) | 56 fasta_title[0] = fasta_title[0][1:] |
58 except: | 57 print(">%s\n%s" % ("_".join(fasta_title), fasta_seq), file=out) |
59 skipped_lines += 1 | 58 except: |
60 if not invalid_line: | 59 skipped_lines += 1 |
61 first_invalid_line = i + 1 | 60 if not invalid_line: |
62 invalid_line = line | 61 first_invalid_line = i + 1 |
63 out.close() | 62 invalid_line = line |
64 | 63 |
65 if skipped_lines > 0: | 64 if skipped_lines > 0: |
66 print 'Data issue: skipped %d blank or invalid lines starting at #%d: "%s"' % ( skipped_lines, first_invalid_line, invalid_line ) | 65 print('Data issue: skipped %d blank or invalid lines starting at #%d: "%s"' % (skipped_lines, first_invalid_line, invalid_line)) |
67 | 66 |
68 if __name__ == "__main__" : __main__() | 67 |
68 if __name__ == "__main__": | |
69 __main__() |