def getCallInfoData(self, call_method_id, phenotype_method_id): """ 2009-5-17 add region & country in the returning data structure 2009-2-2 get ecotype id, name, latitude, longitude, pc1, pc2, phenotype for all call_infos in one call_method_id """ #1st get call_info_id 2 PC12 mapping 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[:2] #2nd check if model.pheno_data exists 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 #3rd finally construct the full data and turn it into json column_name_type_ls = [("date", ("date", "Date")), ("ecotypeid", ("number", "Ecotype ID")), ("label", ("string","ID Name Phenotype")), \ ("lat",("number", "Latitude")), ("lon",("number", "Longitude")), ("name", ("string", "Native Name")), \ ("pc1", ("number","PC1")), ("pc2", ("number", "PC2")), ("phenotype", ("number", "Phenotype")),\ ("region", ("string", "Region")), ("country", ("string", "Country"))] 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 = [] for row in rows: pc12 = call_info_id2pcs.get(row.call_info_id) if pc12: call_info_pc1, call_info_pc2 = pc12[:2] pc1 = call_info_pc1.pc_value pc2 = call_info_pc2.pc_value else: pc1 = pc2 = 0 pheno_row_index = pheno_data.row_id2row_index.get(row.ecotype_id) pheno_col_index = pheno_data.col_id2col_index.get(int(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() label = '%s ID:%s Phenotype:%s.'%(row.nativename, row.ecotype_id, phenotype_value) #label = 'ID:%s. Name=%s. Phenotype=%s.'%(row.ecotype_id, row.nativename, phenotype_value) return_ls.append(dict(ecotypeid=row.ecotype_id, name=row.nativename, lat=row.latitude, lon=row.longitude,\ pc1=pc1, pc2=pc2, phenotype=phenotype_value, label=label, date=datetime.date(2009,2,3), \ region=row.region, 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 getPhenotypeData(cls): """ 2009-4-30 get data from all the phenotypes into one matrix (accession by phenotype) """ phenoData = getattr(model, "phenoData", None) if phenoData is None: from variation.src.OutputPhenotype import OutputPhenotype phenoData = 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.phenoData = phenoData return phenoData
def getPhenotypeValue(self, id=None, returnJson=True): """ 2009-4-6 given a phenotype method id return 1. ecotype id : phenotype value 2. min & max phenotype value for requests to make phenotype markers on the map """ phenotype_method_id = request.params.get('phenotype_method_id', id) #2nd check if model.phenoData exists phenoData = getattr(model, 'phenoData', None) if phenoData is None: from variation.src.OutputPhenotype import OutputPhenotype phenoData = 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.phenoData = phenoData ecotype_id2phenotype_value = {} pheno_col_index = phenoData.col_id2col_index.get(int(phenotype_method_id)) min_value = None max_value = None for ecotype_id,pheno_row_index in phenoData.row_id2row_index.iteritems(): 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] ecotype_id2phenotype_value[ecotype_id] = phenotype_value if phenotype_value != 'NA': if min_value == None or phenotype_value<min_value: min_value = phenotype_value if max_value == None or phenotype_value>max_value: max_value = phenotype_value dc = dict(min_value=min_value, max_value=max_value, ecotype_id2phenotype_value=ecotype_id2phenotype_value) if returnJson: response.headers['Content-Type'] = 'application/json' return simplejson.dumps(dc) else: return dc
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