annotate remove_terminal_stop_codons.py @ 0:0290a7285026 draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
author iuc
date Fri, 05 Dec 2025 23:22:35 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
1 #!/usr/bin/env python3
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
2 """
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
3 Remove terminal stop codons from coding sequences.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
4
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
5 Trim all terminal stop codons from sequences in a FASTA file, using a chosen
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
6 NCBI genetic code (translation table). Leave non-stop terminal codons alone.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
7 If any INTERNAL, in-frame stop codon is found, exit with an error.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
8
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
9 This tool is designed as a preprocessing step for tools like cawlign and HyPhy
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
10 that do not permit internal stop codons in their input sequences.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
11
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
12 Requires: Biopython
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
13 """
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
14
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
15 import argparse
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
16 import sys
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
17
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
18 from Bio import SeqIO
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
19 from Bio.Data import CodonTable
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
20 from Bio.Seq import Seq
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
21
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
22
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
23 def load_table(table_arg):
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
24 """Return a DNA codon table from an NCBI table id (int) or name (str)."""
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
25 if table_arg is None:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
26 return CodonTable.unambiguous_dna_by_id[1] # Standard
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
27 # try as integer id
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
28 try:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
29 tid = int(table_arg)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
30 return CodonTable.unambiguous_dna_by_id[tid]
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
31 except (ValueError, KeyError):
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
32 pass
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
33 # try as name
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
34 try:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
35 return CodonTable.unambiguous_dna_by_name[table_arg]
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
36 except KeyError:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
37 # Build a helpful hint list
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
38 valid_ids = sorted(CodonTable.unambiguous_dna_by_id.keys())
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
39 valid_names = sorted(CodonTable.unambiguous_dna_by_name.keys())
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
40 sys.stderr.write(
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
41 f"ERROR: Unknown genetic code '{table_arg}'.\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
42 f"Try an NCBI table id (e.g., 1) or one of these names:\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
43 f" {', '.join(valid_names)}\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
44 f"(Valid ids include: {', '.join(map(str, valid_ids))})\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
45 )
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
46 sys.exit(2)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
47
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
48
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
49 def trim_terminal_stops_and_validate(record, stop_codons, check_internal=True):
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
50 """
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
51 Remove ALL trailing stop codons (0+ at the end).
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
52
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
53 If check_internal is True and any internal in-frame stop codon exists
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
54 (excluding the trailing block), exit with an error message.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
55
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
56 Ignore a terminal codon that is not a stop codon.
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
57 """
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
58 # Work with DNA letters; treat any RNA U as T
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
59 seq_str = str(record.seq).upper().replace("U", "T")
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
60
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
61 # Count how many full codons sit at the end that are stops
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
62 idx = len(seq_str)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
63 trailing_stops = 0
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
64 while idx >= 3:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
65 codon = seq_str[idx - 3:idx]
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
66 if codon in stop_codons:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
67 trailing_stops += 1
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
68 idx -= 3
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
69 else:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
70 break
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
71
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
72 # Scan for INTERNAL stops: all complete codons up to (but not including)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
73 # the trailing stop block (and ignoring any trailing partial codon).
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
74 if check_internal:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
75 scan_end = (idx // 3) * 3 # only complete codons
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
76 for pos in range(0, scan_end, 3):
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
77 codon = seq_str[pos:pos + 3]
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
78 if codon in stop_codons:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
79 sys.stderr.write(
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
80 f"ERROR: Found an internal stop codon in sequence "
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
81 f"'{record.id}' at position {pos}.\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
82 f"Tools like HyPhy and cawlign do not permit internal "
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
83 f"stop codons. Please review your input sequences.\n"
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
84 )
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
85 sys.exit(2)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
86
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
87 # Finally, remove the trailing stop codons (if any)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
88 if trailing_stops > 0:
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
89 seq_str = seq_str[:idx]
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
90
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
91 # Leave sequences with non-stop terminal codons unchanged by design
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
92 return Seq(seq_str)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
93
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
94
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
95 def main():
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
96 ap = argparse.ArgumentParser(
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
97 description="Remove all terminal stop codons from a FASTA, using a "
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
98 "chosen genetic code. Optionally fail if any internal "
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
99 "in-frame stop codon is present."
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
100 )
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
101 ap.add_argument("-i", "--input", required=True, help="Input FASTA file")
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
102 ap.add_argument("-o", "--output", required=True, help="Output FASTA file")
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
103 ap.add_argument(
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
104 "-t", "--table",
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
105 help="NCBI translation table id (e.g., 1) or name "
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
106 "(e.g., 'Vertebrate Mitochondrial'). Default: 1 (Standard)."
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
107 )
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
108 ap.add_argument(
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
109 "--no-check-internal",
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
110 action="store_true",
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
111 help="Do not check for internal stop codons (only remove terminal)."
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
112 )
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
113 args = ap.parse_args()
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
114
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
115 table = load_table(args.table)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
116 stop_codons = set(table.stop_codons) # e.g., {'TAA','TAG','TGA'} for Standard
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
117
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
118 check_internal = not args.no_check_internal
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
119
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
120 records_out = []
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
121 for rec in SeqIO.parse(args.input, "fasta"):
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
122 new_seq = trim_terminal_stops_and_validate(rec, stop_codons, check_internal)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
123 rec.seq = new_seq
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
124 records_out.append(rec)
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
125
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
126 SeqIO.write(records_out, args.output, "fasta")
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
127
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
128
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
129 if __name__ == "__main__":
0290a7285026 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/main/tools/remove_terminal_stop_codons commit 0c1c0e260ebecab6beb23fd56322b391e62d12fa
iuc
parents:
diff changeset
130 main()