-
Notifications
You must be signed in to change notification settings - Fork 0
/
topone.py
605 lines (459 loc) · 22.5 KB
/
topone.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
import numpy as np
import pandas as pd
from ripser import Rips
from simseq import SimSeq
from virseq import VirSeq
from plot_diagrams import plot_diagrams
class TopONE:
def __init__(self,
samples,
seqtype="SIM",
maxdim=2,
simulations=None,
segsites=None,
):
"""
Initializes an instance of the class
--------------------
Parameters:
samples : int
Number of samples or sequences in the input file
seqtype : String, default = "SIM"
Defines the type of sequence used in the class, which can be:
SIM - use of simulated sequences in biallelic (0 and 1) format
VIR - use of viral sequences in ATCG format
maxdim : int, default = 2
Highest homology dimension to be used in the computation
of homologies, default set at 2, or the H2 homology group
simulations : int, default = None
Number of simulations used in an input file.
Applicable only for "SIM" seqtype.
segsites : int, default = None
Number of segregating sites in an input file.
"""
# Assign class variables to instantiation parameters
self.SAMPLES = samples
self.SEQTYPE = seqtype
self.MAXDIM = maxdim
self.SIMULATIONS = simulations
self.SEGSITES = segsites
# Create varlist and spalist for adding noise,
# and taking fractions of the population
self.varlist = np.around(np.linspace(0, 100, 21),
decimals=2).tolist()
self.spalist = np.around(np.linspace(0.1, 1, 10),
decimals=2).tolist()
# Instantiate Ripser for persistent homology computations
# using the maxdim parameter
self.ripser = Rips(maxdim=maxdim, verbose=False)
# If the given seqtype by the user is neither SIM nor VIR
# raise an error
if self.SEQTYPE not in ["SIM", "VIR"]:
raise Exception("Seqtype mode does not exist. " +
"Please choose between \"SIM\" and \"VIR\".")
# Instantiate the helper functions from SimSeq and VirSeq
self.simseq = SimSeq()
self.virseq = VirSeq()
# Place all parameters in a dictionary `params`
self.params = {
"SAMPLES": self.SAMPLES,
"SEQTYPE": self.SEQTYPE,
"MAXDIM": self.MAXDIM,
"SIMULATIONS": self.SIMULATIONS,
"SEGSITES": self.SEGSITES,
"VARLIST": self.varlist,
"SPALIST": self.spalist
}
def fit_transform(self, data, is_dist_mat=True):
"""
Produces the birth and death time pairs in each homology,
sorting the pairs according to increasing death time.
--------------------
Parameters:
data : numpy array
The data used to produce the birth and death time
pairs, which can be a distance matrix or a pair of numbers
is_dist_mat : boolean, default = True
A boolean value that dictates whether the data passed
is a distance matrix or not
Returns:
hom : list
The birth and death time pairs of each homology
from H_0 to H_maxdim.
"""
hom = self.ripser.fit_transform(data,
distance_matrix=is_dist_mat)
hom[0] = np.delete(hom[0], -1, axis=0)
for i in range(self.ripser.maxdim + 1):
hom[i] = hom[i][np.argsort(hom[i][:, 1])]
return hom
def switch_seqtype(self):
"""
Switches the sequence type from SIM to VIR or vice versa.
"""
if self.SEQTYPE == "SIM":
self.SEQTYPE = "VIR"
else:
self.SEQTYPE = "SIM"
print(f"Switched SEQTYPE to {self.SEQTYPE}.")
def get_sample_sequences(self, fname):
"""
Gets the sample sequences from an input file.
For seqtype SIM, the sequences in each simulation are obtained
from a generated population, stored in a text file.
For seqtype VIR, the sequences are stored in a FASTA file.
--------------------
Parameters:
fname : String
The filename of the input file.
Returns:
sequences : list
The list of sequences taken from the input files.
"""
sequences = None
if self.SEQTYPE == "SIM":
sequences = self.simseq.get_sample_sequences(fname, self.params)
else:
sequences = self.virseq.get_sample_sequences(fname, self.params)
return sequences
def get_hdmatrices(self, sequences):
"""
Produces the hamming distance matrix of a collection of sequences.
--------------------
Parameters:
sequences : list
[SEQTYPE - VIR] a 1-D list of dimension (SAMPLES) containing
the processed biological sequences from viral samples.
[SEQTYPE - SIM] a 2-D list of dimension (SIMULATION, SAMPLES)
containing all simulations each with a list of in biallelic (0 and 1)
formatted sequences.
Returns:
hdmatrices : list
[SEQTYPE - VIR] a 2-D matrix of dimension (SAMPLES, SAMPLES).
[SEQTYPE - SIM] a 3-D list of dimension (SIMULATION, SAMPLES, SAMPLES)
where each simulation has a 2-D matrix of dimension (SAMPLES, SAMPLES).
"""
hdmatrices = None
if self.SEQTYPE == "SIM":
hdmatrices = self.simseq.get_hdmatrices(sequences,
self.params)
else:
hdmatrices = self.virseq.get_hdmatrices(sequences,
self.params)
return hdmatrices
def get_all_homologies(self, hdmatrices):
"""
Used exclusively for SEQTYPE SIM.
Gets the homologies for each Hamming distance matrix per simulation.
Computing homologies per simulation is repeated 21 times for each variance value
in "varlist" to add noise to the matrices, or 10 times for each sparsity value in
"spalist" to apply to the population of sequences.
--------------------
Parameters:
hdmatrices : list
A 3-D list of dimension (SIMULATIONS, SAMPLES, SAMPLES), containing
the Hamming distance matrices of per simulation. For noise-added matrices,
the matrix dimensions are (SAMPLES, SAMPLES). For matrices from sparse populations,
the dimensions are (SAMPLES*X, SAMPLES*X), where X is a value in "spalist".
Returns:
all_homologies : list
A 4-D list of dimension (21/10, SIMULATIONS, MAXDIM+1, X), where
for every value of variance (21) or sparsity (10), each simulation has a
list of (MAXDIM+1) homologies, with each homology group having X pairs of
birth and death times.
"""
all_homologies = []
for sim in hdmatrices:
hom_grps = []
for i in range(len(sim)):
hom_grps.append(self.fit_transform(sim[i], True))
all_homologies.append(hom_grps)
return all_homologies
def get_all_topoquants(self, homologies):
"""
Used exclusively for SEQTYPE SIM.
Gets the topological quantities (Betti numbers, barcode length mean, barcode length variance)
in each list of homologies per simulation. Computing topological quantities is
repeated 21 times for every variance value in "varlist" or 10 times for every
sparsity value in "spalist".
--------------------
Parameters:
homologies : list
A 4-D list of dimension (21/10, SIMULATIONS, MAXDIM+1, X), where
for every value of variance (21) or sparsity (10), each simulation has a
list of (MAXDIM+1) homologies, with each homology group having X pairs of
birth and death times.
Returns:
betti_nums : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a
list of length (MAXDIM+1) containing the Betti numbers of each homology group.
bcode_mean_lens : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length mean for each homology group.
bcode_vars_lens : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length variance for each homology group.
"""
betti_nums = []
bcode_mean_lens = []
bcode_vars_lens = []
# 21/10
for i in range(len(homologies)):
sim_betti_nums = []
sim_bcode_mean_lens = []
sim_bcode_vars_lens = []
# SIMULATIONS
for j in range(len(homologies[i])):
hom = homologies[i][j]
# MAXDIM + 1
bn = self.get_betti_numbers(hom)
bl = self.get_barcode_lengths(hom)
bml, blv = self.get_barcode_length_statistics(bl)
sim_betti_nums.append(bn)
sim_bcode_mean_lens.append(bml)
sim_bcode_vars_lens.append(blv)
betti_nums.append(sim_betti_nums)
bcode_mean_lens.append(sim_bcode_mean_lens)
bcode_vars_lens.append(sim_bcode_vars_lens)
return betti_nums, bcode_mean_lens, bcode_vars_lens
def sparsity_sampling(self, sequences):
"""
Used exclusively for SEQTYPE SIM.
Takes a fraction of sequences from the population of sequences, where each
fraction or percentage to apply to the sequences is taken from "spalist", a
list containing fractional values from 0.1 to 1.0 in 0.1 increments.
--------------------
Parameters:
sequences : list
A 2D list of dimension (SIMULATION, SAMPLES)
where each simulation contains the population of biallelic sequences.
Returns:
sparse_samples : list
A 3-D list of dimension (10, SIMULATIONS, SAMPLES),
where each element contains a fraction of the populations in each
simulation with varying values of sparsity.
"""
sparse_samples = None
if self.SEQTYPE == "SIM":
sparse_samples = self.simseq.sparsity_sampling(sequences,
self.params)
else:
print("Error! This is used for simulated sequence types only.")
return sparse_samples
def add_stochasticity(self, hdmatrices):
"""
Used exclusively for SEQTYPE SIM.
Adds stochasticity or noise to the Hamming distance matrices. The noise is taken
from a Normal distribution of mean = 0 and standard deviation of sqrt(var), where
var is a value in the list "varlist" and is added to the original Hamming distance
matrices.
--------------------
Parameters:
hdmatrices : list
A 3-D list of dimension (SIMULATION, SAMPLES, SAMPLES)
where each simulation has a 2-D matrix of dimension (SAMPLES, SAMPLES).
Returns:
hdmatrices_stoch : list
A 4-D list of dimension (21, SIMULATION, SAMPLES, SAMPLES)
where 21 HD matrices are made for every variance value, for each simulation.
"""
hdmatrices_stoch = None
if self.SEQTYPE == "SIM":
hdmatrices_stoch = self.simseq.add_stochasticity(hdmatrices,
self.params)
else:
print("Error! This is used for simulated sequence types only.")
return hdmatrices_stoch
def get_betti_numbers(self, homologies):
"""
Computes for the Betti numbers of every homology
Parameters:
homologies - a 3-D list, where each element is a homology
with a 2-D list of its birth and death time pairs
Returns:
betti_numbers - a 1-D list, where the kth element of the list
represents the kth Betti number of homology k
"""
betti_numbers = np.array([])
for b in homologies:
betti_numbers = np.append(betti_numbers, len(b))
return betti_numbers
def get_barcode_lengths(self, homologies):
"""
Computes for the barcode lengths of every homology.
The barcode lengths are computed as the difference between
the death time from the birth time.
Parameters:
homologies - a 3-D list, where each element is a homology
with a 2-D list of its birth and death time pairs
Returns:
barcode_lengths - a 2-D list, where each element is a homology
with a 1-D list of the barcode lengths
"""
barcode_lengths = []
for i in range(0, self.ripser.maxdim + 1):
barcode_lengths.append(homologies[i][:, 1] - homologies[i][:, 0])
return barcode_lengths
def get_barcode_length_statistics(self, barcode_lengths):
"""
Gets the means and variances of the barcode lengths for every homology.
--------------------
Parameters:
barcode_lengths : list
A 2-D list of dimension (MAXDIM+1, X), containing the
X barcode lengths all (MAXDIM+1) homologies.
Returns:
barcode_length_means : list
A 1-D list with (MAXDIM+1) elements, where the kth element
is the barcode length mean of the kth homology.
barcode_length_vars : list
A 1-D list with (MAXDIM+1) elements, where the kth element
is the variance of the barcode lengths in homology H_k.
"""
barcode_length_means = []
barcode_length_vars = []
for l in barcode_lengths:
barcode_length_means.append(np.mean(l))
barcode_length_vars.append(np.var(l))
return barcode_length_means, barcode_length_vars
def plot(self, hom, title="Persistence Diagram"):
"""
Creates a persistence diagram plot, containing the birth and death time
pairs of each homology group in the topology, and the frequency distributions
of the birth and death times for each homology, except for the H0 birth times.
--------------------
Parameters:
hom : list
A 2-D list of dimension (MAXDIM+1, X), containing the (MAXDIM+1) homology
groups, each with its list of birth and death time pairs.
title : String, default = "Persistence Diagram"
A string containing the title of the persistence diagram.
"""
plot_diagrams.plot(hom, title)
def plot_barcode_diagram(self, betti_nums, hom, title="Barcode Diagram"):
"""
Creates a barcode diagram, that visualizes the lengths of the barcodes for
every homology group, which is the difference of the death times from the birth times.
--------------------
Parameters:
betti_nums : list
A 1-D list of length (MAXDIM+1), where the kth element is the
kth Betti number of the kth homology.
hom : list
A 2-D list of dimension (MAXDIM+1, X), containing the (MAXDIM+1) homology
groups, each with its list of birth and death time pairs.
title : String, default = "Barcode Diagram"
A string containing the title of the barcode diagram.
"""
plot_diagrams.plot_barcode_diagrams(betti_nums, hom, title)
def plot_topoquant_diagram(self, betti_numbers,
barcode_mean_length,
barcode_vars_length,
list_type,
save_as_pdf=False,
pdf_fname=None):
"""
Creates the plots that displays the values of the topological quantities
when variance increases or sparsity increases.
--------------------
Parameters:
betti_nums : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a
list of length (MAXDIM+1) containing the Betti numbers of each homology group.
barcode_mean_length : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length mean for each homology group.
barcode_vars_length : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length variance for each homology group.
list_type : String
A string that determines whether the dataframe will use values from
the varlist ("VARLIST") or spalist ("SPALIST").
save_as_pdf : Boolean, default = False
A Boolean value dictating whether the plot should be saved in
a PDF file or be displayed as is.
pdf_fname : String, default = None
A String containing the filename of the plots in a PDF file format.
"""
plot_diagrams.plot_topoquant_diagram(self.params,
betti_numbers,
barcode_mean_length,
barcode_vars_length,
list_type,
save_as_pdf,
pdf_fname)
def create_topquant_dataframe(self, betti_numbers,
barcode_mean_length,
barcode_vars_length,
list_type,
fname):
"""
Creates the dataframe for the topological quantities per simulation
and per value of sparsity or variance.
--------------------
Parameters:
betti_nums : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a
list of length (MAXDIM+1) containing the Betti numbers of each homology group.
barcode_mean_length : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length mean for each homology group.
barcode_vars_length : list
A 3-D list of dimension (21/10, SIMULATIONS, MAXDIM+1), where for every
value of variance (21) or sparsity (10), each simulation has a list of length
(MAXDIM+1) containing the barcode length variance for each homology group
list_type : String
A string that determines whether the dataframe will use values from
the varlist ("VARLIST") or spalist ("SPALIST").
fname : String
The filename of the dataframe.
"""
val = len(self.params[list_type])
if list_type == "SPALIST":
data_type = "sparsity"
else:
data_type = "variance"
cols = ["simulation",
data_type,
"h0",
"h1",
"h2",
"avg_h0",
"avg_h1",
"avg_h2",
"var_h0",
"var_h1",
"var_h2"]
df = pd.DataFrame(columns=cols)
# Per simulation, per variance value
# 21 or 10
for v in range(val):
# SIMULATIONS
for i in range(self.SIMULATIONS):
if list_type == "SPALIST":
list_val = self.spalist[v]
else:
list_val = self.varlist[v]
row = pd.DataFrame([{"simulation": i,
data_type: list_val,
"h0": betti_numbers[v][i][0],
"h1": betti_numbers[v][i][1],
"h2": betti_numbers[v][i][2],
"avg_h0": barcode_mean_length[v][i][0],
"avg_h1": barcode_mean_length[v][i][1],
"avg_h2": barcode_mean_length[v][i][2],
"var_h0": barcode_vars_length[v][i][0],
"var_h1": barcode_vars_length[v][i][1],
"var_h2": barcode_vars_length[v][i][2],
}])
df = pd.concat([df, row], ignore_index = True)
df.sort_values(by=["simulation", data_type], ascending=[True, True])
df.to_csv(fname, index=False)