-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodonAlign2.py
More file actions
executable file
·62 lines (54 loc) · 2.11 KB
/
Copy pathCodonAlign2.py
File metadata and controls
executable file
·62 lines (54 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Codon_Align
# Take unaligned fasta nucleotide file and fasta file of their
# amino acid translations and create a codon-aligned nucleotide fasta
# modified from https://github.com/santiagosnchez/CodonAlign.git
import sys
import warnings
warnings.simplefilter("ignore")
try:
from Bio import AlignIO
from Bio import SeqIO
from Bio import codonalign
except ImportError:
print(
"Biopython (AlignIO, SeqIO) may not be installed. Try with: conda install biopython, or pip install biopython"
)
sys.exit()
else:
# read data
# alignment of amino acid sequences
PRO_ALN = AlignIO.read("primates_protein_mafft.fas", format="fasta")
# unaligned nucleotide seqs matching those in aa alignment
NUC_SEQS = list(SeqIO.parse("primates.fas", format="fasta"))
# set outfile for codon alignment
CODON_OUTFILE = "primates_codons.fas"
# get seq names from input files
PRO_NAMES = [s.name for s in PRO_ALN]
NUC_NAMES = [s.name for s in NUC_SEQS]
# check that number of seqs match in aa and nucleotide files
if len(PRO_NAMES) != len(NUC_NAMES):
sys.exit("Number of sequences in both files does not match.")
# check names match between nucleotide and amino acid files
if all([name in PRO_NAMES for name in NUC_NAMES]) and all(
[name in NUC_NAMES for name in PRO_NAMES]
):
# build codon alignment
try:
CODON_ALN = codonalign.build(PRO_ALN, NUC_SEQS)
except:
print(
"Could not generate codon alignment. Sequences probably include ambiguous or missing data.",
file=sys.stderr,
)
else:
# delete the <unknown description> label
for i, _ in enumerate(CODON_ALN):
CODON_ALN[i].description = ""
# write to file
AlignIO.write(CODON_ALN, CODON_OUTFILE, "fasta")
print(
f"{len(PRO_NAMES)} aligned CDS sequences saved to {CODON_OUTFILE}.",
file=sys.stderr,
)
else:
sys.exit("Amino acid and nucleotide sequences do not have the same labels.")