annotate cpt_stops/stop_stats.py @ 0:89c78d269676 draft

Uploaded
author cpt
date Fri, 13 May 2022 05:39:51 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89c78d269676 Uploaded
cpt
parents:
diff changeset
1 #!/usr/bin/env python
89c78d269676 Uploaded
cpt
parents:
diff changeset
2 import argparse
89c78d269676 Uploaded
cpt
parents:
diff changeset
3 from CPT_GFFParser import gffParse, gffWrite
89c78d269676 Uploaded
cpt
parents:
diff changeset
4 from Bio import SeqIO
89c78d269676 Uploaded
cpt
parents:
diff changeset
5 from gff3 import feature_lambda, feature_test_type
89c78d269676 Uploaded
cpt
parents:
diff changeset
6
89c78d269676 Uploaded
cpt
parents:
diff changeset
7
89c78d269676 Uploaded
cpt
parents:
diff changeset
8 def main(fasta, gff3):
89c78d269676 Uploaded
cpt
parents:
diff changeset
9 seq_dict = SeqIO.to_dict(SeqIO.parse(fasta, "fasta"))
89c78d269676 Uploaded
cpt
parents:
diff changeset
10
89c78d269676 Uploaded
cpt
parents:
diff changeset
11 codon_usage = {}
89c78d269676 Uploaded
cpt
parents:
diff changeset
12
89c78d269676 Uploaded
cpt
parents:
diff changeset
13 for rec in gffParse(gff3, base_dict=seq_dict):
89c78d269676 Uploaded
cpt
parents:
diff changeset
14 for feat in feature_lambda(
89c78d269676 Uploaded
cpt
parents:
diff changeset
15 rec.features, feature_test_type, {"type": "CDS"}, subfeatures=True
89c78d269676 Uploaded
cpt
parents:
diff changeset
16 ):
89c78d269676 Uploaded
cpt
parents:
diff changeset
17 seq = str(feat.extract(rec).seq)[-3:]
89c78d269676 Uploaded
cpt
parents:
diff changeset
18 try:
89c78d269676 Uploaded
cpt
parents:
diff changeset
19 codon_usage[seq] += 1
89c78d269676 Uploaded
cpt
parents:
diff changeset
20 except KeyError:
89c78d269676 Uploaded
cpt
parents:
diff changeset
21 codon_usage[seq] = 1
89c78d269676 Uploaded
cpt
parents:
diff changeset
22
89c78d269676 Uploaded
cpt
parents:
diff changeset
23 names = {
89c78d269676 Uploaded
cpt
parents:
diff changeset
24 "TAG": "Amber",
89c78d269676 Uploaded
cpt
parents:
diff changeset
25 "TAA": "Ochre",
89c78d269676 Uploaded
cpt
parents:
diff changeset
26 "TGA": "Opal",
89c78d269676 Uploaded
cpt
parents:
diff changeset
27 }
89c78d269676 Uploaded
cpt
parents:
diff changeset
28
89c78d269676 Uploaded
cpt
parents:
diff changeset
29 # TODO: print all actg combinations? Or just ones that are there
89c78d269676 Uploaded
cpt
parents:
diff changeset
30 print ("# Name\tCodon\tCount")
89c78d269676 Uploaded
cpt
parents:
diff changeset
31 for key in sorted(codon_usage):
89c78d269676 Uploaded
cpt
parents:
diff changeset
32 print ("\t".join((names.get(key.upper(), "None"), key, str(codon_usage[key]))))
89c78d269676 Uploaded
cpt
parents:
diff changeset
33
89c78d269676 Uploaded
cpt
parents:
diff changeset
34
89c78d269676 Uploaded
cpt
parents:
diff changeset
35 if __name__ == "__main__":
89c78d269676 Uploaded
cpt
parents:
diff changeset
36 parser = argparse.ArgumentParser(
89c78d269676 Uploaded
cpt
parents:
diff changeset
37 description="Summarise stop codon usage", epilog=""
89c78d269676 Uploaded
cpt
parents:
diff changeset
38 )
89c78d269676 Uploaded
cpt
parents:
diff changeset
39 parser.add_argument("fasta", type=argparse.FileType("r"), help="Fasta Genome")
89c78d269676 Uploaded
cpt
parents:
diff changeset
40 parser.add_argument("gff3", type=argparse.FileType("r"), help="GFF3 File")
89c78d269676 Uploaded
cpt
parents:
diff changeset
41 args = parser.parse_args()
89c78d269676 Uploaded
cpt
parents:
diff changeset
42 main(**vars(args))