-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArribaToXena.py
168 lines (148 loc) · 4.68 KB
/
ArribaToXena.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys, os, json, datetime
sys.path.insert(0,os.path.dirname(__file__))
import filearray_convert
suffix = 'arriba.fusions.tsv$'
columns = ['breakpoint1', 'breakpoint2',
'gene1', 'gene2',
'site1', 'site2',
'strand1(gene/fusion)', 'strand2(gene/fusion)',
'direction1', 'direction2',
'type', 'fusion_transcript', 'reading_frame', 'confidence']
compliment = {
'A' : 'T',
'T' : 'A',
'G' : 'C',
'C' : 'G'
}
def parsePos(genomicPos):
Chr, pos = genomicPos.split(':')
if Chr[0:3].upper() != 'CHR':
Chr = 'chr' + Chr
return {
'Chr': Chr,
'pos': pos
}
def doChr1 (data, columnPos): return parsePos(data[columnPos['breakpoint1']])['Chr']
def doPos1 (data, columnPos): return parsePos(data[columnPos['breakpoint1']])['pos']
def doRef1 (data, columnPos):
DNA = data[columnPos['fusion_transcript']].split('|')[0][-1]
strand = data[columnPos['strand1(gene/fusion)']].split('/')[1]
if strand == '-':
DNA = compliment[DNA]
return DNA
def doAlt1 (data, columnPos):
ref = doRef1(data, columnPos)
bp2 = data[columnPos['breakpoint2']]
d1 = data[columnPos['direction1']]
d2 = data[columnPos['direction2']]
if d1 == 'downstream':
if d2 == 'upstream': # d2 is the downstream piece
alt = ref + '[' + bp2 + '['
if d2 == 'downstream': # d2 is the upstream piece
alt = ref + ']' + bp2 + ']'
if d1 == 'upstream':
if d2 == 'downstream' : # d2 is the upstream piece
alt = ']' + bp2 + ']' + ref
if d2 == 'upstream': # d2 is the downstream piece
alt = '[' + bp2 + '[' + ref
return alt
def doGene1(data, columnPos):
if data[columnPos['site1']] != 'intergenic':
return data[columnPos['gene1']]
else:
return 'intergenic'
def doAltGene1(data, columnPos):
if data[columnPos['site2']] != 'intergenic':
return data[columnPos['gene2']]
else:
return 'intergenic'
def doEffect (data, columnPos): return data[columnPos['type']]
def doReadingFrame (data, columnPos): return data[columnPos['reading_frame']]
def doConfidence(data, columnPos): return data[columnPos['confidence']]
def doChr2 (data, columnPos): return parsePos(data[columnPos['breakpoint2']])['Chr']
def doPos2 (data, columnPos): return parsePos(data[columnPos['breakpoint2']])['pos']
def doRef2 (data, columnPos):
DNA = data[columnPos['fusion_transcript']].split('|')[1][0]
strand = data[columnPos['strand2(gene/fusion)']].split('/')[1]
if strand == '-':
DNA = compliment[DNA]
return DNA
def doAlt2 (data, columnPos):
ref = doRef2(data, columnPos)
bp2 = data[columnPos['breakpoint1']]
d1 = data[columnPos['direction2']]
d2 = data[columnPos['direction1']]
if d1 == 'downstream':
if d2 == 'upstream': # d2 is the downstream piece
alt = ref + '[' + bp2 + '['
if d2 == 'downstream': # d2 is the upstream piece
alt = ref + ']' + bp2 + ']'
if d1 == 'upstream':
if d2 == 'downstream' : # d2 is the upstream piece
alt = ']' + bp2 + ']' + ref
if d2 == 'upstream': # d2 is the downstream piece
alt = '[' + bp2 + '[' + ref
return alt
def doGene2(data, columnPos):
if data[columnPos['site2']] != 'intergenic':
return data[columnPos['gene2']]
else:
return 'intergenic'
def doAltGene2(data, columnPos):
if data[columnPos['site1']] != 'intergenic':
return data[columnPos['gene1']]
else:
return 'intergenic'
columnfunctions = {
'chr': [doChr1, 0],
'start': [doPos1, 1],
'end': [doPos1, 2],
'reference': [doRef1, 3],
'alt': [doAlt1, 4],
'gene': [doGene1, 5],
'altGene': [doAltGene1, 6],
'effect': [doEffect, 7],
'reading_frame': [doReadingFrame, 8],
'confidence' :[doConfidence, 9]
}
paired_columnfunctions = {
'chr': [doChr2, 0],
'start': [doPos2, 1],
'end': [doPos2, 2],
'reference': [doRef2, 3],
'alt': [doAlt2, 4],
'gene': [doGene2, 5],
'altGene': [doAltGene2, 6],
'effect': [doEffect, 7],
'reading_frame': [doReadingFrame, 8],
'confidence' :[doConfidence, 9]
}
def filterfunction (data, columnPos): return data[columnPos['confidence']] == 'high'
def buildjson(assembly, cohort, output):
output = output + '.json'
fout = open(output, 'w')
J = {}
J['type'] ='mutationVector'
J['dataSubtype'] = 'somatic structural variant'
J['label'] = 'Arriba fusion'
J['assembly'] = assembly
J['cohort'] = cohort
J['version'] = datetime.date.today().isoformat()
json.dump(J, fout, indent = 4)
fout.close()
if len(sys.argv[:]) < 5:
print ("python SVToXena.py inputFileDir output cohort \
assembly sample_id_mapping(optional, id file_id)")
print ()
sys.exit(1)
inputdir = sys.argv[1]
output = sys.argv[2]
cohort = sys.argv[3]
assembly = sys.argv[4]
if len(sys.argv[:]) > 5 :
sample_mapping_file = sys.argv[5]
else:
sample_mapping_file = None
filearray_convert.fileArrayFusionToXena(suffix, columns, inputdir, output, columnfunctions, paired_columnfunctions,
filterfunction, sample_mapping_file)
buildjson(assembly, cohort, output)