annotate fasta_compute_length.py @ 6:96c921261e41 draft default tip

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