<commit_before>#! /usr/bin/env python

"""

Copyright [1999-2016] EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""

"""

	Please email comments or questions to the public Ensembl
	developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

	Questions may also be sent to the Ensembl help desk at
	<http://www.ensembl.org/Help/Contact>.

"""

import postgap.REST
from postgap.DataModel import *
import postgap.Globals
from postgap.Utils import *

ENSEMBL_REST_SERVER = "http://grch37.rest.ensembl.org"
known_genes = {}

def get_gene(gene_name):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	if gene_name not in known_genes:
		if gene_name[:4] != 'ENSG':
			known_genes[gene_name] = fetch_gene(gene_name)
		else:
			known_genes[gene_name] = fetch_gene_id(gene_name)
	return known_genes[gene_name]

def fetch_gene(gene_name):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	server = ENSEMBL_REST_SERVER
	ext = "/lookup/symbol/%s/%s?content-type=application/json" % (postgap.Globals.SPECIES, gene_name)
	try:
		hash = postgap.REST.get(server, ext)
		return Gene(
			name = gene_name,
			id = hash['id'],
			chrom = hash['seq_region_name'],
			tss = int(hash['start']) if hash['strand'] > 0 else int(hash['end']),
			biotype = hash['biotype']
			)
	except:
		return None

def fetch_gene_id(gene_id):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	server = ENSEMBL_REST_SERVER
	ext = "/lookup/id/%s?content-type=application/json" % (gene_id)
	try:
		hash = postgap.REST.get(server, ext)
		return Gene(
			name = hash['display_name'],
			id = hash['id'],
			chrom = hash['seq_region_name'],
			tss = int(hash['start']) if hash['strand'] > 0 else int(hash['end']),
			biotype = hash['biotype']
			)
	except:
		return None


def get_ensembl_gene(ensembl_id):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	if ensembl_id not in known_genes:
		known_genes[ensembl_id] = fetch_ensembl_gene(ensembl_id)
	return known_genes[ensembl_id]

def fetch_ensembl_gene(ensembl_id):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	server = ENSEMBL_REST_SERVER
	ext = "/lookup/id/%s?content-type=application/json" % (ensembl_id)
	hash = postgap.REST.get(server, ext)
	return Gene(
		name = hash['display_name'],
		id = ensembl_id,
		chrom = hash['seq_region_name'],
		tss = int(hash['start']) if hash['strand'] > 0 else int(hash['end']),
		biotype = hash['biotype']
		)

def get_snp_locations(rsIDs):
	"""

		Get SNP details from rsID
		* [ string ]
		Returntype: [ SNP ]

	"""
	if len(rsIDs) == 0:
		return []

	res = get_snp_locations_simple(rsIDs) 

	if len(res) == 0:
		if len(rsIDs) == 1:
			return []
		else:
			return get_snp_locations(rsIDs[:len(rsIDs)/2]) + get_snp_locations(rsIDs[len(rsIDs)/2:])
	else:
		return res
	


def get_snp_locations_simple(rsIDs):
	"""

		Get SNP details from rsID<commit_msg>Allow Ensembl queries to switch servers<commit_after>#! /usr/bin/env python

"""

Copyright [1999-2016] EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

"""

"""

	Please email comments or questions to the public Ensembl
	developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.

	Questions may also be sent to the Ensembl help desk at
	<http://www.ensembl.org/Help/Contact>.

"""

import postgap.REST
from postgap.DataModel import *
import postgap.Globals
from postgap.Utils import *

GRCH37_ENSEMBL_REST_SERVER = "http://grch37.rest.ensembl.org"
GRCH38_ENSEMBL_REST_SERVER = "http://rest.ensembl.org"
known_genes = {}

def get_gene(gene_name, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	key = (gene_name, ENSEMBL_REST_SERVER)
	if key not in known_genes:
		if gene_name[:4] != 'ENSG':
			gene = fetch_gene(gene_name, ENSEMBL_REST_SERVER)
			if gene is None:
				known_genes[key] = None
			else:
				key2 = (gene.id, ENSEMBL_REST_SERVER)
				if key2 in known_genes:
					# Already found the same gene but under a different name (capitalisation changes etc)
					known_genes[key] = known_genes[key2]
				else:
					known_genes[key] = gene
					known_genes[key2] = gene
		else:
			known_genes[key] = fetch_gene_id(gene_name)
	return known_genes[key]

def fetch_gene(gene_name, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	server = ENSEMBL_REST_SERVER
	ext = "/lookup/symbol/%s/%s?content-type=application/json" % (postgap.Globals.SPECIES, gene_name)
	try:
		hash = postgap.REST.get(server, ext)
		return Gene(
			name = gene_name,
			id = hash['id'],
			chrom = hash['seq_region_name'],
			tss = int(hash['start']) if hash['strand'] > 0 else int(hash['end']),
			biotype = hash['biotype']
			)
	except:
		return None

def fetch_gene_id(gene_id, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	server = ENSEMBL_REST_SERVER
	ext = "/lookup/id/%s?content-type=application/json" % (gene_id)
	try:
		hash = postgap.REST.get(server, ext)
		return Gene(
			name = hash['display_name'],
			id = hash['id'],
			chrom = hash['seq_region_name'],
			tss = int(hash['start']) if hash['strand'] > 0 else int(hash['end']),
			biotype = hash['biotype']
			)
	except:
		return None


def get_ensembl_gene(ensembl_id, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get gene details from name
		* string
		Returntype: Gene

	"""
	key = (ensembl_id, ENSEMBL_REST_SERVER)
	if key not in known_genes:
		known_genes[key] = fetch_gene_id(ensembl_id, ENSEMBL_REST_SERVER)
	return known_genes[key]

def get_snp_locations(rsIDs, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get SNP details from rsID
		* [ string ]
		Returntype: [ SNP ]

	"""
	if len(rsIDs) == 0:
		return []

	res = get_snp_locations_simple(rsIDs, ENSEMBL_REST_SERVER) 

	if len(res) == 0:
		if len(rsIDs) == 1:
			return []
		else:
			return get_snp_locations(rsIDs[:len(rsIDs)/2]) + get_snp_locations(rsIDs[len(rsIDs)/2:])
	else:
		return res
	


def get_snp_locations_simple(rsIDs, ENSEMBL_REST_SERVER = GRCH37_ENSEMBL_REST_SERVER):
	"""

		Get SNP details from rsID
{'fraction_non_alphanumeric': 0.11632088520055325, 'fraction_numerical': 0.008990318118948824, 'mean_word_length': 4.270408163265306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '335424', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2531, 'n_words': 664}