Views
No views yet
transformers library needs to be installed from source with the following command in order to use the models:pip install --upgrade git+https://github.com/huggingface/transformers.gitrescaling_factor of the Rotary Embedding layer in the esm model num_dna_tokens_inference / max_num_tokens_nt where num_dna_tokens_inference is the number of tokens at inference
(i.e 6669 for a sequence of 40008 base pairs) and max_num_tokens_nt is the max number of tokens on which the backbone nucleotide-transformer was trained on, i.e 2048../inference_segment_nt.ipynb can be run in Google Colab by clicking on the icon and shows how to handle inference on sequence lengths require changing
the rescaling factor and sequence lengths that do not. One can run the notebook and reproduce Fig.1 and Fig.3 from the SegmentNT paper.1# Load model and tokenizer
2from transformers import AutoTokenizer, AutoModel
3import torch
4
5tokenizer = AutoTokenizer.from_pretrained("InstaDeepAI/segment_nt", trust_remote_code=True)
6model = AutoModel.from_pretrained("InstaDeepAI/segment_nt", trust_remote_code=True)
7
8# Choose the length to which the input sequences are padded. By default, the
9# model max length is chosen, but feel free to decrease it as the time taken to
10# obtain the embeddings increases significantly with it.
11# The number of DNA tokens (excluding the CLS token prepended) needs to be dividible by
12# 2 to the power of the number of downsampling block, i.e 4.
13max_length = 12 + 1
14
15assert (max_length - 1) % 4 == 0, (
16 "The number of DNA tokens (excluding the CLS token prepended) needs to be dividible by"
17 "2 to the power of the number of downsampling block, i.e 4.")
18
19# Create a dummy dna sequence and tokenize it
20sequences = ["ATTCCGATTCCGATTCCG", "ATTTCTCTCTCTCTCTGAGATCGATCGATCGAT"]
21tokens = tokenizer.batch_encode_plus(sequences, return_tensors="pt", padding="max_length", max_length = max_length)["input_ids"]
22
23# Infer
24attention_mask = tokens != tokenizer.pad_token_id
25outs = model(
26 tokens,
27 attention_mask=attention_mask,
28 output_hidden_states=True
29)
30
31# Obtain the logits over the genomic features
32logits = outs.logits.detach()
33# Transform them in probabilities
34probabilities = torch.nn.functional.softmax(logits, dim=-1)
35print(f"Probabilities shape: {probabilities.shape}")
36
37# Get probabilities associated with intron
38idx_intron = model.config.features.index("intron")
39probabilities_intron = probabilities[:,:,idx_intron]
40print(f"Intron probabilities shape: {probabilities_intron.shape}")
41
42<CLS> <ACGTGT> <ACGTGC> <ACGGAC> <GACTAG> <TCAGCA>1@article{de2024segmentnt,
2 title={SegmentNT: annotating the genome at single-nucleotide resolution with DNA foundation models},
3 author={de Almeida, Bernardo P and Dalla-Torre, Hugo and Richard, Guillaume and Blum, Christopher and Hexemer, Lorenz and Gelard, Maxence and Pandey, Priyanka and Laurent, Stefan and Laterre, Alexandre and Lang, Maren and others},
4 journal={bioRxiv},
5 pages={2024--03},
6 year={2024},
7 publisher={Cold Spring Harbor Laboratory}
8}
9