### Install BioRosetta
Source: https://context7.com/reemagit/biorosetta/llms.txt
Install the BioRosetta package using pip.
```bash
pip install biorosetta
```
--------------------------------
### Import Libraries and Setup Display
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Imports necessary libraries for Biorosetta and Pandas, and sets up IPython display for HTML. This is a common setup for interactive environments.
```python
import biorosetta as br
import importlib as imp
imp.reload(br)
import pandas as pd
from IPython.core.display import display, HTML
display(HTML(""))
```
--------------------------------
### Creating IDMapper Instance
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initializes an IDMapper object with a list of source mappers. This is the setup required before performing any ID conversions.
```python
sources = [m.EnsemblMapper(),m.HGNCMapper()]
idm = m.IDMapper(sources)
```
--------------------------------
### Example DataFrame Columns
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
This is an example output showing a list of column names from a DataFrame, likely representing various biological annotations and identifiers.
```text
[
'AllianceGenome',
'HGNC',
'MIM',
'_id',
'_version',
'alias',
'entrezgene',
'exons',
'exons_hg19',
'generif',
'ipi',
'map_location',
'name',
'other_names',
'pdb',
'pfam',
'pharmgkb',
'summary',
'symbol',
'taxid',
'type_of_gene',
'unigene',
'accession.genomic',
'accession.protein',
'accession.rna',
'accession.translation',
'ensembl.gene',
'ensembl.protein',
'ensembl.transcript',
'ensembl.translation',
'ensembl.type_of_gene',
'exac._license',
'exac.all.exp_lof',
'exac.all.exp_mis',
'exac.all.exp_syn',
'exac.all.lof_z',
'exac.all.mis_z',
'exac.all.mu_lof',
'exac.all.mu_mis',
'exac.all.mu_syn',
'exac.all.n_lof',
'exac.all.n_mis',
'exac.all.n_syn',
'exac.all.p_li',
'exac.all.p_null',
'exac.all.p_rec',
'exac.all.syn_z',
'exac.bp',
'exac.cds_end',
'exac.cds_start',
'exac.n_exons',
'exac.nonpsych.exp_lof',
'exac.nonpsych.exp_mis',
'exac.nonpsych.exp_syn',
'exac.nonpsych.lof_z',
'exac.nonpsych.mis_z',
'exac.nonpsych.mu_lof',
'exac.nonpsych.mu_mis',
'exac.nonpsych.mu_syn',
'exac.nonpsych.n_lof',
'exac.nonpsych.n_mis',
'exac.nonpsych.n_syn',
'exac.nonpsych.p_li',
'exac.nonpsych.p_null',
'exac.nonpsych.p_rec',
'exac.nonpsych.syn_z',
'exac.nontcga.exp_lof',
'exac.nontcga.exp_mis',
'exac.nontcga.exp_syn',
'exac.nontcga.lof_z',
'exac.nontcga.mis_z',
'exac.nontcga.mu_lof',
'exac.nontcga.mu_mis',
'exac.nontcga.mu_syn',
'exac.nontcga.n_lof',
'exac.nontcga.n_mis',
'exac.nontcga.n_syn',
'exac.nontcga.p_li',
'exac.nontcga.p_null',
'exac.nontcga.p_rec',
'exac.nontcga.syn_z',
'exac.transcript',
'genomic_pos.chr',
'genomic_pos.end',
'genomic_pos.ensemblgene',
'genomic_pos.start',
'genomic_pos.strand',
'genomic_pos_hg19.chr',
'genomic_pos_hg19.end',
'genomic_pos_hg19.start',
'genomic_pos_hg19.strand',
'go.BP',
'go.CC',
'go.MF',
'homologene.genes',
'homologene.id',
'interpro.desc',
'interpro.id',
'interpro.short_desc',
'pantherdb.HGNC',
'pantherdb._license',
'pantherdb.ortholog',
'pantherdb.uniprot_kb',
'pathway.biocarta.id',
'pathway.biocarta.name',
'pathway.kegg.id',
'pathway.kegg.name',
'pathway.pid.id',
'pathway.pid.name',
'pathway.reactome',
'pathway.wikipathways',
'pharos.target_id',
'reagent.CondMedia_CM_LibrAB.id',
'reagent.CondMedia_CM_LibrAB.relationship',
'reagent.GNF_Qia_hs-genome_v1_siRNA',
'reagent.GNF_hs-Origene.id',
'reagent.GNF_hs-Origene.relationship',
'reagent.GNF_mm+hs-MGC',
'reagent.NIBRI_hs-Secretome_pDEST.id',
'reagent.NIBRI_hs-Secretome_pDEST.relationship',
'reagent.NOVART_hs-genome_siRNA',
'refseq.genomic',
'refseq.protein',
'refseq.rna',
'refseq.translation.protein',
'refseq.translation.rna',
'reporter.HG-U133_Plus_2',
'reporter.HG-U95Av2',
'reporter.HTA-2_0',
'reporter.HuEx-1_0',
'reporter.HuGene-1_1',
'reporter.HuGene-2_1',
'umls.cui',
'uniprot.Swiss-Prot',
'wikipedia.url_stub'
]
```
--------------------------------
### IDMapper Initialization Aliases
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Use convenient aliases for initializing the IDMapper with predefined sets of sources. For example, 'all' includes Ensembl, HGNC, and MyGene mappers.
```python
idmap = br.IDMapper('all') # equivalent to [br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()]
```
```python
idmap = br.IDMapper('local') # equivalent to [br.EnsemblBiomartMapper(),br.HGNCBiomartMapper()]
```
```python
idmap = br.IDMapper('remote') # equivalent to [br.MyGeneMapper()]
```
```python
idmap = br.IDMapper('ensembl_biomart') # equivalent to [br.EnsemblBiomartMapper()]
```
```python
idmap = br.IDMapper('hgnc_biomart') # equivalent to [br.HGNCBiomartMapper()]
```
```python
idmap = br.IDMapper('mygene') # equivalent to [br.MyGene()]
```
--------------------------------
### Convenience Method: Ensembl to Symbol with DataFrame Output
Source: https://context7.com/reemagit/biorosetta/llms.txt
Demonstrates using the `df=True` parameter with the `ensg2symb` convenience method to get conversion results as a pandas DataFrame. This is useful for quality control and detailed reporting.
```python
report = idmap.ensg2symb(['ENSG00000159388'], df=True)
print(report)
```
--------------------------------
### Import Biorosetta and Pandas
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled1.ipynb
Imports the biorosetta library, the importlib module for reloading, and pandas for data manipulation. Ensure biorosetta is installed.
```python
import biorosetta as br
import importlib as imp
imp.reload(br)
import pandas as pd
```
--------------------------------
### Group and Aggregate Pandas DataFrame
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Group a Pandas DataFrame by a specific column and then apply an aggregation function. This example joins ENSG identifiers with a '|' separator for each 'entr' group.
```python
nonunq.groupby('entr').apply(lambda x: '|'.join(x.ensg))
```
--------------------------------
### Get Protein Information using MyGene.info API
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Retrieves information for a specific Ensembl Protein ID (ENSP) using the MyGene.info API, returning results as a DataFrame.
```python
output = mg.getgenes(['ENSP00000433553'], scopes='ensembl.protein', fields='*', species='human', as_dataframe=True, returnall=False)
output
```
--------------------------------
### Convert Data with Specified Input and Output Types
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Converts data using the biorosetta library's `convert` method. This example attempts to convert a list of mixed types to 'ensgs' and 'symb' types. Note: This specific call resulted in an error due to invalid input ID type.
```python
idm._sources[0].convert(['1017','ciao',1,'ENSG00000123374','A1BG'], 'ensgs', 'symb')
```
--------------------------------
### Convert Gene ID using HGNC Biomart Only
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Attempt to convert a gene ID using only the HGNC Biomart source. This example demonstrates a case where the conversion might return 'N/A' if the source does not support the requested mapping.
```python
idmap = br.IDMapper('hgnc_biomart') # Equivalent to br.IDMapper([br.HGNCBiomartMapper()])
idmap.convert('ENSG00000271254','ensg','entr') # Returns 'N/A'
```
--------------------------------
### Convert Gene ID using Ensembl Biomart
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Perform gene ID conversion using only the Ensembl Biomart source. This example maps an Ensembl gene ID to an Entrez gene ID.
```python
idmap.convert('ENSG00000159388','ensg','entr')
```
--------------------------------
### Get Gene Information using MyGene.info API
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Retrieves comprehensive gene information from the MyGene.info API for a list of gene IDs, specifying scopes and returning results as a DataFrame.
```python
output = mg.getgenes(id_list, scopes=id_relabel['ensg'], fields='*', species='human', as_dataframe=True, returnall=False)
```
--------------------------------
### Convert Gene IDs from Entrez to Ensembl
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Convert a list of Entrez gene IDs to Ensembl gene IDs using the initialized IDMapper. The output is returned as a pandas DataFrame. This example converts the first three gene IDs from `gene_list`.
```python
idmap.convert(gene_list[:3],'entr','ensg',df=True)
```
--------------------------------
### Convert Gene IDs using Biorosetta
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Converts a list of gene IDs from one format to another. Supports multiple hits and returns a pandas DataFrame. Ensure the biorosetta library is installed and imported as 'idm'.
```python
idm.convert(['ENSG00000210049','ENSG00000278457','ENSG00000233864','ENSG00000244646'],'ensg','symb',multi_hits='all',df=True)[1]
```
--------------------------------
### Convert Single Gene ID
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Convert a single gene ID from one type to another using the initialized `IDMapper`. This example converts an Ensembl gene ID (ENSG) to an Entrez gene ID (entr).
```python
idmap.convert('ENSG00000159388','ensg','entr') # Outputs '7832'
```
--------------------------------
### Initialize IDMapper with Aliases and Custom Sources
Source: https://context7.com/reemagit/biorosetta/llms.txt
Demonstrates initializing the IDMapper using convenient string aliases for common source configurations (all, local, remote) and custom lists for explicit priority ordering.
```python
import biorosetta as br
# Use all sources with default priority (Ensembl > HGNC > MyGene)
idmap_all = br.IDMapper('all')
# Use only local sources for reproducible offline conversions
idmap_local = br.IDMapper('local') # Equivalent to [EnsemblBiomartMapper(), HGNCBiomartMapper()]
# Use only remote sources for up-to-date conversions
idmap_remote = br.IDMapper('remote') # Equivalent to [MyGeneMapper()]
# Use single specific sources
idmap_ensembl = br.IDMapper('ensembl_biomart') # Equivalent to [EnsemblBiomartMapper()]
idmap_hgnc = br.IDMapper('hgnc_biomart') # Equivalent to [HGNCBiomartMapper()]
idmap_mygene = br.IDMapper('mygene') # Equivalent to [MyGeneMapper()]
# Custom source list with explicit priority ordering
idmap_custom = br.IDMapper([
br.MyGeneMapper(), # Highest priority
br.EnsemblBiomartMapper(),
br.HGNCBiomartMapper() # Lowest priority
])
# Verify by converting a gene ID
result = idmap_ensembl.convert('ENSG00000271254', 'ensg', 'entr')
print(result) # Output: '102724250'
```
--------------------------------
### Get DataFrame Shape
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Retrieves the dimensions (number of rows and columns) of a pandas DataFrame.
```python
pruned.shape
```
--------------------------------
### Initialize IDMapper with Default Sources
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Create an `IDMapper` instance using the string 'all' to automatically include all supported local and remote sources with a default priority order (Ensembl Biomart, HGNC Biomart, MyGene).
```python
idmap = br.IDMapper('all') # Equivalent to br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()])
```
--------------------------------
### Initialize EnsemblBiomartMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initializes the EnsemblBiomartMapper. If biomart data is not downloaded, it will prompt a download.
```python
import biorosetta as br
idm = br.EnsemblBiomartMapper()
```
--------------------------------
### Initialize IDMapper with multiple sources
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initializes the IDMapper with Ensembl BioMart, HGNC BioMart, and MyGene as data sources. Set list_all_hits=True to retrieve all possible mappings.
```python
idm = m.IDMapper([m.EnsemblBiomart(list_all_hits=True),m.HGNCBiomart(list_all_hits=True),m.MyGene()])
```
--------------------------------
### Initialize Gene Client with BioThings
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Initializes a BioThings client specifically for gene-related queries. This client is used to interact with the BioThings API for gene data.
```python
from biothings_client import get_client
mg = get_client('gene')
```
--------------------------------
### Initialize MyGene Remote Mapper
Source: https://context7.com/reemagit/biorosetta/llms.txt
Initializes the MyGeneMapper for remote gene ID conversions via the MyGene.info web service. Requires an internet connection.
```python
import biorosetta as br
# Initialize MyGene remote mapper
mygene_mapper = br.MyGeneMapper()
```
--------------------------------
### List Supported Sources and ID Types
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Use `br.list_sources()` to display all supported gene ID types and the available data sources for mapping. This helps in understanding the available conversion options.
```python
br.list_sources()
```
--------------------------------
### Handle Multiple Hits with 'first' Policy
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Configure IDMapper with the 'first' multi-hits policy to prioritize conversions from sources based on their order and successful hits. This is useful when a primary source is known.
```python
idmap = br.IDMapper([br.MyGeneMapper(),br.EnsemblBiomartMapper(),br.HGNCBiomartMapper()], multi_hits='first', df=True) # Notice that MyGene is first
idmap.convert(['ENSG00000210049','ENSG00000211459','ENSG00000210082'],'ensg','symb',df=True) # Outputs ['TRNF', 'RNR1', 'RNR2']
```
--------------------------------
### Initialize IDMapper with Multiple Sources
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Instantiate an IDMapper with a list of desired mapping sources. The `fill_value='passthrough'` argument ensures that IDs without a mapping are returned as-is.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()], fill_value='passthrough') # Multiple sources
```
--------------------------------
### Initialize IDMapper with a Single Local Source
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Create an `IDMapper` instance using a list containing a specific mapper, such as `EnsemblBiomartMapper()`, to perform conversions using only that single source.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper()]) # Single local source
```
--------------------------------
### Initialize Another EnsemblBiomartMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Initializes a second EnsemblBiomartMapper object. This might be for a different session or configuration.
```python
idm2 = br.EnsemblBiomartMapper()
```
--------------------------------
### Download File with Progress Bar
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Downloads a file from a given URL and saves it to a specified filename, displaying a progress bar during the download. It uses the requests library for fetching and tqdm for the progress bar.
```python
import requests
from tqdm import tqdm
def download(url: str, fname: str):
resp = requests.get(url, stream=True)
total = int(resp.headers.get('content-length', 0))
# Can also replace 'file' with a io.BytesIO object
with open(fname, 'wb') as file, tqdm(
desc=fname,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in resp.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
```
--------------------------------
### Initialize and Use IDMapper for Gene ID Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Demonstrates initializing the IDMapper with default sources and performing single and batch gene ID conversions between different types like Ensembl, NCBI/Entrez, and gene symbols.
```python
import biorosetta as br
# Initialize with all sources (default priority: Ensembl > HGNC > MyGene)
idmap = br.IDMapper('all')
# Convert single gene ID from Ensembl to NCBI/Entrez
result = idmap.convert('ENSG00000159388', 'ensg', 'entr')
print(result) # Output: '7832'
# Convert list of gene IDs
gene_list = ['ENSG00000159388', 'ENSG00000121022', 'ENSG00000134574']
results = idmap.convert(gene_list, 'ensg', 'entr')
print(results) # Output: ['7832', '10987', '1643']
# Convert Entrez IDs to gene symbols
symbols = idmap.convert(['7832', '10987', '1643'], 'entr', 'symb')
print(symbols) # Output: ['ZNF8', 'COPS3', 'DLD']
# Convert gene symbols to Ensembl IDs
ensg_ids = idmap.convert(['ZNF8', 'COPS3', 'DLD'], 'symb', 'ensg')
print(ensg_ids) # Output: ['ENSG00000159388', 'ENSG00000121022', 'ENSG00000134574']
```
--------------------------------
### Create Pandas DataFrame for Lookup
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initializes a pandas DataFrame and sets a column as the index, then converts it to a Series. Useful for creating lookup tables.
```python
lookup = pd.DataFrame([['ciao','Zio'],['pippo','Pluto'],['tizio','caio']],columns=['id1','id3']).set_index('id1').squeeze()
```
--------------------------------
### Load Mapper Data from Pickle File
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Loads the previously saved mapper data (lookup and data structures) from a pickle file. This bypasses the need to re-initialize and load the mapper from scratch.
```python
with open('/Users/reema/Postdoc/Temp/dict.pickle', 'rb') as f:
loaded_dict = pickle.load(f)
```
--------------------------------
### Initialize IDMapper with Different Source Order and Convert IDs to DataFrame with All Hits
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Initializes an IDMapper with Ensembl, HGNC, and MyGene sources in a different order. It converts Ensembl IDs to Ensembl protein IDs, requesting all possible hits and returning the result as a DataFrame. Note that some sources may not support the requested mapping.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()]) # Multiple sources
print(idmap.convert(['ENSG00000159388','ENSG00000211459','ENSG00000159388'],'ensg','ensp', multi_hits='all',df=True).to_markdown())
```
--------------------------------
### Initialize and Convert Gene IDs
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initializes a MyGene object and converts a list of mixed IDs (numeric, string, ENSG) from 'entr' to 'ensg' format. Handles potential 'notfound' entries.
```python
mygene = m.MyGene()
mygene.convert(['1017','ciao',1,'ENSG00000123374','A1BG'], 'entr', 'ensg')
```
--------------------------------
### Download Biomart Data
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Initiates the download of the 'biomart.tsv' file. Shows download progress and size.
```python
utils.download_biomart()
```
--------------------------------
### Debugging Pandas DataFrame Column Selection and Squeezing
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
This snippet shows debugging the selection of a DataFrame column and then using `.squeeze()` to convert it to a Series. It also includes checking the pandas version.
```python
return id_list_out.tolist()
```
```python
out_df[self._src_ids[0]]
```
```python
out_df['biomart'].squeeze()
```
```python
pd.__version__
```
--------------------------------
### Initialize IDMapper with All Sources
Source: https://context7.com/reemagit/biorosetta/llms.txt
Initializes an IDMapper instance to use all available gene ID mapping sources, including remote ones like MyGene. This provides maximum coverage for conversions.
```python
import biorosetta as br
idmap = br.IDMapper('all')
```
--------------------------------
### Initialize IDMapper with Multiple Sources and Convert IDs to List
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Initializes an IDMapper with MyGene, Ensembl, and HGNC sources. It then converts a list of Ensembl IDs to symbols, returning the result as a Python list. This is useful for simple lookups where a list format is preferred.
```python
idmap = br.IDMapper([br.MyGeneMapper(),br.EnsemblBiomartMapper(),br.HGNCBiomartMapper()]) # Multiple sources
print(idmap.convert(['ENSG00000210049','ENSG00000211459','ENSG00000210082'],'ensg','symb',df=True, multi_hits='consensus').to_markdown())
print(idmap.convert(['ENSG00000210049','ENSG00000211459','ENSG00000210082'],'ensg','symb',df=False, multi_hits='consensus'))
```
--------------------------------
### Initialize HGNC Mapper Without Synonyms
Source: https://context7.com/reemagit/biorosetta/llms.txt
Initializes an HGNCBiomartMapper instance, disabling synonym integration for potentially faster loading. This is useful when synonym data is not required.
```python
hgnc_no_syn = br.HGNCBiomartMapper(symb_aliases=False)
```
--------------------------------
### Initialize IDMapper with Multiple Sources and Convert IDs to DataFrame
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Initializes an IDMapper with MyGene, Ensembl, and HGNC sources. It then converts a list of Ensembl IDs to symbols, returning the result as a pandas DataFrame. This method is suitable when you need structured output for further analysis.
```python
idmap = br.IDMapper([br.MyGeneMapper(),br.EnsemblBiomartMapper(),br.HGNCBiomartMapper()]) # Multiple sources
print(idmap.convert(['ENSG00000210049','ENSG00000211459','ENSG00000210082'],'ensg','symb',df=True, multi_hits='consensus').to_markdown())
```
--------------------------------
### Initialize IDMapper Objects
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Instantiate IDMapper objects with source data. `idm` is initialized with `sources`, and `idm2` with `sources2`. These objects are used for mapping identifiers.
```python
idm = m.IDMapper(sources)
idm2 = m.IDMapper(sources2)
```
--------------------------------
### Download Data from Ensembl
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Use this function to download data from Ensembl using a provided XML query and save it to a local file.
```python
download('http://www.ensembl.org/biomart/martservice?query=','/Users/reema/Postdoc/Temp/results.txt')
```
--------------------------------
### Display conversion report
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Prints the generated report DataFrame, which includes mappings and a 'Mismatch' column indicating discrepancies.
```python
report
```
--------------------------------
### Initialize IDMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled1.ipynb
Instantiates the IDMapper class with a 'sources' argument. This is typically used to map identifiers across different biological databases.
```python
idm = br.IDMapper(sources)
```
--------------------------------
### Display DataFrame
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Displays the loaded Pandas DataFrame 'data'. This is useful for inspecting the data after loading.
```python
data
```
--------------------------------
### Configure Pandas Display Options
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Sets the maximum and minimum number of rows to display for Pandas DataFrames. This helps in controlling the output verbosity.
```python
pd.options.display.max_rows = 100
pd.options.display.min_rows = 100
```
--------------------------------
### Initialize IDMapper with Multiple Sources
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Initializes an IDMapper object with multiple biological data sources (Ensembl, HGNC, MyGene). It then converts the first 1000 Ensembl Gene IDs to symbols, returning a pandas DataFrame.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()]) # Multiple sources
results =idmap.convert(idmap._sources[0]._lookup['ensg']['symb'][:1000].index,'ensg','symb',df=True)
```
--------------------------------
### Save Mapper Data to Pickle File
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Saves the internal lookup and data structures of the HGNCBiomartMapper to a pickle file for persistence. This allows for faster loading in subsequent sessions.
```python
import pickle
with open('/Users/reema/Postdoc/Temp/dict.pickle', 'wb') as f:
pickle.dump([idm._lookup,idm._data], f)
```
--------------------------------
### Display HTML in IPython
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Displays HTML content within an IPython environment. Note: Importing display from IPython.core.display is deprecated.
```python
from IPython.core.display import display, HTML
display(HTML(""))
```
--------------------------------
### Load Biomart Data with Custom Headers
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Loads 'biomart.tsv' with custom column names ('ensg', 'symb', 'entr') and specifies 'entr' as a string type. Reorders columns to 'ensg', 'entr', 'symb'.
```python
pd.read_table('data/biomart.tsv',header=None,names=['ensg','symb','entr'], dtype={'entr':'str'})[['ensg','symb','entr']]
```
--------------------------------
### Generate Conversion Report with pandas DataFrame
Source: https://context7.com/reemagit/biorosetta/llms.txt
Utilize the `convert()` method with `df=True` to obtain a detailed pandas DataFrame of conversion results from each source, useful for diagnosing mapping issues and verifying accuracy.
```python
import biorosetta as br
# Initialize mapper with all sources
idmap = br.IDMapper([br.EnsemblBiomartMapper(), br.HGNCBiomartMapper(), br.MyGeneMapper()])
# Convert with full report
gene_list = ['1643', '10987', '23369']
report = idmap.convert(gene_list, 'entr', 'ensg', df=True)
print(report)
# Output DataFrame:
```
--------------------------------
### Display internal lookup table
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Prints the content of the internal lookup table, showing the mapping structure between different identifier types.
```python
tempvar
```
--------------------------------
### Convert Identifiers with IDMapper (with reporting)
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Use the `convert` method of the IDMapper object to translate a list of 'ensg' identifiers to 'symb' identifiers. The `report=True` argument provides detailed reporting on the conversion process.
```python
print(idm.convert(['ENSG00000210049','ENSG00000278457','ENSG00000233864','ENSG00000244646'],'ensg','symb',report=True))
```
--------------------------------
### Convert IDs with Multiple Hits
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Use `multi_hits='all'` to retrieve all ID outputs for each source, concatenated with a pipe symbol. This is useful when a single input ID can map to multiple output IDs.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()]) # Multiple sources
idmap.convert(['ENSG00000159388','ENSG00000211459','ENSG00000159388'],'ensg','ensp', multi_hits='all') # Returns ['ENSP00000433553|ENSP00000290551', 'N/A', 'ENSP00000433553|ENSP00000290551']
```
--------------------------------
### Load Biomart Data with Pandas
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Loads the 'biomart.tsv' file into a pandas DataFrame using tab as a separator. Assumes standard column names.
```python
pd.read_table('data/biomart.tsv',sep=' ')
```
--------------------------------
### Initialize HGNCBiomartMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Initializes an HGNCBiomartMapper object from the Biorosetta library. This mapper is used for converting between HGNC IDs and other identifiers.
```python
idm = br.HGNCBiomartMapper()
```
--------------------------------
### Display Combined Lookup DataFrame
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Displays the 'new_lookup' DataFrame, which contains the combined data from the original lookup and the newly processed entries.
```python
new_lookup
```
--------------------------------
### Force HGNC Data Download
Source: https://context7.com/reemagit/biorosetta/llms.txt
Forces a re-download of the latest HGNC data from the Biomart source. Use this when you need the most current HGNC information.
```python
br.HGNCBiomartMapper.download_data() # Downloads fresh data from HGNC Biomart
```
--------------------------------
### Download Ensembl Data
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Downloads Ensembl data from a specified URL to a local file. This function is useful for obtaining reference datasets for further analysis.
```python
br.utils.download(br.queries.ENSEMBL, '/Users/reema/Postdoc/Temp/ensembl.tsv')
```
--------------------------------
### Benchmark Groupby Apply for All Entries
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Measures the execution time for grouping by 'ensg' and joining 'entr' values for all entries in the 'pruned' DataFrame. This operation can be time-consuming for large datasets.
```python
%timeit pruned.groupby('ensg').apply(lambda x: '|'.join(x.entr))
```
--------------------------------
### List Available BioRosetta Sources and ID Types
Source: https://context7.com/reemagit/biorosetta/llms.txt
Use the `list_sources()` function to display all supported data sources and gene identifier types, including their input and output capabilities.
```python
import biorosetta as br
# List all available sources and supported ID types
br.list_sources()
# Output:
# ID types:
# 'ensg' = Ensembl gene ID
# 'entr' = NCBI gene ID (entrezgene)
# 'symb' = Gene symbol
# 'ensp' = Ensembl protein ID
# 'hgnc' = HGNC ID
# Sources:
# "ensembl_biomart": Ensembl Biomart (http://useast.ensembl.org/biomart/martview)
# ID in: ['ensg', 'entr', 'symb', 'ensp', 'hgnc']
# ID out: ['ensg', 'entr', 'symb', 'ensp', 'hgnc']
# "hgnc_biomart": HGNC Biomart (https://biomart.genenames.org/)
# ID in: ['ensg', 'entr', 'symb', 'hgnc']
# ID out: ['ensg', 'entr', 'symb', 'hgnc']
# "mygene": MyGene (https://mygene.info/)
# ID in: ['ensg', 'entr']
# ID out: ['ensg', 'entr', 'symb']
```
--------------------------------
### Import biorosetta and pandas
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Imports the biorosetta library and pandas for data manipulation. Reloads the biorosetta module to ensure the latest version is used.
```python
import biorosetta as m
import importlib as imp
imp.reload(m)
import pandas as pd
```
--------------------------------
### Benchmark Groupby Apply for Non-Unique Entries
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Measures the execution time for grouping by 'ensg' and joining 'entr' values specifically for non-unique entries. This is typically faster than processing all entries.
```python
%timeit nonunq.groupby('ensg').apply(lambda x: '|'.join(x.entr))
```
--------------------------------
### Convenience Method: Ensembl to Symbol Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Uses the convenience method `ensg2symb` to convert a list of Ensembl IDs to their corresponding gene symbols. This method is part of the IDMapper class.
```python
symbols = idmap.ensg2symb(['ENSG00000159388', 'ENSG00000121022'])
print(symbols) # ['ZNF8', 'COPS3']
```
--------------------------------
### Map Gene IDs to Symbols with Multiple Sources
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Maps a list of Ensembl IDs to gene symbols using Ensembl, HGNC, and MyGene sources. It returns a Pandas DataFrame with detailed mapping information, including hits from each source. Use 'all' for multi_hits to retrieve all possible matches.
```python
gene_list=[1643, 10987, 23369, 1044, 10015, 2185, 10107, 3320, 55014, 9342]
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()]) # Multiple sources
idmap.convert(gene_list,'entr','symb',df=True, multi_hits='all')
```
--------------------------------
### Convenience Method: Ensembl to Entrez Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Uses the convenience method `ensg2entr` to convert a list of Ensembl IDs to their corresponding Entrez IDs. This method is part of the IDMapper class.
```python
entrez_ids = idmap.ensg2entr(['ENSG00000159388', 'ENSG00000121022'])
print(entrez_ids) # ['7832', '10987']
```
--------------------------------
### Convert Ensembl IDs using CompoundMapper (DataFrame output)
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Converts Ensembl IDs to symbols using a CompoundMapper, returning a DataFrame with detailed mapping information including hits from each source.
```python
idm.convert(genes_to_convert,'ensg','symb',multi_hits='consensus',df=True)
```
--------------------------------
### Initialize EnsemblBiomartMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Initializes an EnsemblBiomartMapper object from the Biorosetta library. This mapper is used for converting between Ensembl IDs.
```python
idm = br.EnsemblBiomartMapper()
```
--------------------------------
### Configure Fill Values for Failed Gene ID Conversions
Source: https://context7.com/reemagit/biorosetta/llms.txt
Customize the output for failed gene ID conversions using the `fill_value` parameter. The default is 'N/A', but 'passthrough' returns the original input ID, or a custom string can be specified.
```python
import biorosetta as br
# Default behavior: return 'N/A' for failed conversions
idmap_default = br.IDMapper('all')
result = idmap_default.convert(['imaginary_gene1', 'imaginary_gene2'], 'ensg', 'entr')
print(result) # Output: ['N/A', 'N/A']
# Passthrough: return original input ID when conversion fails
idmap_passthrough = br.IDMapper('all', fill_value='passthrough')
result = idmap_passthrough.convert(['imaginary_gene1', 'ENSG00000159388'], 'ensg', 'entr')
print(result) # Output: ['imaginary_gene1', '7832']
# Custom fill value
idmap_custom = br.IDMapper('all', fill_value='UNKNOWN')
result = idmap_custom.convert(['imaginary_gene1', 'imaginary_gene2'], 'ensg', 'entr')
print(result) # Output: ['UNKNOWN', 'UNKNOWN']
```
--------------------------------
### Configure Multi-Hit Policies for Gene ID Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Control how multiple conversion candidates are handled using the `multi_hits` parameter. Options include 'first' for priority-based selection, 'consensus' for the most frequent ID, and 'all' to concatenate all hits.
```python
import biorosetta as br
# Initialize with custom source priority (MyGene first)
idmap = br.IDMapper([br.MyGeneMapper(), br.EnsemblBiomartMapper(), br.HGNCBiomartMapper()])
# multi_hits='first' - Returns first hit from highest priority source with match
genes = ['ENSG00000210049', 'ENSG00000211459', 'ENSG00000210082']
result_first = idmap.convert(genes, 'ensg', 'symb', multi_hits='first', df=True)
print(result_first[['input', 'output', 'mygene', 'ensembl', 'hgnc']])
# Output: ['TRNF', 'RNR1', 'RNR2'] (from MyGene, highest priority)
# input output mygene ensembl hgnc
# ENSG00000210049 TRNF TRNF MT-TF MT-TF
# ENSG00000211459 RNR1 RNR1 MT-RNR1 MT-RNR1
# ENSG00000210082 RNR2 RNR2 MT-RNR2 MT-RNR2
# multi_hits='consensus' - Returns most frequent ID across all sources
result_consensus = idmap.convert(genes, 'ensg', 'symb', multi_hits='consensus', df=True)
print(result_consensus[['input', 'output', 'mygene', 'ensembl', 'hgnc']])
# Output: ['MT-TF', 'MT-RNR1', 'MT-RNR2'] (consensus from Ensembl + HGNC)
# multi_hits='all' - Concatenates all hits with pipe separator
idmap_ensembl = br.IDMapper([br.EnsemblBiomartMapper()])
genes_with_multiple = ['ENSG00000159388']
result_all = idmap_ensembl.convert(genes_with_multiple, 'ensg', 'ensp', multi_hits='all')
print(result_all) # Output: ['ENSP00000433553|ENSP00000433553']
```
--------------------------------
### Generate Conversion Report with Multiple Sources
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Use IDMapper with multiple sources to convert gene IDs and generate a detailed report in a pandas DataFrame. This is useful for small conversions where accuracy is critical.
```python
idmap = br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()])
idmap.convert(gene_list[:3],'entr','ensg',df=True)
```
--------------------------------
### Use HGNCBiomartMapper for Local Gene ID Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Employ the `HGNCBiomartMapper` for gene ID conversion using locally cached HGNC Biomart data. It supports several ID types and gene symbol synonyms. Data is downloaded on first run.
```python
import biorosetta as br
# Initialize HGNC Biomart mapper (downloads data on first run)
hgnc_mapper = br.HGNCBiomartMapper()
# Output: "- Loading lookup tables from cache..."
# Use directly for single-source conversion
idmap = br.IDMapper([hgnc_mapper])
# Convert gene symbol to HGNC ID
hgnc_id = idmap.convert('TP53', 'symb', 'hgnc')
print(hgnc_id) # Output: HGNC ID for TP53
```
--------------------------------
### Convenience Method: Entrez to Symbol Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Uses the convenience method `entr2symb` to convert a list of Entrez IDs to their corresponding gene symbols. This method is part of the IDMapper class.
```python
symbols = idmap.entr2symb(['1643', '10987', '23369'])
print(symbols) # ['DLD', 'COPS3', 'PUM1']
```
--------------------------------
### Debugging Pandas DataFrame Attribute Access
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
This snippet demonstrates debugging attribute access on a Pandas DataFrame, specifically when using `__getattr__` and `__setattr__`. It highlights the difference between attribute access and item access.
```python
return object.__getattribute__(self, name)
```
--------------------------------
### Download Ensembl Data with BioRosetta
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Initiates the download of Ensembl data using the EnsemblBiomartMapper. This function fetches necessary lookup tables for ID mapping.
```python
br.EnsemblBiomartMapper.download_data()
```
--------------------------------
### Import BioRosetta Package
Source: https://github.com/reemagit/biorosetta/blob/master/README_pypi.md
This is the standard import statement for using the biorosetta package in your Python scripts.
```python
import biorosetta as br
```
--------------------------------
### Use EnsemblBiomartMapper for Local Gene ID Conversion
Source: https://context7.com/reemagit/biorosetta/llms.txt
Utilize the `EnsemblBiomartMapper` for gene ID conversion with locally cached Ensembl Biomart data. It supports multiple ID types and gene symbol synonyms. Data is downloaded on first run.
```python
import biorosetta as br
# Initialize Ensembl Biomart mapper (downloads data on first run)
ensembl_mapper = br.EnsemblBiomartMapper()
# Output: "- Loading lookup tables from cache..."
# Use directly for single-source conversion
idmap = br.IDMapper([ensembl_mapper])
# Convert Ensembl gene ID to protein ID (ENSP)
result = idmap.convert('ENSG00000159388', 'ensg', 'ensp')
print(result) # Output: Ensembl protein ID
# Convert to HGNC ID
hgnc_id = idmap.convert('ENSG00000159388', 'ensg', 'hgnc')
print(hgnc_id) # Output: HGNC ID
# Force re-download of Ensembl data
br.EnsemblBiomartMapper.download_data() # Downloads fresh data from Ensembl Biomart
# Custom data path
custom_mapper = br.EnsemblBiomartMapper(data_path='/path/to/custom/ensembl.tsv')
```
--------------------------------
### Print Processed DataFrames
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Prints the 'pruned_curr' DataFrame and the original 'lookup' DataFrame to the console, followed by a blank line for separation.
```python
print(pruned_curr)
print()
print(lookup)
```
--------------------------------
### Debug Python Code
Source: https://github.com/reemagit/biorosetta/blob/master/test_package.ipynb
Enters the Python debugger at the current execution point. This is useful for inspecting variables and stepping through code execution.
```python
%debug
```
--------------------------------
### Convert Gene ID using Default Sources
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Map an Ensembl gene ID to an Entrez gene ID using the default 'all' configuration of `IDMapper`, which includes multiple sources with a defined priority.
```python
idmap = br.IDMapper('all') # Equivalent to br.IDMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()])
idmap.convert('ENSG00000271254','ensg','entr') # Returns '102724250'
```
--------------------------------
### Convert Multiple Ensembl IDs to Ensembl Protein IDs
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Converts a list of the first 100 unique Ensembl Gene IDs to their corresponding Ensembl Protein IDs using the 'convert' method with 'multi_hits' set to 'all'. The result is then zipped with the original IDs.
```python
list(zip(idm._data['ensg'].unique()[:100],idm.convert(idm._data['ensg'].unique()[:100],'ensg','ensp',multi_hits='all')))
```
--------------------------------
### Converting Gene IDs with Reporting
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Converts a single gene ID from one symbol type to another and reports mismatches. Useful for verifying conversion accuracy.
```python
idm.convert('FLJ23569','symb','ensg', report=True)
```
--------------------------------
### Display Biomart DataFrame
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Displays the entire biomart DataFrame, showing gene identifiers (ensg), symbols (symb), and Entrez IDs (entr). This provides an overview of the loaded gene annotation data.
```python
biomart
```
--------------------------------
### Convert Ensembl IDs using CompoundMapper
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Converts Ensembl IDs to symbols using a CompoundMapper that includes Ensembl, HGNC, and MyGene mappers. Retrieves a consensus symbol when multiple hits are found.
```python
import biorosetta as br
genes_to_convert = ['ENSG00000210049','ENSG00000278457','ENSG00000233864','ENSG00000244646']
idm = br.CompoundMapper([br.EnsemblBiomartMapper(),br.HGNCBiomartMapper(),br.MyGeneMapper()])
idm.convert(genes_to_convert,'ensg','symb',multi_hits='consensus',df=True)
```
--------------------------------
### Batch Convert Ensembl IDs to Gene Symbols via MyGene
Source: https://context7.com/reemagit/biorosetta/llms.txt
Performs batch conversion of multiple Ensembl Gene IDs to their corresponding gene symbols using the remote MyGene.info API. This is more efficient for large lists.
```python
gene_list = ['ENSG00000159388', 'ENSG00000121022', 'ENSG00000134574']
symbols = idmap.convert(gene_list, 'ensg', 'symb')
print(symbols) # Output: list of gene symbols
```
--------------------------------
### Create a List of Gene IDs
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Defines a list containing Ensembl Gene IDs for subsequent processing or querying.
```python
id_list = ['ENSG00000159388']
```
--------------------------------
### Convert Ensembl Gene IDs to Protein IDs
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Converts a list of Ensembl Gene IDs (ENSG) to Ensembl Protein IDs (ENSP). The 'all' option for multi_hits ensures all possible protein IDs are returned for a given gene.
```python
idm.convert(['ENSG00000159388','ENSG00000112115','ENSG00000210049'],'ensg','ensp',multi_hits='all')
```
--------------------------------
### Create Pandas DataFrame for Data Addition
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled.ipynb
Defines a pandas DataFrame with multiple entries, including duplicates and variations, for subsequent data processing.
```python
add = pd.DataFrame([['ciao','zio'],
['ciao','zio'],
['prova','sbagliata'],
['prova2','sbagliata'],
['prova','sbagliata'],
['ciao','zio2'],
['ciao','zio3'],
['pippo','pluto'],
['pippo','pluto2']]
,columns=['id1','id2'])
```
--------------------------------
### Set Default Value for Failed Conversions
Source: https://github.com/reemagit/biorosetta/blob/master/README.md
Configure the default value for failed ID conversions when initializing the IDMapper. The default is 'N/A'.
```python
idmap = br.IDMapper('all')
idmap.convert(['imaginary_gene1','imaginary_gene2'],'ensg','entr') # Returns ['N/A', 'N/A']
```
--------------------------------
### Convert ENTR IDs to Symbols, taking the first hit
Source: https://github.com/reemagit/biorosetta/blob/master/Untitled2.ipynb
Converts a list of Entrez IDs (ENTR) to their corresponding symbols, taking only the first match if multiple are found. Returns a DataFrame.
```python
idm = br.get_mapper('all')
idm.convert(['100132596','81399','8293'],'entr','symb',multi_hits='first',df=True)
```