annotate fasta_compute_length.py @ 6:5cbde03c1103 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:55 +0000
parents e12f68d2cc4e
children
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 """
6
5cbde03c1103 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
5cbde03c1103 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
5cbde03c1103 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
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
ece409f6573c Imported from capsule None
devteam
parents:
diff changeset
9
4
e12f68d2cc4e "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
5cbde03c1103 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):
5cbde03c1103 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)
5cbde03c1103 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 = ''
5cbde03c1103 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
5cbde03c1103 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 9f0b8eb9fd7cf9e2513e7c822767153dbd4bc3b2
devteam
parents: 4
diff changeset
15
5cbde03c1103 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
5cbde03c1103 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:
5cbde03c1103 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
5cbde03c1103 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:
5cbde03c1103 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
5cbde03c1103 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 9f0b8eb9fd7cf9e2513e7c822767153dbd4bc3b2
devteam
parents: 4
diff changeset
21
5cbde03c1103 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
5cbde03c1103 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:
5cbde03c1103 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:
5cbde03c1103 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()
5cbde03c1103 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('#'):
5cbde03c1103 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
5cbde03c1103 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] == '>':
5cbde03c1103 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:
5cbde03c1103 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:
5cbde03c1103 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]
5cbde03c1103 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))
5cbde03c1103 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:
5cbde03c1103 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
5cbde03c1103 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
5cbde03c1103 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
5cbde03c1103 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:
5cbde03c1103 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)
5cbde03c1103 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 9f0b8eb9fd7cf9e2513e7c822767153dbd4bc3b2
devteam
parents: 4
diff changeset
39
5cbde03c1103 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
5cbde03c1103 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:
5cbde03c1103 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]
5cbde03c1103 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))
5cbde03c1103 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 9f0b8eb9fd7cf9e2513e7c822767153dbd4bc3b2
devteam
parents: 4
diff changeset
44
5cbde03c1103 planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/fasta_compute_length commit 9f0b8eb9fd7cf9e2513e7c822767153dbd4bc3b2
devteam
parents: 4
diff changeset
45
5cbde03c1103 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__":
5cbde03c1103 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')