-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexternal.py
More file actions
572 lines (507 loc) · 19.7 KB
/
Copy pathexternal.py
File metadata and controls
572 lines (507 loc) · 19.7 KB
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
"""Import models from ProBound, MotifCentral, JASPAR, and HOCOMOCO."""
import io
import json
import re
from collections.abc import Iterable, Sequence
from typing import Any, TypeAlias
import numpy as np
import pandas as pd
import requests
import torch
from Bio import motifs
from Bio.motifs import jaspar
from numpy.typing import NDArray
from pandas import DataFrame
from .aggregate import Aggregate, Contribution
from .alphabets import DNA, Alphabet, Protein
from .cooperativity import Cooperativity, Spacing
from .experiment import Experiment
from .layers import PSAM, Conv1d, Layer, NonSpecific, get_padding_layers
from .loss import MultiExperimentLoss
from .mode import Mode
from .rounds import (
BoundRound,
BoundUnsaturatedRound,
ExponentialRound,
RhoGammaRound,
Round,
)
from .table import CountTable
FloatArray: TypeAlias = NDArray[Any]
def get_fit_final(path: str) -> dict[str, Any]:
"""Gets the best model fit from the fit.models.json output by ProBound."""
fits = []
with open(path, mode="r", encoding="utf-8") as f:
for line in f:
fits.append(json.loads(line))
return min(fits, key=lambda fit: fit["metadata"]["logLikelihood"])
def parse_probound_alphabet(fit_final: dict[str, Any]) -> Alphabet:
"""Parses the alphabet used in a ProBound fit into an Alphabet."""
model_settings = fit_final["modelSettings"]
if (
"letterOrder" not in model_settings
or model_settings["letterOrder"] == "ACGT"
):
return DNA()
if model_settings["letterOrder"] == Protein().alphabet:
return Protein()
if "letterComplement" in model_settings:
letters = ""
for pair in model_settings["letterComplement"].split(","):
letters = pair[0] + letters + pair[-1]
return Alphabet(letters, complement=True)
return Alphabet(model_settings["letterOrder"], complement=True)
def parse_probound_tables(
fit_final: dict[str, Any], dataframes: Iterable[DataFrame]
) -> list[CountTable]:
"""Parses the count tables used in a ProBound fit into CountTables.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
dataframes: The raw count tables, parsed by get_dataframe.
Returns:
A list of CountTable objects.
"""
model_settings = fit_final["modelSettings"]
alphabet = parse_probound_alphabet(fit_final)
max_flank_length = max(
i["flankLength"] for i in model_settings["bindingModes"]
)
count_tables = []
for dataframe, table_settings in zip(
dataframes, model_settings["countTable"], strict=True
):
transliterate = dict(
zip(
table_settings["transliterate"]["in"],
table_settings["transliterate"]["out"],
)
)
count_tables.append(
CountTable(
dataframe[[i + 1 for i in table_settings["modeledColumns"]]],
alphabet,
transliterate=transliterate,
transliterate_flanks=False,
left_flank=table_settings["leftFlank"],
right_flank=table_settings["rightFlank"],
left_flank_length=min(
max_flank_length, len(table_settings["leftFlank"])
),
right_flank_length=min(
max_flank_length, len(table_settings["rightFlank"])
),
)
)
return count_tables
def parse_probound_psam(fit_final: dict[str, Any], mode_idx: int) -> PSAM:
"""Parses a single PSAM from a ProBound fit.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
mode_idx: The index of the binding mode in the fit.
Returns:
A PSAM.
"""
alphabet = parse_probound_alphabet(fit_final)
alphalen = len(alphabet.alphabet)
letter_order = {
alphabet.get_index[val]: i
for i, val in enumerate(fit_final["modelSettings"]["letterOrder"])
}
pairwise_order = {
ori_1 * alphalen + ori_2: new_1 * alphalen + new_2
for ori_1, new_1 in letter_order.items()
for ori_2, new_2 in letter_order.items()
}
mode_settings = fit_final["modelSettings"]["bindingModes"][mode_idx]
mode_coefficients = fit_final["coefficients"]["bindingModes"][mode_idx]
psam = PSAM(
kernel_size=mode_settings["size"],
alphabet=alphabet,
pairwise_distance=min(
mode_settings["dinucleotideDistance"], mode_settings["size"] - 1
),
score_reverse=not mode_settings["singleStrand"],
)
for key, param in psam.betas.items():
elements = [int(i) for i in key.split("-")]
elements = [i - 1 for i in elements[:-1]] + elements[-1:]
if len(elements) == 2:
torch.nn.init.constant_(
param,
mode_coefficients["mononucleotide"][
elements[0] * len(alphabet.alphabet)
+ letter_order[elements[1]]
],
)
else:
torch.nn.init.constant_(
param,
mode_coefficients["dinucleotide"][
elements[1] - elements[0] - 1
][
elements[0] * (len(alphabet.alphabet) ** 2)
+ pairwise_order[elements[2]]
],
)
return psam
def parse_probound_modes(
fit_final: dict[str, Any],
layer_specs: Iterable[NonSpecific | PSAM],
count_table: CountTable,
table_idx: int,
) -> list[Mode]:
"""Parses all modes associated with an experiment in a ProBound fit.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
layer_specs: The NonSpecific or PSAM encoding of the binding modes.
count_table: The CountTable of that experiment, from
parse_probound_tables.
table_idx: The index of the experiment in the ProBound fit.
Returns:
A list of Mode objects.
"""
alphabet = parse_probound_alphabet(fit_final)
modes = []
for mode_idx, layer_spec in enumerate(layer_specs):
mode_settings = fit_final["modelSettings"]["bindingModes"][mode_idx]
posbias = mode_settings["positionBias"]
if isinstance(layer_spec, NonSpecific):
modes.append(Mode.from_nonspecific(layer_spec, count_table))
else:
# Get padding
flank_length = mode_settings["flankLength"]
prev: Layer | CountTable
if (
flank_length < count_table.left_flank_length
or flank_length < count_table.right_flank_length
):
layers = get_padding_layers(
alphabet,
count_table,
min(0, flank_length - count_table.left_flank_length),
min(0, flank_length - count_table.right_flank_length),
)
prev = layers[-1]
else:
layers = []
prev = count_table
# Create mode
layers.append(
Conv1d.from_psam(layer_spec, prev, train_posbias=posbias)
)
mode = Mode(layers)
# Import position bias parameters
if posbias:
for out_idx, _ in enumerate(mode.layers[-1].log_posbias[0]):
mode.layers[-1].log_posbias[0, out_idx] = torch.Tensor(
fit_final["coefficients"]["bindingModes"][mode_idx][
"positionBias"
][table_idx][out_idx][:: 1 if out_idx == 0 else -1]
)
modes.append(mode)
return modes
def parse_probound_interactions(
fit_final: dict[str, Any], modes: Sequence[Mode], table_idx: int
) -> list[Cooperativity]:
"""Parses all interactions associated with an experiment in a ProBound fit.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
modes: The Mode encoding of binding modes, from parse_probound_modes.
table_idx: The index of the experiment in the ProBound fit.
Returns:
A list of Mode objects.
"""
cooperativities = []
for coop_settings, coop_coefficients in zip(
fit_final["modelSettings"]["bindingModeInteractions"],
fit_final["coefficients"]["bindingModeInteractions"],
):
# Get parameters
max_spacing = None
max_overlap = None
if coop_settings["maxSpacing"] != -1:
max_spacing = coop_settings["maxSpacing"]
if coop_settings["maxOverlap"] != -1:
max_overlap = coop_settings["maxOverlap"]
# Create cooperativity
mode_a = modes[coop_settings["bindingModes"][0]]
mode_b = modes[coop_settings["bindingModes"][1]]
cooperativity = Cooperativity(
Spacing(
mode_a.key(),
mode_b.key(),
max_overlap=max_overlap,
max_spacing=max_spacing,
ignore_pad=True,
),
mode_a,
mode_b,
)
cooperativities.append(cooperativity)
# Parse log_posbias
log_posbias = torch.tensor(
coop_coefficients["positionMatrix"][table_idx]
)
log_posbias_shape = cooperativity.get_log_spacing_matrix()[0, 0].shape
log_posbias = log_posbias[
:, :, : log_posbias_shape[-2], : log_posbias_shape[-1]
] # Trim if necessary
assert log_posbias.shape == log_posbias_shape
log_posbias[1] = log_posbias[1].flip(-2) # ProBound writes 5'->3'
log_posbias[:, 1] = log_posbias[:, 1].flip(-1)
# Fill in log_posbias values
for strand_a in range(log_posbias_shape[0]):
for strand_b in range(log_posbias_shape[1]):
square = log_posbias[strand_a, strand_b]
for posbias_idx, diag_idx in enumerate(
range(-square.shape[0] + 1, square.shape[1])
):
cooperativity.log_posbias[posbias_idx][
0, 0, strand_a, strand_b
] = square.diagonal(diag_idx)
return cooperativities
def parse_probound_aggregate(
fit_final: dict[str, Any],
modes: Iterable[Mode],
cooperativities: Iterable[Cooperativity],
table_idx: int,
round_idx: int,
) -> Aggregate:
"""Parses all binding modes and interactions of a round into an Aggregate.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
modes: The Mode encoding of binding modes, from parse_probound_modes.
cooperativities: The Mode encoding of binding mode interactions, from
parse_probound_interactions.
table_idx: The index of the experiment in the ProBound fit.
round_idx: The index of the sequencing round in the experiment.
Returns:
An Aggregate.
"""
enrichment_settings = fit_final["modelSettings"]["enrichmentModel"][
table_idx
]
contributions: list[Contribution] = []
throwaway_activities: list[float] = []
for mode_idx, mode in enumerate(modes):
log_activity = fit_final["coefficients"]["bindingModes"][mode_idx][
"activity"
][table_idx][round_idx]
if mode_idx in enrichment_settings["bindingModes"]:
contributions.append(
Contribution(binding=mode, log_activity=log_activity)
)
else:
throwaway_activities.append(log_activity)
for coop_idx, coop in enumerate(cooperativities):
log_activity = fit_final["coefficients"]["bindingModeInteractions"][
coop_idx
]["activity"][table_idx][round_idx]
if coop_idx in enrichment_settings["bindingModeInteractions"]:
contributions.append(
Contribution(binding=coop, log_activity=log_activity)
)
else:
throwaway_activities.append(log_activity)
aggregate = Aggregate(
contributions=contributions,
target_concentration=enrichment_settings["concentration"],
)
aggregate.throaway_activities = torch.nn.ParameterList(
torch.tensor(log_activity) for log_activity in throwaway_activities
)
return aggregate
def parse_probound_rounds(
fit_final: dict[str, Any],
modes: Iterable[Mode],
cooperativities: Iterable[Cooperativity],
table_idx: int,
) -> list[Round]:
"""Parses all rounds associated with an experiment in a ProBound fit.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
modes: The Mode encoding of binding modes, from parse_probound_modes.
cooperativities: The Mode encoding of binding mode interactions, from
parse_probound_interactions.
table_idx: The index of the experiment in the ProBound fit.
Returns:
A list of Round objects.
"""
enrichment_settings = fit_final["modelSettings"]["enrichmentModel"][
table_idx
]
rounds: list[Round] = []
log_depths = fit_final["coefficients"]["countTable"][table_idx]["h"]
for round_idx, log_depth in enumerate(log_depths):
# Get round type
constructor: (
type[BoundRound]
| type[BoundUnsaturatedRound]
| type[RhoGammaRound]
| type[ExponentialRound]
)
if enrichment_settings["modelType"] == "SELEX":
constructor = (
BoundRound
if enrichment_settings["bindingSaturation"]
else BoundUnsaturatedRound
)
elif enrichment_settings["modelType"] == "RhoGamma":
constructor = RhoGammaRound
elif enrichment_settings["modelType"] == "ExponentialKinetics":
constructor = ExponentialRound
else:
raise RuntimeError(
f"modelType {enrichment_settings['modelType']} not recognized"
)
# Create next round
if enrichment_settings["cumulativeEnrichment"]:
reference_round = rounds[-1] if len(rounds) > 0 else None
else:
reference_round = rounds[0] if len(rounds) > 0 else None
next_round = constructor(
aggregate=parse_probound_aggregate(
fit_final, modes, cooperativities, table_idx, round_idx
),
reference_round=reference_round,
log_depth=log_depth,
train_depth=False,
)
# Fill in remaining parameters
enrichment_coefficients = fit_final["coefficients"]["enrichmentModel"][
table_idx
]
if enrichment_settings["modelType"] == "RhoGamma":
torch.nn.init.constant_(
next_round.rho, enrichment_coefficients["rho"][round_idx]
)
torch.nn.init.constant_(
next_round.gamma, enrichment_coefficients["gamma"][round_idx]
)
if enrichment_settings["modelType"] == "ExponentialKinetics":
torch.nn.init.constant_(
next_round.delta, enrichment_coefficients["delta"][round_idx]
)
rounds.append(next_round)
return rounds
def parse_probound_model(
fit_final: dict[str, Any],
config: dict[str, Any],
count_tables: Iterable[CountTable],
) -> MultiExperimentLoss:
"""Parses all rounds associated with an experiment in a ProBound fit.
Args:
fit_final: The ProBound fit, parsed by get_fit_final.
config: The config.json file, parsed into a dictionary.
count_tables: The CountTable objects of an experiment, from
parse_probound_tables.
Returns:
A MultiExperimentLoss.
"""
# Parse alphabet
alphabet = parse_probound_alphabet(fit_final)
# Parse PSAMs
layer_specs: list[NonSpecific | PSAM] = []
for mode_idx, mode_settings in enumerate(
fit_final["modelSettings"]["bindingModes"]
):
if mode_settings["size"] == 0:
layer_specs.append(
NonSpecific(alphabet=alphabet, ignore_length=True)
)
else:
layer_specs.append(parse_probound_psam(fit_final, mode_idx))
# Parse experiments
experiments: list[Experiment] = []
for table_idx, count_table in enumerate(count_tables):
modes = parse_probound_modes(
fit_final, layer_specs, count_table, table_idx
)
cooperativities = parse_probound_interactions(
fit_final, modes, table_idx
)
rounds = parse_probound_rounds(
fit_final, modes, cooperativities, table_idx
)
experiments.append(
Experiment(rounds, counts_per_round=count_table.counts_per_round)
)
# Get which parameters to exclude from regularization
exclude_regularization = [
"log_target_concentration",
"log_hill",
"layer_spec.bias",
"log_spacing",
]
for expt_idx, expt in enumerate(experiments):
for ctrb_idx, ctrb in enumerate(
expt.rounds[0].aggregate.contributions
):
if (
isinstance(ctrb.binding, Mode)
and not ctrb.binding.layers[-1].train_posbias
):
exclude_regularization.append(
f"experiments.{expt_idx}.rounds.0.aggregate"
f".contributions.{ctrb_idx}.binding.layers"
f".{len(ctrb.binding.layers)-1}.log_posbias"
)
return MultiExperimentLoss(
experiments,
lambda_l2=config["optimizerSetting"]["lambdaL2"],
pseudocount=config["optimizerSetting"]["pseudocount"],
exponential_bound=config["optimizerSetting"]["expBound"],
full_loss=False,
dilute_regularization=True,
weights=[1] * len(experiments),
exclude_regularization=exclude_regularization,
)
def import_motif_central(fit_id: int) -> PSAM:
"""Parses a PSAM from a fit_id in MotifCentral."""
model = json.loads(
requests.get(
"https://prod-gateway.motifcentral.org/"
f"cellx/api/web/utility/fit/{fit_id}",
timeout=5,
).text
)
return parse_probound_psam(model, -1)
def import_matrix(matrix: FloatArray, alphabet: Alphabet) -> PSAM:
"""Parses a PSAM from a (alphabet size, length) matrix."""
matrix = np.asarray(matrix)
if len(matrix) != len(alphabet.alphabet):
raise ValueError(
f"Second dimension of {matrix}"
f" does not match size of alphabet {alphabet}"
)
psam = PSAM(kernel_size=len(matrix[0]), alphabet=alphabet, normalize=False)
for key, param in psam.betas.items():
elements = [int(i) for i in key.split("-")]
elements = [i - 1 for i in elements[:-1]] + elements[-1:]
if len(elements) == 2:
torch.nn.init.constant_(param, matrix[elements[1]][elements[0]])
return psam
def import_hocomoco(model: str) -> PSAM:
"""Parses a PSAM from a model in HOCOMOCO 11."""
root = "https://hocomoco11.autosome.org"
response = requests.get(f"{root}/motif/{model}", timeout=5)
match = re.search(r"/final_bundle/.*\.pwm", response.text)
if match is None:
raise RuntimeError(f"Could not find HOCOMOCO PWM for {model}")
text = requests.get(f"{root}/{match.group(0)}", timeout=5).text
matrix = pd.read_csv(
io.StringIO(text), sep="\t", header=None, skiprows=1
).to_numpy()
matrix = matrix.T
matrix -= matrix.max(axis=0, keepdims=True)
return import_matrix(matrix, DNA())
def import_jaspar(fit_id: str) -> PSAM:
"""Parses a PSAM from an ID in JASPAR."""
text = requests.get(
f"https://jaspar.elixir.no/api/v1/matrix/{fit_id}.jaspar", timeout=5
).text
motif = motifs.read(io.StringIO(text), "jaspar") # type: ignore[no-untyped-call]
motif.pseudocounts = jaspar.calculate_pseudocounts(motif) # type: ignore[no-untyped-call]
matrix = np.array(list(motif.pssm.values())) / np.log2(np.e)
matrix -= matrix.max(axis=0, keepdims=True)
return import_matrix(matrix, DNA())