Skip to content

Instantly share code, notes, and snippets.

@peter-b
Created April 28, 2016 21:40
Show Gist options
  • Save peter-b/eacf85e8fd2df21cd50937355da60493 to your computer and use it in GitHub Desktop.
Save peter-b/eacf85e8fd2df21cd50937355da60493 to your computer and use it in GitHub Desktop.
Contig to chromosome mapping with Ensembl
import requests
import sys
def ensembl_get(ext):
uri = 'http://rest.ensemblgenomes.org' + ext
r = requests.get(uri,
headers = { "Content-Type": "application/json" })
if not r.ok:
r.raise_for_status()
return r.json()
def ensembl_assembly(species):
return ensembl_get('/info/assembly/{}?'.format(species))
def ensembl_genome_assembly(assembly_id):
return ensembl_get('/info/genomes/assembly/{}?'.format(assembly_id))
def ensembl_assembly_region(species, region):
return ensembl_get('/info/assembly/{}/{}?'.format(species, region))
def ensembl_map(species, source_asm, source_region,
target_asm, coord_system,
target_coord_system):
ext = '/map/{}/{}/{}/{}?'.format(species, source_asm,
source_region, target_asm)
ext += 'coord_system={}&target_coord_system={}'.format(coord_system, target_coord_system)
return ensembl_get(ext)
def ensembl_map_contig_to_chromosome(species, asm, region):
return ensembl_map(species, asm, region, asm, 'contig', 'chromosome')
##################
def remap(contig):
info = ensembl_map_contig_to_chromosome('hordeum_vulgare', 'ASM32608v1', contig)
if len(info['mappings']) != 1:
raise 'More than one mapping for {}!'.format(contig)
mapping = info['mappings'][0]['mapped']
chromosome = mapping['seq_region_name']
start = mapping['start']
end = mapping['end']
print('{}\t{}:{}-{}'.format(contig,chromosome,start,end))
for contig in sys.argv[1:]:
remap(contig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment