<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 sys
import json
import postgap.LD
import postgap.Globals
from postgap.DataModel import GWAS_Cluster
import logging

import collections
snp_with_zscore = collections.namedtuple(
	'snp_with_zscore', 
	[
		'snp_id',
		'z_score'
	]
)

class ZScoreComputationException(Exception):
	pass

class FinemapFailedException(Exception):
	pass

class IntegrityCheckException(Exception):
	pass


def GWAS_Clusters_ok(gwas_clusters):
	for gwas_cluster in gwas_clusters:
		if not(ld_snps_contain_gwas_snps(gwas_cluster)):
			return False, "Gwas snps not among ld snps"
	return True, ""

def compute_gwas_cluster_with_finemap_posteriors(gwas_cluster):
	
	try:
		finemap_posteriors = compute_finemap_posteriors(gwas_cluster)
		
		if finemap_posteriors is None:
			raise FinemapFailedException("Got no finemap results!")

	except ZScoreComputationException, z:
		logger = logging.getLogger(__name__)
		logger.warning(str(z))
		logger.warning("Skipping this cluster.")
		return
	
	return GWAS_Cluster(
		gwas_snps          = gwas_cluster.gwas_snps,
		ld_snps            = gwas_cluster.ld_snps,
		finemap_posteriors = finemap_posteriors
	)

def compute_finemap_posteriors(gwas_cluster):
	
	ld_snps   = gwas_cluster.ld_snps
	gwas_snps = gwas_cluster.gwas_snps
	
	(approximated_gwas_zscores, r2_array, SNP_ids) = compute_approximated_gwas_zscores(
		gwas_snps = gwas_snps,
		ld_snps   = ld_snps
	)
	logger = logging.getLogger(__name__)
	logger.info("Running finemap")
	
	kstart = 1
	kmax   = 1
	max_iter = "Not used when kstart == kmax"

	import finemap.stochastic_search as sss
	finemap_posteriors = sss.finemap(
		labels     = SNP_ids,
		z_scores   = approximated_gwas_zscores,
		cov_matrix = r2_array,
		n          = len(r2_array),
		kstart     = kstart,
		kmax       = kmax,
		max_iter   = max_iter,
		prior      = "independence"
	)
	logger.info("Done running finemap")
	
	if finemap_posteriors is None:
		raise Exception ("ERROR: Didn't get posteriors!")<commit_msg>Added cluster names for better stringification<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 sys
import json
import postgap.LD
import postgap.Globals
from postgap.DataModel import GWAS_Cluster
import logging

import collections
snp_with_zscore = collections.namedtuple(
	'snp_with_zscore', 
	[
		'snp_id',
		'z_score'
	]
)

class ZScoreComputationException(Exception):
	pass

class FinemapFailedException(Exception):
	pass

class IntegrityCheckException(Exception):
	pass


def GWAS_Clusters_ok(gwas_clusters):
	for gwas_cluster in gwas_clusters:
		if not(ld_snps_contain_gwas_snps(gwas_cluster)):
			return False, "Gwas snps not among ld snps"
	return True, ""

def compute_gwas_cluster_with_finemap_posteriors(gwas_cluster, cluster_name="Unnamed cluster"):
	
	try:
		finemap_posteriors = compute_finemap_posteriors(gwas_cluster, cluster_name = cluster_name)
		
		if finemap_posteriors is None:
			raise FinemapFailedException("Got no finemap results!")

	except ZScoreComputationException, z:
		logger = logging.getLogger(__name__)
		logger.warning(str(z))
		logger.warning("Skipping this cluster.")
		return
	
	return GWAS_Cluster(
		gwas_snps          = gwas_cluster.gwas_snps,
		ld_snps            = gwas_cluster.ld_snps,
		finemap_posteriors = finemap_posteriors
	)

def compute_finemap_posteriors(gwas_cluster, cluster_name="Unnamed cluster"):
	
	ld_snps   = gwas_cluster.ld_snps
	gwas_snps = gwas_cluster.gwas_snps
	
	(approximated_gwas_zscores, r2_array, SNP_ids) = compute_approximated_gwas_zscores(
		gwas_snps = gwas_snps,
		ld_snps   = ld_snps
	)
	logger = logging.getLogger(__name__)
	logger.info("Running finemap")
	
	kstart = 1
	kmax   = 1
	max_iter = "Not used when kstart == kmax"

	import finemap.stochastic_search as sss
	finemap_posteriors = sss.finemap(
		labels       = SNP_ids,
		z_scores     = approximated_gwas_zscores,
		cov_matrix   = r2_array,
		n            = len(r2_array),
		kstart       = kstart,
		kmax         = kmax,
		max_iter     = max_iter,
		prior        = "independence",
		sample_label = cluster_name
	)
	logger.info("Done running finemap")
	
	if finemap_posteriors is None:
		raise Exception ("ERROR: Didn't get posteriors!")
{'fraction_non_alphanumeric': 0.07704213241616509, 'fraction_numerical': 0.005846947549441101, 'mean_word_length': 4.455909943714822, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '939865', 'n_tokens_mistral': 2136, 'n_tokens_neox': 1955, 'n_words': 518}