Exemple #1
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]
Exemple #2
0
	def getMultiPhenotypeData(self):
		"""
		2009-5-28
			present all phenotype data. no filter by phenotype_method_id_list
		2009-5-24
			add 'region' as a column into the data
		2009-5-3
		"""
		call_method_id = request.params.get('call_method_id', None)
		snpData = hc.getSNPDataGivenCallMethodID(call_method_id)
		phenoData = hc.getPhenotypeDataInSNPDataOrder(snpData)
		
		
		rows = model.Stock_250kDB.CallInfo.query.filter_by(method_id=call_method_id).all()
		call_info_id2pcs = {}
		for row in rows:
			call_info_id2pcs[row.id] = row.pc_value_ls[:10]
		
		column_name_type_ls = [("label", ("string","ID Name Phenotype")), ("date", ("date", "Date")), \
							("lon",("number", "Longitude")), ("lat",("number", "Latitude")), \
							("ecotypeid", ("number", "Ecotype ID")), ("name", ("string", "Native Name")), \
							("country", ("string", "Country")), ("region", ("string", "Region")),\
							("pc1", ("number","PC1")), ("pc2", ("number", "PC2")), ("pc3", ("number","PC3")), \
							("pc4", ("number", "PC4")), ("pc5", ("number","PC5")), ("pc6", ("number", "PC6"))]
		
		no_of_non_phenotype_cols = len(column_name_type_ls)
		phenotype_id_included = []
		#for phenotype_id  in self.phenotype_method_id_list:
		for phenotype_id in phenoData.col_id2col_index:
			i = phenoData.col_id2col_index.get(phenotype_id)
			if i is not None:
				phenotype_name = phenoData.col_label_ls[i]
				column_name_type_ls.append(("phenotype %s"%phenotype_id, ("number", phenotype_name)))
				phenotype_id_included.append(phenotype_id)
		
		description = dict(column_name_type_ls)
		rows = model.db.metadata.bind.execute("select * from view_call where call_method_id=%s"%(call_method_id))
		return_ls = []
		phenoData_row_id_type = type(phenoData.row_id2row_index.keys()[0])
		for row in rows:
			pcs = call_info_id2pcs.get(row.call_info_id)
			if pcs:
				pc_value_ls = [getattr(call_info_pc, 'pc_value') for call_info_pc in pcs]
			else:
				pc_value_ls = [0]*10
			label = '%s ID:%s'%(row.nativename, row.ecotype_id)
			
			data = dict(date=datetime.date(2009,2,3), ecotypeid=row.ecotype_id, label=label, name=row.nativename, \
								lat=row.latitude, lon=row.longitude,\
								pc1=pc_value_ls[0], pc2=pc_value_ls[1], pc3=pc_value_ls[2], pc4=pc_value_ls[3], \
								pc5=pc_value_ls[4], pc6=pc_value_ls[5], \
								country=row.country, region=row.region)
			# construct a tuple with (phenotype_name, phenotype_value) as entry, incorporated into data in the end
			phenotype_name_value_tup_ls = []	#
			for i in range(no_of_non_phenotype_cols, len(column_name_type_ls)):
				phenotype_name = column_name_type_ls[i][0]
				phenotype_id = phenotype_id_included[i-no_of_non_phenotype_cols]
				pheno_row_index = phenoData.row_id2row_index.get(phenoData_row_id_type(row.ecotype_id))
				pheno_col_index = phenoData.col_id2col_index.get(phenotype_id)
				if pheno_row_index is not None and pheno_col_index is not None:
					phenotype_value = phenoData.data_matrix[pheno_row_index][pheno_col_index]
					if phenotype_value == 'NA' or numpy.isnan(phenotype_value):
						phenotype_value = None
				else:
					phenotype_value = None	#numpy.nan can't be recognized by ToJSon()
				phenotype_name_value_tup_ls.append((phenotype_name, phenotype_value))
			data.update(dict(phenotype_name_value_tup_ls))
			return_ls.append(data)
		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
		return json_result