Mercurial > repos > cpt > cpt_fix_aragorn
comparison fix-aragorn-gff3.py @ 3:f0f0ab9db43f draft
planemo upload commit 94b0cd1fff0826c6db3e7dc0c91c0c5a8be8bb0c
author | cpt |
---|---|
date | Mon, 05 Jun 2023 02:42:12 +0000 |
parents | |
children | 733cb0807083 |
comparison
equal
deleted
inserted
replaced
2:dc22c76a57bd | 3:f0f0ab9db43f |
---|---|
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, | |
17 feature_test_type, | |
18 {"types": ["tRNA", "tmRNA"]}, | |
19 subfeatures=True, | |
20 ) | |
21 ): | |
22 | |
23 fid = "%s-%03d" % (feature.type, 1 + idx) | |
24 try: | |
25 name = [feature.type + "-" + feature.qualifiers["Codon"][0]] | |
26 except KeyError: | |
27 name = [feature.qualifiers["product"][0]] | |
28 try: | |
29 origSource = feature.qualifiers["source"][0] | |
30 except: | |
31 origSource = "." | |
32 gene = gffSeqFeature( | |
33 location=feature.location, | |
34 type="gene", | |
35 qualifiers={"ID": [fid + ".gene"], "source": [origSource], "Name": name}, | |
36 ) | |
37 feature.qualifiers["Name"] = name | |
38 # Below that we have an mRNA | |
39 exon = gffSeqFeature( | |
40 location=feature.location, | |
41 type="exon", | |
42 qualifiers={"source": [origSource], "ID": ["%s.exon" % fid], "Name": name}, | |
43 ) | |
44 feature.qualifiers["ID"] = [fid] | |
45 exon.qualifiers["Parent"] = [fid] | |
46 feature.qualifiers["Parent"] = [fid + ".gene"] | |
47 # gene -> trna -> exon | |
48 feature.sub_features = [exon] | |
49 gene.sub_features = [feature] | |
50 yield gene | |
51 | |
52 | |
53 def gff_filter(gff3): | |
54 found_gff = False | |
55 for rec in gffParse(gff3): | |
56 found_gff = True | |
57 rec.features = sorted(list(fixed_feature(rec)), key=lambda x: x.location.start) | |
58 rec.annotations = {} | |
59 gffWrite([rec], sys.stdout) | |
60 if not found_gff: | |
61 print("##gff-version 3") | |
62 | |
63 | |
64 if __name__ == "__main__": | |
65 parser = argparse.ArgumentParser(description="add parent gene features to CDSs") | |
66 parser.add_argument("gff3", type=argparse.FileType("r"), help="GFF3 annotations") | |
67 args = parser.parse_args() | |
68 gff_filter(**vars(args)) |