File size: 3,936 Bytes
ca9a9bd
 
 
 
08fff2c
ca9a9bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08fff2c
ca9a9bd
 
 
 
 
 
 
 
 
 
 
 
08fff2c
 
d3654a6
 
 
 
 
 
 
 
 
ca9a9bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08fff2c
ca9a9bd
 
 
 
 
 
 
 
 
08fff2c
ca9a9bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Union\n",
    "\n",
    "import torch\n",
    "from transformers import AutoModel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModel.from_pretrained(\"InstaDeepAI/segment_borzoi\", trust_remote_code=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define useful functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def encode_sequences(sequences: Union[str, List[str]]) -> torch.Tensor:\n",
    "    \"\"\"\n",
    "    One-hot encode a DNA sequence or a batch of DNA sequences.\n",
    "\n",
    "    Args:\n",
    "        sequences (Union[str, List[str]]): Either a DNA sequence or a list of DNA sequences\n",
    "\n",
    "    Returns:\n",
    "        torch.Tensor: One-hot encoded\n",
    "            - If `sequences` is just one sequence (str), output shape is (seq_len, 4), seq_len being the length of a sequence\n",
    "            - If `sequences` is a list of sequences, output shape is (num_sequences, seq_len, 4)\n",
    "    \n",
    "    Example:\n",
    "        >>> sequences = [\"AC\", \"GT\"]\n",
    "        >>> encode_sequences(sequences)\n",
    "        tensor([[[1., 0., 0., 0.],\n",
    "                 [0., 1., 0., 0.]],\n",
    "\n",
    "                [[0., 0., 1., 0.],\n",
    "                 [0., 0., 0., 1.]]])\n",
    "    \"\"\"\n",
    "    one_hot_map = {\n",
    "        'a': torch.tensor([1., 0., 0., 0.]),\n",
    "        'c': torch.tensor([0., 1., 0., 0.]),\n",
    "        'g': torch.tensor([0., 0., 1., 0.]),\n",
    "        't': torch.tensor([0., 0., 0., 1.]),\n",
    "        'n': torch.tensor([0., 0., 0., 0.]),\n",
    "        'A': torch.tensor([1., 0., 0., 0.]),\n",
    "        'C': torch.tensor([0., 1., 0., 0.]),\n",
    "        'G': torch.tensor([0., 0., 1., 0.]),\n",
    "        'T': torch.tensor([0., 0., 0., 1.]),\n",
    "        'N': torch.tensor([0., 0., 0., 0.])\n",
    "    }\n",
    "\n",
    "    def encode_sequence(seq_str):\n",
    "        one_hot_list = []\n",
    "        for char in seq_str:\n",
    "            one_hot_vector = one_hot_map.get(char, torch.tensor([0.25, 0.25, 0.25, 0.25]))\n",
    "            one_hot_list.append(one_hot_vector)\n",
    "        return torch.stack(one_hot_list)\n",
    "\n",
    "    if isinstance(sequences, list):\n",
    "        return torch.stack([encode_sequence(seq) for seq in sequences])\n",
    "    else:\n",
    "        return encode_sequence(sequences)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inference example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequences = [\"A\"*524_288, \"G\"*524_288]\n",
    "one_hot_encoding = encode_sequences(sequences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "preds = model(one_hot_encoding)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(preds['logits'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "genomics-research-env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}