File size: 6,478 Bytes
1c2f864
 
 
 
 
 
 
 
 
 
2f25aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2ee685
 
 
 
2f25aea
 
 
 
 
 
 
 
 
e2ee685
2f25aea
 
 
1c2f864
 
 
 
acd253c
2f25aea
1c2f864
 
 
 
 
e2ee685
 
 
1c2f864
acd253c
 
 
 
e2ee685
acd253c
 
 
2f25aea
 
 
acd253c
 
 
 
1c2f864
 
 
 
 
 
 
 
 
 
e2ee685
 
acd253c
e2ee685
acd253c
1c2f864
 
 
 
acd253c
1c2f864
 
 
 
e2ee685
 
 
1c2f864
acd253c
 
 
2f25aea
1c2f864
 
 
 
 
 
 
 
 
 
e2ee685
1c2f864
e2ee685
1c2f864
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2ee685
1c2f864
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e10ac0c9-40ce-41fb-b6fa-3d62b76f2e57",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geneformer import InSilicoPerturber\n",
    "from geneformer import InSilicoPerturberStats\n",
    "from geneformer import EmbExtractor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbd6851c-060e-4967-b816-e605ffe58b23",
   "metadata": {
    "tags": []
   },
   "source": [
    "### in silico perturbation in deletion mode to determine genes whose deletion in the dilated cardiomyopathy (dcm) state significantly shifts the embedding towards non-failing (nf) state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c53e98cd-c603-4878-82ba-db471181bb55",
   "metadata": {},
   "outputs": [],
   "source": [
    "# first obtain start, goal, and alt embedding positions\n",
    "# this function was changed to be separate from perturb_data\n",
    "# to avoid repeating calcuations when parallelizing perturb_data\n",
    "cell_states_to_model={\"state_key\": \"disease\", \n",
    "                      \"start_state\": \"dcm\", \n",
    "                      \"goal_state\": \"nf\", \n",
    "                      \"alt_states\": [\"hcm\"]}\n",
    "\n",
    "filter_data_dict={\"cell_type\":[\"Cardiomyocyte1\",\"Cardiomyocyte2\",\"Cardiomyocyte3\"]}\n",
    "\n",
    "# OF NOTE: token_dictionary_file must be set to the gc-30M token dictionary if using a 30M series model\n",
    "# (otherwise the EmbExtractor will use the current default model dictionary)\n",
    "# 30M token dictionary: https://huggingface.co/ctheodoris/Geneformer/blob/main/geneformer/gene_dictionaries_30m/token_dictionary_gc30M.pkl\n",
    "embex = EmbExtractor(model_type=\"CellClassifier\", # if using previously fine-tuned cell classifier model\n",
    "                     num_classes=3,\n",
    "                     filter_data=filter_data_dict,\n",
    "                     max_ncells=1000,\n",
    "                     emb_layer=0,\n",
    "                     summary_stat=\"exact_mean\",\n",
    "                     forward_batch_size=256,\n",
    "                     nproc=16)\n",
    "\n",
    "state_embs_dict = embex.get_state_embs(cell_states_to_model,\n",
    "                                       \"../fine_tuned_models/gf-6L-30M-i2048_CellClassifier_cardiomyopathies_220224\", # example 30M fine-tuned model\n",
    "                                       \"path/to/input_data\",\n",
    "                                       \"path/to/output_directory\",\n",
    "                                       \"output_prefix\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "981e1190-62da-4543-b7d3-6e2a2d6a6d56",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# OF NOTE: token_dictionary_file must be set to the gc-30M token dictionary if using a 30M series model\n",
    "# (otherwise the InSilicoPerturber will use the current default model dictionary)\n",
    "# 30M token dictionary: https://huggingface.co/ctheodoris/Geneformer/blob/main/geneformer/gene_dictionaries_30m/token_dictionary_gc30M.pkl\n",
    "isp = InSilicoPerturber(perturb_type=\"delete\",\n",
    "                        perturb_rank_shift=None,\n",
    "                        genes_to_perturb=\"all\",\n",
    "                        combos=0,\n",
    "                        anchor_gene=None,\n",
    "                        model_type=\"CellClassifier\", # if using previously fine-tuned cell classifier model\n",
    "                        num_classes=3,\n",
    "                        emb_mode=\"cell\",\n",
    "                        cell_emb_style=\"mean_pool\",\n",
    "                        filter_data=filter_data_dict,\n",
    "                        cell_states_to_model=cell_states_to_model,\n",
    "                        state_embs_dict=state_embs_dict,\n",
    "                        max_ncells=2000,\n",
    "                        emb_layer=0,\n",
    "                        forward_batch_size=400,\n",
    "                        nproc=16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0525a663-871a-4ce0-a135-cc203817ffa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# outputs intermediate files from in silico perturbation\n",
    "\n",
    "isp.perturb_data(\"../fine_tuned_models/gf-6L-30M-i2048_CellClassifier_cardiomyopathies_220224\", # example 30M fine-tuned model\n",
    "                 \"path/to/input_data\",\n",
    "                 \"path/to/isp_output_directory\",\n",
    "                 \"output_prefix\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8aadabb-516a-4dc0-b307-6de880e64e26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# OF NOTE: token_dictionary_file must be set to the gc-30M token dictionary if using a 30M series model\n",
    "# (otherwise the InSilicoPerturberStats will use the current default model dictionary)\n",
    "# 30M token dictionary: https://huggingface.co/ctheodoris/Geneformer/blob/main/geneformer/gene_dictionaries_30m/token_dictionary_gc30M.pkl\n",
    "ispstats = InSilicoPerturberStats(mode=\"goal_state_shift\",\n",
    "                                  genes_perturbed=\"all\",\n",
    "                                  combos=0,\n",
    "                                  anchor_gene=None,\n",
    "                                  cell_states_to_model=cell_states_to_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffecfae6-e737-43e3-99e9-fa37ff46610b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# extracts data from intermediate files and processes stats to output in final .csv\n",
    "ispstats.get_stats(\"path/to/isp_output_directory\", # this should be the directory \n",
    "                   None,\n",
    "                   \"path/to/isp_stats_output_directory\",\n",
    "                   \"output_prefix\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}