annotate fasta_to_tabular.py @ 1:5cabbe4cfaf4 draft

planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author devteam
date Tue, 13 Oct 2015 12:20:15 -0400
parents ae709fd50581
children ff4751ce764d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
2 # This code exists in 2 places: ~/datatypes/converters and ~/tools/fasta_tools
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
3 """
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
4 Input: fasta (input file), tabular (output file), int (truncation of id), int (columns from description)
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
5 Output: tabular
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
6 format convert: fasta to tabular
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
7 """
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
8
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
9 import sys, os
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
10
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
11 def stop_err( msg ):
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
12 sys.stderr.write( msg )
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
13 sys.exit()
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
14
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
15 def __main__():
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
16 if len(sys.argv) != 5:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
17 stop_err("Wrong number of argument. Expect four (fasta, tabular, truncation, columns)")
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
18 infile = sys.argv[1]
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
19 outfile = sys.argv[2]
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
20 keep_first = int( sys.argv[3] )
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
21 descr_split = int( sys.argv[4] )
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
22 fasta_title = fasta_seq = ''
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
23 if keep_first == 0:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
24 keep_first = None
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
25 elif descr_split == 1:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
26 #Added one for the ">" character
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
27 #(which is removed if using descr_split > 1)
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
28 keep_first += 1
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
29 if descr_split < 1:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
30 stop_err("Bad description split value (should be 1 or more)")
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
31 out = open( outfile, 'w' )
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
32 for i, line in enumerate( open( infile ) ):
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
33 line = line.rstrip( '\r\n' )
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
34 if not line or line.startswith( '#' ):
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
35 continue
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
36 if line.startswith( '>' ):
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
37 #Don't want any existing tabs to trigger extra columns:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
38 line = line.replace('\t', ' ')
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
39 if i > 0:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
40 out.write('\n')
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
41 if descr_split == 1:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
42 out.write(line[1:keep_first])
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
43 else:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
44 words = line[1:].split(None, descr_split-1)
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
45 #apply any truncation to first word (the id)
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
46 words[0] = words[0][0:keep_first]
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
47 #pad with empty columns if required
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
48 words += [""]*(descr_split-len(words))
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
49 out.write("\t".join(words))
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
50 out.write('\t')
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
51 else:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
52 out.write(line)
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
53 if i > 0:
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
54 out.write('\n')
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
55 out.close()
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
56
ae709fd50581 Imported from capsule None
devteam
parents:
diff changeset
57 if __name__ == "__main__" : __main__()