forked from jorvis/biocode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsplit_isoforms.py
executable file
·54 lines (42 loc) · 1.98 KB
/
split_isoforms.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
#!/usr/bin/env python3
"""
split_isoforms.py - splits all GFF3 mRNA isoforms into their own gene models (adds _# to old gene ID to get new gene ID)
"""
import argparse
import os
import biocodegff
import biothings
import biocodeutils
def main():
parser = argparse.ArgumentParser( description='Splits all GFF3 mRNA isoforms into their own gene models')
## Get the variables
parser.add_argument('-i', '--input_file', type=str, required=True, help='Input GFF3 file' )
parser.add_argument('-o', '--output_file', type=str, required=True, help='Output GFF3 file' )
args = parser.parse_args()
ofh = open(args.output_file, 'wt')
print("INFO: Parsing GFF3 features\n")
(assemblies, ref_features) = biocodegff.get_gff3_features( args.input_file )
print("INFO: Finding genes with isoforms and splitting them\n")
ofh.write("##gff-version 3\n")
for assembly_id in assemblies:
for gene in assemblies[assembly_id].genes():
# only changing the gene features with isoforms
if len(gene.mRNAs()) > 1:
counter = 1
for mRNA in gene.mRNAs():
new_gene_id = str(gene.id) + "_" + str(counter)
counter += 1
mRNA_loc = mRNA.location()
print("Splitting " + gene.id)
# create a new gene model, correcting the gene coords to the mRNA coords
new_gene = biothings.Gene( id = new_gene_id)
new_gene.locate_on( target=assemblies[assembly_id], fmin=mRNA_loc.fmin, fmax=mRNA_loc.fmax, strand=mRNA_loc.strand )
mRNA.parent.id = new_gene_id
#Now add the mRNA to the gene model
new_gene.add_mRNA(mRNA)
# print out the new gene model
new_gene.print_as(fh=ofh, source='IGS', format='gff3')
else:
gene.print_as(fh=ofh, source='IGS', format='gff3')
if __name__ == '__main__':
main()