File size: 13,292 Bytes
61c95b3 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"id": "70438228",
"metadata": {},
"source": [
"# Data Preprocessing (Interactive Notebook)\n",
"\n",
"This notebook runs the repository's Step1→Step9 data-preprocessing steps interactively so you can execute, inspect and debug individual stages locally.\n",
"\n",
"Outputs from these steps will be written under `example/data_preprocessing` and downstream under `example/machine_learning` by default."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2348a749",
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"git clone https://github.com/hlnicholls/GenePrioritiser\n",
"cd GenePrioritiser\n",
"\n",
"conda env create -f GenePrioritiser_env.yml\n",
"conda activate GenePrioritiser_env\n",
"pip install --force-reinstall scikit-learn==1.4.2\n",
"pip install --force-reinstall scipy==1.11.4\n",
"pip install --force-reinstall numpy==1.23.0\n",
"pip install scikit-optimize\n",
"conda install -c bioconda bedtools htslib\n",
"conda install -c conda-forge parallel\n",
"pip install pybedtools intervaltree requests"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "74317926",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.chdir(\"/Users/hannahnicholls/GitHub/GenePrioritiser\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d9113571",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using cached gene bed: /Users/hannahnicholls/GitHub/GenePrioritiser/utils/gencode_v19.genes.ext10000.sorted.bed\n",
"Processing /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_DBP.txt.gz in /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_DBP.txt_27680\n",
"Parsing GWAS input and extracting SNP positions (Python)...\n",
"Extracted 7160657 SNP positions from /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_DBP.txt.gz\n",
"Running bedtools closest (this is the fast step, but can be I/O heavy on large GWAS)...\n",
"Building gene map (line -> gene, distance)...\n",
"Joining original rows with gene map and writing outputs via Python...\n",
"Wrote annotated 7160657 SNPs to ./example/data_preprocessing/output/variants/Annotated_GWAS_DBP.csv\n",
"Wrote variant data to ./example/data_preprocessing/output/variants/variant_data_DBP.csv\n",
"Cleaning up temporary files: /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_DBP.txt_27680\n",
"Done for /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_DBP.txt.gz\n",
"Processing /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_SBP.txt.gz in /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_SBP.txt_27680\n",
"Parsing GWAS input and extracting SNP positions (Python)...\n",
"Extracted 7088121 SNP positions from /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_SBP.txt.gz\n",
"Running bedtools closest (this is the fast step, but can be I/O heavy on large GWAS)...\n",
"Building gene map (line -> gene, distance)...\n",
"Joining original rows with gene map and writing outputs via Python...\n",
"Wrote annotated 7088121 SNPs to ./example/data_preprocessing/output/variants/Annotated_GWAS_SBP.csv\n",
"Wrote variant data to ./example/data_preprocessing/output/variants/variant_data_SBP.csv\n",
"Cleaning up temporary files: /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_SBP.txt_27680\n",
"Done for /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_SBP.txt.gz\n",
"Processing /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_PP.txt.gz in /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_PP.txt_27680\n",
"Parsing GWAS input and extracting SNP positions (Python)...\n",
"Extracted 7088842 SNP positions from /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_PP.txt.gz\n",
"Running bedtools closest (this is the fast step, but can be I/O heavy on large GWAS)...\n",
"Building gene map (line -> gene, distance)...\n",
"Joining original rows with gene map and writing outputs via Python...\n",
"Wrote annotated 7088842 SNPs to ./example/data_preprocessing/output/variants/Annotated_GWAS_PP.csv\n",
"Wrote variant data to ./example/data_preprocessing/output/variants/variant_data_PP.csv\n",
"Cleaning up temporary files: /Users/hannahnicholls/GitHub/GenePrioritiser/tmp_annotation_Evangelou_30224653_PP.txt_27680\n",
"Done for /Users/hannahnicholls/GitHub/GenePrioritiser/./example/data_preprocessing/input/Evangelou_30224653_PP.txt.gz\n"
]
}
],
"source": [
"!bash /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step1_annotate_genes_bedtools.sh"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9ff8bfe6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Repo root : /Users/hannahnicholls/GitHub/GenePrioritiser\n",
"Variant directory : example/data_preprocessing/output/variants\n",
"HGNC file : /Users/hannahnicholls/GitHub/GenePrioritiser/utils/hgnc_complete_set.txt\n",
"Building HGNC synonym map...\n",
"Loaded 102319 synonym mappings\n",
"\n",
"=== Harmonising Annotated_GWAS_* files ===\n",
" Processing Annotated_GWAS_DBP.csv ... [OK, 428054 gene names changed]\n",
" Processing Annotated_GWAS_PP.csv ... [OK, 423689 gene names changed]\n",
" Processing Annotated_GWAS_SBP.csv ... [OK, 423690 gene names changed]\n",
"\n",
"=== Harmonising variant_data_* files ===\n",
" Processing variant_data_DBP.csv ... [OK, 428054 gene names changed]\n",
" Processing variant_data_PP.csv ... [OK, 423689 gene names changed]\n",
" Processing variant_data_SBP.csv ... [OK, 423690 gene names changed]\n",
"\n",
"Done. All applicable files overwritten with HGNC-harmonised Gene symbols.\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step2_harmonise_genes.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8a20f2dd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All files processed and merged. Output saved to: ./databases/variant_level/merged_gene_median_variant_measures.csv\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step3_process_variant_level_data.py"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1ef583bf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of unmapped entries: 0\n",
"Sample unmapped entries:\n",
"Empty DataFrame\n",
"Columns: [protein1, protein2, coexpression, experiments, database, Gene1, Gene2]\n",
"Index: []\n",
"Interactors collected with max_depth=3: 19430 genes\n",
"Direct only interactors (depth=1): 17554\n",
"One-level interactors (depth=2): 19428\n",
"Found 3 annotated GWAS file(s) to load:\n",
" - ./example/data_preprocessing/output/variants/Annotated_GWAS_DBP.csv\n",
" - ./example/data_preprocessing/output/variants/Annotated_GWAS_PP.csv\n",
" - ./example/data_preprocessing/output/variants/Annotated_GWAS_SBP.csv\n",
"Wrote intermediate least-likely genes to /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_intermediate.tsv (will not overwrite ./example/data_preprocessing/input/least_likely_genes.tsv)\n",
"Applying additional filtering based on: ./example/data_preprocessing/input/BP_loci_Apr2020_LDr2-8_500kb.csv\n",
"Filtered out 65 genes based on additional criteria (no genes with SNPs in any LD with any BP loci).\n",
"Updated intermediate least-likely genes at /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_intermediate.tsv\n",
"Least likely genes identified and saved.\n",
"Number of least likely genes: 571\n",
"Number of probable genes: 145\n",
"Number of most likely genes: 51\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step4_least_likely_gene_selection.py"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e42781c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using intermediate least-likely genes file: /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_intermediate.tsv\n",
"Identified 13758 genes with at least one P<0.01 SNP in EACH Annotated_GWAS file (per-file intersection)\n",
"Probable genes: 254 -> 145 after applying per-file P<0.01 intersection filter\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step6_identify_training_genes.py"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "33902de9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 5116 duplicate Gene entries in merged data - aggregating numeric features by mean to collapse duplicates.\n",
"All merged databases saved to ./example/data_preprocessing/output/all_genes_merged_all_data.csv\n",
"Training genes counts per label:\n",
" least likely: 571\n",
" probable: 145\n",
" most likely: 51\n",
"Joined data saved to ['./example/data_preprocessing/output/training_data_all_features.csv', './example/machine_learning/eda/input/training_data_all_features.csv']\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step7_merge_all_databases_and_get_training_data.py"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f2f179e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using intermediate least-likely genes file: /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_intermediate.tsv\n",
"/Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step8_downsample_least_likely_genes.py:311: UserWarning: The palette list has more values (2) than needed (1), which may not be intended.\n",
" sns.boxplot(data=[combined_pos['variant_count'], least_df['variant_count']], palette=['C0', 'C1'])\n",
"Wrote diagnostic plots: /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/variant_count_hist_seed42.png /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/variant_count_box_seed42.png\n",
"Wrote updated training_genes to ./example/data_preprocessing/input/training_genes.txt\n",
"Wrote sampled least file: /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_sampled_seed42.tsv\n",
"Wrote audit: /Users/hannahnicholls/GitHub/GenePrioritiser/example/data_preprocessing/input/sampling/least_likely_sampling_audit_seed42.json\n",
"Sampled size: 51 target: 51\n",
"Wrote training features to: ./example/data_preprocessing/output/training_data_all_features.csv\n",
"Wrote EDA training input to: ./example/machine_learning/eda/input/training_data_all_features.csv\n"
]
}
],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step8_downsample_least_likely_genes.py"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "26b264fa",
"metadata": {},
"outputs": [],
"source": [
"!python /Users/hannahnicholls/GitHub/GenePrioritiser/src/data_preprocessing/Step9_subset_genes_to_prioritise.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58784add",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "GenePrioritiser_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.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|