示例#1
0
	def fetchTopCandidateGenesFromOneResultOneGeneList(self, id=None, type_id=None, list_type_id=0, \
			max_rank=200):
		"""
		2009-7-2 split out of showTopCandidateGenesFromOneResultOneGeneList()
			a server API which returns a google visualization table in json
			
			used by templates underlying both showTopCandidateGenesFromOneResultOneGeneList and showResultsGeneForOnePhenotype
		"""
		results_id = request.params.get('id', id)
		type_id = request.params.get('type_id', type_id)
		list_type_id = int(request.params.get('list_type_id', list_type_id))
		max_rank = request.params.get('max_rank', max_rank)
		if max_rank is not None:
			max_rank = int(max_rank)
		c.max_rank = max_rank
		c.type = ScoreRankHistogramType.get(type_id)
		c.list_type = model.Stock_250kDB.GeneListType.get(list_type_id)
		
		c.snp_gene_association_id2desc = self.snp_gene_association_id2desc
		c.row_ls = []
		c.counter = 0
		
		if results_id is None:
			results_id = request.params.get('results_id', None)
		if not results_id:
			call_method_id = request.params.get('call_method_id', None)
			if call_method_id is None:
				call_method_id = c.type.call_method_id
			phenotype_method_id = request.params.get('phenotype_method_id', None)
			analysis_method_id = request.params.get('analysis_method_id', None)
			rm = ResultsMethod.query.filter_by(call_method_id=call_method_id).filter_by(phenotype_method_id=phenotype_method_id).\
					filter_by(analysis_method_id=analysis_method_id).first()
			if rm is None:
				c.result = None
				candidate_gene_set = set()	#return nothing
				return self.getAssociationsGivenGene(type_id, maxRank=1, results_id=results_id, candidate_gene_set=candidate_gene_set)
			results_id = rm.id
		if results_id is None:
			candidate_gene_set = set()	#return nothing
			return self.getAssociationsGivenGene(type_id, maxRank=1, results_id=results_id, candidate_gene_set=candidate_gene_set)
		
		from variation.src.GeneListRankTest import GeneListRankTest
		if list_type_id>0:	#2009-2-22
			candidate_gene_set = GeneListRankTest.dealWithCandidateGeneList(list_type_id, return_set=True)
		else:
			candidate_gene_set = None
		return self.getAssociationsGivenGene(type_id, maxRank=c.max_rank, results_id=results_id, candidate_gene_set=candidate_gene_set)
示例#2
0
	def getOneResultJsonData(cls, rm, min_MAF, no_of_top_snps):
		"""
		2009-4-24
			refactored out of fetchOne()
			called upon only if its return is not in db.
		"""
		log.info("Getting json_data from result %s ... \n"%rm.id)
		param_data = PassingData(min_MAF=min_MAF)
		genome_wide_result = GeneListRankTest.getResultMethodContent(rm, min_MAF=min_MAF, pdata=param_data)
		
		max_value = genome_wide_result.max_value
		chr2length = {}
		max_length = 0
		for chr, min_max_pos in genome_wide_result.chr2min_max_pos.iteritems():
			chr2length[chr] = min_max_pos[1]-min_max_pos[0]
			max_length = max(max_length, chr2length[chr])
		
		return_ls = []
		description = {"position": ("number", "Position"),"value": ("number", "-log Pvalue")}
		chr2data = {}
		for i in range(no_of_top_snps):
			data_obj = genome_wide_result.get_data_obj_at_given_rank(i+1)
			if data_obj.chromosome not in chr2data:
				chr2data[data_obj.chromosome] = []
			chr2data[data_obj.chromosome].append(dict(position=data_obj.position, value=data_obj.value))
		
		json_result = {}
		for chr in chr2data:
			data_table = gviz_api.DataTable(description)
			data_table.LoadData(chr2data[chr])
			json_result[chr] = data_table.ToJSon(columns_order=("position", "value"))
												#,order_by="position")
		"""
		for i in range(10000):
			data_obj = genome_wide_result.get_data_obj_at_given_rank(i+1)
			return_ls.append(dict(chromosome=data_obj.chromosome, position=data_obj.position, value=data_obj.value))
		"""
		result = {
				'chr2data': json_result,
				'chr2length': chr2length,
				'max_value': max_value,
				'max_length': max_length,
				}
		log.info("Done.\n")
		return simplejson.dumps(result)
示例#3
0
	def fetchGeneList(self, id=None):
		"""
		2009-7-2
			fixed and functional
		2009-7-2
			return a google visualization data table of genes based on gene list
		"""
		list_type_id = int(request.params.get('list_type_id', id))
		
		column_name_type_ls = [("gene_symbol", ("string", "Symbol")), ("description", ("string", "Description")), \
							("type_of_gene", ("string", "Type Of Gene")), \
							("dbxrefs", ("string", "DB Cross References")), \
							("gene_id", ("string","Gene ID")), ("chromosome", ("string","chromosome")),\
							("strand", ("string","Strand")),\
							("start", ("number","Start")),("stop", ("number","Stop"))]
		description = dict(column_name_type_ls)
		
		from variation.src.GeneListRankTest import GeneListRankTest
		if list_type_id>0:	#2009-2-22
			candidate_gene_set = GeneListRankTest.dealWithCandidateGeneList(list_type_id, return_set=True)
		else:
			candidate_gene_set = set()
		
		return_ls = []
		counter = 0		
		for gene_id in candidate_gene_set:
			entry = dict()
			self.fillGVizDataEntryForOneGene(entry, gene_id, column_name_type_ls)
			counter += 1
			return_ls.append(entry)
		
		data_table = gviz_api.DataTable(description)
		data_table.LoadData(return_ls)
		column_ls = [row[0] for row in column_name_type_ls]
		json_result = data_table.ToJSon(columns_order=column_ls)	#ToJSonResponse only works with google.visualization.Query
		response.headers['Content-Type'] = 'application/json'
		return json_result
示例#4
0
	def getPlot(self):
		"""
		2010-1-22
			add argument call_method_id when calling DrawSNPRegion.drawRegionAroundThisSNP()
		2009-4-30
		"""
		chromosome = int(request.params.get('chromosome', 1))
		start = int(request.params.get('start', 1))
		stop = int(request.params.get('stop', 10))
		if start>stop:
			start, stop = stop, start
		snps_id = '%s_%s'%(chromosome, start)
		
		call_method_id = int(request.params.getone('call_method_id'))
		phenotype_method_id = int(request.params.getone('phenotype_method_id'))
		list_type_id = int(request.params.get('list_type_id', None))
		
		plot_key = (chromosome, start, stop, call_method_id, phenotype_method_id, list_type_id)
		
		if not hasattr(model, "plot_key2png_data"):
			model.plot_key2png_data = {}
		
		png_data = model.plot_key2png_data.get(plot_key)
		if png_data is not None:
			if hasattr(png_data, 'getvalue'):	# png_data is StringIO
				response.headers['Content-type'] = 'image/png'
				return png_data.getvalue()
			else:
				return png_data
		
		if not getattr(model, 'phenotype_id2analysis_method_id2gwr', None):
			model.phenotype_id2analysis_method_id2gwr = {}
		analysis_method_id2gwr = model.phenotype_id2analysis_method_id2gwr.get(phenotype_method_id)
		if not analysis_method_id2gwr:
			analysis_method_id2gwr = DrawSNPRegion.getSimilarGWResultsGivenResultsByGene(phenotype_method_id, call_method_id)
			model.phenotype_id2analysis_method_id2gwr[phenotype_method_id] = analysis_method_id2gwr
		
		if list_type_id>0:	#2009-2-22
			candidate_gene_set = GeneListRankTest.dealWithCandidateGeneList(list_type_id, return_set=True)
		else:
			candidate_gene_set = set()
		gene_annotation = model.gene_annotation
		
		snpData = hc.getSNPDataGivenCallMethodID(call_method_id)
		pheno_data = hc.getPhenotypeDataInSNPDataOrder(snpData)
	
		if h.ecotype_info is None:	#2009-3-6 not used right now
			h.ecotype_info = getEcotypeInfo(model.db)
		
		snp_info = getattr(model, 'snp_info', None)
		if snp_info is None:
			snp_info = DrawSNPRegion.getSNPInfo(model.db)
			model.snp_info = snp_info

		LD_info = None
		output_dir = '/tmp/'	#not used at all, place holder
		which_LD_statistic = 1
		this_snp = SNPPassingData(chromosome=chromosome, position=start, stop=stop, snps_id='%s_%s'%(chromosome, start))
		
		DrawSNPRegion.construct_chr_pos2index_forSNPData(snpData)	#prerequisite

		after_plot_data = DrawSNPRegion.drawRegionAroundThisSNP(phenotype_method_id, this_snp, candidate_gene_set, \
													gene_annotation, snp_info, \
								analysis_method_id2gwr, LD_info, output_dir, which_LD_statistic, \
								min_distance=20000, list_type_id=list_type_id,
								label_gene=True, \
								draw_LD_relative_to_center_SNP=False,\
								commit=True, snpData=snpData, phenData=pheno_data, \
								ecotype_info=h.ecotype_info, snpData_before_impute=None,\
								snp_matrix_data_type=1, call_method_id=call_method_id)
		
		if getattr(after_plot_data, 'png_data', None):
			response.headers['Content-type'] = 'image/png'
			model.plot_key2png_data[plot_key] = after_plot_data.png_data
			return after_plot_data.png_data.getvalue()
		else:
			model.plot_key2png_data[plot_key] = "No Plot"
			return model.plot_key2png_data[plot_key]