forked from J35P312/MegaFusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MegaFusion.py
149 lines (122 loc) · 5.67 KB
/
MegaFusion.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import argparse
import json
def retrieve_required_entry(data,tag,content):
if type(data["required"][tag]) is dict:
info=content[ data["required"][tag]["column"] ].split(data["required"][tag]["delimiter"])[data["required"][tag]["element"] ]
else:
info=content[ data["required"][tag] ]
return(info)
version = "0.0.1"
parser = argparse.ArgumentParser("""Fusion2VCF-{}: convert gene fusion tab files to SV vcf""".format(version))
parser.add_argument('--fusion' ,required=True , type=str, help="path to fusion tab file")
parser.add_argument('--json' ,required=True , type=str, help="path to a config json file")
parser.add_argument('--sample' , type=str,default="Bob", help="Sample name (default=Bob")
args = parser.parse_args()
args.version=version
with open(args.json) as json_file:
data = json.load(json_file)
print("##fileformat=VCFv4.1")
print("##source={}".format(data["source"]))
print("##ALT=<ID=BND,Description=\"Break end\">")
print("##INFO=<ID=CHRA,Number=1,Type=String,Description=\"Chromosome A\">")
print("##INFO=<ID=CHRB,Number=1,Type=String,Description=\"Chromosome B\">")
print("##INFO=<ID=GENEA,Number=.,Type=String,Description=\"Gene A\">")
print("##INFO=<ID=GENEB,Number=.,Type=String,Description=\"Gene B\">")
print("##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant\">")
print("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">")
print("##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"Number of paired-ends that support the event\">")
print("##FORMAT=<ID=RV,Number=1,Type=Integer,Description=\"Number of split reads that support the event\">")
required_format="GT:DV:RV"
for entry in sorted(data["custom"].keys()):
try:
print ("##{}=<ID={},Number=.,Type={},Description=\"{}\">".format(data["custom"][entry]["entry"],entry,data["custom"][entry]["type"],data["custom"][entry]["description"]) )
if "FORMAT" == data["custom"][entry]["entry"]:
required_format+= ":{}".format(entry)
except:
print ("invalid config")
quit()
filter_string="##FILTER=<ID={},Description=\"Not up to snuff\">"
filter_set=set([])
if data["filter"]["filter"]:
for line in open(args.fusion):
if line.startswith(data["header"]):
continue
content=line.strip().split(data["delimiter"])
if data["filter"]["pass"] == content[data["filter"]["column"]]:
continue
if not "delimiter" in data["filter"]:
if not content[data["filter"]["column"]] in filter_set:
print (filter_string.format(content[data["filter"]["column"]]) )
filter_set.add( content[data["filter"]["column"]])
else:
for entry in content[data["filter"]["column"]].split(data["filter"]["delimiter"]):
if not entry in filter_set:
print (filter_string.format(entry) )
filter_set.add(entry)
print("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t{}".format(args.sample))
required_INFO="SVTYPE=BND;CHRA={};CHRB={};GENEA={};GENEB={};ORIENTATION={},{}"
required_columns="{}\t{}\t{}\tN\t{}\t{}\t{}\t{}\t{}\t{}"
required_sample="./1:{}:{}"
i=1
for line in open(args.fusion):
if line.startswith(data["header"]):
continue
content=line.strip().split(data["delimiter"])
chrA=retrieve_required_entry(data,"chromosomeA",content)
chrB=retrieve_required_entry(data,"chromosomeB",content)
posA=retrieve_required_entry(data,"posA",content)
posB=retrieve_required_entry(data,"posB",content)
geneA=retrieve_required_entry(data,"geneA",content)
geneB=retrieve_required_entry(data,"geneB",content)
strand1=retrieve_required_entry(data,"strand1",content)
strand2=retrieve_required_entry(data,"strand2",content)
if not strand1 in ["+","-"] or not strand2 in ["+","-"]:
altA="N[{}:{}[".format(chrB,posB)
#altB="]{}:{}]N".format(chrB,posB)
elif strand1 == "-" and strand2 == "-":
altA="[{}:{}[N".format(chrB,posB)
#altB="[{}:{}[N".format(chrB,posB)
elif strand1 == "+" and strand2 == "-":
altA="N]{}:{}]".format(chrB,posB)
#altB="N]{}:{}]".format(chrB,posB)
elif strand1 == "-" and strand2 == "+":
altA="N]{}:{}]".format(chrB,posB)
#altB="N]{}:{}]".format(chrB,posB)
else:
altA="N[{}:{}[".format(chrB,posB)
#altB="]{}:{}]N".format(chrB,posB)
INFO=required_INFO.format(chrA,chrB,geneA,geneB,strand1,strand2)
for entry in data["custom"]:
if data["custom"][entry]["entry"] == "INFO":
if "none" in data["custom"][entry]:
if data["custom"][entry]["none"] == content[ data["custom"][entry]["column"] ]:
continue
if "remove" in data["custom"][entry]:
for r in data["custom"][entry]["remove"]:
content[ data["custom"][entry]["column"] ]=content[ data["custom"][entry]["column"] ].replace(r,"")
INFO+= ";{}={}".format(entry,content[ data["custom"][entry]["column"] ])
pairs=retrieve_required_entry(data,"discordant_pairs",content)
reads=retrieve_required_entry(data,"split_reads",content)
FORMAT=required_sample.format(pairs,reads)
for entry in sorted(data["custom"].keys()):
if data["custom"][entry]["entry"] == "FORMAT":
if "none" in data["custom"][entry]:
if data["custom"][entry]["none"] == content[ data["custom"][entry]["column"] ]:
content[ data["custom"][entry]["column"] ] = "."
if "remove" in data["custom"][entry]:
for r in data["custom"][entry]["remove"]:
entry=entry.replace(r,"")
FORMAT+= ":{}".format(content[ data["custom"][entry]["column"] ])
ID="{}_Fusion_{}".format(data["source"],i)
qual="."
filt="PASS"
if data["filter"]["filter"]:
if content[ data["filter"]["column"] ] == data["filter"]["pass"]:
pass
else:
if not "delimiter" in data["filter"]:
qual=content[ data["filter"]["column"] ]
else:
qual=content[ data["filter"]["column"] ].replace(data["filter"]["delimiter"],",")
print( required_columns.format(chrA,posA,ID+"_1",altA,qual,filt,INFO,required_format,FORMAT) )
i+=1