annotate tabular_to_fasta.py @ 0:db61adc12770 draft

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