annotate utils/fasta_to_len.py @ 3:2051602a5f97 draft

"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 6ba0996437dfa7c51f3c2d2ded79dd85ad099b65"
author devteam
date Wed, 11 Sep 2019 09:41:59 -0400
parents ece409f6573c
children e12f68d2cc4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
2 """
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
3 Input: fasta, int
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
4 Output: tabular
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
5 Return titles with lengths of corresponding seq
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
6 """
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
7
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
8 import sys, os
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
9
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
10 assert sys.version_info[:2] >= ( 2, 4 )
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
11
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
12 def compute_fasta_length( fasta_file, out_file, keep_first_char, keep_first_word=False ):
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
13
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
14 infile = fasta_file
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
15 out = open( out_file, 'w')
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
16 keep_first_char = int( keep_first_char )
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
17
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
18 fasta_title = ''
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
19 seq_len = 0
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
20
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
21 # number of char to keep in the title
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
22 if keep_first_char == 0:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
23 keep_first_char = None
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
24 else:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
25 keep_first_char += 1
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
26
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
27 first_entry = True
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
28
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
29 for line in open( infile ):
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
30 line = line.strip()
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
31 if not line or line.startswith( '#' ):
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
32 continue
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
33 if line[0] == '>':
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
34 if first_entry == False:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
35 if keep_first_word:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
36 fasta_title = fasta_title.split()[0]
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
37 out.write( "%s\t%d\n" % ( fasta_title[ 1:keep_first_char ], seq_len ) )
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
38 else:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
39 first_entry = False
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
40 fasta_title = line
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
41 seq_len = 0
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
42 else:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
43 seq_len += len(line)
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
44
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
45 # last fasta-entry
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
46 if keep_first_word:
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
47 fasta_title = fasta_title.split()[0]
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
48 out.write( "%s\t%d\n" % ( fasta_title[ 1:keep_first_char ], seq_len ) )
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
49 out.close()
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
50
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
51 if __name__ == "__main__" :
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
52 compute_fasta_length( sys.argv[1], sys.argv[2], sys.argv[3], True )