Add files using upload-large-folder tool
Browse files- flax_model/alphagenome/_sdk/data/gene_annotation.py +445 -0
- weight/alphagenome-all-folds/ocdbt.process_0/d/48900811ab32b5ad3873d40e96d63846 +3 -0
- weight/alphagenome-all-folds/ocdbt.process_0/d/6b08a83c9d6a2ade925dbcdd91299ce2 +3 -0
- weight/alphagenome-all-folds/ocdbt.process_0/d/bda173d4f00d3afcef0a043614df05a6 +3 -0
flax_model/alphagenome/_sdk/data/gene_annotation.py
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Google LLC.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""Utilities for working with gene annotations (e.g., GTFs)."""
|
| 16 |
+
|
| 17 |
+
from collections.abc import Sequence
|
| 18 |
+
import enum
|
| 19 |
+
|
| 20 |
+
from flax_model.alphagenome._sdk.data import genome
|
| 21 |
+
import numpy as np
|
| 22 |
+
import pandas as pd
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@enum.unique
|
| 26 |
+
class TranscriptType(enum.Enum):
|
| 27 |
+
"""Valid Transcript types available in the GENCODE GTF."""
|
| 28 |
+
|
| 29 |
+
IG_C_GENE = 'IG_C_gene'
|
| 30 |
+
IG_C_PSEUDOGENE = 'IG_C_pseudogene'
|
| 31 |
+
IG_D_GENE = 'IG_D_gene'
|
| 32 |
+
IG_J_GENE = 'IG_J_gene'
|
| 33 |
+
IG_J_PSEUDOGENE = 'IG_J_pseudogene'
|
| 34 |
+
IG_V_GENE = 'IG_V_gene'
|
| 35 |
+
IG_V_PSEUDOGENE = 'IG_V_pseudogene'
|
| 36 |
+
IG_PSEUDOGENE = 'IG_pseudogene'
|
| 37 |
+
MT_RRNA = 'Mt_rRNA'
|
| 38 |
+
MT_TRNA = 'Mt_tRNA'
|
| 39 |
+
TEC = 'TEC'
|
| 40 |
+
TR_C_GENE = 'TR_C_gene'
|
| 41 |
+
TR_D_GENE = 'TR_D_gene'
|
| 42 |
+
TR_J_GENE = 'TR_J_gene'
|
| 43 |
+
TR_J_PSEUDOGENE = 'TR_J_pseudogene'
|
| 44 |
+
TR_V_GENE = 'TR_V_gene'
|
| 45 |
+
TR_V_PSEUDOGENE = 'TR_V_pseudogene'
|
| 46 |
+
ARTIFACT = 'artifact'
|
| 47 |
+
LNCRNA = 'lncRNA'
|
| 48 |
+
MIRNA = 'miRNA'
|
| 49 |
+
MISC_RNA = 'misc_RNA'
|
| 50 |
+
NON_STOP_DECAY = 'non_stop_decay'
|
| 51 |
+
NONSENSE_MEDIATED_DECAY = 'nonsense_mediated_decay'
|
| 52 |
+
PROCESSED_PSEUDOGENE = 'processed_pseudogene'
|
| 53 |
+
PROCESSED_TRANSCRIPT = 'processed_transcript'
|
| 54 |
+
PROTEIN_CODING = 'protein_coding'
|
| 55 |
+
PROTEIN_CODING_CDS_NOT_DEFINED = 'protein_coding_CDS_not_defined'
|
| 56 |
+
PROTEIN_CODING_LOF = 'protein_coding_LoF'
|
| 57 |
+
RRNA = 'rRNA'
|
| 58 |
+
RRNA_PSEUDOGENE = 'rRNA_pseudogene'
|
| 59 |
+
RETAINED_INTRON = 'retained_intron'
|
| 60 |
+
RIBOZYME = 'ribozyme'
|
| 61 |
+
SRNA = 'sRNA'
|
| 62 |
+
SCRNA = 'scRNA'
|
| 63 |
+
SCARNA = 'scaRNA'
|
| 64 |
+
SNRNA = 'snRNA'
|
| 65 |
+
SNORNA = 'snoRNA'
|
| 66 |
+
TRANSCRIBED_PROCESSED_PSEUDOGENE = 'transcribed_processed_pseudogene'
|
| 67 |
+
TRANSCRIBED_UNITARY_PSEUDOGENE = 'transcribed_unitary_pseudogene'
|
| 68 |
+
TRANSCRIBED_UNPROCESSED_PSEUDOGENE = 'transcribed_unprocessed_pseudogene'
|
| 69 |
+
TRANSLATED_PROCESSED_PSEUDOGENE = 'translated_processed_pseudogene'
|
| 70 |
+
UNITARY_PSEUDOGENE = 'unitary_pseudogene'
|
| 71 |
+
UNPROCESSED_PSEUDOGENE = 'unprocessed_pseudogene'
|
| 72 |
+
VAULT_RNA = 'vault_RNA'
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def extract_tss(gtf: pd.DataFrame, feature: str = 'transcript') -> pd.DataFrame:
|
| 76 |
+
"""Extract transcription start sites (TSS) from a DataFrame.
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
gtf: pd.DataFrame containing gene annotation.
|
| 80 |
+
feature: Feature in the GTF file to use (either transcript or gene).
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
pd.DataFrame containing transcription start sites as zero-width point
|
| 84 |
+
intervals (Start == End, 0-based).
|
| 85 |
+
"""
|
| 86 |
+
tss = gtf[(gtf.Feature == feature)].copy()
|
| 87 |
+
|
| 88 |
+
# Remove the extra base to make it width=0.
|
| 89 |
+
# .....[)TRANSCRIPT (strand = +)
|
| 90 |
+
# TPIRCSNART[)..... (strand = -)
|
| 91 |
+
new_start = np.where(tss.Strand == '-', tss.End, tss.Start)
|
| 92 |
+
tss.Start = new_start
|
| 93 |
+
tss.End = new_start
|
| 94 |
+
|
| 95 |
+
return tss
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def filter_transcript_type(
|
| 99 |
+
gtf: pd.DataFrame,
|
| 100 |
+
transcript_types: tuple[TranscriptType, ...] | None = None,
|
| 101 |
+
) -> pd.DataFrame:
|
| 102 |
+
"""Filter GTF entries by transcript types.
|
| 103 |
+
|
| 104 |
+
This function takes a GTF DataFrame and a list of transcript types and returns
|
| 105 |
+
a new DataFrame containing only the transcripts with the specified types.
|
| 106 |
+
|
| 107 |
+
The GTF DataFrame must contain a column named 'transcript_type' or
|
| 108 |
+
'transcript_biotype'. The function will raise a ValueError if neither of these
|
| 109 |
+
columns is present.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
gtf: pd.DataFrame or pyranges.PyRanges.
|
| 113 |
+
transcript_types: List of valid transcript types to use for filtering.
|
| 114 |
+
|
| 115 |
+
Returns:
|
| 116 |
+
pd.DataFrame of GENCODE GTF entries subset to rows with the requested
|
| 117 |
+
transcript types.
|
| 118 |
+
"""
|
| 119 |
+
if transcript_types is not None:
|
| 120 |
+
transcript_types_str = [x.value for x in transcript_types]
|
| 121 |
+
if 'transcript_type' in gtf.columns:
|
| 122 |
+
gtf = gtf[gtf.transcript_type.isin(transcript_types_str)]
|
| 123 |
+
elif 'transcript_biotype' in gtf.columns:
|
| 124 |
+
gtf = gtf[gtf.transcript_biotype.isin(transcript_types_str)]
|
| 125 |
+
else:
|
| 126 |
+
raise ValueError('transcript_type or transcript_biotype not in gtf.')
|
| 127 |
+
return gtf
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def filter_protein_coding(
|
| 131 |
+
gtf: pd.DataFrame, include_gene_entries: bool = False
|
| 132 |
+
) -> pd.DataFrame:
|
| 133 |
+
"""Filter GTF entries to only protein-coding genes.
|
| 134 |
+
|
| 135 |
+
Args:
|
| 136 |
+
gtf: pd.DataFrame of GENCODE GTF entries. This data frame must contain a
|
| 137 |
+
column named 'transcript_type' or 'transcript_biotype'.
|
| 138 |
+
include_gene_entries: Whether to include gene entries in addition to
|
| 139 |
+
transcript entries.
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
pd.DataFrame of GENCODE GTF entries subset to rows with protein-coding
|
| 143 |
+
genes.
|
| 144 |
+
"""
|
| 145 |
+
if include_gene_entries:
|
| 146 |
+
if 'gene_type' in gtf.columns:
|
| 147 |
+
gtf = gtf[gtf.gene_type == TranscriptType.PROTEIN_CODING.value]
|
| 148 |
+
else:
|
| 149 |
+
raise ValueError('gene_type not in gtf.')
|
| 150 |
+
else:
|
| 151 |
+
gtf = filter_transcript_type(gtf, (TranscriptType.PROTEIN_CODING,))
|
| 152 |
+
return gtf
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def filter_to_longest_transcript(
|
| 156 |
+
gtf: pd.DataFrame,
|
| 157 |
+
) -> pd.DataFrame:
|
| 158 |
+
"""Filter GTF entries to only the longest transcript per gene.
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
gtf: pd.DataFrame of GENCODE GTF entries. Must contain columns 'Feature',
|
| 162 |
+
'End', 'Start', 'gene_id', and 'transcript_id'.
|
| 163 |
+
|
| 164 |
+
Returns:
|
| 165 |
+
pd.DataFrame of GENCODE GTF entries subset to rows with the longest
|
| 166 |
+
transcript per gene.
|
| 167 |
+
"""
|
| 168 |
+
lengths = gtf[gtf['Feature'] == 'transcript'].reset_index(drop=True)
|
| 169 |
+
lengths['transcript_length'] = lengths['End'] - lengths['Start'] + 1
|
| 170 |
+
|
| 171 |
+
# Identify longest transcripts per gene_id.
|
| 172 |
+
longest_transcripts = lengths.loc[
|
| 173 |
+
lengths.groupby('gene_id')['transcript_length'].idxmax()
|
| 174 |
+
]
|
| 175 |
+
|
| 176 |
+
return gtf[gtf['transcript_id'].isin(longest_transcripts['transcript_id'])]
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def filter_to_mane_select_transcript(gtf: pd.DataFrame) -> pd.DataFrame:
|
| 180 |
+
"""Filter GTF entries to only the MANE select transcript.
|
| 181 |
+
|
| 182 |
+
Note that the MANE_Select tag only exists for the human GTF file.
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
gtf: pd.DataFrame of GENCODE GTF entries. Must contain columns 'tag'.
|
| 186 |
+
|
| 187 |
+
Returns:
|
| 188 |
+
pd.DataFrame of GENCODE GTF entries subset to rows representing MANE
|
| 189 |
+
select transcripts, which are transcripts that are well-supported,
|
| 190 |
+
conserved, and expressed.
|
| 191 |
+
"""
|
| 192 |
+
filtered_gtf = gtf[gtf['tag'].fillna('').str.contains('MANE_Select')]
|
| 193 |
+
if filtered_gtf.empty:
|
| 194 |
+
raise ValueError(
|
| 195 |
+
'No MANE_Select transcripts found in the GTF, possibly due to non-human'
|
| 196 |
+
' GTF.'
|
| 197 |
+
)
|
| 198 |
+
return filtered_gtf
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def filter_transcript_support_level(
|
| 202 |
+
gtf: pd.DataFrame,
|
| 203 |
+
transcript_support_levels: str | Sequence[str],
|
| 204 |
+
) -> pd.DataFrame:
|
| 205 |
+
"""Filter GTF to only transcripts with specific GENCODE support levels.
|
| 206 |
+
|
| 207 |
+
As documented in the [Ensembl
|
| 208 |
+
glossary](https://www.ensembl.org/Help/Glossary),
|
| 209 |
+
the transcript support level (TSL) indicates the degree of evidence that was
|
| 210 |
+
used to construct the transcript.
|
| 211 |
+
|
| 212 |
+
As taken from the glossary, the levels are:
|
| 213 |
+
|
| 214 |
+
| Transcript support level | Description | |
|
| 215 |
+
|---|---|---|
|
| 216 |
+
| 1 | A transcript where all splice junctions are supported by at least one
|
| 217 |
+
non-suspect mRNA. |
|
| 218 |
+
| 2 | A transcript where the best supporting mRNA is flagged as suspect or the
|
| 219 |
+
support is from multiple ESTs |
|
| 220 |
+
| 3 | A transcript where the only support is from a single EST |
|
| 221 |
+
| 4 | A transcript where the best supporting EST is flagged as suspect |
|
| 222 |
+
| 5 | A transcript where no single transcript supports the model structure. |
|
| 223 |
+
| NA | A transcript that was not analysed for TSL. |
|
| 224 |
+
|
| 225 |
+
Args:
|
| 226 |
+
gtf: pd.DataFrame of GENCODE GTF entries. Must contain column
|
| 227 |
+
'transcript_support_level'.
|
| 228 |
+
transcript_support_levels: List of valid transcript support levels to use
|
| 229 |
+
for filtering. This must be a subset of ['1', '2', '3', '4', '5']. Can
|
| 230 |
+
also be single string.
|
| 231 |
+
|
| 232 |
+
Returns:
|
| 233 |
+
pd.DataFrame exactly as provided, but subset to rows with the specified
|
| 234 |
+
support level(s).
|
| 235 |
+
|
| 236 |
+
Transcripts are scored by GENCODE according to how well mRNA and EST
|
| 237 |
+
alignments
|
| 238 |
+
match over its full length. Valid levels are:
|
| 239 |
+
'1': All splice junctions of the transcript are supported by at least one
|
| 240 |
+
non-suspect mRNA.
|
| 241 |
+
'2': The best supporting mRNA is flagged as suspect or the support is from
|
| 242 |
+
multiple ESTs.
|
| 243 |
+
'3': The only support is from a single EST.
|
| 244 |
+
'4': The best supporting EST is flagged as suspect.
|
| 245 |
+
'5': No single transcript supports the model structure.
|
| 246 |
+
'NA': The transcript was not analyzed (not supported by this filter function).
|
| 247 |
+
|
| 248 |
+
See GENCODE GTF format documentation for further details:
|
| 249 |
+
https://www.gencodegenes.org/pages/data_format.html
|
| 250 |
+
"""
|
| 251 |
+
if isinstance(transcript_support_levels, str):
|
| 252 |
+
transcript_support_levels = list(transcript_support_levels)
|
| 253 |
+
|
| 254 |
+
supported_tsls = {'1', '2', '3', '4', '5'}
|
| 255 |
+
if not set(transcript_support_levels).issubset(supported_tsls):
|
| 256 |
+
raise ValueError(
|
| 257 |
+
f'transcript_support_level must be one of {supported_tsls}, but was'
|
| 258 |
+
f' {transcript_support_levels}'
|
| 259 |
+
)
|
| 260 |
+
return gtf[gtf.transcript_support_level.isin(transcript_support_levels)]
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
def upgrade_annotation_ids(
|
| 264 |
+
old_ids: pd.Series, new_ids: pd.Series, patchless: bool = False
|
| 265 |
+
) -> pd.Series:
|
| 266 |
+
"""Upgrade or add transcript id patch version to Ensembl IDs.
|
| 267 |
+
|
| 268 |
+
This function works by
|
| 269 |
+
|
| 270 |
+
1. Dropping the patch version from `old_ids` and `new_ids`
|
| 271 |
+
2. Merging the two on the patch-less ids.
|
| 272 |
+
3. Returning the result of the merge as a pd.Series, with the 'old_ids' as
|
| 273 |
+
the index and the 'new_ids' as the values.
|
| 274 |
+
|
| 275 |
+
Ensembl patch versions have two formats: ENST####.<patch>_PAR_Y or
|
| 276 |
+
ENST####.<patch>. Both are handled.
|
| 277 |
+
|
| 278 |
+
The function will raise a ValueError if either `old_ids` or `new_ids` result
|
| 279 |
+
in duplicates after dropping the patch version.
|
| 280 |
+
|
| 281 |
+
Examples:
|
| 282 |
+
* If the old ids are ENST00010.1 and the new ids are ENST00010.3,
|
| 283 |
+
then the mapping will be ENST00010.1 -> ENST00010.3.
|
| 284 |
+
* If the old ids are ENST00010 and the new ids are ENST00010.3, then the
|
| 285 |
+
mapping will be ENST00010 -> ENST00010.3.
|
| 286 |
+
|
| 287 |
+
Args:
|
| 288 |
+
old_ids: A pd.Series of Ensembl transcript or gene ids with older or missing
|
| 289 |
+
version/patch numbers. The index of the series is ignored.
|
| 290 |
+
new_ids: A pd.Series of transcript or gene ids with newer version/patch
|
| 291 |
+
numbers. The index of the series is ignored.
|
| 292 |
+
patchless: whether old_ids are missing patch.
|
| 293 |
+
|
| 294 |
+
Returns:
|
| 295 |
+
A pd.Series, with the 'old_ids' as the index and the 'new_ids' as the
|
| 296 |
+
values.
|
| 297 |
+
"""
|
| 298 |
+
new_ids = new_ids.drop_duplicates()
|
| 299 |
+
|
| 300 |
+
def drop_version(x):
|
| 301 |
+
"""Drop the patch version from an Ensembl ID."""
|
| 302 |
+
if not x.str.contains('.', regex=False).all():
|
| 303 |
+
raise ValueError('All ids need to contain the patch version.')
|
| 304 |
+
|
| 305 |
+
id_split = x.str.partition('.')
|
| 306 |
+
|
| 307 |
+
# Retain anything after _ such as PAR_Y for ENST####.<patch>_PAR_Y.
|
| 308 |
+
return id_split[0] + id_split[2].str.partition('_')[2]
|
| 309 |
+
|
| 310 |
+
old_ids_nopatch = old_ids if patchless else drop_version(old_ids)
|
| 311 |
+
new_ids_nopatch = drop_version(new_ids)
|
| 312 |
+
assert (
|
| 313 |
+
not old_ids_nopatch.duplicated().any()
|
| 314 |
+
), 'old_ids not unique without version'
|
| 315 |
+
assert (
|
| 316 |
+
not new_ids_nopatch.duplicated().any()
|
| 317 |
+
), 'new_ids not unique without version'
|
| 318 |
+
df = pd.merge(
|
| 319 |
+
pd.DataFrame({'old': old_ids.values, 'no_version': old_ids_nopatch}),
|
| 320 |
+
pd.DataFrame({
|
| 321 |
+
'new': new_ids.values,
|
| 322 |
+
'no_version': new_ids_nopatch,
|
| 323 |
+
}),
|
| 324 |
+
on='no_version',
|
| 325 |
+
how='left',
|
| 326 |
+
)
|
| 327 |
+
return pd.Series(
|
| 328 |
+
df.set_index('old').loc[old_ids].new.values, index=old_ids.index
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
def get_gene_intervals(
|
| 333 |
+
gtf: pd.DataFrame,
|
| 334 |
+
gene_symbols: Sequence[str] | None = None,
|
| 335 |
+
gene_ids: Sequence[str] | None = None,
|
| 336 |
+
) -> list[genome.Interval]:
|
| 337 |
+
"""Returns a list of stranded `genome.Interval`s for the given identifiers.
|
| 338 |
+
|
| 339 |
+
Args:
|
| 340 |
+
gtf: pd.DataFrame of GENCODE GTF entries. Must contain columns 'Feature',
|
| 341 |
+
'gene_name', 'gene_id', 'Chromosome', 'Start', 'End', and 'Strand'.
|
| 342 |
+
gene_symbols: A sequence of gene names or gene symbols (e.g., ['EGFR',
|
| 343 |
+
'TNF', 'TP53']). Matching is case-insensitive.
|
| 344 |
+
gene_ids: A sequence of Ensembl gene IDs, which can be patched (e.g.
|
| 345 |
+
['ENSG00000141510.17']) or unpatched (e.g., ['ENSG00000141510']). Matching
|
| 346 |
+
is done on unpatched IDs.
|
| 347 |
+
|
| 348 |
+
Returns:
|
| 349 |
+
A list of `genome.Interval`s for the given identifiers. The
|
| 350 |
+
returned list of intervals is in the same order as the input gene
|
| 351 |
+
identifiers.
|
| 352 |
+
|
| 353 |
+
Raises:
|
| 354 |
+
ValueError: If neither or both gene_symbols and gene_ids are set, or if no
|
| 355 |
+
interval or multiple intervals are found for any of the given gene
|
| 356 |
+
identifiers.
|
| 357 |
+
"""
|
| 358 |
+
if (gene_symbols is None) == (gene_ids is None):
|
| 359 |
+
raise ValueError('Exactly one of gene_symbols or gene_ids must be set.')
|
| 360 |
+
|
| 361 |
+
gtf_genes = gtf[gtf['Feature'] == 'gene'].copy()
|
| 362 |
+
|
| 363 |
+
if gene_symbols is not None:
|
| 364 |
+
id_col = 'gene_name'
|
| 365 |
+
input_ids = gene_symbols
|
| 366 |
+
process_fn = lambda s: s.str.upper()
|
| 367 |
+
else:
|
| 368 |
+
id_col = 'gene_id'
|
| 369 |
+
input_ids = gene_ids
|
| 370 |
+
process_fn = lambda s: s.str.split('.', n=1).str[0]
|
| 371 |
+
|
| 372 |
+
processed_input_ids = process_fn(pd.Series(input_ids, dtype=str))
|
| 373 |
+
gtf_genes['processed_id'] = process_fn(gtf_genes[id_col])
|
| 374 |
+
|
| 375 |
+
# Filter the GTF to only the genes that are in the input IDs.
|
| 376 |
+
gtf_subset = gtf_genes[
|
| 377 |
+
gtf_genes['processed_id'].isin(processed_input_ids.unique())
|
| 378 |
+
]
|
| 379 |
+
|
| 380 |
+
dup_mask = gtf_subset['processed_id'].duplicated(keep=False)
|
| 381 |
+
if dup_mask.any():
|
| 382 |
+
offending_ids = gtf_subset.loc[dup_mask, id_col].unique()
|
| 383 |
+
raise ValueError(
|
| 384 |
+
'Multiple intervals found for gene(s):'
|
| 385 |
+
f' {", ".join(sorted(offending_ids))}.'
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
# Create a lookup map from processed_id to GTF data.
|
| 389 |
+
# Use reindex to order genes by input and insert NaNs for missing genes.
|
| 390 |
+
gtf_map = gtf_subset.set_index('processed_id')
|
| 391 |
+
result_df = gtf_map.reindex(processed_input_ids)
|
| 392 |
+
|
| 393 |
+
missing_mask = result_df['Chromosome'].isnull()
|
| 394 |
+
if missing_mask.any():
|
| 395 |
+
missing_ids = pd.Series(input_ids)[missing_mask.values].unique()
|
| 396 |
+
raise ValueError(
|
| 397 |
+
f'No interval found for gene(s): {", ".join(sorted(missing_ids))}.'
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
# Add original identifiers to the result for the 'name' field of the interval.
|
| 401 |
+
result_df[id_col] = list(input_ids)
|
| 402 |
+
|
| 403 |
+
return [
|
| 404 |
+
genome.Interval(
|
| 405 |
+
chromosome=row.Chromosome,
|
| 406 |
+
start=row.Start,
|
| 407 |
+
end=row.End,
|
| 408 |
+
strand=row.Strand,
|
| 409 |
+
name=getattr(row, id_col),
|
| 410 |
+
)
|
| 411 |
+
for row in result_df.itertuples()
|
| 412 |
+
]
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def get_gene_interval(
|
| 416 |
+
gtf: pd.DataFrame,
|
| 417 |
+
gene_symbol: str | None = None,
|
| 418 |
+
gene_id: str | None = None,
|
| 419 |
+
) -> genome.Interval:
|
| 420 |
+
"""Returns a stranded `genome.Interval` given a gene identifier.
|
| 421 |
+
|
| 422 |
+
Either gene_symbol or gene_id must be set, but not both.
|
| 423 |
+
|
| 424 |
+
Args:
|
| 425 |
+
gtf: pd.DataFrame of GENCODE GTF entries. Must contain columns 'Feature',
|
| 426 |
+
'gene_name', 'gene_id', 'Chromosome', 'Start', 'End', and 'Strand'.
|
| 427 |
+
gene_symbol: A gene name or gene symbol (e.g., 'EGFR', 'TNF', 'TP53')
|
| 428 |
+
gene_id: An Ensembl gene ID, which can be patched (e.g.
|
| 429 |
+
'ENSG00000141510.17') or unpatched (e.g., 'ENSG00000141510').
|
| 430 |
+
|
| 431 |
+
Returns:
|
| 432 |
+
A `genome.Interval` for the given gene identifier.
|
| 433 |
+
|
| 434 |
+
Raises:
|
| 435 |
+
ValueError: If neither or both gene_symbol and gene_id are set, or if no
|
| 436 |
+
interval or multiple intervals are found for the given gene identifier.
|
| 437 |
+
"""
|
| 438 |
+
if sum(x is not None for x in [gene_symbol, gene_id]) != 1:
|
| 439 |
+
raise ValueError('Exactly one of gene_symbol or gene_id must be set.')
|
| 440 |
+
|
| 441 |
+
return get_gene_intervals(
|
| 442 |
+
gtf,
|
| 443 |
+
[gene_symbol] if gene_symbol else None,
|
| 444 |
+
[gene_id] if gene_id else None,
|
| 445 |
+
)[0]
|
weight/alphagenome-all-folds/ocdbt.process_0/d/48900811ab32b5ad3873d40e96d63846
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:10427eeb88365daf0a9e9e2fe31b4738063c54cc23eead51903a13c8fdf1b9fe
|
| 3 |
+
size 165916672
|
weight/alphagenome-all-folds/ocdbt.process_0/d/6b08a83c9d6a2ade925dbcdd91299ce2
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:128c1ff1ec757d1c5e8e4ca7bd0156c2b33ac57c7a5733e8ed0e3b9806b9d0da
|
| 3 |
+
size 152793088
|
weight/alphagenome-all-folds/ocdbt.process_0/d/bda173d4f00d3afcef0a043614df05a6
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8724bef26aa0ef7fbdf4bbf6fce3d502bf06c69aaba8aec97d6c55a80b6ff880
|
| 3 |
+
size 414797824
|