Righor
Companion to righor, to publish the python package. Install with pip install righor
.
Load a model:
import righor
import matplotlib.pyplot as plt
import seaborn
import pandas as pd
from tqdm.notebook import tqdm
from collections import Counter
import numpy as np
igor_model = righor.load_model("human", "trb")
Generate sequences fast:
generator = igor_model.generator(seed=42)
for _ in tqdm(range(10000)):
sequence = generator.generate_without_errors(functional=True)
if "IGH" in sequence.cdr3_aa:
print("TRB CDR3 containing \"IGH\":", sequence.cdr3_aa)
V_genes = righor.genes_matching("TRBV5", igor_model)
J_genes = righor.genes_matching("TRBJ", igor_model)
generator = igor_model.generator(seed=42, available_v=V_genes, available_j=J_genes)
generation_result = generator.generate_without_errors(functional=True)
print("Result:")
print(generation_result)
print("Explicit recombination event:")
print(generation_result.recombination_event)
Evaluate a given sequence:
my_sequence = "ACCCTCCAGTCTGCCAGGCCCTCACATACCTCTCAGTACCTCTGTGCCAGCAGTGAGGACAGGGACGTCACTGAAGCTTTCTTTGGACAAGGCACC"
result_inference = igor_model.evaluate(my_sequence)
best_event = result_inference.best_event
print(f"Probability that this specific event chain created the sequence: {best_event.likelihood / result_inference.likelihood:.2f}.")
print(f"Reconstructed sequence (without errors):", best_event.reconstructed_sequence)
print(f"Pgen: {result_inference.pgen:.1e}")
Infer a model:
generator = igor_model.generator()
example_seq = generator.generate(False)
sequences = [generator.generate(False).full_seq for _ in range(500)]
align_params = righor.AlignmentParameters()
align_params.left_v_cutoff = 70
infer_params = righor.InferenceParameters()
model = igor_model.copy()
model.p_ins_vd = np.ones(model.p_ins_vd.shape)
model.error_rate = 0
aligned_sequences = model.align_all_sequences(sequences, align_params)
models = {}
model = igor_model.uniform()
model.error_rate = 0
models[0] = model
for ii in tqdm(range(35)):
models[ii+1] = models[ii].copy()
models[ii+1].infer(aligned_sequences, infer_params)
Visualize and save the model
fig = righor.plot_vdj(*[models[ii] for ii in [10, 2, 1, 0]] + [igor_model],
plots_kws=[{'label':f'Round #{ii}', 'alpha':0.8} for ii in [10,2, 1, 0]] + [{'label':f'og'}] )
models[10].save_model('test_save')
igor_model = righor.vdj.Model.load_model_from_files('test_save/model_params.txt',
'test_save/model_marginals.txt',
'test_save/V_gene_CDR3_anchors.csv',
'test_save/J_gene_CDR3_anchors.csv')
models[10].save_json('test_save.json')
igor_model = righor.vdj.Model.load_json('test_save.json')