88from .Events import ListInserts
99from .indexing import Readbam
1010from .Sequences import BuildConsensus
11-
11+ from AminoExtract . gff_data import GFFColumns
1212
1313def WriteGFF (gffheader , gffdict , output_gff , name ):
1414 """Function takes a GFF header, a dictionary of GFF features, an output directory, and a name for
@@ -26,16 +26,49 @@ def WriteGFF(gffheader, gffdict, output_gff, name):
2626 the name of the file you want to write
2727
2828 """
29+ cols = GFFColumns .get_names ()
30+ cols_without_attr = [col for col in cols if col != "attributes" ]
31+
32+ def combine_dict_into_attributes (input_dict : dict [str , str ]) -> str :
33+ attribute_dict = {}
34+ for k , v in input_dict .items ():
35+ if k == "attributes" :
36+ for attribute in v .split (";" ):
37+ if attribute == "" :
38+ continue
39+ key , value = attribute .split ("=" )
40+ attribute_dict [key ] = value
41+ else :
42+ attribute_dict [k ] = v
43+
44+ return ";" .join (f"{ k } ={ v } " for k , v in attribute_dict .items ())
45+
46+ def clean_dict (input_dict : dict [str , str ]) -> dict [str , str ]:
47+ clean_dict = {}
48+ attribute_dict = {}
49+ for k , v in input_dict .items ():
50+ if str (k ).lower () not in cols_without_attr :
51+ attribute_dict [str (k ).lower ()] = str (v )
52+ else :
53+ clean_dict [str (k ).lower ()] = str (v )
54+
55+ attribute_str = combine_dict_into_attributes (attribute_dict )
56+ clean_dict ["attributes" ] = attribute_str
57+ assert list (clean_dict .keys ()) == cols
58+ return clean_dict
59+
60+
61+ # the gffdict will have 0, 1, 2, etc as keys, for each line in the GFF file
62+ # the values will be dictionaries containing the GFF columns for that line, with a lot of additional columns
63+ # these additional columns will all be forced into the attributes column
64+
2965 with open (output_gff , "w" ) as out :
30- out .write (gffheader )
66+ out .write (gffheader .raw_text )
67+
3168
32- for k , v in gffdict .items ():
33- for nk , nv in v .items ():
34- if str (nk ) == str (list (v .keys ())[- 1 ]):
35- out .write (str (nv ))
36- else :
37- out .write (str (nv ) + "\t " )
38- out .write ("\n " )
69+ for line_number , gff_data in gffdict .items ():
70+ cleaned_data = clean_dict (gff_data )
71+ out .write ("\t " .join ([str (v ) for v in cleaned_data .values ()]) + "\n " )
3972
4073
4174def WriteOutputs (
@@ -65,6 +98,7 @@ def WriteOutputs(
6598 )[0 ]
6699
67100 if output_gff is not None :
101+
68102 WriteGFF (gffheader , newgff , output_gff , name )
69103
70104 if output_vcf is not None :
0 commit comments