annotate tools/fasta_tools/tabular_to_fasta.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 Input: fasta, minimal length, maximal length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Output: fasta
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 Return sequences whose lengths are within the range.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys, os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 title_col = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 seq_col = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 outfile = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if title_col == None or title_col == 'None' or seq_col == None or seq_col == 'None':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 stop_err( "Columns not specified." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 seq_col = int( seq_col ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 stop_err( "Invalid Sequence Column: %s." %str( seq_col ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 title_col_list = title_col.split( ',' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 out = open( outfile, 'w' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 invalid_line = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for i, line in enumerate( open( infile ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 error = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 fasta_title = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 for j in title_col_list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 j = int( j ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 fasta_title.append( fields[j] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if not invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 first_invalid_line = i + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 invalid_line = line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 error = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if not error:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 fasta_seq = fields[seq_col]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if fasta_title[0].startswith( ">" ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 fasta_title[0] = fasta_title[0][1:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 print >> out, ">%s\n%s" % ( "_".join( fasta_title ), fasta_seq )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if not invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 first_invalid_line = i + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 invalid_line = line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 if skipped_lines > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 print 'Data issue: skipped %d blank or invalid lines starting at #%d: "%s"' % ( skipped_lines, first_invalid_line, invalid_line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if __name__ == "__main__" : __main__()