def type(self, id=None): ScoreRankHistogram = model.Stock_250kDB.ScoreRankHistogram ScoreRankHistogramType = model.Stock_250kDB.ScoreRankHistogramType query = ScoreRankHistogram.query if id is None: id = request.params.get('id', 1) hist_type_id = id extra_condition = 's.hist_type_id=%s'%hist_type_id c.phenotype_info = hc.getPhenotypeInfo(ScoreRankHistogram.table.name, extra_condition) c.list_info = hc.getListTypeInfo(ScoreRankHistogram.table.name, extra_condition) c.hist_type = ScoreRankHistogramType.get(hist_type_id) rows = query.filter_by(hist_type_id=hist_type_id).all() data_matrix = numpy.zeros([len(c.list_info.list_type_id_ls), len(c.phenotype_info.phenotype_method_id_ls)], numpy.int) data_matrix[:] = -1 counter = 0 for row in rows: row_index = c.list_info.list_type_id2index[row.list_type_id] col_index = c.phenotype_info.phenotype_method_id2index[row.phenotype_method_id] data_matrix[row_index, col_index] = row.id counter +=1 c.counter = counter c.data_matrix = data_matrix return render('/score_rank_histgram_type.html')
def type_by_snp(self, id=None): """ 2008-10-17 display SNP region plots in a matrix fashion by spanning the phenotype axis. """ SNPRegionPlot = model.Stock_250kDB.SNPRegionPlot if id is None: id = request.params.get('id', 1) extra_condition = 's.plot_type_id=%s'%id c.phenotype_info = hc.getPhenotypeInfo(SNPRegionPlot.table.name, extra_condition) c.snp_info = self.getSNPInfo(plot_type_id=id) data_matrix = numpy.zeros([len(c.snp_info.snp_ls), len(c.phenotype_info.phenotype_method_id_ls)], numpy.int) data_matrix[:] = -1 counter = 0 rows = SNPRegionPlot.query.filter_by(plot_type_id=id).all() for row in rows: snp_key = (row.chromosome, row.start, row.stop) row_index = c.snp_info.snp2index[snp_key] col_index = c.phenotype_info.phenotype_method_id2index[row.phenotype_method_id] data_matrix[row_index, col_index] = row.id counter +=1 c.counter = counter c.data_matrix = data_matrix return render('/snp_region_plot_type_by_snp.html')
def type_by_gene(self, id=None): """ 2008-10-17 """ SNPRegionPlot = model.Stock_250kDB.SNPRegionPlot SNPRegionPlotToGene = model.Stock_250kDB.SNPRegionPlotToGene if id is None: id = request.params.get('id', 1) extra_condition = 's.plot_type_id=%s'%id c.phenotype_info = hc.getPhenotypeInfo(SNPRegionPlot.table.name, extra_condition) c.gene_info = self.getGeneInfo(plot_type_id=id) data_matrix = numpy.zeros([len(c.gene_info.gene_ls), len(c.phenotype_info.phenotype_method_id_ls)], numpy.int) #data_matrix[:] = -1 counter = 0 rows = SNPRegionPlotToGene.query.filter(SNPRegionPlotToGene.snp_region_plot.has(plot_type_id=id)).all() for row in rows: row_index = c.gene_info.gene_id2index[row.gene_id] c.gene_info.gene_ls[row_index].snp_region_plot_ls.append(row.snp_region_plot) col_index = c.phenotype_info.phenotype_method_id2index[row.snp_region_plot.phenotype_method_id] data_matrix[row_index, col_index] = row.snp_region_plot.id counter +=1 for row_index in range(len(c.gene_info.gene_ls)): c.gene_info.gene_ls[row_index].snp_region_plot_ls.sort(lambda x, y: int(x.phenotype_method_id-y.phenotype_method_id)) #comparison function must return int. and long is NOT int. c.gene_info.gene_ls[row_index].snp_region_plot_id_ls = [str(snp_region_plot.id) for snp_region_plot in c.gene_info.gene_ls[row_index].snp_region_plot_ls] c.counter = counter c.data_matrix = data_matrix return render('/snp_region_plot_type_by_gene.html')
def getGeneInfo(self, plot_type_id=None): """ 2008-10-17 return data structure containing sorted gene list and its info """ SNPRegionPlot = model.Stock_250kDB.SNPRegionPlot SNPRegionPlotToGene = model.Stock_250kDB.SNPRegionPlotToGene rows = model.db.metadata.bind.execute("select g.gene_id, count(distinct s.phenotype_method_id) as count from %s g, %s s\ where s.id=g.plot_id and s.plot_type_id=%s group by g.gene_id order by count desc, gene_id"\ %(SNPRegionPlotToGene.table.name, SNPRegionPlot.table.name, plot_type_id)) gene_ls = [] gene_id2index = {} gene_label_ls = [] gene_desc_names = ['gene_id', 'gene_symbol', 'type_of_gene', 'chr', 'start', 'stop', 'protein_label', 'protein_comment', 'protein_text'] for row in rows: gene_id2index[row.gene_id] = len(gene_ls) matrix_of_gene_descriptions = hc.returnGeneDescLs(model.gene_annotation, gene_id_ls=[row.gene_id]) if len(matrix_of_gene_descriptions)>0: gene_desc_ls = matrix_of_gene_descriptions[0] gene = PassingData(gene_id=row.gene_id, gene_symbol=gene_desc_ls[1], type_of_gene=gene_desc_ls[2], chr=gene_desc_ls[3],\ start=gene_desc_ls[4], stop=gene_desc_ls[5], protein_label=gene_desc_ls[6], protein_comment=gene_desc_ls[7], \ protein_text=gene_desc_ls[8]) else: gene = PassingData(gene_id=row.gene_id) gene.count = row.count gene.snp_region_plot_ls = [] gene_ls.append(gene) gene_label_ls.append(getattr(gene, 'gene_symbol', row.gene_id)) gene_info = PassingData() gene_info.gene_id2index = gene_id2index gene_info.gene_ls = gene_ls gene_info.gene_label_ls = gene_label_ls return gene_info
def getCallMethodLs(): """ 2009-1-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name list_info = hc.getCallMethodInfo(affiliated_table_name=affiliated_table_name) call_method_ls = [] for i in range(len(list_info.id_ls)): id = list_info.id_ls[i] label = list_info.label_ls[i] call_method_ls.append([id, label]) return call_method_ls
def getPhenotypeMethodLs(call_method_id): """ 2009-1-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name extra_condition = 's.call_method_id=%s'%(call_method_id) phenotype_info = hc.getPhenotypeInfo(affiliated_table_name=affiliated_table_name, extra_condition=extra_condition) phenotype_method_ls = [] for i in range(len(phenotype_info.phenotype_method_id_ls)): phenotype_method_id = phenotype_info.phenotype_method_id_ls[i] phenotype_method_label = phenotype_info.phenotype_method_label_ls[i] phenotype_method_ls.append([phenotype_method_id, phenotype_method_label]) return phenotype_method_ls
def call_method(self, id=None): if id is None: id = request.params.get('id', 17) MAFVsScorePlot = model.Stock_250kDB.MAFVsScorePlot affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name extra_condition = 's.call_method_id=%s and s.id=m.results_method_id'%id extra_tables = '%s m'%model.Stock_250kDB.MAFVsScorePlot.table.name c.phenotype_info = hc.getPhenotypeInfo(affiliated_table_name, extra_condition, extra_tables) c.analysis_info = hc.getAnalysisMethodInfo(affiliated_table_name, extra_condition, extra_tables) rows = MAFVsScorePlot.query.filter(MAFVsScorePlot.results_method.has(call_method_id=id)) data_matrix = numpy.zeros([len(c.phenotype_info.phenotype_method_id_ls), len(c.analysis_info.id_ls)], numpy.int) data_matrix[:] = -1 counter = 0 for row in rows: row_index = c.phenotype_info.phenotype_method_id2index[row.results_method.phenotype_method_id] col_index = c.analysis_info.id2index[row.results_method.analysis_method_id] data_matrix[row_index, col_index] = row.id counter +=1 c.counter = counter c.data_matrix = data_matrix c.call_method_id = id return render('/maf_vs_score_call_method.html')
def getPhenotypeMethodLs(self, call_method_id=None, affiliated_table_name=None): """ 2009-4-6 copied from DisplayResults.py """ #call_method_id = request.params.getone('call_method_id') #affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name #extra_condition = 's.call_method_id=%s'%(call_method_id) extra_condition = '' phenotype_info = hc.getPhenotypeInfo(affiliated_table_name=affiliated_table_name, extra_condition=extra_condition) phenotype_method_ls = [] for i in range(len(phenotype_info.phenotype_method_id_ls)): phenotype_method_id = str(phenotype_info.phenotype_method_id_ls[i]) phenotype_method_label = phenotype_info.phenotype_method_label_ls[i] phenotype_method_ls.append([phenotype_method_id, phenotype_method_label]) return phenotype_method_ls
def getAnalysisMethodLs(call_method_id, phenotype_method_id): """ 2009-1-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name #alias is 's' extra_condition = 's.call_method_id=%s and s.phenotype_method_id=%s'%\ (call_method_id, phenotype_method_id) list_info = hc.getAnalysisMethodInfo(affiliated_table_name, extra_condition=extra_condition) analysis_method_ls = [] for i in range(len(list_info.id_ls)): id = list_info.id_ls[i] label = list_info.label_ls[i] description = list_info.description_ls[i] analysis_method_ls.append([id, label, description]) return analysis_method_ls
def getGeneListTypeLsGivenTypeAndPhenotypeMethodAndAnalysisMethod(): """ 2009-3-4 called by getGeneListTypeLsGivenTypeAndPhenotypeMethodAndAnalysisMethodJson() """ #if not affiliated_table_name: # affiliated_table_name = model.Stock_250kDB.CandidateGeneTopSNPTestRM.table.name #alias is 's' #extra_tables = ' %s c '%model.Stock_250kDB.ResultsMethod.table.name #extra_condition = 's.results_id=c.id and s.type_id=%s and c.call_method_id=%s and c.phenotype_method_id=%s and c.analysis_method_id=%s'%\ # (type_id, call_method_id, phenotype_method_id, analysis_method_id) list_info = hc.getListTypeInfo(affiliated_table_name=None, extra_condition=None, extra_tables=None) ls = [[0, u'No candidate gene list. (All SNPs)']] for i in range(len(list_info.list_type_id_ls)): id = list_info.list_type_id_ls[i] label = list_info.list_type_label_ls[i] ls.append([id, label]) return ls
def getPhenotypeMethodLsGivenType(type_id, call_method_id, extra_table_name=None): """ 2009-2-22 add argument extra_table_name 2008-12-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name if not extra_table_name: extra_table_name = model.Stock_250kDB.CandidateGeneTopSNPTestRM.table.name extra_tables = ' %s c '%extra_table_name extra_condition = 'c.results_id=s.id and c.type_id=%s and s.call_method_id=%s'%(type_id, call_method_id) phenotype_info = hc.getPhenotypeInfo(affiliated_table_name=affiliated_table_name, extra_condition=extra_condition, extra_tables=extra_tables) phenotype_method_ls = [] for i in range(len(phenotype_info.phenotype_method_id_ls)): phenotype_method_id = phenotype_info.phenotype_method_id_ls[i] phenotype_method_label = phenotype_info.phenotype_method_label_ls[i] phenotype_method_ls.append([phenotype_method_id, phenotype_method_label]) return phenotype_method_ls
def getCallMethodLsGivenType(type_id, extra_table_name=None): """ 2009-2-22 add argument extra_table_name 2008-12-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name if not extra_table_name: extra_table_name = model.Stock_250kDB.CandidateGeneTopSNPTestRM.table.name extra_tables = ' %s c '%extra_table_name extra_condition = 'c.results_id=s.id and c.type_id=%s'%type_id list_info = hc.getCallMethodInfo(affiliated_table_name=affiliated_table_name, extra_condition=extra_condition, extra_tables=extra_tables) call_method_ls = [] for i in range(len(list_info.id_ls)): id = list_info.id_ls[i] label = list_info.label_ls[i] call_method_ls.append([id, label]) return call_method_ls
def show_plot(self, id=None, snp_region_plot_ids=None): """ 2008-10-30 add option snp_region_plot_ids for display_results_gene_one.html to call but not necessary. request.params is good enough. 2008-10-17 get request params: get_other_phenotypes, snp_region_plot_ids 2008-10-06 """ SNPRegionPlot = model.Stock_250kDB.SNPRegionPlot if id: c.snp_region_plot = SNPRegionPlot.get(id) else: c.snp_region_plot = None get_other_phenotypes = request.params.get('get_other_phenotypes', False) snp_region_plot_ids = request.params.get('snp_region_plot_ids', snp_region_plot_ids) if get_other_phenotypes: c.snp_region_plot_ls = SNPRegionPlot.query.filter_by(chromosome=c.snp_region_plot.chromosome).\ filter_by(start=c.snp_region_plot.start).\ filter_by(stop=c.snp_region_plot.stop).\ filter_by(plot_type_id=c.snp_region_plot.plot_type_id).order_by(SNPRegionPlot.phenotype_method_id).all() elif snp_region_plot_ids: snp_region_plot_id_ls = snp_region_plot_ids.split(',') #snp_region_plot_id_ls = map(int, snp_region_plot_id_ls) c.snp_region_plot_ls = [SNPRegionPlot.get(int(snp_region_plot_id)) for snp_region_plot_id in snp_region_plot_id_ls] else: c.snp_region_plot_ls = [] if c.snp_region_plot is None and len(c.snp_region_plot_ls)>0: c.snp_region_plot = c.snp_region_plot_ls[0] #plot_file_path = os.path.join(config['app_conf']['plots_store'], 'snp_region_plot_%s.png'%c.snp_region_plot.id) #if not os.path.isfile(plot_file_path): # self.savePlot(c.snp_region_plot, plot_file_path) gene_annotation = model.gene_annotation gene_id_ls = [plot2gene.gene_id for plot2gene in c.snp_region_plot.plot2gene_ls] c.matrix_of_gene_descriptions = hc.returnGeneDescLs(gene_annotation, gene_id_ls) #print abc #raise Exception('Just testing the interactive debugger!') #c.matrix_of_gene_descriptions = [] return render('/snp_region_plot_single.html')
def getGeneListTypeLsGivenTypeAndPhenotypeMethodAndAnalysisMethod(type_id, call_method_id, phenotype_method_id, analysis_method_id, affiliated_table_name=None): """ 2009-2-22 add argument affiliated_table_name (but not used) 2008-12-30 """ if not affiliated_table_name: affiliated_table_name = model.Stock_250kDB.CandidateGeneTopSNPTestRM.table.name #alias is 's' extra_tables = ' %s c '%model.Stock_250kDB.ResultsMethod.table.name extra_condition = 's.results_id=c.id and s.type_id=%s and c.call_method_id=%s and c.phenotype_method_id=%s and c.analysis_method_id=%s'%\ (type_id, call_method_id, phenotype_method_id, analysis_method_id) list_info = hc.getListTypeInfo(affiliated_table_name, extra_condition=extra_condition, extra_tables=extra_tables) ls = [] for i in range(len(list_info.list_type_id_ls)): id = list_info.list_type_id_ls[i] label = list_info.list_type_label_ls[i] ls.append([id, label]) return ls
def getCallMethodLsJson(self): """ 2009-11-30 return all possible call_method_ids in table association_overlapping_stat """ affiliated_table_name = model.Stock_250kDB.AssociationOverlappingStat.table.name list_info = hc.getCallMethodInfo(affiliated_table_name=affiliated_table_name) call_method_ls = [] for i in range(len(list_info.id_ls)): id = list_info.id_ls[i] label = list_info.label_ls[i] call_method_ls.append([id, label]) result = { 'options': [ dict(id=value, value=id) for id, value in call_method_ls ] } #result['options'].append({'id': u'[At the end]', 'value': u''}) result['options'].insert(0, {'id': u'Please Choose ...', 'value': 0}) return simplejson.dumps(result)
def getAnalysisMethodLsGivenTypeAndPhenotypeMethod(type_id, call_method_id, phenotype_method_id, extra_table_name=None): """ 2009-2-22 add argument extra_table_name 2008-12-30 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name #alias is 's' if not extra_table_name: extra_table_name = model.Stock_250kDB.CandidateGeneTopSNPTestRM.table.name extra_tables = ' %s c '%extra_table_name extra_condition = 'c.results_id=s.id and c.type_id=%s and s.call_method_id=%s and s.phenotype_method_id=%s'%\ (type_id, call_method_id, phenotype_method_id) list_info = hc.getAnalysisMethodInfo(affiliated_table_name, extra_condition=extra_condition, extra_tables=extra_tables) analysis_method_ls = [] for i in range(len(list_info.id_ls)): id = list_info.id_ls[i] label = list_info.label_ls[i] analysis_method_ls.append([id, label]) return analysis_method_ls
def getTrendData(self): """ 2009-5-3 """ phenoData = hc.getPhenotypeData() column_name_type_ls = [("label", ("string","ID Name")), ("date", ("date", "Date")), \ ("which_phenotype", ("number","Which Phenotype")), ("phenotype_value", ("number","Phenotype Value"))] no_of_non_phenotype_cols = len(column_name_type_ls) phenotype_id_included = [] for phenotype_id in self.phenotype_method_id_list: i = phenoData.col_id2col_index.get(phenotype_id) if i is not None: phenotype_id_included.append(phenotype_id) description = dict(column_name_type_ls) return_ls = [] for i in range(len(phenotype_id_included)): phenotype_id = phenotype_id_included[i] date=datetime.date(i+1000,1,1) for j in range(len(phenoData.row_id_ls)): row_label = phenoData.row_label_ls[j] row_id = phenoData.row_id_ls[j] label = '%s ID:%s'%(row_label, row_id) pheno_row_index = j 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() data = dict(date=date, label=label, which_phenotype=i, phenotype_value=phenotype_value) 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
def getPhenotypeMethodLsGivenType(type_id, extra_table_name=None): """ 2009-4-13 added results_gene2type into the condition to restrict phenotype_methods type_id is actually used now. 2009-3-4 """ affiliated_table_name = model.Stock_250kDB.ResultsMethod.table.name type = ScoreRankHistogramType.get(type_id) call_method_id = type.call_method_id if not extra_table_name: extra_table_name = model.Stock_250kDB.ResultsGene.table.name extra_tables = ' %s c, %s y'%(extra_table_name, "results_gene2type") extra_condition = 'c.results_id=s.id and s.call_method_id=%s and y.score_rank_histogram_type_id=%s and y.results_gene_id=c.id'%\ (call_method_id, type_id) phenotype_info = hc.getPhenotypeInfo(affiliated_table_name=affiliated_table_name, extra_condition=extra_condition, extra_tables=extra_tables) phenotype_method_ls = [] for i in range(len(phenotype_info.phenotype_method_id_ls)): phenotype_method_id = phenotype_info.phenotype_method_id_ls[i] phenotype_method_label = phenotype_info.phenotype_method_label_ls[i] phenotype_method_ls.append([phenotype_method_id, phenotype_method_label]) return phenotype_method_ls, call_method_id
def getOverlappingDataAcrossPhenotypes(self, no_of_top_snps=1000): """ 2009-11-30 """ call_method_id = int(request.params.get('call_method_id', config['app_conf']['published_call_method_id'])) no_of_top_snps = int(request.params.get('no_of_top_snps', no_of_top_snps)) phenotype_info = hc.getPhenotypeInfo(affiliated_table_name=model.Stock_250kDB.AssociationOverlappingStat.table.name, \ extra_condition="s.call_method_id=%s"%call_method_id, extra_tables=None, \ with_category_separator=False) overlapping_type_info = hc.getAssociationOverlappingTypeInfo(affiliated_table_name=model.Stock_250kDB.AssociationOverlappingStat.table.name, \ extra_condition="s.call_method_id=%s"%call_method_id, extra_tables=None) #construct the full data and turn it into json column_name_type_ls = [("label", ("string","ID Name")), ("date", ("date", "Date")), ("phenotype_method_id", ("number", "Phenotype ID")), \ ("phenotype_name", ("string", "Phenotype Name")), \ ("category", ("string","Category")), ] no_of_non_stat_cols = len(column_name_type_ls) for i in range(len(overlapping_type_info.list_type_label_ls)): column_name_type_ls.append(('%s'%overlapping_type_info.list_type_id_ls[i], ("number", overlapping_type_info.list_type_label_ls[i]))) return_ls = [] for i in range(len(phenotype_info.phenotype_method_id2index)): entry = {} for column_name_type in column_name_type_ls[no_of_non_stat_cols:]: # set the default value for the overlapping stats column_name = column_name_type[0] column_type = column_name_type[1][0] entry[column_name] = None return_ls.append(entry) rows = model.Stock_250kDB.AssociationOverlappingStat.query.filter_by(call_method_id=call_method_id).filter_by(no_of_top_snps=no_of_top_snps) for row in rows: phenotype_index = phenotype_info.phenotype_method_id2index[row.phenotype_method_id] entry = return_ls[phenotype_index] for column_name_type in column_name_type_ls[:no_of_non_stat_cols]: # only the info related to phenotype part column_name = column_name_type[0] column_type = column_name_type[1][0] if column_type=='string': default_value = '' elif column_type=='date': default_value = datetime.date(2050, 1, 1) else: default_value = None if column_name == 'phenotype_name': pm = model.Stock_250kDB.PhenotypeMethod.get(row.phenotype_method_id) column_value = pm.short_name elif column_name=='category': pm = model.Stock_250kDB.PhenotypeMethod.get(row.phenotype_method_id) if pm.biology_category: column_value = pm.biology_category.short_name else: column_value = None elif column_name=='label': pm = model.Stock_250kDB.PhenotypeMethod.get(row.phenotype_method_id) column_value = '%s %s'%(row.phenotype_method_id, pm.short_name) else: column_value = getattr(row, column_name, default_value) if column_value is None: column_value = default_value entry[column_name] = column_value # set the overlapping stat column_name = '%s'%row.overlapping_type_id entry[column_name] = row.no_of_overlapping_snps description = dict(column_name_type_ls) 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
def getPhenotypeTableData(self): """ 2009-11-17 "#Accessions" means the number of accessions that were phenotyped. add "#Genotyped" column in the output, which means the no of accessions that were phenotyped and genotyped (in certain call method). 2009-11-17 getNoOfAccessionsGivenPhenotypeAndCallMethodID() replaces getNoOfAccessionsGivenPhenotypeMethodID() 2009-7-30 fetch the number of accessions for each phenotype by calling hc.getNoOfAccessionsGivenPhenotypeMethodID(), rather than static value in table phenotype_method 2009-5-24 add column transformation_description back 2009-5-13 remove column transformation_description, citations 2009-5-11 add no_of_accessions, growth_condition, phenotype_scoring, citations in the output table 2009-4-21 return a google data table containing information of all phenotypes belonging to a category """ call_method_id = request.params.get('call_method_id', int(config['app_conf']['published_call_method_id'])) call_method_id = int(call_method_id) biology_category_id = request.params.get('biology_category_id', 1) if biology_category_id==0 or biology_category_id=='0': biology_category_id=None #construct the full data and turn it into json column_name_type_ls = [("id", ("number", "Phenotype ID")), \ ("short_name", ("string", "Phenotype Name")), \ ("association_results", ("string","Association Results")), \ ("no_of_accessions", ("number","#Accessions")), \ ("no_of_genotyped_accessions", ("number","#Genotyped")), \ ("growth_condition", ("string","Growth Condition")), \ ("phenotype_scoring", ("string","Phenotype Scoring")), \ ("method_description", ("string","Source")), \ ("data_type", ("string", "Data Type")),\ ("transformation_description", ("string", "Transformation"))] rows = model.Stock_250kDB.PhenotypeMethod.query.filter(model.Stock_250kDB.PhenotypeMethod.biology_category_id==biology_category_id) return_ls = [] for row in rows: entry = dict() for column_name_type in column_name_type_ls: column_name = column_name_type[0] column_type = column_name_type[1][0] if column_type=='string': default_value = '' elif column_type =='number': default_value = -1 elif column_type=='date': default_value = datetime.date(2050, 1, 1) else: default_value = None if column_name == 'association_results': results = model.Stock_250kDB.ResultsMethod.query.filter_by(call_method_id=call_method_id).filter_by(phenotype_method_id=row.id) analysis_method_short_name_ls = [] for result in results: analysis_method_short_name_ls.append(result.analysis_method.short_name) if analysis_method_short_name_ls: column_value = ','.join(analysis_method_short_name_ls) else: column_value = "" elif column_name=='no_of_accessions': column_value = hc.getNoOfAccessionsGivenPhenotypeMethodID(row.id) elif column_name=='no_of_genotyped_accessions': column_value = hc.getNoOfAccessionsGivenPhenotypeAndCallMethodID(row.id, call_method_id) else: column_value = getattr(row, column_name, default_value) if column_value is None: column_value = default_value entry[column_name] = column_value return_ls.append(entry) description = dict(column_name_type_ls) 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
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
def type(cls, id=None, TypeClass=model.Stock_250kDB.CandidateGeneTopSNPTestRMType, \ template='/display_top_snp_test_rm_type.html', \ phenotype_id_ls_str = '1-7,39-61,80-82,9-13,32-38,65-74', \ list_type_id_ls_str = '3,6,8,28,51,64,65,68,71,76,129,24,29,30,130,131',\ analysis_method_id_ls_str = '1,5,6,7',\ figure_type=1, call_method_id=17): """ 2009-10-13 allow user to specify phenotype_id_ls_str and list_type_id_ls_str in URL add figure_type argument: 1: candidate vs non-candidate ratio (in db beforehand) 2: 2D matrix-plot of pvalue for enrichment statistic. row is cutoff. column is distance between gene and SNP. add call_method_id argument add argument analysis_method_id_ls_str 2008-10-30 generalized so that DisplayResultsGene could call it """ ResultsMethod = model.Stock_250kDB.ResultsMethod if id is None: id = request.params.get('id', '1') phenotype_id_ls_str = request.params.get('phenotype_id_ls_str', phenotype_id_ls_str) # 2009-10-13 list_type_id_ls_str = request.params.get('list_type_id_ls_str', list_type_id_ls_str) # 2009-10-13 analysis_method_id_ls_str = request.params.get('analysis_method_id_ls_str', analysis_method_id_ls_str) # 2009-10-13 c.figure_type = request.params.get('figure_type', figure_type) # 2009-10-13 call_method_id = request.params.get('call_method_id', call_method_id) # 2009-10-13 phenotype_id_ls = getListOutOfStr(phenotype_id_ls_str, data_type=str) list_type_id_ls = getListOutOfStr(list_type_id_ls_str, data_type=str) analysis_method_id_ls = getListOutOfStr(analysis_method_id_ls_str, data_type=int) #extra_tables = ' %s c '%(CandidateGeneTopSNPTestRM.table.name) extra_condition = 'p.id in (%s)'%(','.join(phenotype_id_ls)) c.phenotype_info = hc.getPhenotypeInfo(extra_condition=extra_condition, extra_tables=None) #extra_condition = 's.type_id =%s'%id extra_condition = 'p.id in (%s)'%(','.join(list_type_id_ls)) c.list_info = hc.getListTypeInfo(extra_condition=extra_condition) data_matrix = [] no_of_rows = len(c.list_info.list_type_id_ls) no_of_cols = len(c.phenotype_info.phenotype_method_id_ls) for i in range(no_of_rows): data_matrix.append([None]*no_of_cols) c.type = TypeClass.get(id) #2008-10-29 a faster way to come up the data_matrix but data is not guaranteed inside rows = ResultsMethod.query.filter(ResultsMethod.phenotype_method_id.in_(map(int, phenotype_id_ls))).\ filter(ResultsMethod.analysis_method_id.in_(analysis_method_id_ls)).\ filter_by(call_method_id=call_method_id).\ order_by(ResultsMethod.analysis_method_id) counter = 0 for row in rows: col_index = c.phenotype_info.phenotype_method_id2index.get(row.phenotype_method_id) if col_index is None: continue for list_type_id, row_index in c.list_info.list_type_id2index.iteritems(): if list_type_id>0: if data_matrix[row_index][col_index] is None: data_matrix[row_index][col_index] = [] data_matrix[row_index][col_index].append((row.id, row.analysis_method.short_name)) counter +=1 """ rows = CandidateGeneTopSNPTestRM.query.filter_by(type_id=id) for row in rows: row_index = c.list_info.list_type_id2index[row.list_type_id] col_index = c.phenotype_info.phenotype_method_id2index[row.result.phenotype_method_id] if data_matrix[row_index][col_index] is None: data_matrix[row_index][col_index] = Set() data_matrix[row_index][col_index].add(row.results_id) counter +=1 """ c.counter = counter c.data_matrix = data_matrix return render(template)