-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode_graph.py
1177 lines (1059 loc) · 52.2 KB
/
barcode_graph.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#
############################################################################
# Copyright (c) 2024 University of Helsinki
# # All Rights Reserved
# See file LICENSE for details.
############################################################################
import math
import numpy as np
import editdistance
import networkx as nx
import matplotlib.pyplot as plt
import igraph as ig
from collections import defaultdict
import pandas as pd
import logging
import edlib
import itertools
from Levenshtein import distance
from concurrent.futures import ProcessPoolExecutor
from index import QGramIndex
from common import get_score, dfs, dfs_without_recursion, rank, unrank
logger = logging.getLogger("BarcodeGraph")
READ_CHUNK_SIZE = 100000
BC_CHUNK_SIZE = 10000
class Chunk:
index = QGramIndex(1, 16, 6)
counts = dict()
class Edgedist:
edges = dict()
dists = dict()
class BarcodeGraph:
def __init__(self, threshold):
self.threshold = threshold
self.counts = defaultdict(int) #list of my nodes with their counts #changing this to ranked instead of sequence as the key, kills all stats functions probably, need to look at them before I use them again
self.edges = defaultdict(list) # idea: change list to set
self.dists = defaultdict(int)
#self.barcodes = defaultdict(str)
#self.numbered = defaultdict(int)
self.clusters = defaultdict(list)
self.clustering = dict()
self.clustered = defaultdict(bool)
self.index = None
#not sure I love this here, think about moving that to a different place
def index_chunk(self, barcode_chunk, bc_len, num):
#print("Processing chunk" + str(num))
index = QGramIndex(self.threshold, bc_len, 6)
counts = dict() #wäre hier defaultdict sinnvoller?
# kann ich nicht theoretisch direkt mit self.counts arbeiten? Oder funktioniert nur daraus lesen aber nicht es verändern
# könnte mir vorstellen dass wenn ich es verändere nicht alle veränderungen übernommen werden
# aber lesen müsste eigentlich gehen und dann hat eh alles alle self.counts
for sequence in barcode_chunk:
if len(sequence) == bc_len + 1:
sequence = sequence[:-1]
if len(sequence) == bc_len:
bc_rank = rank(sequence, bc_len)
if bc_rank in counts.keys():
counts[bc_rank] += 1
else:
# bc_rank = rank(sequence, bc_len) #TODO add unrank function
counts[bc_rank] = 1
index.add_to_index(sequence, bc_rank)
chunk = Chunk()
chunk.index = index
chunk.counts = counts
logger.info("Finished indexing chunk" + str(num))
return chunk
def compare_chunk(self, bc_chunk, bc_len, num): # qär vielleicht sinnvoll zu self.index zu machen damit ich es nicht jedes mal weitergeben muss
# zu bc_chunk: gibt es irgendeine Option, dass ich das nicht machen muss? Die sind ja alle in self.counts, da kann ich drauf zugreifen
edges = defaultdict(list)
dists = defaultdict(int)
for bc_rank in bc_chunk:
barcode = unrank(bc_rank, bc_len)
closest = self.index.get_close(barcode, bc_rank)
for seq_rank in closest:
if seq_rank > bc_rank:
sequence = unrank(seq_rank, bc_len)
if sequence == "" or sequence == barcode:
continue
else:
dist = min(editdistance.eval(barcode, sequence), editdistance.eval(barcode[:-1],sequence), editdistance.eval(barcode,sequence[:-1]))
if dist <= self.threshold:
edges[bc_rank].append(seq_rank)
edges[seq_rank].append(bc_rank)
dists[(bc_rank, seq_rank)] = dist
dists[(seq_rank, bc_rank)] = dist
output = Edgedist()
output.edges = edges
output.dists = dists
logger.info("Finished comparing chunk" + str(num))
return output
def get_read_chunks(self, barcodes):
current_chunk = []
for r in barcodes:
current_chunk.append(r)
if len(current_chunk) >= READ_CHUNK_SIZE:
yield current_chunk
current_chunk = []
yield current_chunk
def get_barcode_chunks(self, barcodes):
current_chunk = []
for bc in barcodes:
current_chunk.append(bc)
if len(current_chunk) >= BC_CHUNK_SIZE:
yield current_chunk
current_chunk = []
yield current_chunk
def index_bc_in_parallel(self, barcodes, bc_len, threads):
#self.index = QGramIndex(self.threshold, bc_len, 6)
barcode_chunks = self.get_read_chunks(barcodes)
indexing_gen = (
self.index_chunk,
barcode_chunks,
itertools.repeat(bc_len),
itertools.count(start=0, step=1),
)
with ProcessPoolExecutor(max_workers = threads) as proc:
chunks = proc.map(*indexing_gen, chunksize = 1)
merged_index = self.index.index
for chunk in chunks:
ix = chunk.index
i = ix.index
c = chunk.counts
for j in range(len(i)):
kmer_dict = i[j]
for key in kmer_dict.keys():
if key not in merged_index[j].keys():
merged_index[j][key] = kmer_dict[key]
for key in c.keys():
self.counts[key] += c[key]
self.index.index = merged_index
#return index
def compare_in_parallel(self, bc_len, threads):
barcodes = sorted(self.counts.keys())
bc_chunks = self.get_barcode_chunks(barcodes)
comparing_gen = (
self.compare_chunk,
bc_chunks,
itertools.repeat(bc_len),
itertools.count(start=0, step=1),
)
with ProcessPoolExecutor(max_workers = threads) as proc:
chunks = proc.map(*comparing_gen, chunksize = 1)
for chunk in chunks:
edges = chunk.edges
dists = chunk.dists
for key in edges.keys():
self.edges[key].extend(edges[key])
for key in dists.keys():
self.dists[key] = dists[key]
def index_bc_single_thread(self, barcodes, bc_len):
for sequence in barcodes:
if len(sequence) == bc_len + 1:
sequence = sequence[:-1]
if len(sequence) == bc_len:
num = rank(sequence, bc_len)
if num in self.counts.keys():
self.counts[num] +=1
else:
#self.barcodes[num] = sequence
#self.numbered[sequence] = num
num = rank(sequence, bc_len)
self.counts[num] = 1
self.index.add_to_index(sequence, num)
#return index
def graph_construction(self, barcodes, bc_len, threads): #could bc_len be a self.len or something like that?
self.index = QGramIndex(self.threshold, bc_len, 6)
num = 0
if threads > 1:
self.index_bc_in_parallel(barcodes, bc_len, threads)
#bc_chunks # don't knoe how I get them yet
self.compare_in_parallel(bc_len, threads)
else:
logger.info("Using edlib")
self.index_bc_single_thread(barcodes, bc_len)
for bc_rank in self.counts.keys():
# info: keys of counts are still the sequences but everywhere else I use the rank
# that means that I need to rank once in the beginning
# and then for everything that is in the closest set I need to unrank
# is this really more efficient than keeping them in a dictionary and looking them up?
# for multithreaded it probably is, and much more space efficient, but I am not sure about single threaded
barcode = unrank(bc_rank, bc_len)
# this is comparing to everything in the index so I do some comparisons multiple times! I might need to rewrite the "closest" function to only compare to lexicographically higher
# however that is only partly what I want for the parallel right? no I think maybe that still makes sense
# If I rewrite already closest might also make
# I might actually have to use the other ranking function because then it should be lexicographically sorted in counts
# and then I just do only compare to things that are higher in the counts
# but does that work with closest or do I then need to do it entirely different
# because in index it is first sorted by k-mer rank and then a dict
# and dicts are not sorted
closest = self.index.get_close(barcode, bc_rank)
for seq_rank in closest:
if seq_rank > bc_rank:
sequence = unrank(seq_rank, bc_len)
if sequence == "" or sequence == barcode:
continue
else:
dist = min(editdistance.eval(barcode, sequence), editdistance.eval(barcode[:-1],sequence), editdistance.eval(barcode,sequence[:-1]))
if dist <= self.threshold:
self.edges[bc_rank].append(seq_rank)
self.edges[seq_rank].append(bc_rank)
self.dists[(bc_rank, seq_rank)] = dist
self.dists[(seq_rank, bc_rank)] = dist
#for s in self.barcodes.keys():
# seq = self.barcodes[s]
# closest = index.get_close(sequence, num)
# for s in closest:
# seq = self.barcodes[s]
# if seq == "" or seq == sequence:
# continue
# else:
# # test: min of three distances to possibly ignore last position for indels
# #dist = editdistance.eval(sequence, seq)
# #dist = min(editdistance.eval(sequence, seq), editdistance.eval(sequence[:-1],seq), editdistance.eval(sequence,seq[:-1]))
# #r1 = edlib.align(sequence, seq, mode = "SHW")
# #r2 = edlib.align(seq, sequence, mode = "SHW")
# #r = edlib.align(sequence, seq, mode = "NW", task = "path")
# #dist = r["editDistance"]
# #path = r["cigar"]
# #if path[-1] == 'I' or path[-1] == 'D':
# # dist = dist -1
# #dist = min(r1["editDistance"], r2["editDistance"])
# dist = min(distance(sequence, seq, score_cutoff = 1), distance(sequence[:-1],seq, score_cutoff = 1), distance(sequence,seq[:-1], score_cutoff = 1))
# #score = get_score(sequence, seq)
# if dist <= self.threshold:
# #if score >= 16*3 - 4: ## fully equal is 3*len, 2 indels would be -4
# self.edges[num].append(s)
# self.edges[s].append(num)
# # #self.dists[(sequence,seq)] = dist # think if I should add both sides or one is enough
# self.dists[(num,s)] = dist
# self.dists[(s,num)] = dist
# num += 1
# if num%500000 == 0:
# logger.info(f"processed {num} distinct barcodes")
def visualize_graph(self):
#G = nx.Graph()
#G.add_nodes_from(self.counts.keys())
edges = []
for edge in self.dists.keys():
#G.add_edge(edge[0], edge[1], weight = self.dists[edge])
edges.append([edge[0], edge[1]])
#G = nx.complete_graph(5)
#nx.draw(G)
#plt.show()
g = ig.Graph(n = len(self.counts.keys()), edges = edges)
unconnected = []
for i in range(0, len(self.counts.keys())):
if g.degree(i) == 0:
unconnected.append(i)
g.delete_vertices(unconnected)
layout = g.layout("fr")
ig.plot(g, target = 'graph_dist_2.pdf', vertex_size = 1, layout = layout, edge_color = ['red', 'black'])
def cluster(self, true_barcodes, barcode_list, n_cells, bc_len):
#self.clustered = [False for node in self.counts.keys()]
sorted_counts = dict(sorted(self.counts.items(), key=lambda item: item[1],reverse = True))
bc_by_counts = list(sorted_counts.keys())
tbcs = []
n = 0
i = 0
if true_barcodes:
tbcs = [rank(bc, bc_len) for bc in true_barcodes]
elif barcode_list:
while n < n_cells and i < len(bc_by_counts):
#print(bc_by_counts[i])
if unrank(bc_by_counts[i], bc_len) in barcode_list:
tbcs.append(bc_by_counts[i])
n+=1
i+=1
else:
tbcs = bc_by_counts[:n_cells]
for tbc in tbcs:
# tbc = rank(tbc, bc_len)
self.clusters[tbc] = [tbc]
self.clustering[tbc] = (tbc, 0)
self.clustered[tbc] = True
for i in range(1,3):
print(i)
for center in self.clusters.keys():
for n in range(len(self.clusters[center])):
node = self.clusters[center][n]
for neighbor in self.edges[node]:
if not self.clustered[neighbor]:
self.clusters[center].append(neighbor)
self.clustering[neighbor] = (center,i)
self.clustered[neighbor] = True
elif self.clustering[neighbor][0] != center and self.clustering[neighbor][0] != -1:
if self.clustering[neighbor][1] == i:
self.clusters[self.clustering[neighbor][0]].remove(neighbor)
self.clustering[neighbor] = (-1, -1)
#move everything statistics related to a different file, instead of self then just give it the graph
#doesn't have to be in a class, more like common
def graph_statistics(self, true_barcodes):
components = []
singletons = []
lengths = []
false_components = []
visited = [False for node in self.barcodes.keys()]
both = 0
degree_better = 0
count_better = 0
min_dist = 32
dists = []
dists_d = []
dists_c = []
dists_n = []
best_is_max = 0
n = 0
correct_in_component = []
for node in self.barcodes.keys():
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
if len(component) == 1:
singletons.append(component[0])
#print(component)
else:
lengths.append(len(component))
#print("Component length:", len(component))
correct = 0
correct_nodes = []
# if len(component) > 80:
# G = nx.Graph()
# G.add_nodes_from(component)
# for node in component:
# for neighbour in self.edges[node]:
# G.add_edge(node, neighbour, weight = self.dists[(node, neighbour)] * 10)
# figname = "lserved_assignments[bc] = tbcarge_component_" + str(n) + ".png"
# nx.draw(G, node_size = 50)
# plt.savefig(figname)
# n += 1
min_dist_n = 32
min_bc_n = -1
min_node = -1
max_degree = 0
max_degree_node = -1
max_count = 0
max_count_node = -1
for node in component:
node_dist = 32
if len(self.edges[node]) == 0:
print(component)
print("Singleton?")
if len(self.edges[node]) > max_degree:
max_degree = len(self.edges[node])
max_degree_node = node
if self.counts[self.barcodes[node]] > max_count:
max_count = self.counts[self.barcodes[node]]
max_count_node = node
for bc in true_barcodes:
dist = editdistance.eval(self.barcodes[node], bc)
if dist < min_dist_n:
min_dist_n = dist
min_node = node
min_bc_n = bc
if dist < node_dist:
node_dist = dist
if node_dist == 0:
correct_nodes.append(node)
correct +=1
dists_n.append(min_dist_n)
if len(self.edges[max_count_node]) == max_degree:
max_degree_node = max_count_node
# #best_is_max += (min_node == max_degree_node or min_node == max_count_node)
#print("New component")
#print("Number of nodes:", len(component))
#print("Node with highest degree:", max_degree_node, "degree:", max_degree)
#print("Count of the node with the highest degree:", self.counts[self.barcodes[max_degree_node]])
#print("Node with highest count:", max_count_node, "count:", max_count)
#print("Degree of the node with the highest count:", len(self.edges[max_count_node]))
min_bc_c = -1
min_bc_d = -1
min_dist_c = 32
min_dist_d = 32
for bc in true_barcodes:
dist_c = editdistance.eval(self.barcodes[max_count_node], bc)
dist_d = editdistance.eval(self.barcodes[max_degree_node], bc)
if dist_c < min_dist_c:
min_dist_c = dist_c
min_bc_c = bc
if dist_d < min_dist_d:
min_dist_d = dist_d
min_bc_d = bc
#print("Closest true barcode to max degree:", min_bc_d, "Distance:", min_dist_d)
#print("Closest true barcode to max count:", min_bc_c, "Distance:", min_dist_c)
#print("Closest true barcode to any node:", min_bc_n, "Distance:", min_dist_n)
both += (max_degree_node == max_count_node)
count_better += (min_dist_c < min_dist_d)
degree_better += (min_dist_d < min_dist_c)
if min_dist_c < min_dist:
min_dist = min_dist_c
if min_dist_d < min_dist:
min_dist = min_dist_d
dists.append(min_dist_c)
dists.append(min_dist_d)
dists_c.append(min_dist_c)
dists_d.append(min_dist_d)
best_is_max += (min_dist_n == min_dist_d or min_dist_n == min_dist_c)
if min(min_dist_d, min_dist_c, min_dist_n) > 1:
#print(len(component))
false_components.append(len(component))
#print("Number of correct barcodes in the component:", correct)
if correct > 1:
bc_in_component = []
G = nx.Graph()
color_map = []
G.add_nodes_from(component)
for node in component:
for neighbour in self.edges[node]:
G.add_edge(node, neighbour, weight = self.dists[(node, neighbour)] * 10)
for node in G:
if self.barcodes[node] in true_barcodes:
bc_in_component.append(self.barcodes[node])
color_map.append('red')
else:
color_map.append('blue')
#nx.draw(G, node_size = 50, node_color = color_map)
#plt.show()
for bc in bc_in_component:
for bc2 in bc_in_component:
if bc!= bc2:
dist = editdistance.eval(bc,bc2)
if dist <= 1:
print(bc, bc2, "distance:", dist)
correct_in_component.append(correct)
components.append(component)
single_counts = []
for node in singletons:
single_counts.append(self.counts[self.barcodes[node]])
print("number of components:", len(components))
print("number of singletons", len(singletons))
print("maximal component size", max(lengths))
print("Number of components with equal max degree and max count node:", both)
print("Number of times max count has closer match than max degree:", count_better)
print("Number of times max degree has closer match than max count:", degree_better)
print("Minimum distance of any max node to a true barcode:", min_dist)
print("Number of times the node with minimum distance to a true barcode is a max node:", best_is_max)
# plt.hist(dists)
# plt.title("Distances of all max nodes")
# plt.show()
# plt.hist(dists_c)
# plt.title("Distances of maximum count nodes")
# plt.show()
# plt.hist(dists_d)
# plt.title("Distances of maximum degree nodes")
# plt.show()
# plt.hist(dists_n)
# plt.title("Minimum distance in each component")
# plt.show()
plt.hist(lengths, bins = 100)
plt.title("Size of the components")
plt.yscale("log")
plt.show()
plt.hist(lengths, bins = 500)
plt.title("Size of the components")
plt.yscale("log")
plt.show()
lengths.remove(max(lengths))
plt.hist(lengths, bins = 100)
plt.title("Size of the components without the largest component")
plt.yscale("log")
plt.show()
plt.hist(correct_in_component, bins = 1000)
plt.title("Number of correct barcodes in components")
plt.yscale("log")
plt.show()
plt.hist(correct_in_component, bins = 100)
plt.title("Number of correct barcodes in components")
plt.yscale("log")
plt.show()
correct_in_component.remove(max(correct_in_component))
plt.hist(correct_in_component, bins = 5)
plt.title("Number of correct barcodes in components")
plt.yscale("log")
plt.show()
plt.hist(false_components, bins = 100)
plt.title("Size of components with no true barcodes")
plt.yscale("log")
plt.show()
plt.hist(single_counts, bins = 20)
plt.title("Counts of singleton nodes")
plt.yscale("log")
plt.show()
# self.closest_true(singletons, true_barcodes)
def closest_true(self, singletons, true_barcodes):
true_barcodes = set(true_barcodes)
closest = []
dists = []
seqs = []
for node in singletons:
min_bc = -1
min_dist = 32
for bc in true_barcodes:
dist = editdistance.eval(self.barcodes[node], bc)
if dist < min_dist:
min_bc = bc
min_dist = dist
closest.append(min_bc)
dists.append(min_dist)
seqs.append(self.barcodes[node])
if min_bc == -1:
print("nothing remotely close")
df = pd.DataFrame(list(zip(seqs, closest, dists)), columns = ['singletons', 'closest true barcode', 'distance'])
df.to_csv('singletons.tsv', sep = '\t')
print(min(dists))
plt.hist(dists)
plt.title("Minimum distance of each singleton")
plt.show()
def get_assignments(self, true_barcodes, components):
observed_assignments = defaultdict(str)
for component in components:
correct_nodes = []
for node in component:
bc = self.barcodes[node]
if bc in true_barcodes:
correct_nodes.append(bc)
for node in component:
bc = self.barcodes[node]
min_dist = 32
for tbc in correct_nodes:
dist = editdistance.eval(tbc, bc)
#dist = min(editdistance.eval(tbc, bc),editdistance.eval(tbc[:-1], bc),editdistance.eval(tbc, bc[:-1]))
if dist < min_dist:
min_dist = dist
observed_assignments[bc] = tbc
return observed_assignments
def assigned_stats(self, barcodes, title):
counts = []
degree = []
for bc in barcodes:
node = self.numbered[bc]
counts.append(self.counts[bc])
degree.append(len(self.edges[node]))
plt.hist(counts, bins = 30)
plt.title(("Counts of" +title +"barcodes"))
plt.show()
plt.hist(degree, bins = 30)
plt.title(("Degree of" +title +"barcodes"))
plt.show()
def assign_by_cluster(self, bc_len):
observed_assignments = defaultdict(str)
for node in self.counts.keys():
if self.clustered[node] and self.clustering[node][0] != -1:
bc = unrank(node, bc_len)
tbc = unrank(self.clustering[node][0], bc_len)
observed_assignments[bc] = tbc
return observed_assignments
def compare_results(self, true_assignment, true_barcodes):
components = []
visited = [False for node in self.barcodes.keys()]
for node in self.barcodes.keys():
#print(node)
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
components.append(component)
observed_assignments = self.get_assignments(true_barcodes, components)
#observed_assignments = self.assign_by_cluster()
n_correct_assignments = 0
n_correct_in_component = 0
n_incorrect = 0
n_unobserved = 0
n_unassigned = 0
unobserved_barcodes = set()
distances_correct = []
distances_in_component = []
correctly_assigned = []
incorrectly_assigned = set()
correct_in_component = []
incorrect_component_size = []
unassigned = set()
correctly_tbcs = []
component_tbcs = []
for component in components:
#if len(component) > 10000:
observed_true = 0
for node in component:
if self.barcodes[node] in true_barcodes:
observed_true += 1
for node in component:
bc = self.barcodes[node]
for tbc in true_assignment[bc].keys():
#for tbc in true_assignment[bc]:
if tbc in self.numbered.keys():
correct_bc = self.numbered[tbc]
#if correct_bc in self.edges[node]:
#n_correct_assignments += 1
#n_correct_in_component += 1
if tbc == observed_assignments[bc]:
n_correct_assignments += true_assignment[bc][tbc]
correctly_assigned.append(bc)
correctly_tbcs.append(tbc)
#n_correct_assignments += 1
for i in range(true_assignment[bc][tbc]):
distances_correct.append(editdistance.eval(bc, tbc))
elif correct_bc in component:
if observed_assignments[bc] == "":
n_unassigned += true_assignment[bc][tbc]
unassigned.add(bc)
else:
n_correct_in_component += true_assignment[bc][tbc]
# if n_correct_in_component <= 100:
# print("correct in component")
# print("barcode:", bc)
# print("assigned barcode:", observed_assignments[bc], "count", self.counts[bc], "dist", editdistance.eval(bc, observed_assignments[bc]))
# print("true barcode:", tbc, "count", true_assignment[bc][tbc], "dist", editdistance.eval(bc,tbc))
#n_correct_in_component += 1
for i in range(true_assignment[bc][tbc]):
distances_in_component.append(editdistance.eval(bc, observed_assignments[bc]))
correct_in_component.append(bc)
component_tbcs.append(observed_assignments[bc])
#print("distance of bc and true bc:" , editdistance.eval(bc, tbc))
#print("distance of bc and observed assignment:" , editdistance.eval(bc, observed_assignments[bc]))
else:
#print("Correct barcode in different component")
#print("Barcode:", self.barcodes[node], "count:", self.counts[self.barcodes[node]])
#print("True assignment:", true_assignment[self.barcodes[node]])
#print("Size of the component:", len(component))
#print("Number of true barcodes in the component:", observed_true)
if observed_assignments[bc] == "":
n_unassigned += true_assignment[bc][tbc]
unassigned.add(bc)
else:
n_incorrect += true_assignment[bc][tbc]
# if n_incorrect <= 100:
# print("incorrect")
# print("barcode:", bc)
# print("assigned barcode:", observed_assignments[bc], "count", self.counts[bc], "dist", editdistance.eval(bc, observed_assignments[bc]))
# print("true barcode:", tbc, "count", true_assignment[bc][tbc], "dist", editdistance.eval(bc,tbc))
#n_incorrect += 1
incorrectly_assigned.add(bc)
incorrect_component_size.append(len(component))
else:
#print("correct barcode never observed")
unobserved_barcodes.add(tbc)
#print(tbc)
n_incorrect += 1
n_unobserved += 1
print("adding count for each distinct barcode")
#print("only for largest component")
print("n_correct_in_component:", n_correct_in_component)
print("n_correct_assignments:", n_correct_assignments)
print("n_incorrect:", n_incorrect)
print("n_unassigned:", n_unassigned)
print("Number of never observed barcodes:", len(unobserved_barcodes))
print("Number of times a barcode is unobserved:", n_unobserved)
# correct = pd.DataFrame({"barcode": correctly_assigned,
# "cluster": correctly_tbcs})
# correct.to_csv('correctly_3it.tsv', sep = '\t')
# comp = pd.DataFrame({"barcode": correct_in_component,
# "cluster": component_tbcs})
# comp.to_csv('component_3it.tsv', sep = '\t')
#plt.hist(incorrect_component_size, bins = 50)
#plt.title("Size of components containing bcs with true bc in another component")
#plt.show()
#plt.hist(distances_correct)
#plt.title("Distance to closest true barcode in the component, assignment correct, with counts")
#plt.show()
#plt.hist(distances_in_component)
#plt.title("Distance to closest true barcode in the component, assignment incorrect, true barcode in component, with counts")
#plt.show()
#self.assigned_stats(unassigned, " not assigned ")
# correct_barcode = self.numbered[true_assignment[self.barcodes[node]]] # figure out what to do if not in the list, maybe start my numbering with 1 so 0 means it doesn't exist or can I change defaultdict to default = -1?
# if correct_barcode in self.edges[node]:
# n_correct_assignments += 1
# n_correct_in_component += 1
# elif correct_barcode in component:
# n_correct_in_component += 1
# else:
# print("Correct barcode in different component")
# print("Barcode:", node, "count:", self.counts[self.barcodes[node]])
# print("True assignment:", correct_barcode, true_assignment[self.barcodes[node]])
def compare_to_cluster(self, true_barcodes, true_assignment):
components = []
visited = [False for node in self.barcodes.keys()]
for node in self.barcodes.keys():
#print(node)
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
components.append(component)
cluster_assignment = self.assign_by_cluster()
assignment = self.get_assignments(true_barcodes, components)
for bc in self.numbered.keys():
if cluster_assignment[bc] != assignment[bc]:
print("barcode:", bc)
print("cluster assignment:", cluster_assignment[bc], "dist:", editdistance.eval(bc, cluster_assignment[bc]))
print("original assignment:", assignment[bc], "dist:", editdistance.eval(bc, assignment[bc]))
if cluster_assignment[bc] in true_assignment[bc].keys():
print("Cluster assignment correct")
if assignment[bc] in true_assignment[bc].keys():
print("Original assignment correct")
def true_barcode_stats(self, true_barcodes):
counts = []
f_counts = []
degree = []
f_degree = []
component_size = []
f_component_size = []
components = []
visited = [False for node in self.barcodes.keys()]
for node in self.barcodes.keys():
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
components.append(component)
print("Before")
for component in components:
#if len(component) < 30000:
for node in component:
if self.barcodes[node] in true_barcodes:
component_size.append(len(component))
counts.append(self.counts[self.barcodes[node]])
degree.append(len(self.edges[node]))
else:
f_component_size.append(len(component))
f_counts.append(self.counts[self.barcodes[node]])
f_degree.append(len(self.edges[node]))
if self.counts[self.barcodes[node]] > 50:
print(self.barcodes[node], self.counts[self.barcodes[node]])
#for bc in true_barcodes:
# counts.append(self.counts[bc])
# tbc = self.numbered[bc]
# degree.append(len(self.edges[tbc]))
print("After")
print("Minumum count:", min(counts))
print("Minimum degree:", min(degree))
plt.figure()
plt.hist(counts, bins = 75, log = True)
plt.hist(f_counts, bins = 14, color = "red", log = True)
plt.title("Counts of all barcodes, true blue, other red")
plt.savefig("counts_all_P7.10p.png")
#plt.show()
plt.figure()
plt.hist(degree, bins = 25)
plt.hist(f_degree, bins = 25, color = "red", log = True)
plt.title("Degrees of all barcodes, true blue, other red")
plt.savefig("degrees_all_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(component_size, counts, alpha = 0.2)
plt.scatter(f_component_size, f_counts, c = "red", alpha = 0.2)
plt.title("Counts of barcodes by component size, true blue, other red")
plt.savefig("counts_all_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(component_size, degree, alpha = 0.2)
plt.scatter(f_component_size, f_degree, c = "red", alpha = 0.2)
plt.title("Degrees of barcodes by component size, true blue, other red")
plt.savefig("degrees_all_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(f_component_size, f_degree, c = "red")
plt.title("Degrees of not true barcodes by component size")
plt.savefig("degrees_nt_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(f_component_size, f_counts, c = "red")
plt.title("Counts of not true barcodes by component size")
plt.savefig("counts_nt_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.hist(f_counts, bins = 80, color = "red", log = True)
plt.title("Counts of not true barcodes")
plt.savefig("counts_nt_P7.10p.png")
#plt.show()
plt.figure()
plt.hist(f_degree, bins = 25, color = "red")
plt.title("Degrees of not true barcodes")
plt.savefig("degrees_nt_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(component_size, counts, c = "blue")
plt.title("Degrees of true barcodes by component size")
plt.savefig("degrees_t_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.scatter(component_size, degree, c = "blue")
plt.title("Counts of true barcodes by component size")
plt.savefig("counts_t_by_size_P7.10p.png")
#plt.show()
plt.figure()
plt.hist(counts, bins = 80, color = "blue", log = True)
plt.title("Counts of true barcodes")
plt.savefig("counts_t_P7.10p.png")
#plt.show()
plt.figure()
plt.hist(degree, bins = 25, color = "blue")
plt.title("Degrees of true barcodes")
plt.savefig("degrees_t_P7.10p.png")
#plt.show()
def components_without_true(self, true_barcodes, true_assignment):
sizes = []
dists = []
components = []
num = 0
comp = 0
bc_to_comp = {}
visited = [False for node in self.barcodes.keys()]
for node in self.barcodes.keys():
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
components.append(component)
min_dist = 32
min_bc = -1
for node in component:
bc = self.barcodes[node]
for tbc in true_barcodes:
dist = editdistance.eval(bc,tbc)
if dist < min_dist:
min_dist = dist
min_bc = tbc
if dist == 0:
bc_to_comp[tbc] = component
comp += 1
for component in components:
min_dist = 32
min_bc = -1
actual_bc = []
for node in component:
bc = self.barcodes[node]
for tbc in true_assignment[bc].keys():
actual_bc.append(tbc)
for tbc in true_barcodes:
dist = editdistance.eval(bc,tbc)
if dist < min_dist:
min_dist = dist
min_bc = tbc
if min_dist > 0:
sizes.append(len(component))
dists.append(min_dist)
if len(component) > 10:
print("Component without true barcode")
print([self.barcodes[x] for x in component])
print("Closest true barcode:", min_bc)
print("Distance to closest true barcode:", min_dist)
print("Actual true barcodes:")
print(actual_bc)
#print("Component containing closest true")
#print([self.barcodes[x] for x in bc_to_comp[min_bc]])
## investigate looking into what is actually the barcode something is connected to in these => is it the closest true for any of them? If not, extraction error?
num += 1
# G = nx.Graph()
# G.add_nodes_from(component)
# for node in component:
# for neighbour in self.edges[node]:
# G.add_edge(node, neighbour, weight = self.dists[(node, neighbour)] * 10)
# figname = "no_true_" + str(num) + ".png"
# plt.figure()
# nx.draw(G, node_size = 20)
# plt.savefig(figname)
print("Number of components without true barcodes > 10:", num)
# plt.hist(sizes, bins = 100, log = True)
# plt.title("Sizes of components without true barcodes")
# plt.show()
# plt.hist(dists)
# plt.title("Distances to closest true barcode from components without")
# plt.show()
def large_component(self, true_barcodes):
components = []
visited = [False for node in self.barcodes.keys()]
subgraphs = []
l_component = []
for node in self.barcodes.keys():
#print(node)
if not visited[node]:
component, visited = dfs_without_recursion(visited, node, [], self.edges)
components.append(component)
if len(component) > 10000:
l_component = component
break
visited = [False for node in self.barcodes.keys()]
#print(l_component)
for tbc in true_barcodes:
tbc = self.numbered[tbc]
if tbc in l_component:
print("here")
subgraph = []
c = 100
if not visited[tbc]:
stack = []
stack.append(tbc)
while stack and c > 0:
node = stack.pop()
if not visited[node]:
subgraph.append(node)
print("subgraph")
visited[node] = True
c = c - 1
for i in self.edges[node]:
stack.append(i)
bc_in_component = []
G = nx.Graph()
color_map = []
G.add_nodes_from(subgraph)
for node in subgraph:
for neighbour in self.edges[node]:
G.add_edge(node, neighbour, weight = self.dists[(node, neighbour)] * 10)
for node in G:
edge_node = False
for bc in self.edges[node]:
if not visited[bc]:
edge_node = True
if self.barcodes[node] in true_barcodes:
bc_in_component.append(self.barcodes[node])
color_map.append('red')
elif edge_node:
color_map.append('green')
else:
color_map.append('blue')