annotate tabular_to_fasta.py @ 0:0b4e36026794 draft

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