0
|
1 #!/usr/bin/env python
|
|
2 import sys
|
|
3 import logging
|
|
4 import argparse
|
|
5 from CPT_GFFParser import gffParse, gffWrite, gffSeqFeature
|
|
6 from Bio.SeqFeature import SeqFeature
|
|
7 from gff3 import feature_lambda, feature_test_type
|
|
8
|
|
9 logging.basicConfig(level=logging.INFO)
|
|
10 log = logging.getLogger(__name__)
|
|
11
|
|
12
|
|
13 def fixed_feature(rec):
|
|
14 for idx, feature in enumerate(
|
|
15 feature_lambda(
|
|
16 rec.features, feature_test_type, {"types": ["tRNA", "tmRNA"]}, subfeatures=True
|
|
17 )
|
|
18 ):
|
|
19
|
|
20 fid = "%s-%03d" % (feature.type, 1 + idx)
|
|
21 try:
|
|
22 name = [feature.type + "-" + feature.qualifiers["Codon"][0]]
|
|
23 except KeyError:
|
|
24 name = [feature.qualifiers['product'][0]]
|
|
25 try:
|
|
26 origSource = feature.qualifiers["source"][0]
|
|
27 except:
|
|
28 origSource = "."
|
|
29 gene = gffSeqFeature(
|
|
30 location=feature.location,
|
|
31 type="gene",
|
|
32 qualifiers={"ID": [fid + ".gene"], "source": [origSource], "Name": name},
|
|
33 )
|
|
34 feature.qualifiers["Name"] = name
|
|
35 # Below that we have an mRNA
|
|
36 exon = gffSeqFeature(
|
|
37 location=feature.location,
|
|
38 type="exon",
|
|
39 qualifiers={"source": [origSource], "ID": ["%s.exon" % fid], "Name": name},
|
|
40 )
|
|
41 feature.qualifiers["ID"] = [fid]
|
|
42 exon.qualifiers["Parent"] = [fid]
|
|
43 feature.qualifiers["Parent"] = [fid + ".gene"]
|
|
44 # gene -> trna -> exon
|
|
45 feature.sub_features = [exon]
|
|
46 gene.sub_features = [feature]
|
|
47 yield gene
|
|
48
|
|
49
|
|
50 def gff_filter(gff3):
|
|
51 found_gff = False
|
|
52 for rec in gffParse(gff3):
|
|
53 found_gff = True
|
|
54 rec.features = sorted(list(fixed_feature(rec)), key=lambda x: x.location.start)
|
|
55 rec.annotations = {}
|
|
56 gffWrite([rec], sys.stdout)
|
|
57 if not found_gff:
|
|
58 print("##gff-version 3")
|
|
59
|
|
60
|
|
61 if __name__ == "__main__":
|
|
62 parser = argparse.ArgumentParser(description="add parent gene features to CDSs")
|
|
63 parser.add_argument("gff3", type=argparse.FileType("r"), help="GFF3 annotations")
|
|
64 args = parser.parse_args()
|
|
65 gff_filter(**vars(args))
|