-
Notifications
You must be signed in to change notification settings - Fork 46
/
shared_samples.py
executable file
·225 lines (211 loc) · 9.06 KB
/
shared_samples.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
#!/usr/bin/env python
import argparse
import gzip
import os
parser = argparse.ArgumentParser(description = "determine which clusters are the shared clusters between two souporcell runs with shared samples")
parser.add_argument("-1","--experiment1", required = True, help = "souporcell directory for experiment 1")
parser.add_argument("-2","--experiment2", required = True, help = "souporcell directory for experiment 2")
parser.add_argument("-n","--shared", required= True, type = int, help = "how many samples should these experiments share?")
args = parser.parse_args()
if os.path.isfile(args.experiment1+"/souporcell_merged_sorted_vcf.vcf.gz"):
vcf1 = gzip.open(args.experiment1+"/souporcell_merged_sorted_vcf.vcf.gz",'rt')
vcf2 = gzip.open(args.experiment2+"/souporcell_merged_sorted_vcf.vcf.gz",'rt')
elif os.path.isfile(args.experiment1+"/common_variants_covered.vcf"):
vcf1 = open(args.experiment1+"/common_variants_covered.vcf")
vcf2 = open(args.experiment2+"/common_variants_covered.vcf")
line1 = "#"
while line1.startswith("#"):
line1 = vcf1.readline()
(chr1, pos1, ref1, alt1) = line1.strip().split()[0:4]
line2 = "#"
while line2.startswith("#"):
line2 = vcf2.readline()
(chr2, pos2, ref2, alt2) = line2.strip().split()[0:4]
assert chr1 == chr2, "vcfs dont start with same chromosome, are you sure you used the same reference?"
last_chr1 = chr1
last_chr2 = chr2
locus1 = 1
locus2 = 1
locus1_matches = []
locus2_matches = []
locus1_matchset = {}
locus2_matchset = {}
save_loci1 = {locus2: (chr1,pos1)}
save_loci2 = {locus2: (chr2,pos2)}
breakme = False
while not breakme:
if pos1 == None:
try:
(chr1, pos1, ref1, alt1) = vcf1.readline().strip().split()[0:4]
locus1 += 1
save_loci1[locus1] = (chr1, pos1)
except:
break
if pos2 == None:
try:
(chr2, pos2, ref2, alt2) = vcf2.readline().strip().split()[0:4]
locus2 += 1
save_loci2[locus2] = (chr2, pos2)
except:
break
if chr1 == chr2:
if pos1 == pos2:
if alt1 == alt2:
locus1_matches.append(locus1)
locus2_matches.append(locus2)
locus1_matchset[locus1] = len(locus1_matches) - 1
locus2_matchset[locus2] = len(locus2_matches) - 1
#print("we have a match chr"+chr1+" "+str(pos1)+" respective loci "+str(locus1)+" "+str(locus2))
pos1 = None
pos2 = None
elif pos1 < pos2:
pos1 = None
else:
pos2 = None
elif not(chr1 == last_chr1):
while not(chr2 == chr1):
try:
(chr2, pos2, ref2, alt2) = vcf2.readline().strip().split()[0:4]
except:
breakme = True
break
locus2 += 1
last_chr1 = chr1
last_chr2 = chr2
elif not(chr2 == last_chr2):
while not(chr1 == chr2):
try:
(chr1, pos1, ref1, alt1) = vcf1.readline().strip().split()[0:4]
except:
breakme = True
break
locus1 += 1
last_chr1 = chr1
last_chr2 = chr2
last_chr1 = chr1
last_chr2 = chr2
print("locus1 matchset "+str(len(locus1_matchset)))
cell_clusters1 = {}
with open(args.experiment1+"/clusters.tsv") as clust:
clust.readline()
for (celldex, line) in enumerate(clust):
toks = line.strip().split()
if toks[1] == "singlet":
cell_clusters1[celldex + 1] = int(toks[2])
print("cell clusters "+str(len(cell_clusters1)))
cell_clusters2 = {}
with open(args.experiment2+"/clusters.tsv") as clust:
clust.readline()
for (celldex, line) in enumerate(clust):
toks = line.strip().split()
if toks[1] == "singlet":
cell_clusters2[celldex + 1] = int(toks[2])
print("cell clusters "+str(len(cell_clusters2)))
cluster1_locus_counts = {} # cluster to locus to count tuple
total_locus_counts1 = {}
with open(args.experiment1+"/alt.mtx") as alts:
with open(args.experiment1+"/ref.mtx") as refs:
alts.readline()
alts.readline()
alts.readline()
refs.readline()
refs.readline()
refs.readline()
for (altline, refline) in zip(alts, refs):
(locus1, cell1, altcount) = altline.strip().split()
(locus2, cell2, refcount) = refline.strip().split()
locus1 = int(locus1)
locus2 = int(locus2)
cell1 = int(cell1)
cell2 = int(cell2)
assert locus1 == locus2, "ref and alt mtx for experiment 1 dont match?"
assert cell1 == cell2, "ref and alt mtx for experiment 1 dont match?"
if locus1 in locus1_matchset:
if cell1 in cell_clusters1:
cluster = cell_clusters1[cell1]
matchlocus = locus1_matchset[locus1]
if not(cluster in cluster1_locus_counts):
cluster1_locus_counts[cluster] = [[0,0] for x in range(len(locus1_matchset))]
locus_counts = cluster1_locus_counts[cluster] #cluster1_locus_counts.setdefault(cluster, [[0,0] for x in range(len(locus1_matchset))])
counts = locus_counts[matchlocus]
counts[0] += int(refcount)
counts[1] += int(altcount)
total_locus_counts1.setdefault(matchlocus, 0)
total_locus_counts1[matchlocus] += int(refcount) + int(altcount)
#print(total_locus_counts1[matchlocus])
print("clusters for experiment1 "+str(len(cluster1_locus_counts)))
cluster2_locus_counts = {}
total_locus_counts2 = {}
with open(args.experiment2+"/alt.mtx") as alts:
with open(args.experiment2+"/ref.mtx") as refs:
alts.readline()
alts.readline()
alts.readline()
refs.readline()
refs.readline()
refs.readline()
for (altline, refline) in zip(alts, refs):
(locus1, cell1, altcount) = altline.strip().split()
(locus2, cell2, refcount) = refline.strip().split()
locus1 = int(locus1)
locus2 = int(locus2)
cell1 = int(cell1)
cell2 = int(cell2)
assert locus1 == locus2, "ref and alt mtx for experiment 1 dont match?"
assert cell1 == cell2, "ref and alt mtx for experiment 1 dont match?"
if locus1 in locus2_matchset:
if cell1 in cell_clusters2:
cluster = cell_clusters2[cell1]
matchlocus = locus2_matchset[locus1]
if not(cluster in cluster2_locus_counts):
cluster2_locus_counts[cluster] = [[0,0] for x in range(len(locus2_matchset))]
locus_counts = cluster2_locus_counts[cluster]#cluster2_locus_counts.setdefault(cluster, [[0,0] for x in range(len(locus2_matchset))])
counts = locus_counts[matchlocus]
counts[0] += int(refcount)
counts[1] += int(altcount)
total_locus_counts2.setdefault(matchlocus, 0)
#print(matchlocus)
total_locus_counts2[matchlocus] += int(refcount) + int(altcount)
print("clusters for experiment2 "+str(len(cluster2_locus_counts)))
distances = {}
loci_to_use = set()
for locus in range(len(cluster1_locus_counts[0])):
has_enough = True
for cluster in cluster1_locus_counts.keys():
locus_counts = cluster1_locus_counts[cluster]
if locus_counts[locus][0] + locus_counts[locus][1] < 8:
has_enough = False
for cluster in cluster2_locus_counts.keys():
locus_counts = cluster2_locus_counts[cluster]
if locus_counts[locus][0] + locus_counts[locus][1] < 8:
has_enough = False
if has_enough:
loci_to_use.add(locus)
for cluster1 in cluster1_locus_counts.keys():
locus_counts1 = cluster1_locus_counts[cluster1]
for cluster2 in cluster2_locus_counts.keys():
locus_counts2 = cluster2_locus_counts[cluster2]
loss = 0
for locus in range(len(locus_counts1)):
#print("are we")
locusname = locus
#if locusname in total_locus_counts1 and total_locus_counts1[locusname] > 20 and locusname in total_locus_counts2 and total_locus_counts2[locusname] > 20:
if locus in loci_to_use:
counts1 = locus_counts1[locus]
counts2 = locus_counts2[locus]
if counts1[0] + counts1[1] > 0:
af1 = counts1[0]/(counts1[0] + counts1[1])
else:
af1 = 1.0
if counts2[0] + counts2[1] > 0:
af2 = counts2[0]/(counts2[0] + counts2[1])
else:
af2 = 1.0
loss += (af1 - af2)**2
distances[(cluster1, cluster2)] = loss
distances_sorted = sorted(distances.items(), key=lambda kv: kv[1])
print("experiment1_cluster\texperiment2_cluster\tloss")
for (index, ((cluster1, cluster2), loss)) in enumerate(distances_sorted):
if index >= args.shared*2:
break
print("\t".join([str(cluster1), str(cluster2), str(loss)]))