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]
def getEcotypeAllelePhenotype(self, id=None): """ 2009-6-22 if phenotype_method.data_type is binary or ordered_categorical, add 1 to phenotype_value to make accessions with zero-phenotype-value visible in the "Ecotype Allele Phenotype BarChart" 2009-3-5 return json data of ecotype, allele, phenotype for the javascript in templates/snp.html """ self.readInRequestParams(request, c) #1st get call_info_id 2 PC12 mapping rows = model.Stock_250kDB.CallInfo.query.filter_by(method_id=c.call_method_id).all() call_info_id2pcs = {} for row in rows: call_info_id2pcs[row.id] = row.pc_value_ls[:10] #if h.ecotype_info is None: #2009-3-6 not used right now # h.ecotype_info = getEcotypeInfo(model.db) pheno_data = getattr(model, 'pheno_data', None) if pheno_data is None: from variation.src.OutputPhenotype import OutputPhenotype pheno_data = OutputPhenotype.getPhenotypeData(model.db.metadata.bind, phenotype_avg_table=model.Stock_250kDB.PhenotypeAvg.table.name,\ phenotype_method_table=model.Stock_250kDB.PhenotypeMethod.table.name) model.pheno_data = pheno_data snpData = hc.getSNPDataGivenCallMethodID(c.call_method_id) pm = PhenotypeMethod.get(c.phenotype_method_id) 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")), \ ("phenotype", ("number", "Phenotype")), \ ("allele", ("string", "Allele")), ("country", ("string", "Country")),\ ("pc1", ("number","PC1")), ("pc2", ("number", "PC2")), ("pc3", ("number","PC3")), \ ("pc4", ("number", "PC4")), ("pc5", ("number","PC5")), ("pc6", ("number", "PC6"))] description = dict(column_name_type_ls) rows = model.db.metadata.bind.execute("select * from view_call where call_method_id=%s"%(c.call_method_id)) return_ls = [] 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 pheno_row_index = pheno_data.row_id2row_index.get(row.ecotype_id) pheno_col_index = pheno_data.col_id2col_index.get(c.phenotype_method_id) if pheno_row_index is not None and pheno_col_index is not None: phenotype_value = pheno_data.data_matrix[pheno_row_index][pheno_col_index] if phenotype_value == 'NA': phenotype_value = None else: phenotype_value = None #numpy.nan can't be recognized by ToJSon() if (pm.data_type=='binary' or pm.data_type=='ordered_categorical') and phenotype_value is not None: phenotype_value += 1 label = '%s ID:%s Phenotype:%s.'%(row.nativename, row.ecotype_id, phenotype_value) snpdata_row_index = snpData.row_id2row_index.get(str(row.ecotype_id)) snpdata_col_index = snpData.col_id2col_index.get('%s_%s'%(c.chromosome, c.position)) if snpdata_row_index is None or snpdata_col_index is None: allele = -2 else: allele = snpData.data_matrix[snpdata_row_index][snpdata_col_index] allele = number2nt[allele] return_ls.append(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], \ phenotype=phenotype_value, allele=allele, country=row.country)) data_table = gviz_api.DataTable(description) data_table.LoadData(return_ls) column_ls = [row[0] for row in column_name_type_ls] #column_ls.sort() #['date', 'ecotypeid', 'label', 'lat', 'lon', 'name', 'pc1', 'pc2', 'phenotype'] json_result = data_table.ToJSon(columns_order=column_ls) #ToJSonResponse only works with google.visualization.Query return json_result
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