def yield_ensembl_names(gene_ids): import biopsy.identifiers.biomart as biomart, csv query = biomart.new_query() dataset = biomart.add_dataset(query, 'mmusculus_gene_ensembl') biomart.add_filter(dataset, 'ensembl_gene_id', ",".join(imap(str, gene_ids))) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'external_gene_id') for row in biomart.yield_csv_query_results(query): yield row[0], row[1]
def yield_uniprot_gene_mappings(gene_ids): import biopsy.identifiers.biomart as biomart, csv query = biomart.new_query() dataset = biomart.add_dataset(query, 'mmusculus_gene_ensembl') biomart.add_filter(dataset, 'ensembl_gene_id', ",".join(gene_ids)) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'unified_uniprot_accession') for row in csv.reader(biomart.execute_query(query), delimiter=','): if row[1]: yield biopsy.DbRef.parse_as(row[0], biopsy.db.ensembl), biopsy.DbRef.parse_as(row[1], biopsy.db.swissprot)
def yield_mouse_orthologs(hs_genes): # map into mouse orthologs using biomart query = B.new_query() dataset = B.add_dataset(query, 'hsapiens_gene_ensembl') B.add_attribute(dataset, 'ensembl_gene_id') B.add_attribute(dataset, 'mouse_ensembl_gene') filter = B.add_filter(dataset, name='ensembl_gene_id', value='') filter.set('value', ','.join(ensembl_hs_genes)) for chunk in B.split_big_list(ensembl_hs_genes, 50): #logging.info('Querying Ensembl biomart for chunk of %d genes', len(chunk)) filter.set('value', ','.join(chunk)) for row in B.yield_csv_query_results(query): if row[1]: yield row[1]
def _mgi_ids_from_biomart(): "Goes to biomart to map all mouse genes to MGI ids" from . import entrez result = cookbook.DictOfSets() # build biomart query for swissprot and execute query = biomart.new_query() dataset = biomart.add_dataset(query, 'mmusculus_gene_ensembl') biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'external_gene_id') for row in csv.reader(biomart.execute_query(query), delimiter=','): gene_ref = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) if row[1]: mgi_acc = row[1] if mgi_acc in mgi.acc2id(): yield gene_ref, mgi.acc2id()[mgi_acc]
def get_orthologs(species1, species2): "Returns a dict mapping ensembl genes of species 1 to genes of species 2." result = cookbook.DictOfSets() # build biomart query and execute query = biomart.new_query() dataset = biomart.add_dataset(query, species[species2].dataset()) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, '%s_ensembl_gene' % species[species1].short_name) biomart.print_query(query, 'query.xml') for row in csv.reader(biomart.execute_query(query), delimiter=','): if row[1]: ref_1 = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) ref_2 = biopsy.transfac.DbRef.parse_as(row[1], biopsy.transfac.db.ensembl) result[ref_2].add(ref_1) return result
def get_ensembl_go_annotations(genes): "@return: A map from the given genes to sets of go annotations." import biopsy.identifiers.biomart as biomart logging.info('Querying Ensembl biomart for GO annotations of %d genes', len(genes)) result = cookbook.DictOfLists() for id_attr, evidence_attr in [ ('go_biological_process_id', 'go_biological_process_linkage_type'), ('go_cellular_component_id', 'go_cellular_component_linkage_type'), ('go_molecular_function_id', 'go_molecular_function_linkage_type'), ]: query = biomart.new_query() dataset = biomart.add_dataset(query, 'mmusculus_gene_ensembl') biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, id_attr) biomart.add_attribute(dataset, evidence_attr) filter = biomart.add_filter(dataset, name='ensembl_gene_id', value='') for chunk in biomart.split_big_list((str(g) for g in genes), 50): #logging.info('Querying Ensembl biomart for chunk of %d genes', len(chunk)) filter.set('value', ','.join(chunk)) for row in biomart.yield_csv_query_results(query): if row[2] not in options.go_evidence_codes_to_ignore: result[row[0]].append(row[1]) logging.info('Found %d go annotations', sum(len(v) for v in result.values())) return result
def proteins_for_species(species): "Goes to biomart to map all genes in species to Swissprot proteins" from . import entrez result = cookbook.DictOfSets() # build biomart query for swissprot and execute query = biomart.new_query() dataset = biomart.add_dataset(query, species) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'ensembl_transcript_id') biomart.add_attribute(dataset, 'uniprot_swissprot_accession') for row in csv.reader(biomart.execute_query(query), delimiter=','): gene_ref = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) transcript_ref = biopsy.transfac.DbRef.parse_as(row[1], biopsy.transfac.db.ensembl) if row[2]: protein_ref = biopsy.transfac.DbRef.parse_as(row[2], biopsy.transfac.db.swissprot) result[gene_ref].add((transcript_ref, protein_ref)) # build biomart query for entrez protein and execute query = biomart.new_query() dataset = biomart.add_dataset(query, species) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'ensembl_transcript_id') biomart.add_attribute(dataset, 'protein') for row in csv.reader(biomart.execute_query(query), delimiter=','): gene_ref = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) transcript_ref = biopsy.transfac.DbRef.parse_as(row[1], biopsy.transfac.db.ensembl) if row[2]: protein_acc = row[2] #print "'%s'" % protein_acc if protein_acc in entrez.mouse_proteins().acc2id: for protein_id in entrez.mouse_proteins().acc2id[protein_acc]: ref = T.DbRef(T.db.entrez_protein, "", protein_id) result[gene_ref].add((transcript_ref, ref)) return result
def proteins_for_species(species): "Goes to biomart to map all genes in species to Swissprot proteins" from . import entrez result = cookbook.DictOfSets() # build biomart query for swissprot and execute query = biomart.new_query() dataset = biomart.add_dataset(query, species) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'ensembl_transcript_id') biomart.add_attribute(dataset, 'uniprot_swissprot_accession') for row in csv.reader(biomart.execute_query(query), delimiter=','): gene_ref = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) transcript_ref = biopsy.transfac.DbRef.parse_as( row[1], biopsy.transfac.db.ensembl) if row[2]: protein_ref = biopsy.transfac.DbRef.parse_as( row[2], biopsy.transfac.db.swissprot) result[gene_ref].add((transcript_ref, protein_ref)) # build biomart query for entrez protein and execute query = biomart.new_query() dataset = biomart.add_dataset(query, species) biomart.add_attribute(dataset, 'ensembl_gene_id') biomart.add_attribute(dataset, 'ensembl_transcript_id') biomart.add_attribute(dataset, 'protein') for row in csv.reader(biomart.execute_query(query), delimiter=','): gene_ref = biopsy.transfac.DbRef.parse_as(row[0], biopsy.transfac.db.ensembl) transcript_ref = biopsy.transfac.DbRef.parse_as( row[1], biopsy.transfac.db.ensembl) if row[2]: protein_acc = row[2] #print "'%s'" % protein_acc if protein_acc in entrez.mouse_proteins().acc2id: for protein_id in entrez.mouse_proteins().acc2id[protein_acc]: ref = T.DbRef(T.db.entrez_protein, "", protein_id) result[gene_ref].add((transcript_ref, ref)) return result