-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodon_check.py
61 lines (48 loc) · 1.43 KB
/
codon_check.py
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
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import argparse
import re
start_codon = "ATG"
cinput = open('codons.txt', 'r')
all_codons = {}
for line in cinput.readlines():
a = line.strip().split()
all_codons[a[0]] = a[1]
parser = argparse.ArgumentParser(description='Checking alignment.')
parser.add_argument('-i', '--input', help="file with alignment")
parser.add_argument('-o', '--output', help="path to the output file")
args = parser.parse_args()
alignments = SeqIO.parse(args.input, "fasta")
output = open(args.output, 'w')
for alignment in alignments:
ranges = [int(x) for x in re.findall("\d+", re.findall(r'\[.*\]', alignment.description)[0])]
ranges.sort()
seq = alignment.seq
exon_union = ""
for i in range(len(ranges) // 2):
exon_union += str(seq[ranges[2*i]:ranges[2*i+1]])
if len(exon_union) < 7 or len(exon_union) % 3 != 0:
continue
if start_codon != str(exon_union[0:3]):
continue
good = True
gen = ""
for i in range(1, len(exon_union) // 3 - 1):
if exon_union[3*i:3*i+3] not in all_codons:
good = False
break
codon = all_codons[exon_union[3*i:3*i+3]]
if codon == 'X':
good = False
break
gen += codon
print()
if not good or all_codons[exon_union[-3:]] != 'X':
continue
record = SeqRecord(
Seq(gen),
id=alignment.id,
description = alignment.description
)
output.write(record.format("fasta"))