annotate cpt_starts/start_stats.py @ 0:9f2517655a1e draft

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