-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixture_runner.py
282 lines (250 loc) · 12.9 KB
/
mixture_runner.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""Make CNV/LOH plots and mixture plot"""
import call_class, os, random, global_settings, utils, sys
from collections import defaultdict
def avg_diffs_per_chr(allele_diffs, cnvs, mix_file):
"""For each chr, use the lost of copy CNVs to compuete the averag allele diff per chr"""
with open(mix_file, 'w') as f:
for chr in allele_diffs:
sum_diffs = float(0)
total_calls = 0
c = 'chr' + chr
if c in cnvs:
for pos in allele_diffs[chr]:
for cnv_st, cnv_end in cnvs[c]:
if pos < cnv_st:
break
if pos >= cnv_st and pos <= cnv_end:
sum_diffs += allele_diffs[chr][pos]
total_calls += 1
break
if total_calls:
avg_diff = str(sum_diffs/float(total_calls))
f.write('%s\t%d\t%s\n' %
(chr, total_calls, avg_diff))
def get_cnvs(afile):
"""Load los of copy CNVs computed by cnv-seq"""
cnvs = defaultdict(list)
with open(afile) as f:
f.readline()
for line in f:
sp = line.strip().split('\t')
if len(sp) == 7:
cnv, chr, st, end, size, log2, pval = line.strip().split('\t')
if log2 != 'NA':
# check for copy loss
if float(log2) < float(-.8):
cnvs[chr].append((int(st), int(end)))
for chr in cnvs:
cnvs[chr].sort()
return cnvs
def get_allele_diffs(afile):
"""Load Murim's normal/cancer diffs for heterozygous calls in normal."""
allele_diffs = defaultdict(dict)
with open(afile) as f:
f.readline()
for line in f:
chr, pos, diff = line.strip().split('\t')
allele_diffs[chr][int(pos)] = float(diff)
return allele_diffs
def find_mixture(loh_diff_file, cnv_file, mix_file):
"""This is the cumulation of the CNV/LOH work. We need the mixture of normal/cancer cells to help call somatic mutations. I will find the mixture per chromosome by looking at copy loss regions, as indicated by CNV-seq, and then look at the allele differences (Murim's plot) to determine the mixture by looking at the difference between 0.5 and the observed normal/cancer allele frequency differences."""
allele_diffs = get_allele_diffs(loh_diff_file)
cnvs = get_cnvs(cnv_file)
avg_diffs_per_chr(allele_diffs, cnvs, mix_file)
def plot_mixture(snp_cut):
"""Use R ggplot2 to plot the mixture for all exome types and cancer/normal pairs"""
subdir = 'all_non_ref_hg19'
os.system('mkdir -p working/mixture/')
os.system('mkdir -p working/mixture/all_non_ref_hg19/')
for exome_type in set(global_settings.exome_types) | set(('exome_all',)):
for cancer, normal in global_settings.pairs:
cnv_file = 'working/cnv_seq/CNV/' + subdir + '/' + exome_type + '.' + cancer.split('0')[0].strip('T') + '.cnvs'
loh_file = 'working/loh/' + subdir + '/' + exome_type + '.' + cancer.split('0')[0].strip('T')
mix_file = 'working/mixture/' + subdir + '/' + exome_type + '.' + cancer.split('0')[0].strip('T') + '.mix'
find_mixture(loh_file, cnv_file, mix_file)
rinput = 'rinput' + str(random.randint(0,1000))
working_dir = os.path.join('working/mixture/', subdir + '/')
with open(rinput, 'w') as f:
f.write('Sample\tExome\tChr\tMixture\n')
for exome_type in set(global_settings.exome_types) | set(('exome_all',)):
for cancer, normal in global_settings.pairs:
mix_file_name = working_dir + exome_type + '.' + cancer + '.mix'
if os.path.exists(mix_file_name):
with open(mix_file_name) as mixfile:
for line in mixfile:
chr, snp_count, avg_diff = line.strip().split('\t')
if int(snp_count) > snp_cut:
if '.' in exome_type:
use_exome = exome_type.split('.')[1]
else:
use_exome = exome_type
f.write('%s\t%s\t%s\t%f\n' %
(cancer, use_exome, chr,
float(0.5)-float(avg_diff)))
# make R calls
rtmp = 'rtmp' + str(random.randint(0,1000))
with open(rtmp, 'w') as f:
f.write('library(ggplot2)\n')
f.write("data<-read.delim('" + rinput + "',header=TRUE,sep='\\t')\n")
f.write("data$Chr <-factor(data$Chr, levels=data$Chr)\n")
f.write("png('plots/" + subdir + ".mixture.png')\n")
f.write('ggplot(data) + aes(x=Chr,y=Mixture) + geom_point() + facet_grid(Sample~Exome)\n')
f.write('dev.off()\n')
f.write('q()\n')
os.system('R CMD BATCH --vanilla ' + rtmp + ' tmpLog')
os.system('rm ' + rinput + ' ' + rtmp)
def mk_cnv_seq_input_chrpos(coverage, chr, pos, file):
""" Print out each position the # of times it is covered (signal strength)."""
for i in xrange(coverage):
file.write('%s\t%s\n' % (chr.split('chr')[1], pos))
def mk_cnv_seq_input(coverages, output_dir):
"""Make input for CNV-seq.
Print out each position the # of times it is covered (signal strength)."""
for sample in coverages:
# look at all exome types combined
total_coverages = {'N':{},'T':{}}
for exome_type in coverages[sample]:
with open(os.path.join(output_dir,
'.'.join([exome_type,
sample + 'N.hits'])), 'w') as nhits:
with open(os.path.join(output_dir,
'.'.join([exome_type,
sample + 'T.hits'])), 'w') as thits:
for chrpos in coverages[sample][exome_type]:
chr, pos = chrpos.split(':')
total_coverages['N'][chrpos] = coverages[sample][exome_type][chrpos]['N']
total_coverages['T'][chrpos] = coverages[sample][exome_type][chrpos]['T']
for coverage, file in ((coverages[sample][exome_type][chrpos]['N'],
nhits),
(coverages[sample][exome_type][chrpos]['T'],
thits)):
mk_cnv_seq_input_chrpos(coverage, chr, pos, file)
with open(os.path.join(output_dir, '.'.join(['exome_all',
sample + 'N.hits'])), 'w') as nhits:
with open(os.path.join(output_dir,
'.'.join(['exome_all',
sample + 'T.hits'])), 'w') as thits:
for sample_type, file in (('N', nhits),
('T', thits)):
for chrpos in total_coverages[sample_type]:
chr, pos = chrpos.split(':')
mk_cnv_seq_input_chrpos(total_coverages[sample_type][chrpos],
chr, pos, file)
def mk_cnv_seq_input_runner():
"""Construct CNV-seq input from all_non_ref_hg19 5 paired samples"""
full_data_dir = 'all_non_ref_hg19/'
cnv_seq_dir = 'working/cnv_seq/'
os.system('mkdir -p ' + cnv_seq_dir)
output_dir = os.path.join(cnv_seq_dir, full_data_dir)
os.system('mkdir -p ' + output_dir)
samples2data = call_class.get_data_for_paired_samples()
coverages = call_class.get_coverages(samples2data)
mk_cnv_seq_input(coverages, output_dir)
sample_pairs = []
for sample in coverages:
sample_pairs.append((sample + 'T',
sample + 'N'))
return sample_pairs
def CNV_plot(cnv_file, subdir, png):
"""Make CNV plots using R cnv library"""
os.system('mkdir -p working/cnv_seq/CNV')
os.system('mkdir -p working/cnv_seq/CNV/'
+ subdir + '/')
output_cnv_file = 'working/cnv_seq/CNV/' + subdir + '/' + cnv_file.split('.hits')[0].rstrip('T') + '.cnvs'
os.system('rm ' + output_cnv_file)
rtmp = 'rtmp' + str(random.randint(0,1000))
with open(rtmp, 'w') as f:
f.write("source('funcs.R')\n")
f.write('library(cnv)\n')
f.write("data<-read.delim('"
+ cnv_file + "')\n")
f.write("png('" + png + "')\n")
f.write("plot.cnv.all.perry(data,colour=9)\n")
f.write('dev.off()\n')
f.write("cnv.print(data, file='" + output_cnv_file + "')\n")
f.write('q()\n')
if utils.check_input(cnv_file):
os.system('R CMD BATCH --vanilla ' + rtmp + ' tmpLog')
os.system('rm ' + rtmp + ' tmpLog')
os.system('mv ' + cnv_file + ' working/cnv_seq/CNV/' + subdir + '/')
os.system('mv ' + cnv_file.replace('cnv', 'count')
+ ' working/cnv_seq/CNV/' + subdir + '/')
def call_cnv_seq(sample_pairs):
"""System calls to CNV-seq"""
return_plots = {}
subdir = 'all_non_ref_hg19'
os.system('mkdir -p ' + os.path.join('plots', 'cnv-seq'))
os.system('mkdir -p ' + os.path.join('plots', 'cnv-seq', subdir))
for exome in set(global_settings.exome_types) | set(('exome_all',)):
for cancer, normal in sample_pairs:
cancer_hits = 'working/cnv_seq/' + subdir + '/' + exome + '.' + cancer + '.hits'
normal_hits = 'working/cnv_seq/' + subdir + '/' + exome + '.' + normal + '.hits'
# os.system('perl cnv-seq.pl --test ' + cancer_hits + ' --ref '
# + normal_hits
# + ' --genome human --log2 0.6 --p 0.001 --bigger-window 1.5 --annotate -minimum-windows 4')
cnv_file = exome + '.' + cancer + '.hits-vs-' + exome + '.' + normal + '.hits.log2-0.6.pvalue-0.001.minw-4.cnv'
plot_file = 'plots/cnv-seq/' + subdir + '/' + exome + '.' + cancer.rstrip('T') + '.png'
#CNV_plot(cnv_file, subdir, plot_file)
return_plots[cancer[0:-1] + '.' + exome] = plot_file
return return_plots
def plot_loh(freq_diffs, plot_file, input_file):
"""Recreate Murim's plots"""
with open(input_file, 'w') as f:
f.write('CHR\tMapInfo\tDiff\n')
for chr_pos in freq_diffs:
chr, pos = chr_pos.split(':')
f.write(chr.split('chr')[1] + '\t' + pos + '\t' + str(freq_diffs[chr_pos]) + '\n')
rtmp = 'rtmp' + str(random.randint(0,1000))
with open(rtmp, 'w') as f:
f.write("source('funcs.R')\n")
f.write("data<-read.delim('"
+ input_file + "')\n")
f.write("png('" + plot_file + "')\n")
f.write("plot.murim(data,colour=9)\n")
f.write('dev.off()\n')
f.write('q()\n')
#os.system('R CMD BATCH --vanilla ' + rtmp + ' tmpLog')
os.system('rm ' + rtmp + ' tmpLog')
def loh():
"""Make data for Murim's plots and call function to plot them"""
plots = {}
os.system('mkdir -p working/loh/')
os.system('mkdir -p working/loh/all_non_ref_hg19')
os.system('mkdir -p plots/loh/')
os.system('mkdir -p plots/loh/all_non_ref_hg19/')
quality_cutoff = float(100)
coverage_cutoff = 8
samples2data = call_class.get_data_for_paired_samples()
for sample in samples2data:
freq_diffs = call_class.get_allele_freq_diffs_for_loh(samples2data[sample],
quality_cutoff,
coverage_cutoff)
all_exome_freq_diffs = {}
for exome in freq_diffs:
plot_file = 'plots/loh/all_non_ref_hg19/' + exome + '.' + sample + '.png'
input_file = 'working/loh/all_non_ref_hg19/' + exome + '.' + sample
plot_loh(freq_diffs[exome], plot_file, input_file)
plots[sample + '.' + exome] = plot_file
for chrpos in freq_diffs[exome]:
all_exome_freq_diffs[chrpos] = freq_diffs[exome][chrpos]
plot_file = 'plots/loh/all_non_ref_hg19/exome_all.' + sample + '.png'
input_file = 'working/loh/all_non_ref_hg19/exome_all.' + sample
plot_loh(all_exome_freq_diffs,
plot_file, input_file)
plots[sample + '.exome_all'] = plot_file
return plots
def main():
"""Entry point"""
snp_cut = int(sys.argv[1])
random.seed()
sample_pairs = mk_cnv_seq_input_runner()
cnv_plots = call_cnv_seq(sample_pairs)
loh_plots = loh()
for sample_exome in cnv_plots:
os.system('montage -geometry 500 -quality 100 -tile 1x2 '
+ cnv_plots[sample_exome] + ' '
+ loh_plots[sample_exome] + ' '
+ cnv_plots[sample_exome].replace('png', 'cnv_loh.png'))
plot_mixture(snp_cut)
if __name__ == '__main__':
main()