From 0931212bc692177cfc0ebcf016bc869dd4f88fd8 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Tue, 27 Nov 2012 14:44:14 -0600 Subject: Renamed webqtlDataSet.py to data_set.py Renamed the class webqtlDataset to DataSet Finished cisLRS and transLRS search types in d_search.py Fixed parent/f1 issue in show_trait.py --- wqflask/base/data_set.py | 162 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100755 wqflask/base/data_set.py (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py new file mode 100755 index 00000000..992c673e --- /dev/null +++ b/wqflask/base/data_set.py @@ -0,0 +1,162 @@ +# Copyright (C) University of Tennessee Health Science Center, Memphis, TN. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# This program is available from Source Forge: at GeneNetwork Project +# (sourceforge.net/projects/genenetwork/). +# +# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010) +# at rwilliams@uthsc.edu and xzhou15@uthsc.edu +# +# +# +# This module is used by GeneNetwork project (www.genenetwork.org) +# +# Created by GeneNetwork Core Team 2010/08/10 +# +# Last updated by GeneNetwork Core Team 2010/10/20 + +from htmlgen import HTMLgen2 as HT + +import webqtlConfig + + + +class DataSet(object): + """ + Dataset class defines a dataset in webqtl, can be either Microarray, + Published phenotype, genotype, or user input dataset(temp) + + """ + + def __init__(self, dbName, cursor=None): + + assert dbName + self.id = 0 + self.name = '' + self.type = '' + self.group = '' + self.cursor = cursor + + #temporary storage + if dbName.find('Temp') >= 0: + self.searchfield = ['name','description'] + self.disfield = ['name','description'] + self.type = 'Temp' + self.id = 1 + self.fullname = 'Temporary Storage' + self.shortname = 'Temp' + elif dbName.find('Publish') >= 0: + pass + elif dbName.find('Geno') >= 0: + self.searchfield = ['name','chr'] + self.disfield = ['name','chr','mb', 'source2', 'sequence'] + self.type = 'Geno' + else: #ProbeSet + self.searchfield = ['name','description','probe_target_description', + 'symbol','alias','genbankid','unigeneid','omim', + 'refseq_transcriptid','probe_set_specificity', 'probe_set_blat_score'] + self.disfield = ['name','symbol','description','probe_target_description', + 'chr','mb','alias','geneid','genbankid', 'unigeneid', 'omim', + 'refseq_transcriptid','blatseq','targetseq','chipid', 'comments', + 'strand_probe','strand_gene','probe_set_target_region', + 'probe_set_specificity', 'probe_set_blat_score','probe_set_blat_mb_start', + 'probe_set_blat_mb_end', 'probe_set_strand', + 'probe_set_note_by_rw', 'flag'] + self.type = 'ProbeSet' + self.name = dbName + if self.cursor and self.id == 0: + self.retrieveName() + + + # Delete this eventually + @property + def riset(): + Weve_Renamed_This_As_Group + + + def get_group(self): + assert self.cursor + if self.type == 'Publish': + query = ''' + SELECT + InbredSet.Name, InbredSet.Id + FROM + InbredSet, PublishFreeze + WHERE + PublishFreeze.InbredSetId = InbredSet.Id AND + PublishFreeze.Name = "%s" + ''' % self.name + elif self.type == 'Geno': + query = ''' + SELECT + InbredSet.Name, InbredSet.Id + FROM + InbredSet, GenoFreeze + WHERE + GenoFreeze.InbredSetId = InbredSet.Id AND + GenoFreeze.Name = "%s" + ''' % self.name + elif self.type == 'ProbeSet': + query = ''' + SELECT + InbredSet.Name, InbredSet.Id + FROM + InbredSet, ProbeSetFreeze, ProbeFreeze + WHERE + ProbeFreeze.InbredSetId = InbredSet.Id AND + ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND + ProbeSetFreeze.Name = "%s" + ''' % self.name + else: + return "" + self.cursor.execute(query) + group, RIID = self.cursor.fetchone() + if group == 'BXD300': + group = "BXD" + self.group = group + self.group_id = RIID + return group + + + def retrieveName(self): + assert self.id == 0 and self.cursor + query = ''' + SELECT + Id, Name, FullName, ShortName + FROM + %sFreeze + WHERE + public > %d AND + (Name = "%s" OR FullName = "%s" OR ShortName = "%s") + '''% (self.type, webqtlConfig.PUBLICTHRESH, self.name, self.name, self.name) + try: + self.cursor.execute(query) + self.id,self.name,self.fullname,self.shortname=self.cursor.fetchone() + except: + raise KeyError, `self.name`+' doesn\'t exist.' + + + def genHTML(self, Class='c0dd'): + return HT.Href(text = HT.Span('%s Database' % self.fullname, Class= "fwb " + Class), + url= webqtlConfig.INFOPAGEHREF % self.name,target="_blank") + +class PhenotypeDataSet(DataSet): + + def __init__(self): + self.searchfield = ['name','post_publication_description','abstract','title','authors'] + self.disfield = ['name','pubmed_id', + 'pre_publication_description', 'post_publication_description', 'original_description', + 'pre_publication_abbreviation', 'post_publication_abbreviation', + 'lab_code', 'submitter', 'owner', 'authorized_users', + 'authors','title','abstract', 'journal','volume','pages','month', + 'year','sequence', 'units', 'comments'] + self.type = 'Publish' \ No newline at end of file -- cgit v1.2.3 From 94dd9844fb55f4576d3a079e9d5e59ebbf911b8c Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Tue, 27 Nov 2012 17:59:17 -0600 Subject: Created subclass for each main data set type and moved the code for getting trait info that was in search_results.py into its respective class Renamed webqtlDataset to DataSet/create_dataset in webqtlTrait.py, webqtlDatabaseFunction.py, and CorrelationPage.py Got search page running again for mRNA assay data sets with these changes --- web/webqtl/search/SearchResultPage.py | 6 +- wqflask/base/data_set.py | 556 ++++++++++++++++++++----- wqflask/base/webqtlTrait.py | 25 +- wqflask/dbFunction/webqtlDatabaseFunction.py | 23 +- wqflask/wqflask/correlation/CorrelationPage.py | 4 +- wqflask/wqflask/do_search.py | 22 +- wqflask/wqflask/search_results.py | 325 +-------------- 7 files changed, 514 insertions(+), 447 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/web/webqtl/search/SearchResultPage.py b/web/webqtl/search/SearchResultPage.py index 029a54c4..d62bb449 100755 --- a/web/webqtl/search/SearchResultPage.py +++ b/web/webqtl/search/SearchResultPage.py @@ -14,7 +14,7 @@ from htmlgen import HTMLgen2 as HT from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell -from base.webqtlDataset import webqtlDataset +from base.data_set import DataSet from base.webqtlTrait import webqtlTrait from base.templatePage import templatePage from utility import webqtlUtil @@ -65,12 +65,12 @@ class SearchResultPage(templatePage): InbredSet where PublishFreeze.Name not like 'BXD300%' and InbredSet.Id = PublishFreeze.InbredSetId""") results = self.cursor.fetchall() - self.database = map(lambda x: webqtlDataset(x[0], self.cursor), results) + self.database = map(lambda x: DataSet(x[0], self.cursor), results) self.databaseCrosses = map(lambda x: x[1], results) self.databaseCrossIds = map(lambda x: x[2], results) self.singleCross = False else: - self.database = map(lambda x: webqtlDataset(x, self.cursor), self.database) + self.database = map(lambda x: DataSet(x, self.cursor), self.database) #currently, webqtl wouldn't allow multiple crosses #for other than multiple publish db search #so we can use the first database as example diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 992c673e..9e3e6d81 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -19,64 +19,64 @@ # # # This module is used by GeneNetwork project (www.genenetwork.org) -# -# Created by GeneNetwork Core Team 2010/08/10 -# -# Last updated by GeneNetwork Core Team 2010/10/20 + +from __future__ import print_function, division from htmlgen import HTMLgen2 as HT import webqtlConfig +from pprint import pformat as pf +# Used by create_database to instantiate objects +DS_NAME_MAP = {} + +def create_dataset(db_conn, dataset_name): + cursor = db_conn.cursor() + cursor.execute(""" + SELECT DBType.Name + FROM DBList, DBType + WHERE DBList.Name = %s and + DBType.Id = DBList.DBTypeId + """, (dataset_name)) + print("dataset_name:", dataset_name) + dataset_type = cursor.fetchone()[0] + print("dataset_type:", pf(dataset_type)) + + dataset_ob = DS_NAME_MAP[dataset_type] + #dataset_class = getattr(data_set, dataset_ob) + + print("DS_NAME_MAP:", pf(DS_NAME_MAP)) + + dataset_class = globals()[dataset_ob] + return dataset_class(dataset_name, db_conn) class DataSet(object): """ - Dataset class defines a dataset in webqtl, can be either Microarray, + DataSet class defines a dataset in webqtl, can be either Microarray, Published phenotype, genotype, or user input dataset(temp) """ - def __init__(self, dbName, cursor=None): - - assert dbName - self.id = 0 - self.name = '' - self.type = '' - self.group = '' - self.cursor = cursor - - #temporary storage - if dbName.find('Temp') >= 0: - self.searchfield = ['name','description'] - self.disfield = ['name','description'] - self.type = 'Temp' - self.id = 1 - self.fullname = 'Temporary Storage' - self.shortname = 'Temp' - elif dbName.find('Publish') >= 0: - pass - elif dbName.find('Geno') >= 0: - self.searchfield = ['name','chr'] - self.disfield = ['name','chr','mb', 'source2', 'sequence'] - self.type = 'Geno' - else: #ProbeSet - self.searchfield = ['name','description','probe_target_description', - 'symbol','alias','genbankid','unigeneid','omim', - 'refseq_transcriptid','probe_set_specificity', 'probe_set_blat_score'] - self.disfield = ['name','symbol','description','probe_target_description', - 'chr','mb','alias','geneid','genbankid', 'unigeneid', 'omim', - 'refseq_transcriptid','blatseq','targetseq','chipid', 'comments', - 'strand_probe','strand_gene','probe_set_target_region', - 'probe_set_specificity', 'probe_set_blat_score','probe_set_blat_mb_start', - 'probe_set_blat_mb_end', 'probe_set_strand', - 'probe_set_note_by_rw', 'flag'] - self.type = 'ProbeSet' - self.name = dbName - if self.cursor and self.id == 0: - self.retrieveName() - - + def __init__(self, name, db_conn): + + assert name + self.name = name + self.db_conn = db_conn + self.cursor = self.db_conn.cursor() + self.id = None + self.type = None + self.group = None + + #if self.cursor and self.id == 0: + self.setup() + + self.check_confidentiality() + + self.retrieve_name() + self.get_group() + + # Delete this eventually @property def riset(): @@ -85,8 +85,93 @@ class DataSet(object): def get_group(self): assert self.cursor - if self.type == 'Publish': - query = ''' + self.cursor.execute(self.query) + self.group, self.group_id = self.cursor.fetchone() + if self.group == 'BXD300': + self.group = "BXD" + #return group + + + def retrieve_name(self): + """ + If the data set name parameter is not found in the 'Name' field of the data set table, + check if it is actually the FullName or ShortName instead. + + This is not meant to retrieve the data set info if no name at all is passed. + + """ + + query_args = tuple(self.db_conn.escape_string(x) for x in ( + (self.type + "Freeze"), + str(webqtlConfig.PUBLICTHRESH), + self.name, + self.name, + self.name)) + print("query_args are:", query_args) + + query = ''' + SELECT + Id, Name, FullName, ShortName + FROM + %s + WHERE + public > %s AND + (Name = "%s" OR FullName = "%s" OR ShortName = "%s") + ''' % (query_args) + + self.cursor.execute(query) + self.id, self.name, self.fullname, self.shortname = self.cursor.fetchone() + + + #def genHTML(self, Class='c0dd'): + # return HT.Href(text = HT.Span('%s Database' % self.fullname, Class= "fwb " + Class), + # url= webqtlConfig.INFOPAGEHREF % self.name,target="_blank") + +class PhenotypeDataSet(DataSet): + DS_NAME_MAP['Publish'] = 'PhenotypeDataSet' + + def setup(self): + # Fields in the database table + self.search_fields = ['Phenotype.Post_publication_description', + 'Phenotype.Pre_publication_description', + 'Phenotype.Pre_publication_abbreviation', + 'Phenotype.Post_publication_abbreviation', + 'Phenotype.Lab_code', + 'Publication.PubMed_ID', + 'Publication.Abstract', + 'Publication.Title', + 'Publication.Authors', + 'PublishXRef.Id'] + + # Figure out what display_fields is + self.display_fields = ['name', + 'pubmed_id', + 'pre_publication_description', + 'post_publication_description', + 'original_description', + 'pre_publication_abbreviation', + 'post_publication_abbreviation', + 'lab_code', + 'submitter', 'owner', + 'authorized_users', + 'authors', 'title', + 'abstract', 'journal', + 'volume', 'pages', + 'month', 'year', + 'sequence', 'units', 'comments'] + + # Fields displayed in the search results table header + self.header_fields = ['', + 'ID', + 'Description', + 'Authors', + 'Year', + 'Max LRS', + 'Max LRS Location'] + + self.type = 'Publish' + + self.query = ''' SELECT InbredSet.Name, InbredSet.Id FROM @@ -94,69 +179,336 @@ class DataSet(object): WHERE PublishFreeze.InbredSetId = InbredSet.Id AND PublishFreeze.Name = "%s" - ''' % self.name - elif self.type == 'Geno': - query = ''' - SELECT - InbredSet.Name, InbredSet.Id - FROM - InbredSet, GenoFreeze - WHERE - GenoFreeze.InbredSetId = InbredSet.Id AND - GenoFreeze.Name = "%s" - ''' % self.name - elif self.type == 'ProbeSet': - query = ''' - SELECT - InbredSet.Name, InbredSet.Id - FROM - InbredSet, ProbeSetFreeze, ProbeFreeze - WHERE - ProbeFreeze.InbredSetId = InbredSet.Id AND - ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND - ProbeSetFreeze.Name = "%s" - ''' % self.name - else: - return "" - self.cursor.execute(query) - group, RIID = self.cursor.fetchone() - if group == 'BXD300': - group = "BXD" - self.group = group - self.group_id = RIID - return group + ''' % self.db_conn.escape_string(self.name) + + def check_confidentiality(self): + # (Urgently?) Need to write this + pass + + def get_trait_info(self, trait_list, species = ''): + for this_trait in trait_list: + if not this_trait.haveinfo: + this_trait.retrieveInfo(QTL=1) + + description = this_trait.post_publication_description + if this_trait.confidential: + if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=this_trait.authorized_users): + description = this_trait.pre_publication_description + this_trait.description_display = description + + if not this_trait.year.isdigit(): + this_trait.pubmed_text = "N/A" + + if this_trait.pubmed_id: + this_trait.pubmed_link = webqtlConfig.PUBMEDLINK_URL % this_trait.pubmed_id + + #LRS and its location + this_trait.LRS_score_repr = "N/A" + this_trait.LRS_score_value = 0 + this_trait.LRS_location_repr = "N/A" + this_trait.LRS_location_value = 1000000 + + if this_trait.lrs: + self.cursor.execute(""" + select Geno.Chr, Geno.Mb from Geno, Species + where Species.Name = '%s' and + Geno.Name = '%s' and + Geno.SpeciesId = Species.Id + """ % (species, this_trait.locus)) + result = self.cursor.fetchone() + if result: + if result[0] and result[1]: + LRS_Chr = result[0] + LRS_Mb = result[1] - def retrieveName(self): - assert self.id == 0 and self.cursor + #XZ: LRS_location_value is used for sorting + try: + LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb) + except: + if LRS_Chr.upper() == 'X': + LRS_location_value = 20*1000 + float(LRS_Mb) + else: + LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb) + + this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs + this_trait.LRS_score_value = LRS_score_value = this_trait.lrs + this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + +class GenotypeDataSet(DataSet): + DS_NAME_MAP['Geno'] = 'GenotypeDataSet' + + def setup(self): + # Fields in the database table + self.search_fields = ['Name', + 'Chr'] + + # Find out what display_fields is + self.display_fields = ['name', + 'chr', + 'mb', + 'source2', + 'sequence'] + + # Fields displayed in the search results table header + self.header_fields = ['', + 'ID', + 'Location'] + + # Todo: Obsolete or rename this field + self.type = 'Geno' + query = ''' SELECT - Id, Name, FullName, ShortName + InbredSet.Name, InbredSet.Id FROM - %sFreeze + InbredSet, GenoFreeze WHERE - public > %d AND - (Name = "%s" OR FullName = "%s" OR ShortName = "%s") - '''% (self.type, webqtlConfig.PUBLICTHRESH, self.name, self.name, self.name) - try: + GenoFreeze.InbredSetId = InbredSet.Id AND + GenoFreeze.Name = "%s" + ''' % self.db_conn.escape_string(self.name) + + def check_confidentiality(self): + return geno_mrna_confidentiality(self) + + def get_trait_info(self, trait_list): + for this_trait in trait_list: + if not this_trait.haveinfo: + this_trait.retrieveInfo() + + #XZ: trait_location_value is used for sorting + trait_location_repr = 'N/A' + trait_location_value = 1000000 + + if this_trait.chr and this_trait.mb: + try: + trait_location_value = int(this_trait.chr)*1000 + this_trait.mb + except: + if this_trait.chr.upper() == 'X': + trait_location_value = 20*1000 + this_trait.mb + else: + trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb + + this_trait.location_repr = 'Chr%s: %.4f' % (this_trait.chr, float(this_trait.mb) ) + this_trait.location_value = trait_location_value + + +class MrnaAssayDataSet(DataSet): + ''' + An mRNA Assay is a quantitative assessment (assay) associated with an mRNA trait + + This used to be called ProbeSet, but that term only refers specifically to the Affymetrix + platform and is far too specific. + + ''' + DS_NAME_MAP['ProbeSet'] = 'MrnaAssayDataSet' + + def setup(self): + # Fields in the database table + self.search_fields = ['Name', + 'Description', + 'Probe_Target_Description', + 'Symbol', + 'Alias', + 'GenbankId', + 'UniGeneId', + 'RefSeq_TranscriptId'] + + # Find out what display_fields is + self.display_fields = ['name', 'symbol', + 'description', 'probe_target_description', + 'chr', 'mb', + 'alias', 'geneid', + 'genbankid', 'unigeneid', + 'omim', 'refseq_transcriptid', + 'blatseq', 'targetseq', + 'chipid', 'comments', + 'strand_probe', 'strand_gene', + 'probe_set_target_region', + 'probe_set_specificity', + 'probe_set_blat_score', + 'probe_set_blat_mb_start', + 'probe_set_blat_mb_end', + 'probe_set_strand', + 'probe_set_note_by_rw', + 'flag'] + + # Fields displayed in the search results table header + self.header_fields = ['', + 'ID', + 'Symbol', + 'Description', + 'Location', + 'Mean Expr', + 'Max LRS', + 'Max LRS Location'] + + # Todo: Obsolete or rename this field + self.type = 'ProbeSet' + + self.query = ''' + SELECT + InbredSet.Name, InbredSet.Id + FROM + InbredSet, ProbeSetFreeze, ProbeFreeze + WHERE + ProbeFreeze.InbredSetId = InbredSet.Id AND + ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND + ProbeSetFreeze.Name = "%s" + ''' % self.db_conn.escape_string(self.name) + + + def check_confidentiality(self): + return geno_mrna_confidentiality(self) + + def get_trait_info(self, trait_list=None, species=''): + + # Note: setting trait_list to [] is probably not a great idea. + if not trait_list: + trait_list = [] + + for this_trait in trait_list: + + if not this_trait.haveinfo: + this_trait.retrieveInfo(QTL=1) + + if this_trait.symbol: + pass + else: + this_trait.symbol = "N/A" + + #XZ, 12/08/2008: description + #XZ, 06/05/2009: Rob asked to add probe target description + description_string = str(this_trait.description).strip() + target_string = str(this_trait.probe_target_description).strip() + + description_display = '' + + if len(description_string) > 1 and description_string != 'None': + description_display = description_string + else: + description_display = this_trait.symbol + + if len(description_display) > 1 and description_display != 'N/A' and len(target_string) > 1 and target_string != 'None': + description_display = description_display + '; ' + target_string.strip() + + # Save it for the jinja2 tablet + this_trait.description_display = description_display + + #XZ: trait_location_value is used for sorting + trait_location_repr = 'N/A' + trait_location_value = 1000000 + + if this_trait.chr and this_trait.mb: + try: + trait_location_value = int(this_trait.chr)*1000 + this_trait.mb + except: + if this_trait.chr.upper() == 'X': + trait_location_value = 20*1000 + this_trait.mb + else: + trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb + + this_trait.location_repr = 'Chr %s: %.4f Mb' % (this_trait.chr, float(this_trait.mb) ) + this_trait.location_value = trait_location_value + #this_trait.trait_location_value = trait_location_value + + #XZ, 01/12/08: This SQL query is much faster. + query = ( +"""select ProbeSetXRef.mean from ProbeSetXRef, ProbeSet + where ProbeSetXRef.ProbeSetFreezeId = %s and + ProbeSet.Id = ProbeSetXRef.ProbeSetId and + ProbeSet.Name = '%s' + """ % (self.db_conn.escape_string(str(this_trait.db.id)), + self.db_conn.escape_string(this_trait.name))) + + print("query is:", pf(query)) + self.cursor.execute(query) - self.id,self.name,self.fullname,self.shortname=self.cursor.fetchone() - except: - raise KeyError, `self.name`+' doesn\'t exist.' + result = self.cursor.fetchone() + if result: + if result[0]: + mean = result[0] + else: + mean=0 + else: + mean = 0 - def genHTML(self, Class='c0dd'): - return HT.Href(text = HT.Span('%s Database' % self.fullname, Class= "fwb " + Class), - url= webqtlConfig.INFOPAGEHREF % self.name,target="_blank") + #XZ, 06/05/2009: It is neccessary to turn on nowrap + this_trait.mean = repr = "%2.3f" % mean -class PhenotypeDataSet(DataSet): + #LRS and its location + this_trait.LRS_score_repr = 'N/A' + this_trait.LRS_score_value = 0 + this_trait.LRS_location_repr = 'N/A' + this_trait.LRS_location_value = 1000000 + + #Max LRS and its Locus location + if this_trait.lrs and this_trait.locus: + self.cursor.execute(""" + select Geno.Chr, Geno.Mb from Geno, Species + where Species.Name = '%s' and + Geno.Name = '%s' and + Geno.SpeciesId = Species.Id + """ % (species, this_trait.locus)) + result = self.cursor.fetchone() + + if result: + if result[0] and result[1]: + LRS_Chr = result[0] + LRS_Mb = result[1] + + #XZ: LRS_location_value is used for sorting + try: + LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb) + except: + if LRS_Chr.upper() == 'X': + LRS_location_value = 20*1000 + float(LRS_Mb) + else: + LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb) + + this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs + this_trait.LRS_score_value = LRS_score_value = this_trait.lrs + this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + + +class TempDataSet(DataSet): + '''Temporary user-generated data set''' - def __init__(self): - self.searchfield = ['name','post_publication_description','abstract','title','authors'] - self.disfield = ['name','pubmed_id', - 'pre_publication_description', 'post_publication_description', 'original_description', - 'pre_publication_abbreviation', 'post_publication_abbreviation', - 'lab_code', 'submitter', 'owner', 'authorized_users', - 'authors','title','abstract', 'journal','volume','pages','month', - 'year','sequence', 'units', 'comments'] - self.type = 'Publish' \ No newline at end of file + def setup(self): + self.search_fields = ['name', + 'description'] + + self.display_fields = ['name', + 'description'] + + self.header_fields = ['Name', + 'Description'] + + self.type = 'Temp' + + # Need to double check later how these are used + self.id = 1 + self.fullname = 'Temporary Storage' + self.shortname = 'Temp' + + +def geno_mrna_confidentiality(ob): + dataset_table = ob.type + "Freeze" + print("dataset_table [%s]: %s" % (type(dataset_table), dataset_table)) + + query = '''SELECT Id, Name, FullName, confidentiality, + AuthorisedUsers FROM %s WHERE Name = %%s''' % (dataset_table) + + ob.cursor.execute(query, ob.name) + + (dataset_id, + name, + full_name, + confidential, + authorized_users) = ob.cursor.fetchall()[0] + + if confidential: + # Allow confidential data later + NoConfindetialDataForYouTodaySorry + \ No newline at end of file diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index 51d36ab2..29087721 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -6,7 +6,7 @@ from htmlgen import HTMLgen2 as HT import webqtlConfig from webqtlCaseData import webqtlCaseData -from webqtlDataset import webqtlDataset +from data_set import create_dataset from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil @@ -20,9 +20,10 @@ class webqtlTrait: """ - def __init__(self, cursor = None, **kw): + def __init__(self, db_conn, **kw): print("in webqtlTrait") - self.cursor = cursor + self.db_conn = db_conn + self.cursor = self.db_conn.cursor() self.db = None # database object self.name = '' # Trait ID, ProbeSet ID, Published ID, etc. self.cellid = '' @@ -50,7 +51,7 @@ class webqtlTrait: if self.db and isinstance(self.db, basestring): assert self.cursor, "Don't have a cursor" - self.db = webqtlDataset(self.db, self.cursor) + self.db = create_dataset(self.db_conn, self.db) #if self.db == None, not from a database print("self.db is:", self.db, type(self.db)) @@ -396,8 +397,8 @@ class webqtlTrait: #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. elif self.db.type == 'ProbeSet': - disfieldString = string.join(self.db.disfield,',ProbeSet.') - disfieldString = 'ProbeSet.' + disfieldString + display_fields_string = ',ProbeSet.'.join(self.db.display_fields) + display_fields_string = 'ProbeSet.' + display_fields_string query = """ SELECT %s FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef @@ -406,12 +407,12 @@ class webqtlTrait: ProbeSetXRef.ProbeSetId = ProbeSet.Id AND ProbeSetFreeze.Name = '%s' AND ProbeSet.Name = '%s' - """ % (disfieldString, self.db.name, self.name) + """ % (display_fields_string, self.db.name, self.name) #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name # to avoid the problem of same marker name from different species. elif self.db.type == 'Geno': - disfieldString = string.join(self.db.disfield,',Geno.') - disfieldString = 'Geno.' + disfieldString + display_fields_string = string.join(self.db.display_fields,',Geno.') + display_fields_string = 'Geno.' + display_fields_string query = """ SELECT %s FROM Geno, GenoFreeze, GenoXRef @@ -420,10 +421,10 @@ class webqtlTrait: GenoXRef.GenoId = Geno.Id AND GenoFreeze.Name = '%s' AND Geno.Name = '%s' - """ % (disfieldString, self.db.name, self.name) + """ % (display_fields_string, self.db.name, self.name) else: #Temp type query = 'SELECT %s FROM %s WHERE Name = "%s"' % \ - (string.join(self.db.disfield,','), self.db.type, self.name) + (string.join(self.db.display_fields,','), self.db.type, self.name) self.cursor.execute(query) @@ -432,7 +433,7 @@ class webqtlTrait: self.haveinfo = 1 #XZ: assign SQL query result to trait attributes. - for i, field in enumerate(self.db.disfield): + for i, field in enumerate(self.db.display_fields): setattr(self, field, traitInfo[i]) if self.db.type == 'Publish': diff --git a/wqflask/dbFunction/webqtlDatabaseFunction.py b/wqflask/dbFunction/webqtlDatabaseFunction.py index 7e33da3f..8f923b8a 100755 --- a/wqflask/dbFunction/webqtlDatabaseFunction.py +++ b/wqflask/dbFunction/webqtlDatabaseFunction.py @@ -19,14 +19,7 @@ # # # This module is used by GeneNetwork project (www.genenetwork.org) -# -# Created by GeneNetwork Core Team 2010/08/10 -# -# Last updated by Xiaodong Zhou 2011/Jan/20 -#webqtlDatabaseFunction.py -# -#This file consists of various database related functions; the names are generally self-explanatory. import MySQLdb import string @@ -206,21 +199,21 @@ def getTissueCountByTissueProbeSetFreezeId(cursor=None, TissueProbeSetFreezeId=N ########################################################################### # input: cursor, TissueProbeSetFreezeId (int) -# output: DatasetName(string),DatasetFullName(string) -# function: retrieve DatasetName, DatasetFullName based on TissueProbeSetFreezeId +# output: DataSetName(string),DataSetFullName(string) +# function: retrieve DataSetName, DataSetFullName based on TissueProbeSetFreezeId ########################################################################### -def getDatasetNamesByTissueProbeSetFreezeId(cursor=None, TissueProbeSetFreezeId=None): +def getDataSetNamesByTissueProbeSetFreezeId(cursor=None, TissueProbeSetFreezeId=None): query ="select Name, FullName from TissueProbeSetFreeze where Id=%s" % TissueProbeSetFreezeId try: cursor.execute(query) result = cursor.fetchone() - DatasetName = result[0] - DatasetFullName =result[1] + DataSetName = result[0] + DataSetFullName =result[1] except: - DatasetName =None - DatasetFullName =None + DataSetName =None + DataSetFullName =None - return DatasetName, DatasetFullName + return DataSetName, DataSetFullName ########################################################################### # input: cursor, geneIdLst (list) diff --git a/wqflask/wqflask/correlation/CorrelationPage.py b/wqflask/wqflask/correlation/CorrelationPage.py index e48ea412..8af30d1e 100644 --- a/wqflask/wqflask/correlation/CorrelationPage.py +++ b/wqflask/wqflask/correlation/CorrelationPage.py @@ -47,7 +47,7 @@ from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell from base.webqtlTrait import webqtlTrait -from base.webqtlDataset import webqtlDataset +from base.data_set import create_dataset from base.templatePage import templatePage from utility import webqtlUtil from dbFunction import webqtlDatabaseFunction @@ -310,7 +310,7 @@ class CorrelationPage(templatePage): #try: #print("target_db_name is:", target_db_name) - self.db = webqtlDataset(self.target_db_name, self.cursor) + self.db = create_dataset(self.db_conn, self.target_db_name) #except: # detail = ["The database you just requested has not been established yet."] # self.error(detail) diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index e2bafb3a..73a72e00 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -147,7 +147,7 @@ class GenotypeSearch(DoSearch): """WHERE %s and Geno.Id = GenoXRef.GenoId and GenoXRef.GenoFreezeId = GenoFreeze.Id and - GenoFreeze.Id = %s"""% ( + GenoFreeze.Id = %s""" % ( self.get_where_clause(), self.escape(self.dataset.id))) @@ -257,7 +257,7 @@ class GoSearch(ProbeSetSearch): statements = ("""%s.symbol=GOgene_product.symbol and GOassociation.gene_product_id=GOgene_product.id and GOterm.id=GOassociation.term_id""" % ( - self.db_conn.escape_string(self.dataset.type))) + self.escape(self.dataset.type))) where_clause = " %s = '%s' and %s " % (field, go_id, statements) @@ -317,14 +317,14 @@ class CisLrsSearch(ProbeSetSearch): Geno.SpeciesId = %s and %s.Chr = Geno.Chr and ABS(%s.Mb-Geno.Mb) < %s """ % ( - self.dataset.type, + self.escape(self.dataset.type), min(lower_limit, upper_limit), - self.dataset.type, + self.escape(self.dataset.type), max(lower_limit, upper_limit), - self.dataset.type, + self.escape(self.dataset.type), self.species_id, - self.dataset.type, - self.dataset.type, + self.escape(self.dataset.type), + self.escape(self.dataset.type), min_threshold ) else: @@ -437,7 +437,7 @@ if __name__ == "__main__": from base import webqtlConfig - from base.webqtlDataset import webqtlDataset + from base.data_set import create_dataset from base.templatePage import templatePage from utility import webqtlUtil from dbFunction import webqtlDatabaseFunction @@ -449,13 +449,13 @@ if __name__ == "__main__": cursor = db_conn.cursor() dataset_name = "HC_M2_0606_P" - dataset = webqtlDataset(dataset_name, cursor) + dataset = create_dataset(db_conn, dataset_name) #results = ProbeSetSearch("salt", dataset, cursor, db_conn).run() #results = RifSearch("diabetes", dataset, cursor, db_conn).run() #results = WikiSearch("nicotine", dataset, cursor, db_conn).run() - results = TransLrsSearch(['25','99','10'], dataset, cursor, db_conn).run() - #results = TransLrsSearch(['9', '999', '10'], dataset, cursor, db_conn).run() + results = CisLrsSearch(['25','99','10'], dataset, cursor, db_conn).run() + #results = TransLrsSearch(['25', '999', '10'], dataset, cursor, db_conn).run() #results = PhenotypeSearch("brain", dataset, cursor, db_conn).run() #results = GenotypeSearch("rs13475699", dataset, cursor, db_conn).run() #results = GoSearch("0045202", dataset, cursor, db_conn).run() diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 05f062fc..b50e45d5 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -10,7 +10,7 @@ from flask import render_template # # ################################################### -import string +#import string import os import cPickle import re @@ -29,7 +29,7 @@ from htmlgen import HTMLgen2 as HT from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell -from base.webqtlDataset import webqtlDataset +from base.data_set import create_dataset from base.webqtlTrait import webqtlTrait from base.templatePage import templatePage from wqflask import parser @@ -43,14 +43,13 @@ from utility import formatting class SearchResultPage(templatePage): + #maxReturn = 3000 - maxReturn = 3000 - nkeywords = 0 def __init__(self, fd): print("initing SearchResultPage") - import logging_tree - logging_tree.printout() + #import logging_tree + #logging_tree.printout() self.fd = fd templatePage.__init__(self, fd) assert self.openMysql(), "Couldn't open MySQL" @@ -59,127 +58,40 @@ class SearchResultPage(templatePage): self.dataset = fd['dataset'] # change back to self.dataset - if not self.dataset or self.dataset == 'spacer': - #Error, No dataset selected - heading = "Search Result" - detail = ['''No dataset was selected for this search, please - go back and SELECT at least one dataset.'''] - self.error(heading=heading,detail=detail,error="No dataset Selected") - return + #if not self.dataset or self.dataset == 'spacer': + # #Error, No dataset selected + # heading = "Search Result" + # detail = ['''No dataset was selected for this search, please + # go back and SELECT at least one dataset.'''] + # self.error(heading=heading,detail=detail,error="No dataset Selected") + # return ########################################### # Names and IDs of RISet / F2 set ########################################### + + # All Phenotypes is a special case we'll deal with later if self.dataset == "All Phenotypes": self.cursor.execute(""" select PublishFreeze.Name, InbredSet.Name, InbredSet.Id from PublishFreeze, InbredSet where PublishFreeze.Name not like 'BXD300%' and InbredSet.Id = PublishFreeze.InbredSetId""") results = self.cursor.fetchall() - self.dataset = map(lambda x: webqtlDataset(x[0], self.cursor), results) + self.dataset = map(lambda x: DataSet(x[0], self.cursor), results) self.dataset_groups = map(lambda x: x[1], results) self.dataset_group_ids = map(lambda x: x[2], results) - self.single_group = False else: print("self.dataset is:", pf(self.dataset)) - self.dataset = webqtlDataset(self.dataset, self.cursor) + self.dataset = create_dataset(self.db_conn, self.dataset) print("self.dataset is now:", pf(self.dataset)) - if self.dataset.type in ("Geno", "ProbeSet"): - db_type = self.dataset.type + "Freeze" - print("db_type [%s]: %s" % (type(db_type), db_type)) - - query = '''SELECT Id, Name, FullName, confidentiality, - AuthorisedUsers FROM %s WHERE Name = %%s''' % (db_type) - - self.cursor.execute(query, self.dataset.name) - - (indId, - indName, - indFullName, - confidential, - AuthorisedUsers) = self.cursor.fetchall()[0] - - if confidential: - # Allow confidential data later - NoConfindetialDataForYouTodaySorry - #access_to_confidential_dataset = 0 - # - ##for the dataset that confidentiality is 1 - ##1. 'admin' and 'root' can see all of the dataset - ##2. 'user' can see the dataset that AuthorisedUsers contains his id(stored in the Id field of User table) - #if webqtlConfig.USERDICT[self.privilege] > webqtlConfig.USERDICT['user']: - # access_to_confidential_dataset = 1 - #else: - # AuthorisedUsersList=AuthorisedUsers.split(',') - # if AuthorisedUsersList.__contains__(self.userName): - # access_to_confidential_dataset = 1 - # - #if not access_to_confidential_dataset: - # Some error - - #else: - # heading = "Search Result" - # detail = ['''The dataset has not been established yet, please - # go back and SELECT at least one dataset.'''] - # self.error(heading=heading,detail=detail,error="No dataset Selected") - # return - - self.dataset.get_group() - self.single_group = True - #XZ, August 24,2010: Since self.single_group = True, it's safe to assign one species Id. - self.species_id = webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, - self.dataset.group) - - #self.db_type = self.dataset.type - if self.dataset.type == "Publish": - self.search_fields = ['Phenotype.Post_publication_description', - 'Phenotype.Pre_publication_description', - 'Phenotype.Pre_publication_abbreviation', - 'Phenotype.Post_publication_abbreviation', - 'Phenotype.Lab_code', - 'Publication.PubMed_ID', - 'Publication.Abstract', - 'Publication.Title', - 'Publication.Authors', - 'PublishXRef.Id'] - self.header_fields = ['', - 'ID', - 'Description', - 'Authors', - 'Year', - 'Max LRS', - 'Max LRS Location'] - - elif self.dataset.type == "ProbeSet": - self.search_fields = ['Name', - 'Description', - 'Probe_Target_Description', - 'Symbol', - 'Alias', - 'GenbankId', - 'UniGeneId', - 'RefSeq_TranscriptId'] - self.header_fields = ['', - 'ID', - 'Symbol', - 'Description', - 'Location', - 'Mean Expr', - 'Max LRS', - 'Max LRS Location'] - elif self.dataset.type == "Geno": - self.search_fields = ['Name', - 'Chr'] - self.header_fields = ['', - 'ID', - 'Location'] - + self.search() self.gen_search_result() def gen_search_result(self): - """Get the info displayed in the search result table from the set of results computed in + """ + Get the info displayed in the search result table from the set of results computed in the "search" function """ @@ -191,26 +103,19 @@ class SearchResultPage(templatePage): if not result: continue - seq = 1 group = self.dataset.group - species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=group) #### Excel file needs to be generated #### print("foo locals are:", locals()) trait_id = result[0] - this_trait = webqtlTrait(db=self.dataset, name=trait_id, cursor=self.cursor) + this_trait = webqtlTrait(self.db_conn, db=self.dataset, name=trait_id) this_trait.retrieveInfo(QTL=True) print("this_trait is:", pf(this_trait)) self.trait_list.append(this_trait) - - if self.dataset.type == "ProbeSet": - self.getTraitInfoForProbeSet(trait_list=self.trait_list, species=species) - elif self.dataset.type == "Publish": - self.getTraitInfoForPublish(trait_list=self.trait_list, species=species) - elif self.dataset.type == "Geno": - self.getTraitInfoForGeno(trait_list=self.trait_list) + + self.dataset.get_trait_info(self.trait_list, species) def search(self): @@ -222,7 +127,7 @@ class SearchResultPage(templatePage): print("[kodak] item is:", pf(a_search)) search_term = a_search['search_term'] if a_search['key']: - search_type = string.upper(a_search['key']) + search_type = a_search['key'].upper() else: # We fall back to the dataset type as the key to get the right object search_type = self.dataset.type @@ -258,187 +163,3 @@ class SearchResultPage(templatePage): keyword = string.replace(keyword,"?",".") wildcardkeyword[i] = keyword#'[[:<:]]'+ keyword+'[[:>:]]' return wildcardkeyword - - - def getTraitInfoForGeno(self, trait_list): - for this_trait in trait_list: - if not this_trait.haveinfo: - this_trait.retrieveInfo() - - #XZ: trait_location_value is used for sorting - trait_location_repr = 'N/A' - trait_location_value = 1000000 - - if this_trait.chr and this_trait.mb: - try: - trait_location_value = int(this_trait.chr)*1000 + this_trait.mb - except: - if this_trait.chr.upper() == 'X': - trait_location_value = 20*1000 + this_trait.mb - else: - trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb - - this_trait.location_repr = 'Chr%s: %.4f' % (this_trait.chr, float(this_trait.mb) ) - this_trait.location_value = trait_location_value - - - def getTraitInfoForPublish(self, trait_list, species = ''): - for this_trait in trait_list: - if not this_trait.haveinfo: - this_trait.retrieveInfo(QTL=1) - - description = this_trait.post_publication_description - if this_trait.confidential: - if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=this_trait.authorized_users): - description = this_trait.pre_publication_description - this_trait.description_display = description - - if not this_trait.year.isdigit(): - this_trait.pubmed_text = "N/A" - - if this_trait.pubmed_id: - this_trait.pubmed_link = webqtlConfig.PUBMEDLINK_URL % this_trait.pubmed_id - - #LRS and its location - this_trait.LRS_score_repr = "N/A" - this_trait.LRS_score_value = 0 - this_trait.LRS_location_repr = "N/A" - this_trait.LRS_location_value = 1000000 - - if this_trait.lrs: - self.cursor.execute(""" - select Geno.Chr, Geno.Mb from Geno, Species - where Species.Name = '%s' and - Geno.Name = '%s' and - Geno.SpeciesId = Species.Id - """ % (species, this_trait.locus)) - result = self.cursor.fetchone() - - if result: - if result[0] and result[1]: - LRS_Chr = result[0] - LRS_Mb = result[1] - - #XZ: LRS_location_value is used for sorting - try: - LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb) - except: - if LRS_Chr.upper() == 'X': - LRS_location_value = 20*1000 + float(LRS_Mb) - else: - LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb) - - this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs - this_trait.LRS_score_value = LRS_score_value = this_trait.lrs - this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) - - - def getTraitInfoForProbeSet(self, trait_list=None, species=''): - - # Note: setting trait_list to [] is probably not a great idea. - if not trait_list: - trait_list = [] - - for this_trait in trait_list: - - if not this_trait.haveinfo: - this_trait.retrieveInfo(QTL=1) - - if this_trait.symbol: - pass - else: - this_trait.symbol = "N/A" - - #XZ, 12/08/2008: description - #XZ, 06/05/2009: Rob asked to add probe target description - description_string = str(this_trait.description).strip() - target_string = str(this_trait.probe_target_description).strip() - - description_display = '' - - if len(description_string) > 1 and description_string != 'None': - description_display = description_string - else: - description_display = this_trait.symbol - - if len(description_display) > 1 and description_display != 'N/A' and len(target_string) > 1 and target_string != 'None': - description_display = description_display + '; ' + target_string.strip() - - # Save it for the jinja2 tablet - this_trait.description_display = description_display - - #XZ: trait_location_value is used for sorting - trait_location_repr = 'N/A' - trait_location_value = 1000000 - - if this_trait.chr and this_trait.mb: - try: - trait_location_value = int(this_trait.chr)*1000 + this_trait.mb - except: - if this_trait.chr.upper() == 'X': - trait_location_value = 20*1000 + this_trait.mb - else: - trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb - - this_trait.location_repr = 'Chr %s: %.4f Mb' % (this_trait.chr, float(this_trait.mb) ) - this_trait.location_value = trait_location_value - #this_trait.trait_location_value = trait_location_value - - #XZ, 01/12/08: This SQL query is much faster. - query = ( -"""select ProbeSetXRef.mean from ProbeSetXRef, ProbeSet - where ProbeSetXRef.ProbeSetFreezeId = %s and - ProbeSet.Id = ProbeSetXRef.ProbeSetId and - ProbeSet.Name = '%s' - """ % (self.db_conn.escape_string(str(this_trait.db.id)), - self.db_conn.escape_string(this_trait.name))) - - print("query is:", pf(query)) - - self.cursor.execute(query) - result = self.cursor.fetchone() - - if result: - if result[0]: - mean = result[0] - else: - mean=0 - else: - mean = 0 - - #XZ, 06/05/2009: It is neccessary to turn on nowrap - this_trait.mean = repr = "%2.3f" % mean - - #LRS and its location - this_trait.LRS_score_repr = 'N/A' - this_trait.LRS_score_value = 0 - this_trait.LRS_location_repr = 'N/A' - this_trait.LRS_location_value = 1000000 - - #Max LRS and its Locus location - if this_trait.lrs and this_trait.locus: - self.cursor.execute(""" - select Geno.Chr, Geno.Mb from Geno, Species - where Species.Name = '%s' and - Geno.Name = '%s' and - Geno.SpeciesId = Species.Id - """ % (species, this_trait.locus)) - result = self.cursor.fetchone() - - if result: - if result[0] and result[1]: - LRS_Chr = result[0] - LRS_Mb = result[1] - - #XZ: LRS_location_value is used for sorting - try: - LRS_location_value = int(LRS_Chr)*1000 + float(LRS_Mb) - except: - if LRS_Chr.upper() == 'X': - LRS_location_value = 20*1000 + float(LRS_Mb) - else: - LRS_location_value = ord(str(LRS_chr).upper()[0])*1000 + float(LRS_Mb) - - this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs - this_trait.LRS_score_value = LRS_score_value = this_trait.lrs - this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) -- cgit v1.2.3 From d1f2863c15f62ae37833fa1311870d7b1aab3355 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Wed, 28 Nov 2012 14:30:04 -0600 Subject: Made some small changes to get code working for genotype searches --- wqflask/base/data_set.py | 2 +- wqflask/wqflask/parser.py | 2 ++ wqflask/wqflask/search_results.py | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 9e3e6d81..d9d3a52b 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -258,7 +258,7 @@ class GenotypeDataSet(DataSet): # Todo: Obsolete or rename this field self.type = 'Geno' - query = ''' + self.query = ''' SELECT InbredSet.Name, InbredSet.Id FROM diff --git a/wqflask/wqflask/parser.py b/wqflask/wqflask/parser.py index dc33fc52..7711942a 100644 --- a/wqflask/wqflask/parser.py +++ b/wqflask/wqflask/parser.py @@ -15,6 +15,8 @@ Both square brackets and parentheses can be used interchangeably. Both can also encapsulate a single value; "cisLRS=[9 999 10)" would be acceptable.] +NEED TO DEAL WITH WILDCARD CHARACTER '*' + """ from __future__ import print_function, division diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index b50e45d5..96350f22 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -55,6 +55,7 @@ class SearchResultPage(templatePage): assert self.openMysql(), "Couldn't open MySQL" print("fd is:", pf(fd)) + print("fd.dict is:", pf(fd['dataset'])) self.dataset = fd['dataset'] # change back to self.dataset @@ -93,7 +94,7 @@ class SearchResultPage(templatePage): """ Get the info displayed in the search result table from the set of results computed in the "search" function - + """ self.trait_list = [] # result_set represents the results for each search term; a search of @@ -114,7 +115,7 @@ class SearchResultPage(templatePage): this_trait.retrieveInfo(QTL=True) print("this_trait is:", pf(this_trait)) self.trait_list.append(this_trait) - + self.dataset.get_trait_info(self.trait_list, species) -- cgit v1.2.3 From 94300b4488aa334ced34981981ad5d0ecdec01d6 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 29 Nov 2012 18:44:01 -0600 Subject: Changed a number of variables (riset to group, db to dataset) Put most of the code for cisLRS and transLRS searches into the class CisTransLrsSearch (might change this name to something else later) Simplified escape code for searches in do_search.py Got search_results working again after some changes --- wqflask/base/data_set.py | 5 +- wqflask/base/webqtlFormData.py | 26 +-- wqflask/base/webqtlTrait.py | 146 ++++++------- wqflask/dbFunction/webqtlDatabaseFunction.py | 4 +- wqflask/wqflask/do_search.py | 237 +++++++++------------- wqflask/wqflask/search_results.py | 18 +- wqflask/wqflask/show_trait/show_trait.py | 83 ++++---- wqflask/wqflask/templates/index_page.html | 4 +- wqflask/wqflask/templates/search_result_page.html | 2 +- wqflask/wqflask/views.py | 3 + 10 files changed, 241 insertions(+), 287 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index d9d3a52b..633f7545 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -392,8 +392,9 @@ class MrnaAssayDataSet(DataSet): if len(description_display) > 1 and description_display != 'N/A' and len(target_string) > 1 and target_string != 'None': description_display = description_display + '; ' + target_string.strip() - # Save it for the jinja2 tablet + # Save it for the jinja2 template this_trait.description_display = description_display + #print(" xxxxdd [%s]: %s" % (type(this_trait.description_display), description_display)) #XZ: trait_location_value is used for sorting trait_location_repr = 'N/A' @@ -418,7 +419,7 @@ class MrnaAssayDataSet(DataSet): where ProbeSetXRef.ProbeSetFreezeId = %s and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSet.Name = '%s' - """ % (self.db_conn.escape_string(str(this_trait.db.id)), + """ % (self.db_conn.escape_string(str(this_trait.dataset.id)), self.db_conn.escape_string(this_trait.name))) print("query is:", pf(query)) diff --git a/wqflask/base/webqtlFormData.py b/wqflask/base/webqtlFormData.py index ff1db0e8..a3537c87 100755 --- a/wqflask/base/webqtlFormData.py +++ b/wqflask/base/webqtlFormData.py @@ -47,7 +47,7 @@ from utility import webqtlUtil class webqtlFormData(object): 'Represents data from a WebQTL form page, needed to generate the next page' - attrs = ('formID','RISet','genotype','samplelist','allsamplelist', 'display_variance' + attrs = ('formID','group','genotype','samplelist','allsamplelist', 'display_variance' 'suggestive','significance','submitID','identification', 'enablevariance', 'nperm','nboot','email','incparentsf1','genotype_1','genotype_2','traitInfo') @@ -104,11 +104,11 @@ class webqtlFormData(object): self.ppolar = None self.mpolar = None - print("[yellow] self.RISet is:", self.RISet) - if self.RISet: + print("[yellow] self.group is:", self.group) + if self.group: #try: # # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py; - _f1, _f12, self.mpolar, self.ppolar = webqtlUtil.ParInfo[self.RISet] + _f1, _f12, self.mpolar, self.ppolar = webqtlUtil.ParInfo[self.group] #except: # f1 = f12 = self.mpolar = self.ppolar = None @@ -129,8 +129,8 @@ class webqtlFormData(object): #self.readGenotype() #self.readData() - if self.RISet == 'BXD300': - self.RISet = 'BXD' + if self.group == 'BXD300': + self.group = 'BXD' def __getitem__(self, key): @@ -153,17 +153,17 @@ class webqtlFormData(object): def readGenotype(self): '''read genotype from .geno file''' - if self.RISet == 'BXD300': - self.RISet = 'BXD' + if self.group == 'BXD300': + self.group = 'BXD' - assert self.RISet, "self.RISet needs to be set" + assert self.group, "self.group needs to be set" #genotype_1 is Dataset Object without parents and f1 #genotype_2 is Dataset Object with parents and f1 (not for intercross) self.genotype_1 = reaper.Dataset() - full_filename = os.path.join(webqtlConfig.GENODIR, self.RISet + '.geno') + full_filename = os.path.join(webqtlConfig.GENODIR, self.group + '.geno') # reaper barfs on unicode filenames, so here we ensure it's a string full_filename = str(full_filename) @@ -173,12 +173,12 @@ class webqtlFormData(object): try: # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py; - _f1, _f12, _mat, _pat = webqtlUtil.ParInfo[self.RISet] + _f1, _f12, _mat, _pat = webqtlUtil.ParInfo[self.group] except KeyError: _f1 = _f12 = _mat = _pat = None self.genotype_2 = self.genotype_1 - if self.genotype_1.type == "riset" and _mat and _pat: + if self.genotype_1.type == "group" and _mat and _pat: self.genotype_2 = self.genotype_1.add(Mat=_mat, Pat=_pat) #, F1=_f1) #determine default genotype object @@ -333,7 +333,7 @@ class webqtlFormData(object): def Sample(self): 'Create some dummy data for testing' - self.RISet = 'BXD' + self.group = 'BXD' self.incparentsf1 = 'on' #self.display = 9.2 #self.significance = 16.1 diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index 29087721..cc0e2321 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -24,11 +24,11 @@ class webqtlTrait: print("in webqtlTrait") self.db_conn = db_conn self.cursor = self.db_conn.cursor() - self.db = None # database object + self.dataset = None # database object self.name = '' # Trait ID, ProbeSet ID, Published ID, etc. self.cellid = '' self.identification = 'un-named trait' - self.riset = '' + self.group = '' self.haveinfo = 0 self.sequence = '' # Blat sequence, available for ProbeSet self.data = {} @@ -41,22 +41,22 @@ class webqtlTrait: elif name == 'fullname': name2 = value.split("::") if len(name2) == 2: - self.db, self.name = name2 + self.dataset, self.name = name2 elif len(name2) == 3: - self.db, self.name, self.cellid = name2 + self.dataset, self.name, self.cellid = name2 else: raise KeyError, repr(value) + ' parameter format error.' else: raise KeyError, repr(name) + ' not a valid parameter for this class.' - if self.db and isinstance(self.db, basestring): + if self.dataset and isinstance(self.dataset, basestring): assert self.cursor, "Don't have a cursor" - self.db = create_dataset(self.db_conn, self.db) + self.dataset = create_dataset(self.db_conn, self.dataset) - #if self.db == None, not from a database - print("self.db is:", self.db, type(self.db)) - if self.db: - if self.db.type == "Temp": + #if self.dataset == None, not from a database + print("self.dataset is:", self.dataset, type(self.dataset)) + if self.dataset: + if self.dataset.type == "Temp": self.cursor.execute(''' SELECT InbredSet.Name @@ -66,9 +66,11 @@ class webqtlTrait: Temp.InbredSetId = InbredSet.Id AND Temp.Name = "%s" ''', self.name) - self.riset = self.cursor.fetchone()[0] + self.group = self.cursor.fetchone()[0] else: - self.riset = self.db.get_group() + self.group = self.dataset.get_group() + + print("trinity, self.group is:", self.group) # # In ProbeSet, there are maybe several annotations match one sequence @@ -82,8 +84,8 @@ class webqtlTrait: # The variable self.sequence should be changed to self.BlatSeq # It also should be changed in other places where it are used. - if self.db: - if self.db.type == 'ProbeSet': + if self.dataset: + if self.dataset.type == 'ProbeSet': print("Doing ProbeSet Query") query = ''' SELECT @@ -95,7 +97,7 @@ class webqtlTrait: ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and ProbeSet.Name = %s and ProbeSetFreeze.Name = %s - ''', (self.name, self.db.name) + ''', (self.name, self.dataset.name) print("query is:", query) self.cursor.execute(*query) self.sequence = self.cursor.fetchone()[0] @@ -104,8 +106,8 @@ class webqtlTrait: def getName(self): str = "" - if self.db and self.name: - str = "%s::%s" % (self.db, self.name) + if self.dataset and self.name: + str = "%s::%s" % (self.dataset, self.name) if self.cellid: str += "::" + self.cellid else: @@ -124,8 +126,8 @@ class webqtlTrait: # def getGivenName(self): str = self.name - if self.db and self.name: - if self.db.type=='Temp': + if self.dataset and self.name: + if self.dataset.type=='Temp': self.cursor.execute('SELECT description FROM Temp WHERE Name=%s', self.name) desc = self.cursor.fetchone()[0] if desc.__contains__('PCA'): @@ -137,16 +139,16 @@ class webqtlTrait: def displayName(self): str = "" - if self.db and self.name: - if self.db.type=='Temp': + if self.dataset and self.name: + if self.dataset.type=='Temp': desc = self.description if desc.__contains__('PCA'): desc = desc[desc.rindex(':')+1:].strip() else: desc = desc[:desc.index('entered')].strip() - str = "%s::%s" % (self.db, desc) + str = "%s::%s" % (self.dataset, desc) else: - str = "%s::%s" % (self.db, self.name) + str = "%s::%s" % (self.dataset, self.name) if self.cellid: str += "::" + self.cellid else: @@ -156,7 +158,7 @@ class webqtlTrait: #def __str__(self): - # #return "%s %s" % (self.getName(), self.riset) + # #return "%s %s" % (self.getName(), self.group) # return self.getName() #__str__ = getName #__repr__ = __str__ @@ -207,7 +209,7 @@ class webqtlTrait: # def getSequence(self): assert self.cursor - if self.db.type == 'ProbeSet': + if self.dataset.type == 'ProbeSet': self.cursor.execute(''' SELECT ProbeSet.BlatSeq @@ -218,7 +220,7 @@ class webqtlTrait: ProbeSetFreeze.Id = ProbeSetXRef.ProbSetFreezeId and ProbeSet.Name = %s ProbeSetFreeze.Name = %s - ''', self.name, self.db.name) + ''', self.name, self.dataset.name) #self.cursor.execute(query) results = self.fetchone() @@ -230,9 +232,9 @@ class webqtlTrait: if samplelist == None: samplelist = [] - assert self.db and self.cursor + assert self.dataset and self.cursor - if self.db.type == 'Temp': + if self.dataset.type == 'Temp': query = ''' SELECT Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id @@ -246,7 +248,7 @@ class webqtlTrait: Strain.Name ''' % self.name #XZ, 03/02/2009: Xiaodong changed Data to PublishData, SE to PublishSE - elif self.db.type == 'Publish': + elif self.dataset.type == 'Publish': query = ''' SELECT Strain.Name, PublishData.value, PublishSE.error, NStrain.count, PublishData.Id @@ -263,7 +265,7 @@ class webqtlTrait: PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id Order BY Strain.Name - ''' % (self.name, self.db.id) + ''' % (self.name, self.dataset.id) #XZ, 03/02/2009: Xiaodong changed Data to ProbeData, SE to ProbeSE elif self.cellid: @@ -287,9 +289,9 @@ class webqtlTrait: ProbeData.StrainId = Strain.Id Order BY Strain.Name - ''' % (self.cellid, self.name, self.db.name) + ''' % (self.cellid, self.name, self.dataset.name) #XZ, 03/02/2009: Xiaodong added this block for ProbeSetData and ProbeSetSE - elif self.db.type == 'ProbeSet': + elif self.dataset.type == 'ProbeSet': #ProbeSet Data query = ''' SELECT @@ -306,7 +308,7 @@ class webqtlTrait: ProbeSetData.StrainId = Strain.Id Order BY Strain.Name - ''' % (self.name, self.db.name) + ''' % (self.name, self.dataset.name) #XZ, 03/02/2009: Xiaodong changeded Data to GenoData, SE to GenoSE else: #Geno Data @@ -326,7 +328,7 @@ class webqtlTrait: GenoData.StrainId = Strain.Id Order BY Strain.Name - ''' % (webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.db.riset), self.name, self.db.name) + ''' % (webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.dataset.group), self.name, self.dataset.name) self.cursor.execute(query) @@ -341,7 +343,7 @@ class webqtlTrait: if not samplelist or (samplelist and name in samplelist): #if value != None: # num_cases = None - # if self.db.type in ('Publish', 'Temp'): + # if self.dataset.type in ('Publish', 'Temp'): # ndata = item[3] name = item[0] self.data[name] = webqtlCaseData(*item) #name, value, variance, num_cases) @@ -352,7 +354,7 @@ class webqtlTrait: # if val != None: # var = item[2] # ndata = None - # if self.db.type in ('Publish', 'Temp'): + # if self.dataset.type in ('Publish', 'Temp'): # ndata = item[3] # self.data[item[0]] = webqtlCaseData(val, var, ndata) # #end for @@ -370,9 +372,9 @@ class webqtlTrait: # return self.__dict__.items() def retrieveInfo(self, QTL = None): - assert self.db and self.cursor - if self.db.type == 'Publish': - #self.db.DisField = ['Name','PubMed_ID','Phenotype','Abbreviation','Authors','Title',\ + assert self.dataset and self.cursor + if self.dataset.type == 'Publish': + #self.dataset.DisField = ['Name','PubMed_ID','Phenotype','Abbreviation','Authors','Title',\ # 'Abstract', 'Journal','Volume','Pages','Month','Year','Sequence',\ # 'Units', 'comments'] query = ''' @@ -393,11 +395,11 @@ class webqtlTrait: Publication.Id = PublishXRef.PublicationId AND PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND PublishFreeze.Id =%s - ''' % (self.name, self.db.id) + ''' % (self.name, self.dataset.id) #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. - elif self.db.type == 'ProbeSet': - display_fields_string = ',ProbeSet.'.join(self.db.display_fields) + elif self.dataset.type == 'ProbeSet': + display_fields_string = ',ProbeSet.'.join(self.dataset.display_fields) display_fields_string = 'ProbeSet.' + display_fields_string query = """ SELECT %s @@ -407,11 +409,11 @@ class webqtlTrait: ProbeSetXRef.ProbeSetId = ProbeSet.Id AND ProbeSetFreeze.Name = '%s' AND ProbeSet.Name = '%s' - """ % (display_fields_string, self.db.name, self.name) + """ % (display_fields_string, self.dataset.name, self.name) #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name # to avoid the problem of same marker name from different species. - elif self.db.type == 'Geno': - display_fields_string = string.join(self.db.display_fields,',Geno.') + elif self.dataset.type == 'Geno': + display_fields_string = string.join(self.dataset.display_fields,',Geno.') display_fields_string = 'Geno.' + display_fields_string query = """ SELECT %s @@ -421,10 +423,10 @@ class webqtlTrait: GenoXRef.GenoId = Geno.Id AND GenoFreeze.Name = '%s' AND Geno.Name = '%s' - """ % (display_fields_string, self.db.name, self.name) + """ % (display_fields_string, self.dataset.name, self.name) else: #Temp type query = 'SELECT %s FROM %s WHERE Name = "%s"' % \ - (string.join(self.db.display_fields,','), self.db.type, self.name) + (string.join(self.dataset.display_fields,','), self.dataset.type, self.name) self.cursor.execute(query) @@ -433,16 +435,16 @@ class webqtlTrait: self.haveinfo = 1 #XZ: assign SQL query result to trait attributes. - for i, field in enumerate(self.db.display_fields): + for i, field in enumerate(self.dataset.display_fields): setattr(self, field, traitInfo[i]) - if self.db.type == 'Publish': + if self.dataset.type == 'Publish': self.confidential = 0 if self.pre_publication_description and not self.pubmed_id: self.confidential = 1 self.homologeneid = None - if self.db.type == 'ProbeSet' and self.riset and self.geneid: + if self.dataset.type == 'ProbeSet' and self.group and self.geneid: #XZ, 05/26/2010: From time to time, this query get error message because some geneid values in database are not number. #XZ: So I have to test if geneid is number before execute the query. #XZ: The geneid values in database should be cleaned up. @@ -463,7 +465,7 @@ class webqtlTrait: InbredSet.Name = '%s' AND InbredSet.SpeciesId = Species.Id AND Species.TaxonomyId = Homologene.TaxonomyId - """ % (self.geneid, self.riset) + """ % (self.geneid, self.group) self.cursor.execute(query) result = self.cursor.fetchone() else: @@ -473,7 +475,7 @@ class webqtlTrait: self.homologeneid = result[0] if QTL: - if self.db.type == 'ProbeSet' and not self.cellid: + if self.dataset.type == 'ProbeSet' and not self.cellid: query = ''' SELECT ProbeSetXRef.Locus, ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.mean @@ -483,14 +485,14 @@ class webqtlTrait: ProbeSetXRef.ProbeSetId = ProbeSet.Id AND ProbeSet.Name = "%s" AND ProbeSetXRef.ProbeSetFreezeId =%s - ''' % (self.name, self.db.id) + ''' % (self.name, self.dataset.id) self.cursor.execute(query) traitQTL = self.cursor.fetchone() if traitQTL: self.locus, self.lrs, self.pvalue, self.mean = traitQTL else: self.locus = self.lrs = self.pvalue = self.mean = "" - if self.db.type == 'Publish': + if self.dataset.type == 'Publish': query = ''' SELECT PublishXRef.Locus, PublishXRef.LRS @@ -500,7 +502,7 @@ class webqtlTrait: PublishXRef.Id = %s AND PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND PublishFreeze.Id =%s - ''' % (self.name, self.db.id) + ''' % (self.name, self.dataset.id) self.cursor.execute(query) traitQTL = self.cursor.fetchone() if traitQTL: @@ -514,7 +516,7 @@ class webqtlTrait: if not self.haveinfo: self.retrieveInfo() - if self.db.type == 'Publish': + if self.dataset.type == 'Publish': PubMedLink = "" if self.pubmed_id: PubMedLink = HT.Href(text="PubMed %d : " % self.pubmed_id, @@ -524,10 +526,10 @@ class webqtlTrait: if formName: setDescription2 = HT.Href(url="javascript:showDatabase3('%s','%s','%s','')" % - (formName, self.db.name, self.name), Class = "fs14") + (formName, self.dataset.name, self.name), Class = "fs14") else: setDescription2 = HT.Href(url="javascript:showDatabase2('%s','%s','')" % - (self.db.name,self.name), Class = "fs14") + (self.dataset.name,self.name), Class = "fs14") if self.confidential and not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=privilege, userName=userName, authorized_users=authorized_users): setDescription2.append('RecordID/%s - %s' % (self.name, self.pre_publication_description)) @@ -545,20 +547,20 @@ class webqtlTrait: setDescription2.append(HT.Italic('%s, and colleagues' % a1)) setDescription = HT.Span(PubMedLink, setDescription2) - elif self.db.type == 'Temp': + elif self.dataset.type == 'Temp': setDescription = HT.Href(text="%s" % (self.description),url="javascript:showDatabase2\ - ('%s','%s','')" % (self.db.name,self.name), Class = "fs14") + ('%s','%s','')" % (self.dataset.name,self.name), Class = "fs14") setDescription = HT.Span(setDescription) - elif self.db.type == 'Geno': # Genome DB only available for single search + elif self.dataset.type == 'Geno': # Genome DB only available for single search if formName: setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ '%2.3f' % self.mb),url="javascript:showDatabase3('%s','%s','%s','')" % \ - (formName, self.db.name, self.name), Class = "fs14") + (formName, self.dataset.name, self.name), Class = "fs14") else: setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ '%2.3f' % self.mb),url="javascript:showDatabase2('%s','%s','')" % \ - (self.db.name,self.name), Class = "fs14") + (self.dataset.name,self.name), Class = "fs14") setDescription = HT.Span(setDescription) @@ -566,20 +568,20 @@ class webqtlTrait: if self.cellid: if formName: setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name, self.cellid),url=\ - "javascript:showDatabase3('%s','%s','%s','%s')" % (formName, self.db.name,self.name,self.cellid), \ + "javascript:showDatabase3('%s','%s','%s','%s')" % (formName, self.dataset.name,self.name,self.cellid), \ Class = "fs14") else: setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name,self.cellid),url=\ - "javascript:showDatabase2('%s','%s','%s')" % (self.db.name,self.name,self.cellid), \ + "javascript:showDatabase2('%s','%s','%s')" % (self.dataset.name,self.name,self.cellid), \ Class = "fs14") else: if formName: setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ - "javascript:showDatabase3('%s','%s','%s','')" % (formName, self.db.name,self.name), \ + "javascript:showDatabase3('%s','%s','%s','')" % (formName, self.dataset.name,self.name), \ Class = "fs14") else: setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ - "javascript:showDatabase2('%s','%s','')" % (self.db.name,self.name), \ + "javascript:showDatabase2('%s','%s','')" % (self.dataset.name,self.name), \ Class = "fs14") if self.symbol and self.chr and self.mb: setDescription.append(' [') @@ -591,9 +593,9 @@ class webqtlTrait: setDescription.append('; %s' % self.probe_target_description) setDescription = HT.Span(setDescription) - if self.db.type != 'Temp' and dispFromDatabase: + if self.dataset.type != 'Temp' and dispFromDatabase: setDescription.append( ' --- FROM : ') - setDescription.append(self.db.genHTML(Class='cori')) + setDescription.append(self.dataset.genHTML(Class='cori')) return setDescription @property @@ -654,13 +656,13 @@ class webqtlTrait: select ProbeFreeze.Name from ProbeFreeze, ProbeSetFreeze where ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND - ProbeSetFreeze.Id = %d""" % thisTrait.db.id) + ProbeSetFreeze.Id = %d""" % thisTrait.dataset.id) probeDBName = self.cursor.fetchone()[0] return dict(name = probeDBName, url = None) else: - return dict(name = self.db.fullname, - url = webqtlConfig.INFOPAGEHREF % self.db.name) + return dict(name = self.dataset.fullname, + url = webqtlConfig.INFOPAGEHREF % self.dataset.name) def calculate_correlation(self, values, method): """Calculate the correlation value and p value according to the method specified""" diff --git a/wqflask/dbFunction/webqtlDatabaseFunction.py b/wqflask/dbFunction/webqtlDatabaseFunction.py index 8f923b8a..1e028ecc 100755 --- a/wqflask/dbFunction/webqtlDatabaseFunction.py +++ b/wqflask/dbFunction/webqtlDatabaseFunction.py @@ -80,9 +80,9 @@ def getAllSpecies(cursor=None): #function: retrieve specie's name info based on RISet ########################################################################### -def retrieveSpecies(cursor=None, RISet=None): +def retrieveSpecies(cursor=None, group=None): try: - cursor.execute("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % RISet) + cursor.execute("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % group) return cursor.fetchone()[0] except: return None diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 2b8efd68..92a754e3 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -20,7 +20,7 @@ class DoSearch(object): def __init__(self, search_term, search_operator, dataset, cursor, db_conn): self.search_term = search_term # Make sure search_operator is something we expect - assert search_operator in ("=", "<", ">", "<=", ">="), "Bad search operator" + assert search_operator in (None, "=", "<", ">", "<=", ">="), "Bad search operator" self.search_operator = search_operator self.dataset = dataset self.db_conn = db_conn @@ -41,6 +41,12 @@ class DoSearch(object): def escape(self, stringy): """Shorter name than self.db_conn.escape_string""" return self.db_conn.escape_string(str(stringy)) + + def mescape(self, *items): + """Multiple escape""" + escaped = [self.escape(item) for item in items] + print("escaped is:", escaped) + return tuple(escaped) def normalize_spaces(self, stringy): """Strips out newlines/extra spaces and replaces them with just spaces""" @@ -91,8 +97,7 @@ class ProbeSetSearch(DoSearch): """Generates and runs a simple search of an mRNA expression dataset""" print("Running ProbeSetSearch") - query = (self.base_query + - """WHERE (MATCH (ProbeSet.Name, + query = self.base_query + """WHERE (MATCH (ProbeSet.Name, ProbeSet.description, ProbeSet.symbol, alias, @@ -102,8 +107,8 @@ class ProbeSetSearch(DoSearch): AGAINST ('%s' IN BOOLEAN MODE)) and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSetXRef.ProbeSetFreezeId = %s - """ % (self.escape(self.search_term), - self.escape(self.dataset.id))) + """ % (self.escape(self.search_term[0]), + self.escape(self.dataset.id)) print("final query is:", pf(query)) @@ -275,7 +280,8 @@ class GoSearch(ProbeSetSearch): class LrsSearch(ProbeSetSearch): """Searches for genes with a QTL within the given LRS values - LRS searches can take 2 different forms: + LRS searches can take 3 different forms: + - LRS > (or <) min/max_LRS - LRS=(min_LRS max_LRS) - LRS=(min_LRS max_LRS chromosome start_Mb end_Mb) where min/max_LRS represent the range of LRS scores and start/end_Mb represent @@ -289,129 +295,128 @@ class LrsSearch(ProbeSetSearch): self.search_term = [float(value) for value in self.search_term] - from_clause = ", Geno" + self.from_clause = ", Geno" if self.search_operator == "=": - if len(self.search_term) >= 2: - if len(self.search_term) == 2: - lrs_min, lrs_max = self.search_term - elif len(self.search_term) == 5: - lrs_min, lrs_max, chr_num, mb_low, mb_high = self.search_term - else: - SomeError - - sub_clause = """ %sXRef.LRS > %s and - %sXRef.LRS < %s and """ % (self.escape(self.dataset.type), - self.escape(min(lrs_min, lrs_max)), - self.escape(self.dataset.type), - self.escape(max(lrs_min, lrs_max))) - + assert isinstance(self.search_term, (list, tuple)) + self.lrs_min, self.lrs_max = self.search_term[:2] + + self.sub_clause = """ %sXRef.LRS > %s and + %sXRef.LRS < %s and """ % self.mescape(self.dataset.type, + min(self.lrs_min, self.lrs_max), + self.dataset.type, + max(self.lrs_min, self.lrs_max)) + + if len(self.search_term) > 2: + self.chr_num = self.search_term[2] + self.sub_clause += """ Geno.Chr = %s and """ % (self.escape(self.chr_num)) if len(self.search_term) == 5: - sub_clause = sub_clause + """ Geno.Mb > %s and + self.mb_low, self.mb_high = self.search_term[3:] + self.sub_clause += """ Geno.Mb > %s and Geno.Mb < %s and - Geno.Chr = %s and - """ % (self.escape(min(mb_low, mb_high)), - self.escape(max(mb_low, mb_high)), - self.escape(chr_num)) + """ % self.mescape(min(self.mb_low, self.mb_high), + max(self.mb_low, self.mb_high)) + print("self.sub_clause is:", pf(self.sub_clause)) else: # Deal with >, <, >=, and <= - sub_clause = """ %sXRef.LRS %s %s and """ % (self.escape(self.dataset.type), - self.escape(self.search_operator), - self.escape(self.search_term[0])) + self.sub_clause = """ %sXRef.LRS %s %s and """ % self.mescape(self.dataset.type, + self.search_operator, + self.search_term[0]) - where_clause = sub_clause + """ %sXRef.Locus = Geno.name and + self.where_clause = self.sub_clause + """ %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and %s.Chr = Geno.Chr - """ % (self.escape(self.dataset.type), - self.escape(self.species_id), - self.escape(self.dataset.type)) + """ % self.mescape(self.dataset.type, + self.species_id, + self.dataset.type) - print("where_clause is:", pf(where_clause)) + print("where_clause is:", pf(self.where_clause)) - query = self.compile_final_query(from_clause, where_clause) + self.query = self.compile_final_query(self.from_clause, self.where_clause) - return self.execute(query) - -class CisLrsSearch(LrsSearch): - """Searches for genes on a particular chromosome with a cis-eQTL within the given LRS values + return self.execute(self.query) - A cisLRS search can take 3 forms: - - cisLRS=(min_LRS max_LRS) - - cisLRS=(min_LRS max_LRS mb_buffer) - - cisLRS>min_LRS - where min/max_LRS represent the range of LRS scores and the mb_buffer is the range around - a particular QTL where its eQTL would be considered "cis". If there is no third parameter, - mb_buffer will default to 5 megabases. - A QTL is a cis-eQTL if a gene's expression is regulated by a QTL in roughly the same area - (where the area is determined by the mb_buffer that the user can choose). +class CisTransLrsSearch(LrsSearch): - """ - - # This is tentatively a child of LrsSearch; I'll need to check what code, if any, overlaps - # between this and the LrsSearch code. In the original code, commands are divided by - # the number of inputs they take, so these commands are completely separate - - DoSearch.search_types['CISLRS'] = "CisLrsSearch" - - def run(self): + def real_run(self, the_operator): #if isinstance(self.search_term, basestring): # self.search_term = [self.search_term] print("self.search_term is:", self.search_term) self.search_term = [float(value) for value in self.search_term] - mb_buffer = 5 # default - - from_clause = ", Geno " - + self.mb_buffer = 5 # default + self.from_clause = ", Geno " + if self.search_operator == "=": if len(self.search_term) == 2: - lower_limit, upper_limit = self.search_term + self.lrs_min, self.lrs_max = self.search_term #[int(value) for value in self.search_term] elif len(self.search_term) == 3: - lower_limit, upper_limit, mb_buffer = self.search_term + self.lrs_min, self.lrs_max, self.mb_buffer = self.search_term else: SomeError - sub_clause = """ %sXRef.LRS > %s and - %sXRef.LRS < %s and - ABS(%s.Mb-Geno.Mb) < %s and """ % ( - self.escape(self.dataset.type), - self.escape(min(lower_limit, upper_limit)), + self.sub_clause = """ %sXRef.LRS > %s and + %sXRef.LRS < %s and """ % ( self.escape(self.dataset.type), - self.escape(max(lower_limit, upper_limit)), + self.escape(min(self.lrs_min, self.lrs_max)), self.escape(self.dataset.type), - self.escape(mb_buffer) + self.escape(max(self.lrs_min, self.lrs_max)) ) - else: # Deal with >, <, >=, and <= - sub_clause = """ %sXRef.LRS %s %s and - ABS(%s.Mb-Geno.Mb) < %s and """ % ( + self.sub_clause = """ %sXRef.LRS %s %s and """ % ( self.escape(self.dataset.type), self.escape(self.search_operator), - self.escape(self.search_term[0]), - self.escape(self.dataset.type), - self.escape(mb_buffer) + self.escape(self.search_term[0]) ) - - where_clause = sub_clause + """%sXRef.Locus = Geno.name and + + self.where_clause = self.sub_clause + """ + ABS(%s.Mb-Geno.Mb) %s %s and + %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and %s.Chr = Geno.Chr""" % ( + self.escape(self.dataset.type), + the_operator, + self.escape(self.mb_buffer), self.escape(self.dataset.type), self.escape(self.species_id), self.escape(self.dataset.type) ) - print("where_clause is:", pf(where_clause)) + print("where_clause is:", pf(self.where_clause)) - query = self.compile_final_query(from_clause, where_clause) + self.query = self.compile_final_query(self.from_clause, self.where_clause) - return self.execute(query) + return self.execute(self.query) + + +class CisLrsSearch(CisTransLrsSearch): + """Searches for genes on a particular chromosome with a cis-eQTL within the given LRS values + + A cisLRS search can take 3 forms: + - cisLRS=(min_LRS max_LRS) + - cisLRS=(min_LRS max_LRS mb_buffer) + - cisLRS>min_LRS + where min/max_LRS represent the range of LRS scores and the mb_buffer is the range around + a particular QTL where its eQTL would be considered "cis". If there is no third parameter, + mb_buffer will default to 5 megabases. + + A QTL is a cis-eQTL if a gene's expression is regulated by a QTL in roughly the same area + (where the area is determined by the mb_buffer that the user can choose). + + """ + + DoSearch.search_types['CISLRS'] = "CisLrsSearch" + + def run(self): + return self.real_run("<") + -class TransLrsSearch(LrsSearch): +class TransLrsSearch(CisTransLrsSearch): """Searches for genes on a particular chromosome with a cis-eQTL within the given LRS values A transLRS search can take 2 forms: @@ -425,70 +430,11 @@ class TransLrsSearch(LrsSearch): (where the area is determined by the mb_buffer that the user can choose). Opposite of cis-eQTL. """ - - # This is tentatively a child of LrsSearch; I'll need to check what code, if any, overlaps - # between this and the LrsSearch code. In the original code, commands are divided by - # the number of inputs they take, so these commands are completely separate DoSearch.search_types['TRANSLRS'] = "TransLrsSearch" def run(self): - if len(self.search_term) == 3: - lower_limit, upper_limit, min_threshold = [int(value) for value in self.search_term] - - where_clause = """ %sXRef.LRS > %s and - %sXRef.LRS < %s and - %sXRef.Locus = Geno.name and - Geno.SpeciesId = %s and - (%s.Chr != Geno.Chr or - ABS(%s.Mb-Geno.Mb) > %s) """ % ( - self.dataset.type, - min(lower_limit, upper_limit), - self.dataset.type, - max(lower_limit, upper_limit), - self.dataset.type, - self.species_id, - self.dataset.type, - self.dataset.type, - min_threshold - ) - - else: - NeedSomeErrorHere - - return None - - -#itemCmd = item[0] -#lowerLimit = float(item[1]) -#upperLimit = float(item[2]) -# -#if itemCmd.upper() in ("TRANSLRS", "CISLRS"): -# if item[3]: -# mthresh = float(item[3]) -# clauseItem = " %sXRef.LRS > %2.7f and %sXRef.LRS < %2.7f " % \ -# (self.dbType, min(lowerLimit, upperLimit), self.dbType, max(lowerLimit, upperLimit)) -# if itemCmd.upper() == "CISLRS": -# clauseItem += """ and %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and %s.Chr = Geno.Chr and ABS(%s.Mb-Geno.Mb) < %2.7f """ % (self.dbType, self.speciesId, self.dbType, self.dbType, mthresh) -# DescriptionText.append(HT.Span(' with a ', HT.U('cis-QTL'), ' having an LRS between %g and %g using a %g Mb exclusion buffer' % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit), mthresh))) -# else: -# clauseItem += """ and %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and (%s.Chr != Geno.Chr or (%s.Chr != Geno.Chr and ABS(%s.Mb-Geno.Mb) > %2.7f)) """ % (self.dbType, self.speciesId, self.dbType, self.dbType, self.dbType, mthresh) -# DescriptionText.append(HT.Span(' with a ', HT.U('trans-QTL'), ' having an LRS between %g and %g using a %g Mb exclusion buffer' % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit), mthresh))) -# query.append(" (%s) " % clauseItem) -# self.orderByDefalut = "LRS" -# else: -# pass -#elif itemCmd.upper() in ("RANGE"): -# #XZ, 03/05/2009: Xiaodong changed Data to ProbeSetData -# clauseItem = " (select Pow(2, max(value) -min(value)) from ProbeSetData where Id = ProbeSetXRef.dataId) > %2.7f and (select Pow(2, max(value) -min(value)) from ProbeSetData where Id = ProbeSetXRef.dataId) < %2.7f " % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit)) -# query.append(" (%s) " % clauseItem) -# DescriptionText.append(HT.Span(' with a range of expression that varied between %g and %g' % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit)), " (fold difference)")) -#else: -# clauseItem = " %sXRef.%s > %2.7f and %sXRef.%s < %2.7f " % \ -# (self.dbType, itemCmd, min(lowerLimit, upperLimit), self.dbType, itemCmd, max(lowerLimit, upperLimit)) -# query.append(" (%s) " % clauseItem) -# self.orderByDefalut = itemCmd -# DescriptionText.append(HT.Span(' with ', HT.U(itemCmd), ' between %g and %g' % (min(lowerLimit, upperLimit), max(lowerLimit, upperLimit)))) + return self.real_run(">") class MeanSearch(ProbeSetSearch): @@ -508,7 +454,6 @@ if __name__ == "__main__": import MySQLdb import sys - from base import webqtlConfig from base.data_set import create_dataset from base.templatePage import templatePage @@ -540,11 +485,11 @@ if __name__ == "__main__": ProbeSetXRef.ProbeSetFreezeId = 112""") #print(pf(cursor.fetchall())) - #results = ProbeSetSearch("salt", dataset, cursor, db_conn).run() + results = ProbeSetSearch("shh", None, dataset, cursor, db_conn).run() #results = RifSearch("diabetes", dataset, cursor, db_conn).run() #results = WikiSearch("nicotine", dataset, cursor, db_conn).run() - results = CisLrsSearch(['99'], '>', dataset, cursor, db_conn).run() # cisLRS > 99 - #results = LrsSearch('9', '99', '1', '50', '150', '=', dataset, cursor, db_conn).run() + #results = CisLrsSearch(['99'], '>', dataset, cursor, db_conn).run() # cisLRS > 99 + #results = LrsSearch('99', '>', dataset, cursor, db_conn).run() #results = TransLrsSearch(['9', '999', '10'], dataset, cursor, db_conn).run() #results = PhenotypeSearch("brain", dataset, cursor, db_conn).run() #results = GenotypeSearch("rs13475699", dataset, cursor, db_conn).run() diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index fe091f97..63e0153d 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -68,7 +68,7 @@ class SearchResultPage(templatePage): # return ########################################### - # Names and IDs of RISet / F2 set + # Names and IDs of group / F2 set ########################################### # All Phenotypes is a special case we'll deal with later @@ -97,23 +97,23 @@ class SearchResultPage(templatePage): """ self.trait_list = [] + + group = self.dataset.group + species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=group) + # result_set represents the results for each search term; a search of # "shh grin2b" would have two sets of results, one for each term print("self.results is:", pf(self.results)) for result in self.results: if not result: continue - - group = self.dataset.group - species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=group) - + #### Excel file needs to be generated #### print("foo locals are:", locals()) trait_id = result[0] - this_trait = webqtlTrait(self.db_conn, db=self.dataset, name=trait_id) + this_trait = webqtlTrait(self.db_conn, dataset=self.dataset, name=trait_id) this_trait.retrieveInfo(QTL=True) - print("this_trait is:", pf(this_trait)) self.trait_list.append(this_trait) self.dataset.get_trait_info(self.trait_list, species) @@ -134,6 +134,8 @@ class SearchResultPage(templatePage): # We fall back to the dataset type as the key to get the right object search_type = self.dataset.type + print("search_type is:", pf(search_type)) + # This is throwing an error when a_search['key'] is None, so I changed above #search_type = string.upper(a_search['key']) #if not search_type: @@ -146,7 +148,7 @@ class SearchResultPage(templatePage): self.dataset, self.cursor, self.db_conn).run()) - + print("in the search results are:", self.results) diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 3dac5933..db2636bc 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -35,12 +35,12 @@ class ShowTrait(templatePage): self.fd = fd templatePage.__init__(self, fd) - assert self.openMysql(), "No datbase!" + assert self.openMysql(), "No database!" this_trait = self.get_this_trait() ##read genotype file - fd.RISet = this_trait.riset + fd.group = this_trait.group fd.readGenotype() if not fd.genotype: @@ -62,7 +62,7 @@ class ShowTrait(templatePage): # Some fields, like method, are defaulted to None; otherwise in IE the field can't be changed using jquery hddn = OrderedDict( FormID = fmID, - RISet = fd.RISet, + group = fd.group, submitID = '', scale = 'physic', additiveCheck = 'ON', @@ -120,7 +120,7 @@ class ShowTrait(templatePage): hddn['attribute_names'] = "" hddn['mappingMethodId'] = webqtlDatabaseFunction.getMappingMethod (cursor=self.cursor, - groupName=fd.RISet) + groupName=fd.group) if fd.identification: hddn['identification'] = fd.identification @@ -159,8 +159,8 @@ class ShowTrait(templatePage): self.hddn = hddn self.sample_group_types = OrderedDict() - self.sample_group_types['samples_primary'] = fd.RISet + " Only" - self.sample_group_types['samples_other'] = "Non-" + fd.RISet + self.sample_group_types['samples_primary'] = fd.group + " Only" + self.sample_group_types['samples_other'] = "Non-" + fd.group self.sample_group_types['samples_all'] = "All Cases" sample_lists = [group.sample_list for group in self.sample_groups] print("sample_lists is:", pf(sample_lists)) @@ -180,12 +180,12 @@ class ShowTrait(templatePage): trait_id = self.fd['trait_id'] cell_id = self.fd.get('CellID') - this_trait = webqtlTrait(db=dataset, name=trait_id, cellid=cell_id, cursor=self.cursor) + this_trait = webqtlTrait(self.db_conn, db=dataset, name=trait_id, cellid=cell_id) ##identification, etc. self.fd.identification = '%s : %s' % (this_trait.db.shortname, trait_id) this_trait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\ - &ProbeSetID=%s&RISet=%s&parentsf1=on' %(dataset, trait_id, self.fd['RISet']) + &ProbeSetID=%s&group=%s&parentsf1=on' %(dataset, trait_id, self.fd['group']) if cell_id: self.fd.identification = '%s/%s'%(self.fd.identification, cell_id) @@ -198,7 +198,7 @@ class ShowTrait(templatePage): def dispTraitInformation(self, fd, title1Body, hddn, this_trait): - _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet) + _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) #tbl = HT.TableLite(cellpadding=2, Class="collap", style="margin-left:20px;", width="840", valign="top", id="target1") @@ -245,9 +245,9 @@ class ShowTrait(templatePage): else: pass - self.cursor.execute('SELECT Name FROM InbredSet WHERE Name="%s"' % fd.RISet) + self.cursor.execute('SELECT Name FROM InbredSet WHERE Name="%s"' % fd.group) if this_trait: - addSelectionButton = HT.Href(url="#redirect", onClick="addRmvSelection('%s', document.getElementsByName('%s')[0], 'addToSelection');" % (fd.RISet, 'dataInput')) + addSelectionButton = HT.Href(url="#redirect", onClick="addRmvSelection('%s', document.getElementsByName('%s')[0], 'addToSelection');" % (fd.group, 'dataInput')) addSelectionButton_img = HT.Image("/images/add_icon.jpg", name="addselect", alt="Add To Collection", title="Add To Collection", style="border:none;") #addSelectionButton.append(addSelectionButton_img) addSelectionText = "Add" @@ -403,8 +403,8 @@ class ShowTrait(templatePage): probeResult = self.cursor.fetchone() if probeResult[0] > 0: - probeurl = "%s?FormID=showProbeInfo&database=%s&ProbeSetID=%s&CellID=%s&RISet=%s&incparentsf1=ON" \ - % (os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), this_trait.db, this_trait.name, this_trait.cellid, fd.RISet) + probeurl = "%s?FormID=showProbeInfo&database=%s&ProbeSetID=%s&CellID=%s&group=%s&incparentsf1=ON" \ + % (os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), this_trait.db, this_trait.name, this_trait.cellid, fd.group) probeButton = HT.Href(url="#", onClick="javascript:openNewWin('%s'); return false;" % probeurl) probeButton_img = HT.Image("/images/probe_icon.jpg", name="probe", alt=" Check sequence of probes ", title=" Check sequence of probes ", style="border:none;") #probeButton.append(probeButton_img) @@ -430,7 +430,7 @@ class ShowTrait(templatePage): # )) #tSpan = HT.Span(Class="fs13") - #tSpan.append(str(_Species).capitalize(), ", ", fd.RISet) + #tSpan.append(str(_Species).capitalize(), ", ", fd.group) # #tbl.append(HT.TR( # HT.TD('Species and Group: ', Class="fwb fs13", valign="top", nowrap="on"), @@ -805,6 +805,7 @@ class ShowTrait(templatePage): #stats_row = HT.TR() #stats_cell = HT.TD() + # This should still be riset here - Sam - Nov. 2012 if fd.genotype.type == "riset": samplelist = fd.f1list + fd.samplelist else: @@ -839,15 +840,15 @@ class ShowTrait(templatePage): other_samples = map(lambda X:"_2nd_"+X, fd.f1list + fd.parlist) + other_samples #XZ: note that fd.f1list and fd.parlist are added. print("ac1") # This is the one used for first sall3 self.MDP_menu.append(('All Cases','0')) - self.MDP_menu.append(('%s Only' % fd.RISet, '1')) - self.MDP_menu.append(('Non-%s Only' % fd.RISet, '2')) + self.MDP_menu.append(('%s Only' % fd.group, '1')) + self.MDP_menu.append(('Non-%s Only' % fd.group, '2')) else: if (len(other_samples) > 0) and (len(primary_samples) + len(other_samples) > 3): print("ac2") self.MDP_menu.append(('All Cases','0')) - self.MDP_menu.append(('%s Only' % fd.RISet,'1')) - self.MDP_menu.append(('Non-%s Only' % fd.RISet,'2')) + self.MDP_menu.append(('%s Only' % fd.group,'1')) + self.MDP_menu.append(('Non-%s Only' % fd.group,'2')) all_samples = primary_samples all_samples.sort(key=webqtlUtil.natsort_key) all_samples = map(lambda X:"_2nd_"+X, fd.f1list + fd.parlist) + all_samples @@ -895,7 +896,7 @@ class ShowTrait(templatePage): # for sampleNameOrig in all_samples]] # - #Using just the RISet sample + #Using just the group sample for sampleNameOrig in primary_samples: sampleName = sampleNameOrig.replace("_2nd_", "") @@ -908,7 +909,7 @@ class ShowTrait(templatePage): vals2.append(thisValFull) - #Using all non-RISet samples only + #Using all non-group samples only for sampleNameOrig in other_samples: sampleName = sampleNameOrig.replace("_2nd_", "") @@ -951,10 +952,10 @@ class ShowTrait(templatePage): break elif (i == 1 and len(primary_samples) < 4): stats_container = HT.Div(id="stats_tabs%s" % i, Class="ui-tabs") - #stats_container.append(HT.Div(HT.Italic("Fewer than 4 " + fd.RISet + " case data were entered. No statistical analysis has been attempted."))) + #stats_container.append(HT.Div(HT.Italic("Fewer than 4 " + fd.group + " case data were entered. No statistical analysis has been attempted."))) elif (i == 2 and len(other_samples) < 4): stats_container = HT.Div(id="stats_tabs%s" % i, Class="ui-tabs") - stats_container.append(HT.Div(HT.Italic("Fewer than 4 non-" + fd.RISet + " case data were entered. No statistical analysis has been attempted."))) + stats_container.append(HT.Div(HT.Italic("Fewer than 4 non-" + fd.group + " case data were entered. No statistical analysis has been attempted."))) #stats_script_text = """$(function() { $("#stats_tabs0").tabs(); $("#stats_tabs1").tabs(); $("#stats_tabs2").tabs();});""" else: continue @@ -995,7 +996,7 @@ class ShowTrait(templatePage): except: plotTitle = str(this_trait.name) - #normalplot_img = BasicStatisticsFunctions.plotNormalProbability(vals=vals, RISet=fd.RISet, title=plotTitle, specialStrains=specialStrains) + #normalplot_img = BasicStatisticsFunctions.plotNormalProbability(vals=vals, group=fd.group, title=plotTitle, specialStrains=specialStrains) #normalplot.append(HT.TR(HT.TD(normalplot_img))) #normalplot.append(HT.TR(HT.TD(HT.BR(),HT.BR(),"This plot evaluates whether data are \ #normally distributed. Different symbols represent different groups.",HT.BR(),HT.BR(), @@ -1018,7 +1019,7 @@ class ShowTrait(templatePage): #barName_div = HT.Div(id="statstabs-3") #barName_container = HT.Paragraph() #barName = HT.TableLite(cellspacing=0, cellpadding=0, width="100%") - #barName_img = BasicStatisticsFunctions.plotBarGraph(identification=fd.identification, RISet=fd.RISet, vals=vals, type="name") + #barName_img = BasicStatisticsFunctions.plotBarGraph(identification=fd.identification, group=fd.group, vals=vals, type="name") #barName.append(HT.TR(HT.TD(barName_img))) #barName_container.append(barName) #barName_div.append(barName_container) @@ -1027,7 +1028,7 @@ class ShowTrait(templatePage): #barRank_div = HT.Div(id="statstabs-4") #barRank_container = HT.Paragraph() #barRank = HT.TableLite(cellspacing=0, cellpadding=0, width="100%") - #barRank_img = BasicStatisticsFunctions.plotBarGraph(identification=fd.identification, RISet=fd.RISet, vals=vals, type="rank") + #barRank_img = BasicStatisticsFunctions.plotBarGraph(identification=fd.identification, group=fd.group, vals=vals, type="rank") #barRank.append(HT.TR(HT.TD(barRank_img))) #barRank_container.append(barRank) #barRank_div.append(barRank_container) @@ -1048,16 +1049,16 @@ class ShowTrait(templatePage): def build_correlation_tools(self, fd, this_trait): - #species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet) + #species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) - RISetgp = fd.RISet + this_group = fd.group # We're checking a string here! - assert isinstance(RISetgp, basestring), "We need a string type thing here" - if RISetgp[:3] == 'BXD': - RISetgp = 'BXD' + assert isinstance(this_group, basestring), "We need a string type thing here" + if this_group[:3] == 'BXD': + this_group = 'BXD' - if RISetgp: + if this_group: #sample_correlation = HT.Input(type='button',name='sample_corr', value=' Compute ', Class="button sample_corr") #lit_correlation = HT.Input(type='button',name='lit_corr', value=' Compute ', Class="button lit_corr") #tissue_correlation = HT.Input(type='button',name='tiss_corr', value=' Compute ', Class="button tiss_corr") @@ -1074,7 +1075,7 @@ class ShowTrait(templatePage): self.cursor.execute('''SELECT PublishFreeze.FullName,PublishFreeze.Name FROM PublishFreeze,InbredSet WHERE PublishFreeze.InbredSetId = InbredSet.Id and InbredSet.Name = %s and PublishFreeze.public > %s''', - (RISetgp, webqtlConfig.PUBLICTHRESH)) + (this_group, webqtlConfig.PUBLICTHRESH)) for item in self.cursor.fetchall(): dataset_menu.append(dict(tissue=None, datasets=[item])) @@ -1082,7 +1083,7 @@ class ShowTrait(templatePage): self.cursor.execute('''SELECT GenoFreeze.FullName,GenoFreeze.Name FROM GenoFreeze, InbredSet WHERE GenoFreeze.InbredSetId = InbredSet.Id and InbredSet.Name = %s and GenoFreeze.public > %s''', - (RISetgp, webqtlConfig.PUBLICTHRESH)) + (this_group, webqtlConfig.PUBLICTHRESH)) for item in self.cursor.fetchall(): dataset_menu.append(dict(tissue=None, datasets=[item])) @@ -1098,7 +1099,7 @@ class ShowTrait(templatePage): InbredSet WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id and ProbeFreeze.TissueId = %s and ProbeSetFreeze.public > %s and ProbeFreeze.InbredSetId = InbredSet.Id and InbredSet.Name like %s order by ProbeSetFreeze.CreateTime desc, ProbeSetFreeze.AvgId ''', - (tissue_id, webqtlConfig.PUBLICTHRESH, "%" + RISetgp + "%")) + (tissue_id, webqtlConfig.PUBLICTHRESH, "%" + this_group + "%")) print("phun8") dataset_sub_menu = [item for item in self.cursor.fetchall() if item] #for item2 in self.cursor.fetchall(): @@ -1257,11 +1258,11 @@ class ShowTrait(templatePage): def dispMappingTools(self, fd, title4Body, this_trait): - _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet) + _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) - RISetgp = fd.RISet - if RISetgp[:3] == 'BXD': - RISetgp = 'BXD' + this_group = fd.group + if this_group[:3] == 'BXD': + this_group = 'BXD' #check boxes - one for regular interval mapping, the other for composite permCheck1= HT.Input(type='checkbox', Class='checkbox', name='permCheck1',checked="on") @@ -1454,7 +1455,7 @@ class ShowTrait(templatePage): # Treat Interval Mapping and Marker Regression and Pair Scan as a group for displaying #disable Interval Mapping and Marker Regression and Pair Scan for human and the dataset doesn't have genotype file - mappingMethodId = webqtlDatabaseFunction.getMappingMethod(cursor=self.cursor, groupName=RISetgp) + mappingMethodId = webqtlDatabaseFunction.getMappingMethod(cursor=self.cursor, groupName=this_group) mapping_script = HT.Script(language="Javascript") mapping_script_text = """$(function() { $("#mapping_tabs").tabs(); });""" @@ -1526,7 +1527,7 @@ class ShowTrait(templatePage): sample_names=primary_sample_names, this_trait=this_trait, sample_group_type='primary', - header="%s Only" % (fd.RISet)) + header="%s Only" % (fd.group)) other_sample_names = [] for sample in this_trait.data.keys(): @@ -1547,7 +1548,7 @@ class ShowTrait(templatePage): sample_names=other_sample_names, this_trait=this_trait, sample_group_type='other', - header="Non-%s" % (fd.RISet)) + header="Non-%s" % (fd.group)) self.sample_groups = (primary_samples, other_samples) else: diff --git a/wqflask/wqflask/templates/index_page.html b/wqflask/wqflask/templates/index_page.html index a113bc15..c01898b3 100644 --- a/wqflask/wqflask/templates/index_page.html +++ b/wqflask/wqflask/templates/index_page.html @@ -92,8 +92,8 @@ "btn" value="Advanced Search" onclick= "javascript:window.open('/index3.html', '_self');"> - + + diff --git a/wqflask/wqflask/templates/search_result_page.html b/wqflask/wqflask/templates/search_result_page.html index e393ced6..54cdd42b 100644 --- a/wqflask/wqflask/templates/search_result_page.html +++ b/wqflask/wqflask/templates/search_result_page.html @@ -23,7 +23,7 @@ {% if search_terms %}
  • {% for word in search_terms %} - {{word.search_term}} {% if not loop.last %} or {% endif %} + {{word.search_term[0]}} {% if not loop.last %} or {% endif %} {% endfor %}
  • {% endif %} diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 41d1d714..fb93af53 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -65,6 +65,9 @@ def search_page(): the_search = search_results.SearchResultPage(request.args) print("template_vars is:", pf(the_search.__dict__)) #print("trait_list is:", pf(the_search.__dict__['trait_list'][0].__dict__)) + #for trait in the_search.trait_list: + # print(" -", trait.description_display) + return render_template("search_result_page.html", **the_search.__dict__) -- cgit v1.2.3 From 43f69f26507d934a15d8e8d20f0ac3023fdb7691 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Fri, 30 Nov 2012 18:03:52 -0600 Subject: Started switching to using basic sqlalchemy to handle db connection/queries Began fixing bugs related to this fix, still in progress --- wqflask/base/data_set.py | 33 ++++++++------- wqflask/base/webqtlConfigLocal.py | 4 +- wqflask/base/webqtlTrait.py | 69 ++++++++++++++++++-------------- wqflask/cfg/zach_settings.py | 3 ++ wqflask/wqflask/do_search.py | 24 ++++++++--- wqflask/wqflask/search_results.py | 3 +- wqflask/wqflask/show_trait/show_trait.py | 25 ++++++------ wqflask/wqflask/views.py | 14 +++++-- 8 files changed, 106 insertions(+), 69 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 633f7545..015b2623 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -22,6 +22,8 @@ from __future__ import print_function, division +from flask import Flask, g + from htmlgen import HTMLgen2 as HT import webqtlConfig @@ -31,25 +33,28 @@ from pprint import pformat as pf # Used by create_database to instantiate objects DS_NAME_MAP = {} -def create_dataset(db_conn, dataset_name): - cursor = db_conn.cursor() - cursor.execute(""" +def create_dataset(dataset_name): + #cursor = db_conn.cursor() + print("dataset_name:", dataset_name) + + dataset_type = g.db.execute(""" SELECT DBType.Name FROM DBList, DBType WHERE DBList.Name = %s and DBType.Id = DBList.DBTypeId - """, (dataset_name)) - print("dataset_name:", dataset_name) - dataset_type = cursor.fetchone()[0] - print("dataset_type:", pf(dataset_type)) + """, (dataset_name)).fetchone().Name + + #dataset_type = cursor.fetchone()[0] + print("[blubber] dataset_type:", pf(dataset_type)) dataset_ob = DS_NAME_MAP[dataset_type] #dataset_class = getattr(data_set, dataset_ob) - + print("dataset_ob:", dataset_ob) print("DS_NAME_MAP:", pf(DS_NAME_MAP)) dataset_class = globals()[dataset_ob] - return dataset_class(dataset_name, db_conn) + return dataset_class(dataset_name) + class DataSet(object): """ @@ -58,12 +63,12 @@ class DataSet(object): """ - def __init__(self, name, db_conn): + def __init__(self, name): assert name self.name = name - self.db_conn = db_conn - self.cursor = self.db_conn.cursor() + #self.db_conn = db_conn + #self.cursor = self.db_conn.cursor() self.id = None self.type = None self.group = None @@ -271,7 +276,7 @@ class GenotypeDataSet(DataSet): def check_confidentiality(self): return geno_mrna_confidentiality(self) - def get_trait_info(self, trait_list): + def get_trait_info(self, trait_list, species=None): for this_trait in trait_list: if not this_trait.haveinfo: this_trait.retrieveInfo() @@ -355,7 +360,7 @@ class MrnaAssayDataSet(DataSet): ProbeFreeze.InbredSetId = InbredSet.Id AND ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = "%s" - ''' % self.db_conn.escape_string(self.name) + ''' % g.db.escape_string(self.name) def check_confidentiality(self): diff --git a/wqflask/base/webqtlConfigLocal.py b/wqflask/base/webqtlConfigLocal.py index 5aab48ac..84686234 100755 --- a/wqflask/base/webqtlConfigLocal.py +++ b/wqflask/base/webqtlConfigLocal.py @@ -4,12 +4,12 @@ MYSQL_SERVER = 'localhost' DB_NAME = 'db_webqtl_zas1024' -DB_USER = 'webqtlupd' +DB_USER = 'webqtl' DB_PASSWD = 'webqtl' MYSQL_UPDSERVER = 'localhost' DB_UPDNAME = 'db_webqtl_zas1024' -DB_UPDUSER = 'webqtlupd' +DB_UPDUSER = 'webqtl' DB_UPDPASSWD = 'webqtl' GNROOT = '/home/zas1024/gn/' diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index cc0e2321..1dceba08 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -12,6 +12,7 @@ from utility import webqtlUtil from pprint import pformat as pf +from flask import Flask, g class webqtlTrait: """ @@ -20,38 +21,46 @@ class webqtlTrait: """ - def __init__(self, db_conn, **kw): + def __init__(self, **kw): print("in webqtlTrait") - self.db_conn = db_conn - self.cursor = self.db_conn.cursor() - self.dataset = None # database object - self.name = '' # Trait ID, ProbeSet ID, Published ID, etc. - self.cellid = '' - self.identification = 'un-named trait' - self.group = '' - self.haveinfo = 0 - self.sequence = '' # Blat sequence, available for ProbeSet - self.data = {} - print("foo") - print("kw in webqtlTrait are:", pf(kw)) - print("printed\n\n") - for name, value in kw.items(): - if self.__dict__.has_key(name): - setattr(self, name, value) - elif name == 'fullname': - name2 = value.split("::") - if len(name2) == 2: - self.dataset, self.name = name2 - elif len(name2) == 3: - self.dataset, self.name, self.cellid = name2 - else: - raise KeyError, repr(value) + ' parameter format error.' - else: - raise KeyError, repr(name) + ' not a valid parameter for this class.' + #self.db_conn = db_conn + #self.cursor = self.db_conn.cursor() + self.dataset = kw.get('dataset', None) # database object + self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. + self.cellid = kw.get('cellid', None) + self.identification = kw.get('identification', 'un-named trait') + self.group = kw.get('group', None) + self.haveinfo = kw.get(haveinfo, False) + self.sequence = kw.get(sequence, None) # Blat sequence, available for ProbeSet + self.data = kw.get(data, {}) + + if kw.get('fullname'): + name2 = value.split("::") + if len(name2) == 2: + self.dataset, self.name = name2 + elif len(name2) == 3: + self.dataset, self.name, self.cellid = name2 + + #print("foo") + #print("kw in webqtlTrait are:", pf(kw)) + #print("printed\n\n") + #for name, value in kw.items(): + # if self.__dict__.has_key(name): + # setattr(self, name, value) + # elif name == 'fullname': + # name2 = value.split("::") + # if len(name2) == 2: + # self.dataset, self.name = name2 + # elif len(name2) == 3: + # self.dataset, self.name, self.cellid = name2 + # else: + # raise KeyError, repr(value) + ' parameter format error.' + # else: + # raise KeyError, repr(name) + ' not a valid parameter for this class.' if self.dataset and isinstance(self.dataset, basestring): - assert self.cursor, "Don't have a cursor" - self.dataset = create_dataset(self.db_conn, self.dataset) + #assert self.cursor, "Don't have a cursor" + self.dataset = create_dataset(self.dataset) #if self.dataset == None, not from a database print("self.dataset is:", self.dataset, type(self.dataset)) @@ -432,7 +441,7 @@ class webqtlTrait: self.cursor.execute(query) traitInfo = self.cursor.fetchone() if traitInfo: - self.haveinfo = 1 + self.haveinfo = True #XZ: assign SQL query result to trait attributes. for i, field in enumerate(self.dataset.display_fields): diff --git a/wqflask/cfg/zach_settings.py b/wqflask/cfg/zach_settings.py index ed97f222..8d3bf4ab 100644 --- a/wqflask/cfg/zach_settings.py +++ b/wqflask/cfg/zach_settings.py @@ -1,2 +1,5 @@ LOGFILE = """/tmp/flask_gn_log""" + TRAP_BAD_REQUEST_ERRORS = True + +DB_URI = """mysql://webqtl:webqtl@localhost/db_webqtl_zas1024""" \ No newline at end of file diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 17078802..bae3df08 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -144,14 +144,22 @@ class PhenotypeSearch(DoSearch): 'Publication.Title', 'Publication.Authors', 'PublishXRef.Id') + + header_fields = ['', + 'Record ID', + 'Description', + 'Authors', + 'Year', + 'Max LRS', + 'Max LRS Location'] def get_where_clause(self): """Generate clause for WHERE portion of query""" #Todo: Zach will figure out exactly what both these lines mean #and comment here - if "'" not in self.search_term: - search_term = "[[:<:]]" + self.search_term + "[[:>:]]" + if "'" not in self.search_term[0]: + search_term = "[[:<:]]" + self.search_term[0] + "[[:>:]]" # This adds a clause to the query that matches the search term # against each field in the search_fields tuple @@ -195,6 +203,10 @@ class GenotypeSearch(DoSearch): FROM GenoXRef, GenoFreeze, Geno """ search_fields = ('Name', 'Chr') + + header_fields = ['', + 'Record ID', + 'Location'] def get_fields_clause(self): """Generate clause for part of the WHERE portion of query""" @@ -203,13 +215,13 @@ class GenotypeSearch(DoSearch): # against each field in search_fields (above) fields_clause = [] - if "'" not in self.search_term: - self.search_term = "[[:<:]]" + self.search_term + "[[:>:]]" + if "'" not in self.search_term[0]: + self.search_term = "[[:<:]]" + self.search_term[0] + "[[:>:]]" for field in self.search_fields: fields_clause.append('''%s REGEXP "%s"''' % ("%s.%s" % self.mescape(self.dataset.type, - field, - self.search_term))) + field), + self.search_term)) print("hello ;where_clause is:", pf(fields_clause)) fields_clause = "(%s)" % ' OR '.join(fields_clause) diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index c7bbdaf2..04b14e8f 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -83,7 +83,8 @@ class SearchResultPage(templatePage): self.dataset_group_ids = map(lambda x: x[2], results) else: print("self.dataset is:", pf(self.dataset)) - self.dataset = create_dataset(self.db_conn, self.dataset) + # Replaces a string with an object + self.dataset = create_dataset(self.dataset) print("self.dataset is now:", pf(self.dataset)) self.search() diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index e8ad0b1d..7060f2ea 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -31,16 +31,15 @@ from pprint import pformat as pf class ShowTrait(templatePage): - def __init__(self, fd): - self.fd = fd + def __init__(self, args): + print("in ShowTrait, args are:", args) + self.group = args.group + self.trait_id = trait_id + self.dataset = dataset - print("red1 fd.group:", fd.group) - templatePage.__init__(self, fd) + #assert self.openMysql(), "No database!" - print("red2 fd.group:", fd.group) - assert self.openMysql(), "No database!" - - print("red3 fd.group:", fd.group) + #print("red3 fd.group:", fd.group) this_trait = self.get_this_trait() print("red4 fd.group:", fd.group) @@ -183,11 +182,13 @@ class ShowTrait(templatePage): #if traitInfos: # database, ProbeSetID, CellID = traitInfos #else: - dataset = self.fd['dataset'] - trait_id = self.fd['trait_id'] - cell_id = self.fd.get('CellID') + #dataset = self.fd['dataset'] + #trait_id = self.fd['trait_id'] + #cell_id = self.fd.get('CellID') - this_trait = webqtlTrait(self.db_conn, dataset=dataset, name=trait_id, cellid=cell_id) + this_trait = webqtlTrait(dataset=dataset, + name=trait_id, + cellid=cell_id) ##identification, etc. self.fd.identification = '%s : %s' % (this_trait.dataset.shortname, trait_id) diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index cdc3379f..17dc42fb 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -7,10 +7,12 @@ import simplejson as json import yaml import flask +import sqlalchemy +#import config from wqflask import app -from flask import render_template, request, make_response, Response +from flask import render_template, request, make_response, Response, Flask, g, config from wqflask import search_results from wqflask.show_trait import show_trait @@ -27,6 +29,10 @@ from pprint import pformat as pf #logging.basicConfig(filename="/tmp/gn_log", level=logging.INFO) #_log = logging.getLogger("correlation") +@app.before_request +def connect_db(): + print("blue app.config:", app.config, pf(vars(app.config))) + g.db = sqlalchemy.create_engine(app.config['DB_URI']) @app.route("/") def index_page(): @@ -86,9 +92,9 @@ def whats_new_page(): @app.route("/show_trait") def show_trait_page(): # Here it's currently too complicated not to use an fd that is a webqtlFormData - fd = webqtlFormData.webqtlFormData(request.args) - print("stp y1:", pf(vars(fd))) - template_vars = show_trait.ShowTrait(fd) + #fd = webqtlFormData.webqtlFormData(request.args) + #print("stp y1:", pf(vars(fd))) + template_vars = show_trait.ShowTrait(request.args) template_vars.js_data = json.dumps(template_vars.js_data, default=json_default_handler, indent=" ", -- cgit v1.2.3 From 21253f4424fbcdf76212a55011e657ebeb87da82 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Fri, 30 Nov 2012 18:27:59 -0600 Subject: Added example of escaping strings now that trasitioning to simple SQLAlchemy --- wqflask/base/data_set.py | 88 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 015b2623..34e5eaa1 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -28,6 +28,7 @@ from htmlgen import HTMLgen2 as HT import webqtlConfig +from MySQLdb import escape_string as escape from pprint import pformat as pf # Used by create_database to instantiate objects @@ -36,22 +37,22 @@ DS_NAME_MAP = {} def create_dataset(dataset_name): #cursor = db_conn.cursor() print("dataset_name:", dataset_name) - + dataset_type = g.db.execute(""" SELECT DBType.Name FROM DBList, DBType WHERE DBList.Name = %s and DBType.Id = DBList.DBTypeId """, (dataset_name)).fetchone().Name - + #dataset_type = cursor.fetchone()[0] print("[blubber] dataset_type:", pf(dataset_type)) - + dataset_ob = DS_NAME_MAP[dataset_type] #dataset_class = getattr(data_set, dataset_ob) print("dataset_ob:", dataset_ob) print("DS_NAME_MAP:", pf(DS_NAME_MAP)) - + dataset_class = globals()[dataset_ob] return dataset_class(dataset_name) @@ -75,12 +76,12 @@ class DataSet(object): #if self.cursor and self.id == 0: self.setup() - + self.check_confidentiality() - + self.retrieve_name() self.get_group() - + # Delete this eventually @property @@ -101,9 +102,9 @@ class DataSet(object): """ If the data set name parameter is not found in the 'Name' field of the data set table, check if it is actually the FullName or ShortName instead. - + This is not meant to retrieve the data set info if no name at all is passed. - + """ query_args = tuple(self.db_conn.escape_string(x) for x in ( @@ -113,7 +114,7 @@ class DataSet(object): self.name, self.name)) print("query_args are:", query_args) - + query = ''' SELECT Id, Name, FullName, ShortName @@ -123,7 +124,7 @@ class DataSet(object): public > %s AND (Name = "%s" OR FullName = "%s" OR ShortName = "%s") ''' % (query_args) - + self.cursor.execute(query) self.id, self.name, self.fullname, self.shortname = self.cursor.fetchone() @@ -147,7 +148,7 @@ class PhenotypeDataSet(DataSet): 'Publication.Title', 'Publication.Authors', 'PublishXRef.Id'] - + # Figure out what display_fields is self.display_fields = ['name', 'pubmed_id', @@ -172,10 +173,10 @@ class PhenotypeDataSet(DataSet): 'Authors', 'Year', 'Max LRS', - 'Max LRS Location'] + 'Max LRS Location'] self.type = 'Publish' - + self.query = ''' SELECT InbredSet.Name, InbredSet.Id @@ -185,11 +186,11 @@ class PhenotypeDataSet(DataSet): PublishFreeze.InbredSetId = InbredSet.Id AND PublishFreeze.Name = "%s" ''' % self.db_conn.escape_string(self.name) - + def check_confidentiality(self): # (Urgently?) Need to write this pass - + def get_trait_info(self, trait_list, species = ''): for this_trait in trait_list: if not this_trait.haveinfo: @@ -238,31 +239,31 @@ class PhenotypeDataSet(DataSet): this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs this_trait.LRS_score_value = LRS_score_value = this_trait.lrs - this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) - + this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + class GenotypeDataSet(DataSet): DS_NAME_MAP['Geno'] = 'GenotypeDataSet' - + def setup(self): # Fields in the database table self.search_fields = ['Name', 'Chr'] - + # Find out what display_fields is self.display_fields = ['name', 'chr', 'mb', 'source2', 'sequence'] - + # Fields displayed in the search results table header self.header_fields = ['', 'ID', - 'Location'] - + 'Location'] + # Todo: Obsolete or rename this field self.type = 'Geno' - + self.query = ''' SELECT InbredSet.Name, InbredSet.Id @@ -272,10 +273,10 @@ class GenotypeDataSet(DataSet): GenoFreeze.InbredSetId = InbredSet.Id AND GenoFreeze.Name = "%s" ''' % self.db_conn.escape_string(self.name) - + def check_confidentiality(self): return geno_mrna_confidentiality(self) - + def get_trait_info(self, trait_list, species=None): for this_trait in trait_list: if not this_trait.haveinfo: @@ -295,16 +296,16 @@ class GenotypeDataSet(DataSet): trait_location_value = ord(str(this_trait.chr).upper()[0])*1000 + this_trait.mb this_trait.location_repr = 'Chr%s: %.4f' % (this_trait.chr, float(this_trait.mb) ) - this_trait.location_value = trait_location_value - - + this_trait.location_value = trait_location_value + + class MrnaAssayDataSet(DataSet): ''' An mRNA Assay is a quantitative assessment (assay) associated with an mRNA trait - + This used to be called ProbeSet, but that term only refers specifically to the Affymetrix platform and is far too specific. - + ''' DS_NAME_MAP['ProbeSet'] = 'MrnaAssayDataSet' @@ -346,7 +347,7 @@ class MrnaAssayDataSet(DataSet): 'Location', 'Mean Expr', 'Max LRS', - 'Max LRS Location'] + 'Max LRS Location'] # Todo: Obsolete or rename this field self.type = 'ProbeSet' @@ -360,12 +361,12 @@ class MrnaAssayDataSet(DataSet): ProbeFreeze.InbredSetId = InbredSet.Id AND ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND ProbeSetFreeze.Name = "%s" - ''' % g.db.escape_string(self.name) + ''' % escape(self.name) def check_confidentiality(self): return geno_mrna_confidentiality(self) - + def get_trait_info(self, trait_list=None, species=''): # Note: setting trait_list to [] is probably not a great idea. @@ -428,7 +429,7 @@ class MrnaAssayDataSet(DataSet): self.db_conn.escape_string(this_trait.name))) print("query is:", pf(query)) - + self.cursor.execute(query) result = self.cursor.fetchone() @@ -475,30 +476,30 @@ class MrnaAssayDataSet(DataSet): this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs this_trait.LRS_score_value = LRS_score_value = this_trait.lrs - this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) class TempDataSet(DataSet): '''Temporary user-generated data set''' - + def setup(self): self.search_fields = ['name', 'description'] - + self.display_fields = ['name', 'description'] - + self.header_fields = ['Name', 'Description'] - + self.type = 'Temp' - + # Need to double check later how these are used self.id = 1 self.fullname = 'Temporary Storage' self.shortname = 'Temp' - - + + def geno_mrna_confidentiality(ob): dataset_table = ob.type + "Freeze" print("dataset_table [%s]: %s" % (type(dataset_table), dataset_table)) @@ -517,4 +518,3 @@ def geno_mrna_confidentiality(ob): if confidential: # Allow confidential data later NoConfindetialDataForYouTodaySorry - \ No newline at end of file -- cgit v1.2.3 From 0e17939e123ec80c4da3f665004b08347aa9480b Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Tue, 4 Dec 2012 16:19:46 -0600 Subject: Began changing references to cursor/db_conn to use sqlalchemy Wrote function for phenotype author searches --- wqflask/base/data_set.py | 34 +++++++------ wqflask/base/webqtlTrait.py | 15 +++--- wqflask/wqflask/do_search.py | 101 ++++++++++++++++++++++++++++---------- wqflask/wqflask/search_results.py | 2 +- 4 files changed, 103 insertions(+), 49 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 34e5eaa1..cd9e810e 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -90,9 +90,7 @@ class DataSet(object): def get_group(self): - assert self.cursor - self.cursor.execute(self.query) - self.group, self.group_id = self.cursor.fetchone() + self.group, self.group_id = g.db.execute(self.query).fetchone() if self.group == 'BXD300': self.group = "BXD" #return group @@ -107,7 +105,7 @@ class DataSet(object): """ - query_args = tuple(self.db_conn.escape_string(x) for x in ( + query_args = tuple(escape(x) for x in ( (self.type + "Freeze"), str(webqtlConfig.PUBLICTHRESH), self.name, @@ -115,18 +113,22 @@ class DataSet(object): self.name)) print("query_args are:", query_args) - query = ''' - SELECT - Id, Name, FullName, ShortName - FROM - %s - WHERE - public > %s AND - (Name = "%s" OR FullName = "%s" OR ShortName = "%s") - ''' % (query_args) + print(""" + SELECT Id, Name, FullName, ShortName + FROM %s + WHERE public > %s AND + (Name = '%s' OR FullName = '%s' OR ShortName = '%s') + """ % (query_args)) + + self.id, self.name, self.fullname, self.shortname = g.db.execute(""" + SELECT Id, Name, FullName, ShortName + FROM %s + WHERE public > %s AND + (Name = '%s' OR FullName = '%s' OR ShortName = '%s') + """ % (query_args)).fetchone() - self.cursor.execute(query) - self.id, self.name, self.fullname, self.shortname = self.cursor.fetchone() + #self.cursor.execute(query) + #self.id, self.name, self.fullname, self.shortname = self.cursor.fetchone() #def genHTML(self, Class='c0dd'): @@ -185,7 +187,7 @@ class PhenotypeDataSet(DataSet): WHERE PublishFreeze.InbredSetId = InbredSet.Id AND PublishFreeze.Name = "%s" - ''' % self.db_conn.escape_string(self.name) + ''' % escape(self.name) def check_confidentiality(self): # (Urgently?) Need to write this diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index 1dceba08..9763e441 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -30,9 +30,9 @@ class webqtlTrait: self.cellid = kw.get('cellid', None) self.identification = kw.get('identification', 'un-named trait') self.group = kw.get('group', None) - self.haveinfo = kw.get(haveinfo, False) - self.sequence = kw.get(sequence, None) # Blat sequence, available for ProbeSet - self.data = kw.get(data, {}) + self.haveinfo = kw.get('haveinfo', False) + self.sequence = kw.get('sequence', None) # Blat sequence, available for ProbeSet + self.data = kw.get('data', {}) if kw.get('fullname'): name2 = value.split("::") @@ -381,7 +381,7 @@ class webqtlTrait: # return self.__dict__.items() def retrieveInfo(self, QTL = None): - assert self.dataset and self.cursor + assert self.dataset if self.dataset.type == 'Publish': #self.dataset.DisField = ['Name','PubMed_ID','Phenotype','Abbreviation','Authors','Title',\ # 'Abstract', 'Journal','Volume','Pages','Month','Year','Sequence',\ @@ -434,10 +434,11 @@ class webqtlTrait: Geno.Name = '%s' """ % (display_fields_string, self.dataset.name, self.name) else: #Temp type - query = 'SELECT %s FROM %s WHERE Name = "%s"' % \ - (string.join(self.dataset.display_fields,','), self.dataset.type, self.name) - + traitInfo = g.db.execute("""SELECT %s FROM %s WHERE Name = '%s' + """, (string.join(self.dataset.display_fields,','), + self.dataset.type, self.name)).fetchone() + self.cursor.execute(query) traitInfo = self.cursor.fetchone() if traitInfo: diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index bae3df08..802cbea5 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -3,6 +3,9 @@ from __future__ import print_function, division +from flask import Flask, g + +from MySQLdb import escape_string as escape from pprint import pformat as pf import sys @@ -34,13 +37,13 @@ class DoSearch(object): """Executes query and returns results""" query = self.normalize_spaces(query) print("in do_search query is:", pf(query)) - self.cursor.execute(query) + g.db.execute(query) results = self.cursor.fetchall() return results def escape(self, stringy): """Shorter name than self.db_conn.escape_string""" - return self.db_conn.escape_string(str(stringy)) + return escape(str(stringy)) def mescape(self, *items): """Multiple escape""" @@ -153,7 +156,7 @@ class PhenotypeSearch(DoSearch): 'Max LRS', 'Max LRS Location'] - def get_where_clause(self): + def get_fields_clause(self): """Generate clause for WHERE portion of query""" #Todo: Zach will figure out exactly what both these lines mean @@ -163,15 +166,17 @@ class PhenotypeSearch(DoSearch): # This adds a clause to the query that matches the search term # against each field in the search_fields tuple - where_clause = [] + fields_clause = [] for field in self.search_fields: - where_clause.append('''%s REGEXP "%s"''' % (field, search_term)) - where_clause = "(%s)" % ' OR '.join(where_clause) + fields_clause.append('''%s REGEXP "%s"''' % (field, search_term)) + fields_clause = "(%s)" % ' OR '.join(fields_clause) - return where_clause + return fields_clause - def run(self): - """Generates and runs a simple search of a phenotype dataset""" + def compile_final_query(self, from_clause = '', where_clause = ''): + """Generates the final query string""" + + from_clause = self.normalize_spaces(from_clause) #Get group information for dataset self.dataset.get_group() @@ -182,12 +187,42 @@ class PhenotypeSearch(DoSearch): PublishXRef.PhenotypeId = Phenotype.Id and PublishXRef.PublicationId = Publication.Id and PublishFreeze.Id = %s""" % ( - self.get_where_clause(), + self.get_fields_clause(), self.escape(self.dataset.group_id), self.escape(self.dataset.id))) - return self.execute(query) + print("query is:", pf(query)) + return query + + def run(self): + """Generates and runs a simple search of a phenotype dataset""" + + self.query = self.compile_final_query(where_clause = self.get_fields_clause()) + +# self.query = """SELECT PublishXRef.Id, +#PublishFreeze.createtime as thistable, +#Publication.PubMed_ID as Publication_PubMed_ID, +#Phenotype.Post_publication_description as Phenotype_Name FROM Phenotype, +#PublishFreeze, Publication, PublishXRef WHERE (Phenotype.Post_publication_description +#REGEXP "[[:<:]]brain[[:>:]]" OR Phenotype.Pre_publication_description REGEXP "[[:<:]]brain[[:>:]]" +#OR Phenotype.Pre_publication_abbreviation REGEXP "[[:<:]]brain[[:>:]]" +#OR Phenotype.Post_publication_abbreviation REGEXP "[[:<:]]brain[[:>:]]" +#OR Phenotype.Lab_code REGEXP "[[:<:]]brain[[:>:]]" +#OR Publication.PubMed_ID REGEXP "[[:<:]]brain[[:>:]]" +#OR Publication.Abstract REGEXP "[[:<:]]brain[[:>:]]" +#OR Publication.Title REGEXP "[[:<:]]brain[[:>:]]" +#OR Publication.Authors REGEXP "[[:<:]]brain[[:>:]]" +#OR PublishXRef.Id REGEXP "[[:<:]]brain[[:>:]]") +#and PublishXRef.InbredSetId = 1 +#and PublishXRef.PhenotypeId = Phenotype.Id +#and PublishXRef.PublicationId = Publication.Id +#and PublishFreeze.Id = 1;""" + + + results = g.db.execute(self.query, no_parameters=True).fetchall() + print("in [df] run results are:", results) + return results class GenotypeSearch(DoSearch): """A search within a genotype dataset""" @@ -606,6 +641,22 @@ class PvalueSearch(ProbeSetSearch): return self.execute(self.query) +class AuthorSearch(PhenotypeSearch): + """Searches for phenotype traits with specified author(s)""" + + DoSearch.search_types["NAME"] = "AuthorSearch" + + def run(self): + + self.search_term = [float(value) for value in self.search_term] + + self.where_clause = """ Publication.Authors LIKE %s and + """ % (self.escape(self.search_term[0])) + + self.query = self.compile_final_query(where_clause = self.where_clause) + + return self.execute(self.query) + if __name__ == "__main__": @@ -630,20 +681,20 @@ if __name__ == "__main__": dataset_name = "HC_M2_0606_P" dataset = create_dataset(db_conn, dataset_name) - cursor.execute(""" - SELECT ProbeSet.Name as TNAME, 0 as thistable, - ProbeSetXRef.Mean as TMEAN, ProbeSetXRef.LRS as TLRS, - ProbeSetXRef.PVALUE as TPVALUE, ProbeSet.Chr_num as TCHR_NUM, - ProbeSet.Mb as TMB, ProbeSet.Symbol as TSYMBOL, - ProbeSet.name_num as TNAME_NUM - FROM ProbeSetXRef, ProbeSet, Geno - WHERE ProbeSetXRef.LRS > 99.0 and - ABS(ProbeSet.Mb-Geno.Mb) < 5 and - ProbeSetXRef.Locus = Geno.name and - Geno.SpeciesId = 1 and - ProbeSet.Chr = Geno.Chr and - ProbeSet.Id = ProbeSetXRef.ProbeSetId and - ProbeSetXRef.ProbeSetFreezeId = 112""") + #cursor.execute(""" + # SELECT ProbeSet.Name as TNAME, 0 as thistable, + # ProbeSetXRef.Mean as TMEAN, ProbeSetXRef.LRS as TLRS, + # ProbeSetXRef.PVALUE as TPVALUE, ProbeSet.Chr_num as TCHR_NUM, + # ProbeSet.Mb as TMB, ProbeSet.Symbol as TSYMBOL, + # ProbeSet.name_num as TNAME_NUM + # FROM ProbeSetXRef, ProbeSet, Geno + # WHERE ProbeSetXRef.LRS > 99.0 and + # ABS(ProbeSet.Mb-Geno.Mb) < 5 and + # ProbeSetXRef.Locus = Geno.name and + # Geno.SpeciesId = 1 and + # ProbeSet.Chr = Geno.Chr and + # ProbeSet.Id = ProbeSetXRef.ProbeSetId and + # ProbeSetXRef.ProbeSetFreezeId = 112""") #print(pf(cursor.fetchall())) #results = ProbeSetSearch("shh", None, dataset, cursor, db_conn).run() diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 04b14e8f..52f628f6 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -113,7 +113,7 @@ class SearchResultPage(templatePage): print("foo locals are:", locals()) trait_id = result[0] - this_trait = webqtlTrait(self.db_conn, dataset=self.dataset, name=trait_id) + this_trait = webqtlTrait(dataset=self.dataset, name=trait_id) this_trait.retrieveInfo(QTL=True) self.trait_list.append(this_trait) -- cgit v1.2.3 From 01785471d63de156fa9787a0fb38c9df09824183 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Tue, 4 Dec 2012 18:08:09 -0600 Subject: Changed parser to allow quotes (i.e. name="rw williams") Renamed webqtlTrait to GeneralTrait and began rewriting parts Changed database code in many places to use simple sqlalchemy --- wqflask/base/data_set.py | 18 ++- wqflask/base/webqtlTrait.py | 166 +++++++++++++-------------- wqflask/dbFunction/webqtlDatabaseFunction.py | 26 ++--- wqflask/wqflask/do_search.py | 78 ++++++------- wqflask/wqflask/parser.py | 4 +- wqflask/wqflask/search_results.py | 7 +- 6 files changed, 138 insertions(+), 161 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index cd9e810e..7833f5c1 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -68,13 +68,10 @@ class DataSet(object): assert name self.name = name - #self.db_conn = db_conn - #self.cursor = self.db_conn.cursor() self.id = None self.type = None self.group = None - #if self.cursor and self.id == 0: self.setup() self.check_confidentiality() @@ -200,6 +197,7 @@ class PhenotypeDataSet(DataSet): description = this_trait.post_publication_description if this_trait.confidential: + continue # for now if not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=this_trait.authorized_users): description = this_trait.pre_publication_description this_trait.description_display = description @@ -217,13 +215,13 @@ class PhenotypeDataSet(DataSet): this_trait.LRS_location_value = 1000000 if this_trait.lrs: - self.cursor.execute(""" + result = g.db.execute(""" select Geno.Chr, Geno.Mb from Geno, Species - where Species.Name = '%s' and - Geno.Name = '%s' and + where Species.Name = %s and + Geno.Name = %s and Geno.SpeciesId = Species.Id - """ % (species, this_trait.locus)) - result = self.cursor.fetchone() + """, (species, this_trait.locus)).fetchone() + #result = self.cursor.fetchone() if result: if result[0] and result[1]: @@ -509,13 +507,13 @@ def geno_mrna_confidentiality(ob): query = '''SELECT Id, Name, FullName, confidentiality, AuthorisedUsers FROM %s WHERE Name = %%s''' % (dataset_table) - ob.cursor.execute(query, ob.name) + result = g.db.execute(query, ob.name) (dataset_id, name, full_name, confidential, - authorized_users) = ob.cursor.fetchall()[0] + authorized_users) = result.fetchall()[0] if confidential: # Allow confidential data later diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index 9763e441..dec5fa00 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -14,7 +14,7 @@ from pprint import pformat as pf from flask import Flask, g -class webqtlTrait: +class GeneralTrait: """ Trait class defines a trait in webqtl, can be either Microarray, Published phenotype, genotype, or user input trait @@ -22,9 +22,7 @@ class webqtlTrait: """ def __init__(self, **kw): - print("in webqtlTrait") - #self.db_conn = db_conn - #self.cursor = self.db_conn.cursor() + print("in GeneralTrait") self.dataset = kw.get('dataset', None) # database object self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. self.cellid = kw.get('cellid', None) @@ -41,45 +39,31 @@ class webqtlTrait: elif len(name2) == 3: self.dataset, self.name, self.cellid = name2 - #print("foo") - #print("kw in webqtlTrait are:", pf(kw)) - #print("printed\n\n") - #for name, value in kw.items(): - # if self.__dict__.has_key(name): - # setattr(self, name, value) - # elif name == 'fullname': - # name2 = value.split("::") - # if len(name2) == 2: - # self.dataset, self.name = name2 - # elif len(name2) == 3: - # self.dataset, self.name, self.cellid = name2 - # else: - # raise KeyError, repr(value) + ' parameter format error.' - # else: - # raise KeyError, repr(name) + ' not a valid parameter for this class.' - - if self.dataset and isinstance(self.dataset, basestring): - #assert self.cursor, "Don't have a cursor" - self.dataset = create_dataset(self.dataset) + #if self.dataset and isinstance(self.dataset, basestring): + self.dataset = create_dataset(self.dataset) + + - #if self.dataset == None, not from a database print("self.dataset is:", self.dataset, type(self.dataset)) - if self.dataset: - if self.dataset.type == "Temp": - self.cursor.execute(''' - SELECT - InbredSet.Name - FROM - InbredSet, Temp - WHERE - Temp.InbredSetId = InbredSet.Id AND - Temp.Name = "%s" - ''', self.name) - self.group = self.cursor.fetchone()[0] - else: - self.group = self.dataset.get_group() + #if self.dataset: + + self.dataset.get_group() + + if self.dataset.type == "Temp": + self.cursor.execute(''' + SELECT + InbredSet.Name + FROM + InbredSet, Temp + WHERE + Temp.InbredSetId = InbredSet.Id AND + Temp.Name = "%s" + ''', self.name) + self.group = self.cursor.fetchone()[0] + else: + self.group = self.dataset.get_group() - print("trinity, self.group is:", self.group) + print("trinity, self.group is:", self.group) # # In ProbeSet, there are maybe several annotations match one sequence @@ -93,24 +77,24 @@ class webqtlTrait: # The variable self.sequence should be changed to self.BlatSeq # It also should be changed in other places where it are used. - if self.dataset: - if self.dataset.type == 'ProbeSet': - print("Doing ProbeSet Query") - query = ''' - SELECT - ProbeSet.BlatSeq - FROM - ProbeSet, ProbeSetFreeze, ProbeSetXRef - WHERE - ProbeSet.Id=ProbeSetXRef.ProbeSetId and - ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and - ProbeSet.Name = %s and - ProbeSetFreeze.Name = %s - ''', (self.name, self.dataset.name) - print("query is:", query) - self.cursor.execute(*query) - self.sequence = self.cursor.fetchone()[0] - print("self.sequence is:", self.sequence) + #if self.dataset: + if self.dataset.type == 'ProbeSet': + print("Doing ProbeSet Query") + query = ''' + SELECT + ProbeSet.BlatSeq + FROM + ProbeSet, ProbeSetFreeze, ProbeSetXRef + WHERE + ProbeSet.Id=ProbeSetXRef.ProbeSetId and + ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and + ProbeSet.Name = %s and + ProbeSetFreeze.Name = %s + ''', (self.name, self.dataset.name) + print("query is:", query) + self.sequence = g.db.execute(*query).fetchone()[0] + #self.sequence = self.cursor.fetchone()[0] + print("self.sequence is:", self.sequence) def getName(self): @@ -380,13 +364,10 @@ class webqtlTrait: #def items(self): # return self.__dict__.items() - def retrieveInfo(self, QTL = None): - assert self.dataset + def retrieve_info(self, QTL=False): + assert self.dataset, "Dataset doesn't exist" if self.dataset.type == 'Publish': - #self.dataset.DisField = ['Name','PubMed_ID','Phenotype','Abbreviation','Authors','Title',\ - # 'Abstract', 'Journal','Volume','Pages','Month','Year','Sequence',\ - # 'Units', 'comments'] - query = ''' + traitInfo = g.db.execute(""" SELECT PublishXRef.Id, Publication.PubMed_ID, Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description, @@ -404,43 +385,50 @@ class webqtlTrait: Publication.Id = PublishXRef.PublicationId AND PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND PublishFreeze.Id =%s - ''' % (self.name, self.dataset.id) + """, (self.name, self.dataset.id)).fetchone() #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. elif self.dataset.type == 'ProbeSet': display_fields_string = ',ProbeSet.'.join(self.dataset.display_fields) display_fields_string = 'ProbeSet.' + display_fields_string - query = """ + traitInfo = g.db.execute(""" SELECT %s FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND - ProbeSetFreeze.Name = '%s' AND - ProbeSet.Name = '%s' - """ % (display_fields_string, self.dataset.name, self.name) + ProbeSetFreeze.Name = %s AND + ProbeSet.Name = %s + """, (display_fields_string, self.dataset.name, self.name)).fetchone() #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name # to avoid the problem of same marker name from different species. elif self.dataset.type == 'Geno': display_fields_string = string.join(self.dataset.display_fields,',Geno.') display_fields_string = 'Geno.' + display_fields_string - query = """ + traitInfo = g.db.execute(""" SELECT %s FROM Geno, GenoFreeze, GenoXRef WHERE GenoXRef.GenoFreezeId = GenoFreeze.Id AND GenoXRef.GenoId = Geno.Id AND - GenoFreeze.Name = '%s' AND - Geno.Name = '%s' - """ % (display_fields_string, self.dataset.name, self.name) + GenoFreeze.Name = %s AND + Geno.Name = %s + """, (display_fields_string, self.dataset.name, self.name)).fetchone() else: #Temp type - traitInfo = g.db.execute("""SELECT %s FROM %s WHERE Name = '%s' + traitInfo = g.db.execute("""SELECT %s FROM %s WHERE Name = %s """, (string.join(self.dataset.display_fields,','), self.dataset.type, self.name)).fetchone() + + query = """SELECT %s FROM %s WHERE Name = %s + """ % (string.join(self.dataset.display_fields,','), + self.dataset.type, self.name) + + print("query is:", pf(query)) + print("traitInfo is: ", pf(traitInfo)) - - self.cursor.execute(query) - traitInfo = self.cursor.fetchone() + + #self.cursor.execute(query) + #traitInfo = self.cursor.fetchone() if traitInfo: self.haveinfo = True @@ -465,7 +453,7 @@ class webqtlTrait: geneidIsNumber = 0 if geneidIsNumber: - query = """ + result = g.db.execute(""" SELECT HomologeneId FROM @@ -475,9 +463,9 @@ class webqtlTrait: InbredSet.Name = '%s' AND InbredSet.SpeciesId = Species.Id AND Species.TaxonomyId = Homologene.TaxonomyId - """ % (self.geneid, self.group) - self.cursor.execute(query) - result = self.cursor.fetchone() + """, (self.geneid, self.group)).fetchone() + #self.cursor.execute(query) + #result = self.cursor.fetchone() else: result = None @@ -486,7 +474,7 @@ class webqtlTrait: if QTL: if self.dataset.type == 'ProbeSet' and not self.cellid: - query = ''' + traitQTL = g.db.execute(""" SELECT ProbeSetXRef.Locus, ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.mean FROM @@ -495,15 +483,15 @@ class webqtlTrait: ProbeSetXRef.ProbeSetId = ProbeSet.Id AND ProbeSet.Name = "%s" AND ProbeSetXRef.ProbeSetFreezeId =%s - ''' % (self.name, self.dataset.id) - self.cursor.execute(query) - traitQTL = self.cursor.fetchone() + """, (self.name, self.dataset.id)).fetchone() + #self.cursor.execute(query) + #traitQTL = self.cursor.fetchone() if traitQTL: self.locus, self.lrs, self.pvalue, self.mean = traitQTL else: self.locus = self.lrs = self.pvalue = self.mean = "" if self.dataset.type == 'Publish': - query = ''' + traitQTL = g.db.execute(""" SELECT PublishXRef.Locus, PublishXRef.LRS FROM @@ -512,9 +500,9 @@ class webqtlTrait: PublishXRef.Id = %s AND PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND PublishFreeze.Id =%s - ''' % (self.name, self.dataset.id) - self.cursor.execute(query) - traitQTL = self.cursor.fetchone() + """, (self.name, self.dataset.id)).fetchone() + #self.cursor.execute(query) + #traitQTL = self.cursor.fetchone() if traitQTL: self.locus, self.lrs = traitQTL else: diff --git a/wqflask/dbFunction/webqtlDatabaseFunction.py b/wqflask/dbFunction/webqtlDatabaseFunction.py index 1e028ecc..299114b4 100755 --- a/wqflask/dbFunction/webqtlDatabaseFunction.py +++ b/wqflask/dbFunction/webqtlDatabaseFunction.py @@ -21,6 +21,8 @@ # This module is used by GeneNetwork project (www.genenetwork.org) +from flask import Flask, g + import MySQLdb import string from base import webqtlConfig @@ -80,25 +82,15 @@ def getAllSpecies(cursor=None): #function: retrieve specie's name info based on RISet ########################################################################### -def retrieveSpecies(cursor=None, group=None): - try: - cursor.execute("select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % group) - return cursor.fetchone()[0] - except: - return None +def retrieve_species(group): + return g.db.execute("""select Species.Name + from Species, InbredSet + where InbredSet.Name = %s and + InbredSet.SpeciesId = Species.Id""", (group)).fetchone()[0] -########################################################################### -#input: cursor, RISet (string) -#output: specie's Id (string), value will be None or else -#function: retrieve specie's Id info based on RISet -########################################################################### +def retrieve_species_id(group): + return g.db.execute("select SpeciesId from InbredSet where Name = %s", (group)).fetchone()[0] -def retrieveSpeciesId(cursor=None, RISet=None): - try: - cursor.execute("select SpeciesId from InbredSet where Name = '%s'" % RISet) - return cursor.fetchone()[0] - except: - return None ########################################################################### # input: cursor diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 802cbea5..2094ed14 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -20,34 +20,32 @@ class DoSearch(object): # Used to translate search phrases into classes search_types = dict() - def __init__(self, search_term, search_operator, dataset, cursor, db_conn): + def __init__(self, search_term, search_operator, dataset): self.search_term = search_term # Make sure search_operator is something we expect assert search_operator in (None, "=", "<", ">", "<=", ">="), "Bad search operator" self.search_operator = search_operator self.dataset = dataset - self.db_conn = db_conn - self.cursor = cursor #Get group information for dataset and the species id self.dataset.get_group() - self.species_id = webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.dataset.group) + self.species_id = webqtlDatabaseFunction.retrieve_species_id(self.dataset.group) def execute(self, query): """Executes query and returns results""" query = self.normalize_spaces(query) print("in do_search query is:", pf(query)) - g.db.execute(query) - results = self.cursor.fetchall() + results = g.db.execute(query).fetchall() + #results = self.cursor.fetchall() return results - def escape(self, stringy): - """Shorter name than self.db_conn.escape_string""" - return escape(str(stringy)) + #def escape(self, stringy): + # """Shorter name than self.db_conn.escape_string""" + # return escape(str(stringy)) def mescape(self, *items): """Multiple escape""" - escaped = [self.escape(item) for item in items] + escaped = [escape(item) for item in items] print("escaped is:", escaped) return tuple(escaped) @@ -96,9 +94,9 @@ class ProbeSetSearch(DoSearch): WHERE %s and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSetXRef.ProbeSetFreezeId = %s - """ % (self.escape(from_clause), + """ % (escape(from_clause), where_clause, - self.escape(self.dataset.id))) + escape(self.dataset.id))) print("query is:", pf(query)) @@ -118,8 +116,8 @@ class ProbeSetSearch(DoSearch): AGAINST ('%s' IN BOOLEAN MODE)) and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSetXRef.ProbeSetFreezeId = %s - """ % (self.escape(self.search_term[0]), - self.escape(self.dataset.id)) + """ % (escape(self.search_term[0]), + escape(str(self.dataset.id))) print("final query is:", pf(query)) @@ -182,14 +180,16 @@ class PhenotypeSearch(DoSearch): self.dataset.get_group() query = (self.base_query + - """WHERE %s and + """%s + WHERE %s PublishXRef.InbredSetId = %s and PublishXRef.PhenotypeId = Phenotype.Id and PublishXRef.PublicationId = Publication.Id and PublishFreeze.Id = %s""" % ( - self.get_fields_clause(), - self.escape(self.dataset.group_id), - self.escape(self.dataset.id))) + from_clause, + where_clause, + escape(str(self.dataset.group_id)), + escape(str(self.dataset.id)))) print("query is:", pf(query)) @@ -272,7 +272,7 @@ class GenotypeSearch(DoSearch): Geno.Id = GenoXRef.GenoId and GenoXRef.GenoFreezeId = GenoFreeze.Id and GenoFreeze.Id = %s"""% (where_clause, - self.escape(self.dataset.id))) + escape(self.dataset.id))) print("query is:", pf(query)) @@ -332,7 +332,7 @@ class GoSearch(ProbeSetSearch): statements = ("""%s.symbol=GOgene_product.symbol and GOassociation.gene_product_id=GOgene_product.id and GOterm.id=GOassociation.term_id""" % ( - self.db_conn.escape_string(self.dataset.type))) + escape(self.dataset.type))) where_clause = " %s = '%s' and %s " % (field, go_id, statements) @@ -377,7 +377,7 @@ class LrsSearch(ProbeSetSearch): if len(self.search_term) > 2: self.chr_num = self.search_term[2] - self.sub_clause += """ Geno.Chr = %s and """ % (self.escape(self.chr_num)) + self.sub_clause += """ Geno.Chr = %s and """ % (escape(self.chr_num)) if len(self.search_term) == 5: self.mb_low, self.mb_high = self.search_term[3:] self.sub_clause += """ Geno.Mb > %s and @@ -429,17 +429,17 @@ class CisTransLrsSearch(LrsSearch): self.sub_clause = """ %sXRef.LRS > %s and %sXRef.LRS < %s and """ % ( - self.escape(self.dataset.type), - self.escape(min(self.lrs_min, self.lrs_max)), - self.escape(self.dataset.type), - self.escape(max(self.lrs_min, self.lrs_max)) + escape(self.dataset.type), + escape(min(self.lrs_min, self.lrs_max)), + escape(self.dataset.type), + escape(max(self.lrs_min, self.lrs_max)) ) else: # Deal with >, <, >=, and <= self.sub_clause = """ %sXRef.LRS %s %s and """ % ( - self.escape(self.dataset.type), - self.escape(self.search_operator), - self.escape(self.search_term[0]) + escape(self.dataset.type), + escape(self.search_operator), + escape(self.search_term[0]) ) self.where_clause = self.sub_clause + """ @@ -447,12 +447,12 @@ class CisTransLrsSearch(LrsSearch): %sXRef.Locus = Geno.name and Geno.SpeciesId = %s and %s.Chr = Geno.Chr""" % ( - self.escape(self.dataset.type), + escape(self.dataset.type), the_operator, - self.escape(self.mb_buffer), - self.escape(self.dataset.type), - self.escape(self.species_id), - self.escape(self.dataset.type) + escape(self.mb_buffer), + escape(self.dataset.type), + escape(self.species_id), + escape(self.dataset.type) ) print("where_clause is:", pf(self.where_clause)) @@ -559,7 +559,7 @@ class RangeSearch(ProbeSetSearch): self.where_clause = """ (SELECT Pow(2, max(value) -min(value)) FROM ProbeSetData WHERE ProbeSetData.Id = ProbeSetXRef.dataId) > %s - """ % (self.escape(self.search_term[0])) + """ % (escape(self.search_term[0])) print("where_clause is:", pf(self.where_clause)) @@ -647,16 +647,14 @@ class AuthorSearch(PhenotypeSearch): DoSearch.search_types["NAME"] = "AuthorSearch" def run(self): - - self.search_term = [float(value) for value in self.search_term] - - self.where_clause = """ Publication.Authors LIKE %s and - """ % (self.escape(self.search_term[0])) + + self.where_clause = """ Publication.Authors REGEXP "[[:<:]]%s[[:>:]]" and + """ % (self.search_term[0]) self.query = self.compile_final_query(where_clause = self.where_clause) return self.execute(self.query) - + if __name__ == "__main__": diff --git a/wqflask/wqflask/parser.py b/wqflask/wqflask/parser.py index efe479e6..f991d8c7 100644 --- a/wqflask/wqflask/parser.py +++ b/wqflask/wqflask/parser.py @@ -28,7 +28,7 @@ def parse(pstring): returned item serach_term is always a list, even if only one element """ - pstring = re.split(r"""(?:(\w+\s*=\s*[\(\[][^)]*[\)\]]) | # LRS=(1 2 3), cisLRS=[4 5 6], etc + pstring = re.split(r"""(?:(\w+\s*=\s*[\('"\[][^)'"]*[\)\]'"]) | # LRS=(1 2 3), cisLRS=[4 5 6], etc (\w+\s*[=:\>\<][\w\*]+) | # wiki=bar, GO:foobar, etc ([\w\*]+)) # shh, brain, etc """, pstring, flags=re.VERBOSE) @@ -78,6 +78,8 @@ if __name__ == '__main__': parse("WIKI=ho*") parse("LRS>9") parse("LRS>=18") + parse("NAME='rw williams'") + parse('NAME="rw williams"') parse("foo <= 2") parse("cisLRS<20") parse("foo=[3 2 1)") diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 52f628f6..efa1c5cc 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -100,7 +100,7 @@ class SearchResultPage(templatePage): self.trait_list = [] group = self.dataset.group - species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=group) + species = webqtlDatabaseFunction.retrieve_species(group=group) # result_set represents the results for each search term; a search of # "shh grin2b" would have two sets of results, one for each term @@ -114,7 +114,7 @@ class SearchResultPage(templatePage): print("foo locals are:", locals()) trait_id = result[0] this_trait = webqtlTrait(dataset=self.dataset, name=trait_id) - this_trait.retrieveInfo(QTL=True) + this_trait.retrieve_info(QTL=True) self.trait_list.append(this_trait) self.dataset.get_trait_info(self.trait_list, species) @@ -147,8 +147,7 @@ class SearchResultPage(templatePage): the_search = search_class(search_term, search_operator, self.dataset, - self.cursor, - self.db_conn) + ) self.results.extend(the_search.run()) print("in the search results are:", self.results) -- cgit v1.2.3 From 292d177f768e8f949bc50f8896b560879aaae178 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Wed, 5 Dec 2012 14:33:02 -0600 Subject: Continued to make changes related to getting rid of cursor/db_conn and using simple sqlalchemy Got Pheno/MrnaAssay dataset searches working again --- misc/new_variable_names.txt | 1 + wqflask/base/data_set.py | 9 ++--- wqflask/base/webqtlTrait.py | 46 +++++++++++----------- wqflask/wqflask/correlation/CorrelationPage.py | 2 +- wqflask/wqflask/correlation/correlationFunction.py | 2 +- wqflask/wqflask/do_search.py | 10 ++--- wqflask/wqflask/search_results.py | 4 +- wqflask/wqflask/show_trait/SampleList.py | 2 +- wqflask/wqflask/show_trait/show_trait.py | 2 +- 9 files changed, 38 insertions(+), 40 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/misc/new_variable_names.txt b/misc/new_variable_names.txt index 2b10c07e..c11c160e 100644 --- a/misc/new_variable_names.txt +++ b/misc/new_variable_names.txt @@ -3,3 +3,4 @@ webqtlDataset.py -> data_set.py webqtlDataset (class object) -> DataSet database/db -> dataset/data_set DataEditingPage -> show_trait.py/show_trait.html +webqtlTrait -> GeneralTrait \ No newline at end of file diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 7833f5c1..70b33014 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -272,7 +272,7 @@ class GenotypeDataSet(DataSet): WHERE GenoFreeze.InbredSetId = InbredSet.Id AND GenoFreeze.Name = "%s" - ''' % self.db_conn.escape_string(self.name) + ''' % escape(self.name) def check_confidentiality(self): return geno_mrna_confidentiality(self) @@ -425,13 +425,12 @@ class MrnaAssayDataSet(DataSet): where ProbeSetXRef.ProbeSetFreezeId = %s and ProbeSet.Id = ProbeSetXRef.ProbeSetId and ProbeSet.Name = '%s' - """ % (self.db_conn.escape_string(str(this_trait.dataset.id)), - self.db_conn.escape_string(this_trait.name))) + """ % (escape(str(this_trait.dataset.id)), + escape(this_trait.name))) print("query is:", pf(query)) - self.cursor.execute(query) - result = self.cursor.fetchone() + result = g.db.execute(query).fetchone() if result: if result[0]: diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py index dec5fa00..5367b41f 100755 --- a/wqflask/base/webqtlTrait.py +++ b/wqflask/base/webqtlTrait.py @@ -10,6 +10,7 @@ from data_set import create_dataset from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil +from MySQLdb import escape_string as escape from pprint import pformat as pf from flask import Flask, g @@ -40,9 +41,7 @@ class GeneralTrait: self.dataset, self.name, self.cellid = name2 #if self.dataset and isinstance(self.dataset, basestring): - self.dataset = create_dataset(self.dataset) - - + self.dataset = create_dataset(self.dataset.name) print("self.dataset is:", self.dataset, type(self.dataset)) #if self.dataset: @@ -367,7 +366,7 @@ class GeneralTrait: def retrieve_info(self, QTL=False): assert self.dataset, "Dataset doesn't exist" if self.dataset.type == 'Publish': - traitInfo = g.db.execute(""" + query = """ SELECT PublishXRef.Id, Publication.PubMed_ID, Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description, @@ -384,47 +383,46 @@ class GeneralTrait: Phenotype.Id = PublishXRef.PhenotypeId AND Publication.Id = PublishXRef.PublicationId AND PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND - PublishFreeze.Id =%s - """, (self.name, self.dataset.id)).fetchone() + PublishFreeze.Id = %s + """ % (self.name, self.dataset.id) + traitInfo = g.db.execute(query).fetchone() #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. elif self.dataset.type == 'ProbeSet': - display_fields_string = ',ProbeSet.'.join(self.dataset.display_fields) + display_fields_string = ', ProbeSet.'.join(self.dataset.display_fields) display_fields_string = 'ProbeSet.' + display_fields_string - traitInfo = g.db.execute(""" + query = """ SELECT %s FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef WHERE ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND - ProbeSetFreeze.Name = %s AND - ProbeSet.Name = %s - """, (display_fields_string, self.dataset.name, self.name)).fetchone() + ProbeSetFreeze.Name = '%s' AND + ProbeSet.Name = '%s' + """ % (display_fields_string, self.dataset.name, self.name) + traitInfo = g.db.execute(query).fetchone() + print("traitInfo is: ", pf(traitInfo)) #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name # to avoid the problem of same marker name from different species. elif self.dataset.type == 'Geno': display_fields_string = string.join(self.dataset.display_fields,',Geno.') display_fields_string = 'Geno.' + display_fields_string - traitInfo = g.db.execute(""" + query = """ SELECT %s FROM Geno, GenoFreeze, GenoXRef WHERE GenoXRef.GenoFreezeId = GenoFreeze.Id AND GenoXRef.GenoId = Geno.Id AND - GenoFreeze.Name = %s AND - Geno.Name = %s - """, (display_fields_string, self.dataset.name, self.name)).fetchone() + GenoFreeze.Name = '%s' AND + Geno.Name = '%s' + """ % (display_fields_string, self.dataset.name, self.name) + traitInfo = g.db.execute(query).fetchone() + print("traitInfo is: ", pf(traitInfo)) else: #Temp type - traitInfo = g.db.execute("""SELECT %s FROM %s WHERE Name = %s - """, (string.join(self.dataset.display_fields,','), - self.dataset.type, self.name)).fetchone() - - query = """SELECT %s FROM %s WHERE Name = %s + query = """SELECT %s FROM %s WHERE Name = %s """ % (string.join(self.dataset.display_fields,','), - self.dataset.type, self.name) - - print("query is:", pf(query)) - print("traitInfo is: ", pf(traitInfo)) + self.dataset.type, self.name) + traitInfo = g.db.execute(query).fetchone() #self.cursor.execute(query) diff --git a/wqflask/wqflask/correlation/CorrelationPage.py b/wqflask/wqflask/correlation/CorrelationPage.py index 8af30d1e..f1dd96ef 100644 --- a/wqflask/wqflask/correlation/CorrelationPage.py +++ b/wqflask/wqflask/correlation/CorrelationPage.py @@ -46,7 +46,7 @@ import reaper from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell -from base.webqtlTrait import webqtlTrait +from base.webqtlTrait import GeneralTrait from base.data_set import create_dataset from base.templatePage import templatePage from utility import webqtlUtil diff --git a/wqflask/wqflask/correlation/correlationFunction.py b/wqflask/wqflask/correlation/correlationFunction.py index 4d62a468..8638cb1e 100644 --- a/wqflask/wqflask/correlation/correlationFunction.py +++ b/wqflask/wqflask/correlation/correlationFunction.py @@ -31,7 +31,7 @@ import pp import string from utility import webqtlUtil -from base.webqtlTrait import webqtlTrait +from base.webqtlTrait import GeneralTrait from dbFunction import webqtlDatabaseFunction diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 2094ed14..4301fb50 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -35,7 +35,7 @@ class DoSearch(object): """Executes query and returns results""" query = self.normalize_spaces(query) print("in do_search query is:", pf(query)) - results = g.db.execute(query).fetchall() + results = g.db.execute(query, no_parameters=True).fetchall() #results = self.cursor.fetchall() return results @@ -167,7 +167,7 @@ class PhenotypeSearch(DoSearch): fields_clause = [] for field in self.search_fields: fields_clause.append('''%s REGEXP "%s"''' % (field, search_term)) - fields_clause = "(%s)" % ' OR '.join(fields_clause) + fields_clause = "(%s) and " % ' OR '.join(fields_clause) return fields_clause @@ -198,7 +198,7 @@ class PhenotypeSearch(DoSearch): def run(self): """Generates and runs a simple search of a phenotype dataset""" - self.query = self.compile_final_query(where_clause = self.get_fields_clause()) + query = self.compile_final_query(where_clause = self.get_fields_clause()) # self.query = """SELECT PublishXRef.Id, #PublishFreeze.createtime as thistable, @@ -220,7 +220,7 @@ class PhenotypeSearch(DoSearch): #and PublishFreeze.Id = 1;""" - results = g.db.execute(self.query, no_parameters=True).fetchall() + results = self.execute(query) print("in [df] run results are:", results) return results @@ -272,7 +272,7 @@ class GenotypeSearch(DoSearch): Geno.Id = GenoXRef.GenoId and GenoXRef.GenoFreezeId = GenoFreeze.Id and GenoFreeze.Id = %s"""% (where_clause, - escape(self.dataset.id))) + escape(str(self.dataset.id)))) print("query is:", pf(query)) diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index efa1c5cc..cd478110 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -30,7 +30,7 @@ from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell from base.data_set import create_dataset -from base.webqtlTrait import webqtlTrait +from base.webqtlTrait import GeneralTrait from base.templatePage import templatePage from wqflask import parser from wqflask import do_search @@ -113,7 +113,7 @@ class SearchResultPage(templatePage): print("foo locals are:", locals()) trait_id = result[0] - this_trait = webqtlTrait(dataset=self.dataset, name=trait_id) + this_trait = GeneralTrait(dataset=self.dataset, name=trait_id) this_trait.retrieve_info(QTL=True) self.trait_list.append(this_trait) diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index df0dc61e..25877521 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division from base import webqtlCaseData from utility import webqtlUtil, Plot, Bunch -from base.webqtlTrait import webqtlTrait +from base.webqtlTrait import GeneralTrait from pprint import pformat as pf diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 7060f2ea..aef9219f 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -13,7 +13,7 @@ from base import webqtlConfig from base import webqtlCaseData from wqflask.show_trait.SampleList import SampleList from utility import webqtlUtil, Plot, Bunch -from base.webqtlTrait import webqtlTrait +from base.webqtlTrait import GeneralTrait from dbFunction import webqtlDatabaseFunction from base.templatePage import templatePage from basicStatistics import BasicStatisticsFunctions -- cgit v1.2.3 From a7cc1119ebfbfab3ba5260be75c87cd4496f09b7 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Wed, 5 Dec 2012 18:03:23 -0600 Subject: Renamed webqtlTrait.py to trait.py Renamed webqtlTrait class to GeneralTrait Began process of removing fd from show_trait.py Created DatasetGroup object in data_set.py (this may end up becoming its own file later if it becomes big enough) --- misc/notes.txt | 10 +- misc/todo.txt | 4 +- wqflask/base/data_set.py | 221 +++++++++- wqflask/base/trait.py | 708 +++++++++++++++++++++++++++++++ wqflask/base/webqtlTrait.py | 695 ------------------------------ wqflask/wqflask/do_search.py | 5 +- wqflask/wqflask/search_results.py | 5 +- wqflask/wqflask/show_trait/show_trait.py | 295 ++++++++----- 8 files changed, 1129 insertions(+), 814 deletions(-) create mode 100755 wqflask/base/trait.py delete mode 100755 wqflask/base/webqtlTrait.py (limited to 'wqflask/base/data_set.py') diff --git a/misc/notes.txt b/misc/notes.txt index 59ab79cb..b0c0762c 100644 --- a/misc/notes.txt +++ b/misc/notes.txt @@ -14,6 +14,9 @@ export TERM=screen To search for commands in history if necessary: history | grep "(whatever is being searched for)" +Run web server: +/usr/local/nginx/sbin/nginx + Run server: python runserver.py @@ -63,11 +66,16 @@ Classes should always inherit "object" htop: Gives information on processes, cpu/memory load, etc dstat: Also gives various system information, resource usage, etc df: Reports file system disk space usage - +d =========================================== tidyp - Improves/beautifies html code tidyp -m -i -w 100 index_page.html +=========================================== + +ps -ax - View processes + +kill (process #) diff --git a/misc/todo.txt b/misc/todo.txt index 609e053f..60655a71 100644 --- a/misc/todo.txt +++ b/misc/todo.txt @@ -1 +1,3 @@ -- Read about grep/locate/find \ No newline at end of file +- Check about using trait id instead of trait name in queries in data_set.py + +- Ask Rob about Probe/cellid traits \ No newline at end of file diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 70b33014..68f5e5ed 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -21,12 +21,16 @@ # This module is used by GeneNetwork project (www.genenetwork.org) from __future__ import print_function, division +import os from flask import Flask, g from htmlgen import HTMLgen2 as HT +import reaper + import webqtlConfig +from utility import webqtlUtil from MySQLdb import escape_string as escape from pprint import pformat as pf @@ -57,6 +61,74 @@ def create_dataset(dataset_name): return dataset_class(dataset_name) +class DatasetGroup(object): + """ + Each group has multiple datasets; each species has multiple groups. + + For example, Mouse has multiple groups (BXD, BXA, etc), and each group + has multiple datasets associated with it. + + """ + def __init__(self, dataset): + """This sets self.group and self.group_id""" + self.name, self.group_id = g.db.execute(dataset.query).fetchone() + if self.name == 'BXD300': + self.name = "BXD" + + self.incparentsf1 = False + + + #def read_genotype(self): + # self.read_genotype_file() + # + # if not self.genotype: # Didn'd succeed, so we try method 2 + # self.read_genotype_data() + + def read_genotype_file(self): + '''read genotype from .geno file instead of database''' + #if self.group == 'BXD300': + # self.group = 'BXD' + # + #assert self.group, "self.group needs to be set" + + #genotype_1 is Dataset Object without parents and f1 + #genotype_2 is Dataset Object with parents and f1 (not for intercross) + + self.genotype_1 = reaper.Dataset() + + # reaper barfs on unicode filenames, so here we ensure it's a string + full_filename = str(os.path.join(webqtlConfig.GENODIR, self.name + '.geno')) + self.genotype_1.read(full_filename) + + print("Got to after read") + + try: + # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py; + _f1, _f12, _mat, _pat = webqtlUtil.ParInfo[self.name] + except KeyError: + _f1 = _f12 = _mat = _pat = None + + self.genotype_2 = self.genotype_1 + if self.genotype_1.type == "group" and _mat and _pat: + self.genotype_2 = self.genotype_1.add(Mat=_mat, Pat=_pat) #, F1=_f1) + + #determine default genotype object + if self.incparentsf1 and self.genotype_1.type != "intercross": + self.genotype = self.genotype_2 + else: + self.incparentsf1 = 0 + self.genotype = self.genotype_1 + + self.samplelist = list(self.genotype.prgy) + self.f1list = [] + self.parlist = [] + + if _f1 and _f12: + self.f1list = [_f1, _f12] + if _mat and _pat: + self.parlist = [_mat, _pat] + + class DataSet(object): """ DataSet class defines a dataset in webqtl, can be either Microarray, @@ -70,27 +142,35 @@ class DataSet(object): self.name = name self.id = None self.type = None - self.group = None self.setup() self.check_confidentiality() self.retrieve_name() - self.get_group() + self.group = DatasetGroup(self) # sets self.group and self.group_id + + + def get_desc(self): + """Gets overridden later, at least for Temp...used by trait's get_given_name""" + return None # Delete this eventually @property def riset(): Weve_Renamed_This_As_Group + + + #@property + #def group(self): + # if not self._group: + # self.get_group() + # + # return self._group + - def get_group(self): - self.group, self.group_id = g.db.execute(self.query).fetchone() - if self.group == 'BXD300': - self.group = "BXD" - #return group def retrieve_name(self): @@ -176,7 +256,7 @@ class PhenotypeDataSet(DataSet): self.type = 'Publish' - self.query = ''' + self.query_for_group = ''' SELECT InbredSet.Name, InbredSet.Id FROM @@ -239,7 +319,29 @@ class PhenotypeDataSet(DataSet): this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs this_trait.LRS_score_value = LRS_score_value = this_trait.lrs - this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb)) + + def retrieve_sample_data(self, trait): + query = """ + SELECT + Strain.Name, PublishData.value, PublishSE.error, NStrain.count, PublishData.Id + FROM + (PublishData, Strain, PublishXRef, PublishFreeze) + left join PublishSE on + (PublishSE.DataId = PublishData.Id AND PublishSE.StrainId = PublishData.StrainId) + left join NStrain on + (NStrain.DataId = PublishData.Id AND + NStrain.StrainId = PublishData.StrainId) + WHERE + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = %s AND + PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id + Order BY + Strain.Name + """ % (self.trait.name, self.id) + results = g.db.execute(query).fetchall() + return results + class GenotypeDataSet(DataSet): DS_NAME_MAP['Geno'] = 'GenotypeDataSet' @@ -297,6 +399,26 @@ class GenotypeDataSet(DataSet): this_trait.location_repr = 'Chr%s: %.4f' % (this_trait.chr, float(this_trait.mb) ) this_trait.location_value = trait_location_value + + def retrieve_sample_data(self, trait): + query = """ + SELECT + Strain.Name, GenoData.value, GenoSE.error, GenoData.Id + FROM + (GenoData, GenoFreeze, Strain, Geno, GenoXRef) + left join GenoSE on + (GenoSE.DataId = GenoData.Id AND GenoSE.StrainId = GenoData.StrainId) + WHERE + Geno.SpeciesId = %s AND Geno.Name = '%s' AND GenoXRef.GenoId = Geno.Id AND + GenoXRef.GenoFreezeId = GenoFreeze.Id AND + GenoFreeze.Name = '%s' AND + GenoXRef.DataId = GenoData.Id AND + GenoData.StrainId = Strain.Id + Order BY + Strain.Name + """ % (webqtlDatabaseFunction.retrieve_species_id(self.group), trait.name, self.name) + results = g.db.execute(query).fetchall() + return results class MrnaAssayDataSet(DataSet): @@ -476,6 +598,42 @@ class MrnaAssayDataSet(DataSet): this_trait.LRS_score_repr = LRS_score_repr = '%3.1f' % this_trait.lrs this_trait.LRS_score_value = LRS_score_value = this_trait.lrs this_trait.LRS_location_repr = LRS_location_repr = 'Chr %s: %.4f Mb' % (LRS_Chr, float(LRS_Mb) ) + + def get_sequence(self): + query = """ + SELECT + ProbeSet.BlatSeq + FROM + ProbeSet, ProbeSetFreeze, ProbeSetXRef + WHERE + ProbeSet.Id=ProbeSetXRef.ProbeSetId and + ProbeSetFreeze.Id = ProbeSetXRef.ProbSetFreezeId and + ProbeSet.Name = %s + ProbeSetFreeze.Name = %s + """ % (escape(self.name), escape(self.dataset.name)) + results = g.db.execute(query).fetchone() + + return results[0] + + def retrieve_sample_data(self, trait): + query = """ + SELECT + Strain.Name, ProbeSetData.value, ProbeSetSE.error, ProbeSetData.Id + FROM + (ProbeSetData, ProbeSetFreeze, Strain, ProbeSet, ProbeSetXRef) + left join ProbeSetSE on + (ProbeSetSE.DataId = ProbeSetData.Id AND ProbeSetSE.StrainId = ProbeSetData.StrainId) + WHERE + ProbeSet.Name = '%s' AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND + ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND + ProbeSetFreeze.Name = '%s' AND + ProbeSetXRef.DataId = ProbeSetData.Id AND + ProbeSetData.StrainId = Strain.Id + Order BY + Strain.Name + """ % (escape(trait.name), escape(self.name)) + results = g.db.execute(query).fetchall() + return results class TempDataSet(DataSet): @@ -497,6 +655,51 @@ class TempDataSet(DataSet): self.id = 1 self.fullname = 'Temporary Storage' self.shortname = 'Temp' + + + @staticmethod + def handle_pca(desc): + if 'PCA' in desc: + # Todo: Modernize below lines + desc = desc[desc.rindex(':')+1:].strip() + else: + desc = desc[:desc.index('entered')].strip() + return desc + + def get_desc(self): + g.db.execute('SELECT description FROM Temp WHERE Name=%s', self.name) + desc = g.db.fetchone()[0] + desc = self.handle_pca(desc) + return desc + + def get_group(self): + self.cursor.execute(""" + SELECT + InbredSet.Name, InbredSet.Id + FROM + InbredSet, Temp + WHERE + Temp.InbredSetId = InbredSet.Id AND + Temp.Name = "%s" + """, self.name) + self.group, self.group_id = self.cursor.fetchone() + #return self.group + + def retrieve_sample_data(self, trait): + query = """ + SELECT + Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id + FROM + TempData, Temp, Strain + WHERE + TempData.StrainId = Strain.Id AND + TempData.Id = Temp.DataId AND + Temp.name = '%s' + Order BY + Strain.Name + """ % escape(trait.name) + + results = g.db.execute(query).fetchall() def geno_mrna_confidentiality(ob): diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py new file mode 100755 index 00000000..d3753fc1 --- /dev/null +++ b/wqflask/base/trait.py @@ -0,0 +1,708 @@ +from __future__ import division, print_function + +import string + +from htmlgen import HTMLgen2 as HT + +import webqtlConfig +from webqtlCaseData import webqtlCaseData +from data_set import create_dataset +from dbFunction import webqtlDatabaseFunction +from utility import webqtlUtil + +from MySQLdb import escape_string as escape +from pprint import pformat as pf + +from flask import Flask, g + +class GeneralTrait: + """ + Trait class defines a trait in webqtl, can be either Microarray, + Published phenotype, genotype, or user input trait + + """ + + def __init__(self, **kw): + print("in GeneralTrait") + self.dataset = kw.get('dataset', None) # database object + self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. + self.cellid = kw.get('cellid', None) + self.identification = kw.get('identification', 'un-named trait') + #self.group = kw.get('group', None) + self.haveinfo = kw.get('haveinfo', False) + self.sequence = kw.get('sequence', None) # Blat sequence, available for ProbeSet + self.data = kw.get('data', {}) + + if kw.get('fullname'): + name2 = value.split("::") + if len(name2) == 2: + self.dataset, self.name = name2 + elif len(name2) == 3: + self.dataset, self.name, self.cellid = name2 + + #if self.dataset and isinstance(self.dataset, basestring): + self.dataset = create_dataset(self.dataset) + + print("self.dataset is:", self.dataset, type(self.dataset)) + #if self.dataset: + + #self.dataset.get_group() + + #if self.dataset.type == "Temp": + # self.cursor.execute(''' + # SELECT + # InbredSet.Name + # FROM + # InbredSet, Temp + # WHERE + # Temp.InbredSetId = InbredSet.Id AND + # Temp.Name = "%s" + # ''', self.name) + # self.group = self.cursor.fetchone()[0] + #else: + # self.group = self.dataset.get_group() + + #print("trinity, self.group is:", self.group) + + # + # In ProbeSet, there are maybe several annotations match one sequence + # so we need use sequence(BlatSeq) as the identification, when we update + # one annotation, we update the others who match the sequence also. + # + # Hongqiang Li, 3/3/2008 + # + + #XZ, 05/08/2009: This block is not neccessary. We can add 'BlatSeq' into disfield. + # The variable self.sequence should be changed to self.BlatSeq + # It also should be changed in other places where it are used. + + #if self.dataset: + #if self.dataset.type == 'ProbeSet': + # print("Doing ProbeSet Query") + # query = ''' + # SELECT + # ProbeSet.BlatSeq + # FROM + # ProbeSet, ProbeSetFreeze, ProbeSetXRef + # WHERE + # ProbeSet.Id=ProbeSetXRef.ProbeSetId and + # ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and + # ProbeSet.Name = %s and + # ProbeSetFreeze.Name = %s + # ''', (self.name, self.dataset.name) + # print("query is:", query) + # self.sequence = g.db.execute(*query).fetchone()[0] + # #self.sequence = self.cursor.fetchone()[0] + # print("self.sequence is:", self.sequence) + + + def get_name(self): + stringy = "" + if self.dataset and self.name: + stringy = "%s::%s" % (self.dataset, self.name) + if self.cellid: + stringy += "::" + self.cellid + else: + stringy = self.description + return stringy + + + def get_given_name(self): + """ + when user enter a trait or GN generate a trait, user want show the name + not the name that generated by GN randomly, the two follow function are + used to give the real name and the database. displayName() will show the + database also, getGivenName() just show the name. + For other trait, displayName() as same as getName(), getGivenName() as + same as self.name + + Hongqiang 11/29/07 + + """ + stringy = self.name + if self.dataset and self.name: + desc = self.dataset.get_desc() + if desc: + #desc = self.handle_pca(desc) + stringy = desc + return stringy + + + + def display_name(self): + stringy = "" + if self.dataset and self.name: + desc = self.dataset.get_desc() + #desc = self.handle_pca(desc) + if desc: + #desc = self.handle_pca(desc) + #stringy = desc + #if desc.__contains__('PCA'): + # desc = desc[desc.rindex(':')+1:].strip() + #else: + # desc = desc[:desc.index('entered')].strip() + #desc = self.handle_pca(desc) + stringy = "%s::%s" % (self.dataset, desc) + else: + stringy = "%s::%s" % (self.dataset, self.name) + if self.cellid: + stringy += "::" + self.cellid + else: + stringy = self.description + + return stringy + + + #def __str__(self): + # #return "%s %s" % (self.getName(), self.group) + # return self.getName() + #__str__ = getName + #__repr__ = __str__ + + def export_data(self, samplelist, the_type="val"): + """ + export data according to samplelist + mostly used in calculating correlation + + """ + result = [] + for sample in samplelist: + if self.data.has_key(sample): + if the_type=='val': + result.append(self.data[sample].val) + elif the_type=='var': + result.append(self.data[sample].var) + elif the_type=='N': + result.append(self.data[sample].N) + else: + raise KeyError, `the_type`+' the_type is incorrect.' + else: + result.append(None) + return result + + def export_informative(self, incVar=0): + """ + export informative sample + mostly used in qtl regression + + """ + samples = [] + vals = [] + the_vars = [] + for sample, value in self.data.items(): + if value.val != None: + if not incVar or value.var != None: + samples.append(sample) + vals.append(value.val) + the_vars.append(value.var) + return samples, vals, the_vars + + + # + # In ProbeSet, there are maybe several annotations match one sequence + # so we need use sequence(BlatSeq) as the identification, when we update + # one annotation, we update the others who match the sequence also. + # + # Hongqiang Li, 3/3/2008 + # + #def getSequence(self): + # assert self.cursor + # if self.dataset.type == 'ProbeSet': + # self.cursor.execute(''' + # SELECT + # ProbeSet.BlatSeq + # FROM + # ProbeSet, ProbeSetFreeze, ProbeSetXRef + # WHERE + # ProbeSet.Id=ProbeSetXRef.ProbeSetId and + # ProbeSetFreeze.Id = ProbeSetXRef.ProbSetFreezeId and + # ProbeSet.Name = %s + # ProbeSetFreeze.Name = %s + # ''', self.name, self.dataset.name) + # #self.cursor.execute(query) + # results = self.fetchone() + # + # return results[0] + + + + def retrieve_sample_data(self, samplelist=None): + if samplelist == None: + samplelist = [] + + assert self.dataset + + #if self.cellid: + # #Probe Data + # query = ''' + # SELECT + # Strain.Name, ProbeData.value, ProbeSE.error, ProbeData.Id + # FROM + # (ProbeData, ProbeFreeze, ProbeSetFreeze, ProbeXRef, + # Strain, Probe, ProbeSet) + # left join ProbeSE on + # (ProbeSE.DataId = ProbeData.Id AND ProbeSE.StrainId = ProbeData.StrainId) + # WHERE + # Probe.Name = '%s' AND ProbeSet.Name = '%s' AND + # Probe.ProbeSetId = ProbeSet.Id AND + # ProbeXRef.ProbeId = Probe.Id AND + # ProbeXRef.ProbeFreezeId = ProbeFreeze.Id AND + # ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id AND + # ProbeSetFreeze.Name = '%s' AND + # ProbeXRef.DataId = ProbeData.Id AND + # ProbeData.StrainId = Strain.Id + # Order BY + # Strain.Name + # ''' % (self.cellid, self.name, self.dataset.name) + # + #else: + results = self.dataset.retrieve_sample_data(self) + + #if self.dataset.type == 'Temp': + # query = ''' + # SELECT + # Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id + # FROM + # TempData, Temp, Strain + # WHERE + # TempData.StrainId = Strain.Id AND + # TempData.Id = Temp.DataId AND + # Temp.name = '%s' + # Order BY + # Strain.Name + # ''' % self.name + ##XZ, 03/02/2009: Xiaodong changed Data to PublishData, SE to PublishSE + #elif self.dataset.type == 'Publish': + # query = ''' + # SELECT + # Strain.Name, PublishData.value, PublishSE.error, NStrain.count, PublishData.Id + # FROM + # (PublishData, Strain, PublishXRef, PublishFreeze) + # left join PublishSE on + # (PublishSE.DataId = PublishData.Id AND PublishSE.StrainId = PublishData.StrainId) + # left join NStrain on + # (NStrain.DataId = PublishData.Id AND + # NStrain.StrainId = PublishData.StrainId) + # WHERE + # PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + # PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = %s AND + # PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id + # Order BY + # Strain.Name + # ''' % (self.name, self.dataset.id) + + #XZ, 03/02/2009: Xiaodong changed Data to ProbeData, SE to ProbeSE + #elif self.cellid: + + #XZ, 03/02/2009: Xiaodong added this block for ProbeSetData and ProbeSetSE + #elif self.dataset.type == 'ProbeSet': + # #ProbeSet Data + # query = ''' + # SELECT + # Strain.Name, ProbeSetData.value, ProbeSetSE.error, ProbeSetData.Id + # FROM + # (ProbeSetData, ProbeSetFreeze, Strain, ProbeSet, ProbeSetXRef) + # left join ProbeSetSE on + # (ProbeSetSE.DataId = ProbeSetData.Id AND ProbeSetSE.StrainId = ProbeSetData.StrainId) + # WHERE + # ProbeSet.Name = '%s' AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND + # ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND + # ProbeSetFreeze.Name = '%s' AND + # ProbeSetXRef.DataId = ProbeSetData.Id AND + # ProbeSetData.StrainId = Strain.Id + # Order BY + # Strain.Name + # ''' % (self.name, self.dataset.name) + ##XZ, 03/02/2009: Xiaodong changeded Data to GenoData, SE to GenoSE + #else: + # #Geno Data + # #XZ: The SpeciesId is not necessary, but it's nice to keep it to speed up database search. + # query = ''' + # SELECT + # Strain.Name, GenoData.value, GenoSE.error, GenoData.Id + # FROM + # (GenoData, GenoFreeze, Strain, Geno, GenoXRef) + # left join GenoSE on + # (GenoSE.DataId = GenoData.Id AND GenoSE.StrainId = GenoData.StrainId) + # WHERE + # Geno.SpeciesId = %s AND Geno.Name = '%s' AND GenoXRef.GenoId = Geno.Id AND + # GenoXRef.GenoFreezeId = GenoFreeze.Id AND + # GenoFreeze.Name = '%s' AND + # GenoXRef.DataId = GenoData.Id AND + # GenoData.StrainId = Strain.Id + # Order BY + # Strain.Name + # ''' % (webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.dataset.group), self.name, self.dataset.name) + + + #self.cursor.execute(query) + #results = self.cursor.fetchall() + + # Todo: is this necessary? If not remove + self.data.clear() + + if results: + #self.mysqlid = results[0][-1] + #if samplelist: + for item in results: + #name, value, variance, num_cases = item + if not samplelist or (samplelist and name in samplelist): + #if value != None: + # num_cases = None + # if self.dataset.type in ('Publish', 'Temp'): + # ndata = item[3] + name = item[0] + self.data[name] = webqtlCaseData(*item) #name, value, variance, num_cases) + #end for + # else: + # for item in results: + # val = item[1] + # if val != None: + # var = item[2] + # ndata = None + # if self.dataset.type in ('Publish', 'Temp'): + # ndata = item[3] + # self.data[item[0]] = webqtlCaseData(val, var, ndata) + # #end for + # #end if + + #def keys(self): + # return self.__dict__.keys() + # + #def has_key(self, key): + # return self.__dict__.has_key(key) + # + #def items(self): + # return self.__dict__.items() + + def retrieve_info(self, QTL=False): + assert self.dataset, "Dataset doesn't exist" + if self.dataset.type == 'Publish': + query = """ + SELECT + PublishXRef.Id, Publication.PubMed_ID, + Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description, + Phenotype.Pre_publication_abbreviation, Phenotype.Post_publication_abbreviation, + Phenotype.Lab_code, Phenotype.Submitter, Phenotype.Owner, Phenotype.Authorized_Users, + Publication.Authors, Publication.Title, Publication.Abstract, + Publication.Journal, Publication.Volume, Publication.Pages, + Publication.Month, Publication.Year, PublishXRef.Sequence, + Phenotype.Units, PublishXRef.comments + FROM + PublishXRef, Publication, Phenotype, PublishFreeze + WHERE + PublishXRef.Id = %s AND + Phenotype.Id = PublishXRef.PhenotypeId AND + Publication.Id = PublishXRef.PublicationId AND + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishFreeze.Id = %s + """ % (self.name, self.dataset.id) + traitInfo = g.db.execute(query).fetchone() + #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name + #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. + elif self.dataset.type == 'ProbeSet': + display_fields_string = ', ProbeSet.'.join(self.dataset.display_fields) + display_fields_string = 'ProbeSet.' + display_fields_string + query = """ + SELECT %s + FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef + WHERE + ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND + ProbeSetXRef.ProbeSetId = ProbeSet.Id AND + ProbeSetFreeze.Name = '%s' AND + ProbeSet.Name = '%s' + """ % (escape(display_fields_string), + escape(self.dataset.name), + escape(self.name)) + traitInfo = g.db.execute(query).fetchone() + print("traitInfo is: ", pf(traitInfo)) + #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name + # to avoid the problem of same marker name from different species. + elif self.dataset.type == 'Geno': + display_fields_string = string.join(self.dataset.display_fields,',Geno.') + display_fields_string = 'Geno.' + display_fields_string + query = """ + SELECT %s + FROM Geno, GenoFreeze, GenoXRef + WHERE + GenoXRef.GenoFreezeId = GenoFreeze.Id AND + GenoXRef.GenoId = Geno.Id AND + GenoFreeze.Name = '%s' AND + Geno.Name = '%s' + """ % (escape(display_fields_string), escape(self.dataset.name), escape(self.name)) + traitInfo = g.db.execute(query).fetchone() + print("traitInfo is: ", pf(traitInfo)) + else: #Temp type + query = """SELECT %s FROM %s WHERE Name = %s + """ % (string.join(self.dataset.display_fields,','), + self.dataset.type, self.name) + traitInfo = g.db.execute(query).fetchone() + + + #self.cursor.execute(query) + #traitInfo = self.cursor.fetchone() + if traitInfo: + self.haveinfo = True + + #XZ: assign SQL query result to trait attributes. + for i, field in enumerate(self.dataset.display_fields): + setattr(self, field, traitInfo[i]) + + if self.dataset.type == 'Publish': + self.confidential = 0 + if self.pre_publication_description and not self.pubmed_id: + self.confidential = 1 + + self.homologeneid = None + if self.dataset.type == 'ProbeSet' and self.dataset.group and self.geneid: + #XZ, 05/26/2010: From time to time, this query get error message because some geneid values in database are not number. + #XZ: So I have to test if geneid is number before execute the query. + #XZ: The geneid values in database should be cleaned up. + try: + junk = float(self.geneid) + geneidIsNumber = 1 + except: + geneidIsNumber = 0 + + if geneidIsNumber: + query = """ + SELECT + HomologeneId + FROM + Homologene, Species, InbredSet + WHERE + Homologene.GeneId =%s AND + InbredSet.Name = '%s' AND + InbredSet.SpeciesId = Species.Id AND + Species.TaxonomyId = Homologene.TaxonomyId + """ % (escape(str(self.geneid)), escape(self.dataset.group.name)) + result = g.db.execute(query).fetchone() + else: + result = None + + if result: + self.homologeneid = result[0] + + if QTL: + if self.dataset.type == 'ProbeSet' and not self.cellid: + traitQTL = g.db.execute(""" + SELECT + ProbeSetXRef.Locus, ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.mean + FROM + ProbeSetXRef, ProbeSet + WHERE + ProbeSetXRef.ProbeSetId = ProbeSet.Id AND + ProbeSet.Name = "%s" AND + ProbeSetXRef.ProbeSetFreezeId =%s + """, (self.name, self.dataset.id)).fetchone() + #self.cursor.execute(query) + #traitQTL = self.cursor.fetchone() + if traitQTL: + self.locus, self.lrs, self.pvalue, self.mean = traitQTL + else: + self.locus = self.lrs = self.pvalue = self.mean = "" + if self.dataset.type == 'Publish': + traitQTL = g.db.execute(""" + SELECT + PublishXRef.Locus, PublishXRef.LRS + FROM + PublishXRef, PublishFreeze + WHERE + PublishXRef.Id = %s AND + PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND + PublishFreeze.Id =%s + """, (self.name, self.dataset.id)).fetchone() + #self.cursor.execute(query) + #traitQTL = self.cursor.fetchone() + if traitQTL: + self.locus, self.lrs = traitQTL + else: + self.locus = self.lrs = "" + else: + raise KeyError, `self.name`+' information is not found in the database.' + + def genHTML(self, formName = "", dispFromDatabase=0, privilege="guest", userName="Guest", authorized_users=""): + if not self.haveinfo: + self.retrieveInfo() + + if self.dataset.type == 'Publish': + PubMedLink = "" + if self.pubmed_id: + PubMedLink = HT.Href(text="PubMed %d : " % self.pubmed_id, + target = "_blank", url = webqtlConfig.PUBMEDLINK_URL % self.pubmed_id) + else: + PubMedLink = HT.Span("Unpublished : ", Class="fs15") + + if formName: + setDescription2 = HT.Href(url="javascript:showDatabase3('%s','%s','%s','')" % + (formName, self.dataset.name, self.name), Class = "fs14") + else: + setDescription2 = HT.Href(url="javascript:showDatabase2('%s','%s','')" % + (self.dataset.name,self.name), Class = "fs14") + + if self.confidential and not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=privilege, userName=userName, authorized_users=authorized_users): + setDescription2.append('RecordID/%s - %s' % (self.name, self.pre_publication_description)) + else: + setDescription2.append('RecordID/%s - %s' % (self.name, self.post_publication_description)) + + #XZ 03/26/2011: Xiaodong comment out the following two lins as Rob asked. Need to check with Rob why in PublishXRef table, there are few row whose Sequence > 1. + #if self.sequence > 1: + # setDescription2.append(' btach %d' % self.sequence) + if self.authors: + a1 = string.split(self.authors,',')[0] + while a1[0] == '"' or a1[0] == "'" : + a1 = a1[1:] + setDescription2.append(' by ') + setDescription2.append(HT.Italic('%s, and colleagues' % a1)) + setDescription = HT.Span(PubMedLink, setDescription2) + + elif self.dataset.type == 'Temp': + setDescription = HT.Href(text="%s" % (self.description),url="javascript:showDatabase2\ + ('%s','%s','')" % (self.dataset.name,self.name), Class = "fs14") + setDescription = HT.Span(setDescription) + + elif self.dataset.type == 'Geno': # Genome DB only available for single search + if formName: + setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ + '%2.3f' % self.mb),url="javascript:showDatabase3('%s','%s','%s','')" % \ + (formName, self.dataset.name, self.name), Class = "fs14") + else: + setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ + '%2.3f' % self.mb),url="javascript:showDatabase2('%s','%s','')" % \ + (self.dataset.name,self.name), Class = "fs14") + + setDescription = HT.Span(setDescription) + + else: + if self.cellid: + if formName: + setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name, self.cellid),url=\ + "javascript:showDatabase3('%s','%s','%s','%s')" % (formName, self.dataset.name,self.name,self.cellid), \ + Class = "fs14") + else: + setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name,self.cellid),url=\ + "javascript:showDatabase2('%s','%s','%s')" % (self.dataset.name,self.name,self.cellid), \ + Class = "fs14") + else: + if formName: + setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ + "javascript:showDatabase3('%s','%s','%s','')" % (formName, self.dataset.name,self.name), \ + Class = "fs14") + else: + setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ + "javascript:showDatabase2('%s','%s','')" % (self.dataset.name,self.name), \ + Class = "fs14") + if self.symbol and self.chr and self.mb: + setDescription.append(' [') + setDescription.append(HT.Italic('%s' % self.symbol,Class="cdg fwb")) + setDescription.append(' on Chr %s @ %s Mb]' % (self.chr,self.mb)) + if self.description: + setDescription.append(': %s' % self.description) + if self.probe_target_description: + setDescription.append('; %s' % self.probe_target_description) + setDescription = HT.Span(setDescription) + + if self.dataset.type != 'Temp' and dispFromDatabase: + setDescription.append( ' --- FROM : ') + setDescription.append(self.dataset.genHTML(Class='cori')) + return setDescription + + @property + def description_fmt(self): + '''Return a text formated description''' + if self.description: + formatted = self.description + if self.probe_target_description: + formatted += "; " + self.probe_target_description + else: + formatted = "Not available" + return formatted.capitalize() + + @property + def alias_fmt(self): + '''Return a text formatted alias''' + if self.alias: + alias = string.replace(self.alias, ";", " ") + alias = string.join(string.split(alias), ", ") + return alias + + + @property + def location_fmt(self): + '''Return a text formatted location + + While we're at it we set self.location in case we need it later (do we?) + + ''' + + if self.chr and self.mb: + self.location = 'Chr %s @ %s Mb' % (self.chr,self.mb) + elif self.chr: + self.location = 'Chr %s @ Unknown position' % (self.chr) + else: + self.location = 'Not available' + + fmt = self.location + ##XZ: deal with direction + if self.strand_probe == '+': + fmt += (' on the plus strand ') + elif self.strand_probe == '-': + fmt += (' on the minus strand ') + + return fmt + + + def get_database(self): + """ + Returns the database, and the url referring to the database if it exists + + We're going to to return two values here, and we don't want to have to call this twice from + the template. So it's not a property called from the template, but instead is called from the view + + """ + if self.cellid: + self.cursor.execute(""" + select ProbeFreeze.Name from ProbeFreeze, ProbeSetFreeze + where + ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND + ProbeSetFreeze.Id = %d""" % thisTrait.dataset.id) + probeDBName = self.cursor.fetchone()[0] + return dict(name = probeDBName, + url = None) + else: + return dict(name = self.dataset.fullname, + url = webqtlConfig.INFOPAGEHREF % self.dataset.name) + + def calculate_correlation(self, values, method): + """Calculate the correlation value and p value according to the method specified""" + + #ZS: This takes the list of values of the trait our selected trait is being correlated against and removes the values of the samples our trait has no value for + #There's probably a better way of dealing with this, but I'll have to ask Christian + updated_raw_values = [] + updated_values = [] + for i in range(len(values)): + if values[i] != "None": + updated_raw_values.append(self.raw_values[i]) + updated_values.append(values[i]) + + self.raw_values = updated_raw_values + values = updated_values + + if method == METHOD_SAMPLE_PEARSON or method == METHOD_LIT or method == METHOD_TISSUE_PEARSON: + corr, nOverlap = webqtlUtil.calCorrelation(self.raw_values, values, len(values)) + else: + corr, nOverlap = webqtlUtil.calCorrelationRank(self.raw_values, values, len(values)) + + self.correlation = corr + self.overlap = nOverlap + + if self.overlap < 3: + self.p_value = 1.0 + else: + #ZS - This is probably the wrong way to deal with this. Correlation values of 1.0 definitely exist (the trait correlated against itself), so zero division needs to br prevented. + if abs(self.correlation) >= 1.0: + self.p_value = 0.0 + else: + ZValue = 0.5*log((1.0+self.correlation)/(1.0-self.correlation)) + ZValue = ZValue*sqrt(self.overlap-3) + self.p_value = 2.0*(1.0 - reaper.normp(abs(ZValue))) diff --git a/wqflask/base/webqtlTrait.py b/wqflask/base/webqtlTrait.py deleted file mode 100755 index 5367b41f..00000000 --- a/wqflask/base/webqtlTrait.py +++ /dev/null @@ -1,695 +0,0 @@ -from __future__ import division, print_function - -import string - -from htmlgen import HTMLgen2 as HT - -import webqtlConfig -from webqtlCaseData import webqtlCaseData -from data_set import create_dataset -from dbFunction import webqtlDatabaseFunction -from utility import webqtlUtil - -from MySQLdb import escape_string as escape -from pprint import pformat as pf - -from flask import Flask, g - -class GeneralTrait: - """ - Trait class defines a trait in webqtl, can be either Microarray, - Published phenotype, genotype, or user input trait - - """ - - def __init__(self, **kw): - print("in GeneralTrait") - self.dataset = kw.get('dataset', None) # database object - self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. - self.cellid = kw.get('cellid', None) - self.identification = kw.get('identification', 'un-named trait') - self.group = kw.get('group', None) - self.haveinfo = kw.get('haveinfo', False) - self.sequence = kw.get('sequence', None) # Blat sequence, available for ProbeSet - self.data = kw.get('data', {}) - - if kw.get('fullname'): - name2 = value.split("::") - if len(name2) == 2: - self.dataset, self.name = name2 - elif len(name2) == 3: - self.dataset, self.name, self.cellid = name2 - - #if self.dataset and isinstance(self.dataset, basestring): - self.dataset = create_dataset(self.dataset.name) - - print("self.dataset is:", self.dataset, type(self.dataset)) - #if self.dataset: - - self.dataset.get_group() - - if self.dataset.type == "Temp": - self.cursor.execute(''' - SELECT - InbredSet.Name - FROM - InbredSet, Temp - WHERE - Temp.InbredSetId = InbredSet.Id AND - Temp.Name = "%s" - ''', self.name) - self.group = self.cursor.fetchone()[0] - else: - self.group = self.dataset.get_group() - - print("trinity, self.group is:", self.group) - - # - # In ProbeSet, there are maybe several annotations match one sequence - # so we need use sequence(BlatSeq) as the identification, when we update - # one annotation, we update the others who match the sequence also. - # - # Hongqiang Li, 3/3/2008 - # - - #XZ, 05/08/2009: This block is not neccessary. We can add 'BlatSeq' into disfield. - # The variable self.sequence should be changed to self.BlatSeq - # It also should be changed in other places where it are used. - - #if self.dataset: - if self.dataset.type == 'ProbeSet': - print("Doing ProbeSet Query") - query = ''' - SELECT - ProbeSet.BlatSeq - FROM - ProbeSet, ProbeSetFreeze, ProbeSetXRef - WHERE - ProbeSet.Id=ProbeSetXRef.ProbeSetId and - ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and - ProbeSet.Name = %s and - ProbeSetFreeze.Name = %s - ''', (self.name, self.dataset.name) - print("query is:", query) - self.sequence = g.db.execute(*query).fetchone()[0] - #self.sequence = self.cursor.fetchone()[0] - print("self.sequence is:", self.sequence) - - - def getName(self): - str = "" - if self.dataset and self.name: - str = "%s::%s" % (self.dataset, self.name) - if self.cellid: - str += "::" + self.cellid - else: - str = self.description - return str - - # - # when user enter a trait or GN generate a trait, user want show the name - # not the name that generated by GN randomly, the two follow function are - # used to give the real name and the database. displayName() will show the - # database also, getGivenName() just show the name. - # For other trait, displayName() as same as getName(), getGivenName() as - # same as self.name - # - # Hongqiang 11/29/07 - # - def getGivenName(self): - str = self.name - if self.dataset and self.name: - if self.dataset.type=='Temp': - self.cursor.execute('SELECT description FROM Temp WHERE Name=%s', self.name) - desc = self.cursor.fetchone()[0] - if desc.__contains__('PCA'): - desc = desc[desc.rindex(':')+1:].strip() - else: - desc = desc[:desc.index('entered')].strip() - str = desc - return str - - def displayName(self): - str = "" - if self.dataset and self.name: - if self.dataset.type=='Temp': - desc = self.description - if desc.__contains__('PCA'): - desc = desc[desc.rindex(':')+1:].strip() - else: - desc = desc[:desc.index('entered')].strip() - str = "%s::%s" % (self.dataset, desc) - else: - str = "%s::%s" % (self.dataset, self.name) - if self.cellid: - str += "::" + self.cellid - else: - str = self.description - - return str - - - #def __str__(self): - # #return "%s %s" % (self.getName(), self.group) - # return self.getName() - #__str__ = getName - #__repr__ = __str__ - - def exportData(self, samplelist, type="val"): - """ - export data according to samplelist - mostly used in calculating correlation - """ - result = [] - for sample in samplelist: - if self.data.has_key(sample): - if type=='val': - result.append(self.data[sample].val) - elif type=='var': - result.append(self.data[sample].var) - elif type=='N': - result.append(self.data[sample].N) - else: - raise KeyError, `type`+' type is incorrect.' - else: - result.append(None) - return result - - def exportInformative(self, incVar=0): - """ - export informative sample - mostly used in qtl regression - """ - samples = [] - vals = [] - vars = [] - for sample, value in self.data.items(): - if value.val != None: - if not incVar or value.var != None: - samples.append(sample) - vals.append(value.val) - vars.append(value.var) - return samples, vals, vars - - - # - # In ProbeSet, there are maybe several annotations match one sequence - # so we need use sequence(BlatSeq) as the identification, when we update - # one annotation, we update the others who match the sequence also. - # - # Hongqiang Li, 3/3/2008 - # - def getSequence(self): - assert self.cursor - if self.dataset.type == 'ProbeSet': - self.cursor.execute(''' - SELECT - ProbeSet.BlatSeq - FROM - ProbeSet, ProbeSetFreeze, ProbeSetXRef - WHERE - ProbeSet.Id=ProbeSetXRef.ProbeSetId and - ProbeSetFreeze.Id = ProbeSetXRef.ProbSetFreezeId and - ProbeSet.Name = %s - ProbeSetFreeze.Name = %s - ''', self.name, self.dataset.name) - #self.cursor.execute(query) - results = self.fetchone() - - return results[0] - - - - def retrieveData(self, samplelist=None): - - if samplelist == None: - samplelist = [] - assert self.dataset and self.cursor - - if self.dataset.type == 'Temp': - query = ''' - SELECT - Strain.Name, TempData.value, TempData.SE, TempData.NStrain, TempData.Id - FROM - TempData, Temp, Strain - WHERE - TempData.StrainId = Strain.Id AND - TempData.Id = Temp.DataId AND - Temp.name = '%s' - Order BY - Strain.Name - ''' % self.name - #XZ, 03/02/2009: Xiaodong changed Data to PublishData, SE to PublishSE - elif self.dataset.type == 'Publish': - query = ''' - SELECT - Strain.Name, PublishData.value, PublishSE.error, NStrain.count, PublishData.Id - FROM - (PublishData, Strain, PublishXRef, PublishFreeze) - left join PublishSE on - (PublishSE.DataId = PublishData.Id AND PublishSE.StrainId = PublishData.StrainId) - left join NStrain on - (NStrain.DataId = PublishData.Id AND - NStrain.StrainId = PublishData.StrainId) - WHERE - PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND - PublishData.Id = PublishXRef.DataId AND PublishXRef.Id = %s AND - PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id - Order BY - Strain.Name - ''' % (self.name, self.dataset.id) - - #XZ, 03/02/2009: Xiaodong changed Data to ProbeData, SE to ProbeSE - elif self.cellid: - #Probe Data - query = ''' - SELECT - Strain.Name, ProbeData.value, ProbeSE.error, ProbeData.Id - FROM - (ProbeData, ProbeFreeze, ProbeSetFreeze, ProbeXRef, - Strain, Probe, ProbeSet) - left join ProbeSE on - (ProbeSE.DataId = ProbeData.Id AND ProbeSE.StrainId = ProbeData.StrainId) - WHERE - Probe.Name = '%s' AND ProbeSet.Name = '%s' AND - Probe.ProbeSetId = ProbeSet.Id AND - ProbeXRef.ProbeId = Probe.Id AND - ProbeXRef.ProbeFreezeId = ProbeFreeze.Id AND - ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id AND - ProbeSetFreeze.Name = '%s' AND - ProbeXRef.DataId = ProbeData.Id AND - ProbeData.StrainId = Strain.Id - Order BY - Strain.Name - ''' % (self.cellid, self.name, self.dataset.name) - #XZ, 03/02/2009: Xiaodong added this block for ProbeSetData and ProbeSetSE - elif self.dataset.type == 'ProbeSet': - #ProbeSet Data - query = ''' - SELECT - Strain.Name, ProbeSetData.value, ProbeSetSE.error, ProbeSetData.Id - FROM - (ProbeSetData, ProbeSetFreeze, Strain, ProbeSet, ProbeSetXRef) - left join ProbeSetSE on - (ProbeSetSE.DataId = ProbeSetData.Id AND ProbeSetSE.StrainId = ProbeSetData.StrainId) - WHERE - ProbeSet.Name = '%s' AND ProbeSetXRef.ProbeSetId = ProbeSet.Id AND - ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND - ProbeSetFreeze.Name = '%s' AND - ProbeSetXRef.DataId = ProbeSetData.Id AND - ProbeSetData.StrainId = Strain.Id - Order BY - Strain.Name - ''' % (self.name, self.dataset.name) - #XZ, 03/02/2009: Xiaodong changeded Data to GenoData, SE to GenoSE - else: - #Geno Data - #XZ: The SpeciesId is not necessary, but it's nice to keep it to speed up database search. - query = ''' - SELECT - Strain.Name, GenoData.value, GenoSE.error, GenoData.Id - FROM - (GenoData, GenoFreeze, Strain, Geno, GenoXRef) - left join GenoSE on - (GenoSE.DataId = GenoData.Id AND GenoSE.StrainId = GenoData.StrainId) - WHERE - Geno.SpeciesId = %s AND Geno.Name = '%s' AND GenoXRef.GenoId = Geno.Id AND - GenoXRef.GenoFreezeId = GenoFreeze.Id AND - GenoFreeze.Name = '%s' AND - GenoXRef.DataId = GenoData.Id AND - GenoData.StrainId = Strain.Id - Order BY - Strain.Name - ''' % (webqtlDatabaseFunction.retrieveSpeciesId(self.cursor, self.dataset.group), self.name, self.dataset.name) - - - self.cursor.execute(query) - results = self.cursor.fetchall() - self.data.clear() - - if results: - self.mysqlid = results[0][-1] - #if samplelist: - for item in results: - #name, value, variance, num_cases = item - if not samplelist or (samplelist and name in samplelist): - #if value != None: - # num_cases = None - # if self.dataset.type in ('Publish', 'Temp'): - # ndata = item[3] - name = item[0] - self.data[name] = webqtlCaseData(*item) #name, value, variance, num_cases) - #end for - # else: - # for item in results: - # val = item[1] - # if val != None: - # var = item[2] - # ndata = None - # if self.dataset.type in ('Publish', 'Temp'): - # ndata = item[3] - # self.data[item[0]] = webqtlCaseData(val, var, ndata) - # #end for - # #end if - #else: - # pass - - #def keys(self): - # return self.__dict__.keys() - # - #def has_key(self, key): - # return self.__dict__.has_key(key) - # - #def items(self): - # return self.__dict__.items() - - def retrieve_info(self, QTL=False): - assert self.dataset, "Dataset doesn't exist" - if self.dataset.type == 'Publish': - query = """ - SELECT - PublishXRef.Id, Publication.PubMed_ID, - Phenotype.Pre_publication_description, Phenotype.Post_publication_description, Phenotype.Original_description, - Phenotype.Pre_publication_abbreviation, Phenotype.Post_publication_abbreviation, - Phenotype.Lab_code, Phenotype.Submitter, Phenotype.Owner, Phenotype.Authorized_Users, - Publication.Authors, Publication.Title, Publication.Abstract, - Publication.Journal, Publication.Volume, Publication.Pages, - Publication.Month, Publication.Year, PublishXRef.Sequence, - Phenotype.Units, PublishXRef.comments - FROM - PublishXRef, Publication, Phenotype, PublishFreeze - WHERE - PublishXRef.Id = %s AND - Phenotype.Id = PublishXRef.PhenotypeId AND - Publication.Id = PublishXRef.PublicationId AND - PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND - PublishFreeze.Id = %s - """ % (self.name, self.dataset.id) - traitInfo = g.db.execute(query).fetchone() - #XZ, 05/08/2009: Xiaodong add this block to use ProbeSet.Id to find the probeset instead of just using ProbeSet.Name - #XZ, 05/08/2009: to avoid the problem of same probeset name from different platforms. - elif self.dataset.type == 'ProbeSet': - display_fields_string = ', ProbeSet.'.join(self.dataset.display_fields) - display_fields_string = 'ProbeSet.' + display_fields_string - query = """ - SELECT %s - FROM ProbeSet, ProbeSetFreeze, ProbeSetXRef - WHERE - ProbeSetXRef.ProbeSetFreezeId = ProbeSetFreeze.Id AND - ProbeSetXRef.ProbeSetId = ProbeSet.Id AND - ProbeSetFreeze.Name = '%s' AND - ProbeSet.Name = '%s' - """ % (display_fields_string, self.dataset.name, self.name) - traitInfo = g.db.execute(query).fetchone() - print("traitInfo is: ", pf(traitInfo)) - #XZ, 05/08/2009: We also should use Geno.Id to find marker instead of just using Geno.Name - # to avoid the problem of same marker name from different species. - elif self.dataset.type == 'Geno': - display_fields_string = string.join(self.dataset.display_fields,',Geno.') - display_fields_string = 'Geno.' + display_fields_string - query = """ - SELECT %s - FROM Geno, GenoFreeze, GenoXRef - WHERE - GenoXRef.GenoFreezeId = GenoFreeze.Id AND - GenoXRef.GenoId = Geno.Id AND - GenoFreeze.Name = '%s' AND - Geno.Name = '%s' - """ % (display_fields_string, self.dataset.name, self.name) - traitInfo = g.db.execute(query).fetchone() - print("traitInfo is: ", pf(traitInfo)) - else: #Temp type - query = """SELECT %s FROM %s WHERE Name = %s - """ % (string.join(self.dataset.display_fields,','), - self.dataset.type, self.name) - traitInfo = g.db.execute(query).fetchone() - - - #self.cursor.execute(query) - #traitInfo = self.cursor.fetchone() - if traitInfo: - self.haveinfo = True - - #XZ: assign SQL query result to trait attributes. - for i, field in enumerate(self.dataset.display_fields): - setattr(self, field, traitInfo[i]) - - if self.dataset.type == 'Publish': - self.confidential = 0 - if self.pre_publication_description and not self.pubmed_id: - self.confidential = 1 - - self.homologeneid = None - if self.dataset.type == 'ProbeSet' and self.group and self.geneid: - #XZ, 05/26/2010: From time to time, this query get error message because some geneid values in database are not number. - #XZ: So I have to test if geneid is number before execute the query. - #XZ: The geneid values in database should be cleaned up. - try: - junk = float(self.geneid) - geneidIsNumber = 1 - except: - geneidIsNumber = 0 - - if geneidIsNumber: - result = g.db.execute(""" - SELECT - HomologeneId - FROM - Homologene, Species, InbredSet - WHERE - Homologene.GeneId =%s AND - InbredSet.Name = '%s' AND - InbredSet.SpeciesId = Species.Id AND - Species.TaxonomyId = Homologene.TaxonomyId - """, (self.geneid, self.group)).fetchone() - #self.cursor.execute(query) - #result = self.cursor.fetchone() - else: - result = None - - if result: - self.homologeneid = result[0] - - if QTL: - if self.dataset.type == 'ProbeSet' and not self.cellid: - traitQTL = g.db.execute(""" - SELECT - ProbeSetXRef.Locus, ProbeSetXRef.LRS, ProbeSetXRef.pValue, ProbeSetXRef.mean - FROM - ProbeSetXRef, ProbeSet - WHERE - ProbeSetXRef.ProbeSetId = ProbeSet.Id AND - ProbeSet.Name = "%s" AND - ProbeSetXRef.ProbeSetFreezeId =%s - """, (self.name, self.dataset.id)).fetchone() - #self.cursor.execute(query) - #traitQTL = self.cursor.fetchone() - if traitQTL: - self.locus, self.lrs, self.pvalue, self.mean = traitQTL - else: - self.locus = self.lrs = self.pvalue = self.mean = "" - if self.dataset.type == 'Publish': - traitQTL = g.db.execute(""" - SELECT - PublishXRef.Locus, PublishXRef.LRS - FROM - PublishXRef, PublishFreeze - WHERE - PublishXRef.Id = %s AND - PublishXRef.InbredSetId = PublishFreeze.InbredSetId AND - PublishFreeze.Id =%s - """, (self.name, self.dataset.id)).fetchone() - #self.cursor.execute(query) - #traitQTL = self.cursor.fetchone() - if traitQTL: - self.locus, self.lrs = traitQTL - else: - self.locus = self.lrs = "" - else: - raise KeyError, `self.name`+' information is not found in the database.' - - def genHTML(self, formName = "", dispFromDatabase=0, privilege="guest", userName="Guest", authorized_users=""): - if not self.haveinfo: - self.retrieveInfo() - - if self.dataset.type == 'Publish': - PubMedLink = "" - if self.pubmed_id: - PubMedLink = HT.Href(text="PubMed %d : " % self.pubmed_id, - target = "_blank", url = webqtlConfig.PUBMEDLINK_URL % self.pubmed_id) - else: - PubMedLink = HT.Span("Unpublished : ", Class="fs15") - - if formName: - setDescription2 = HT.Href(url="javascript:showDatabase3('%s','%s','%s','')" % - (formName, self.dataset.name, self.name), Class = "fs14") - else: - setDescription2 = HT.Href(url="javascript:showDatabase2('%s','%s','')" % - (self.dataset.name,self.name), Class = "fs14") - - if self.confidential and not webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=privilege, userName=userName, authorized_users=authorized_users): - setDescription2.append('RecordID/%s - %s' % (self.name, self.pre_publication_description)) - else: - setDescription2.append('RecordID/%s - %s' % (self.name, self.post_publication_description)) - - #XZ 03/26/2011: Xiaodong comment out the following two lins as Rob asked. Need to check with Rob why in PublishXRef table, there are few row whose Sequence > 1. - #if self.sequence > 1: - # setDescription2.append(' btach %d' % self.sequence) - if self.authors: - a1 = string.split(self.authors,',')[0] - while a1[0] == '"' or a1[0] == "'" : - a1 = a1[1:] - setDescription2.append(' by ') - setDescription2.append(HT.Italic('%s, and colleagues' % a1)) - setDescription = HT.Span(PubMedLink, setDescription2) - - elif self.dataset.type == 'Temp': - setDescription = HT.Href(text="%s" % (self.description),url="javascript:showDatabase2\ - ('%s','%s','')" % (self.dataset.name,self.name), Class = "fs14") - setDescription = HT.Span(setDescription) - - elif self.dataset.type == 'Geno': # Genome DB only available for single search - if formName: - setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ - '%2.3f' % self.mb),url="javascript:showDatabase3('%s','%s','%s','')" % \ - (formName, self.dataset.name, self.name), Class = "fs14") - else: - setDescription = HT.Href(text="Locus %s [Chr %s @ %s Mb]" % (self.name,self.chr,\ - '%2.3f' % self.mb),url="javascript:showDatabase2('%s','%s','')" % \ - (self.dataset.name,self.name), Class = "fs14") - - setDescription = HT.Span(setDescription) - - else: - if self.cellid: - if formName: - setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name, self.cellid),url=\ - "javascript:showDatabase3('%s','%s','%s','%s')" % (formName, self.dataset.name,self.name,self.cellid), \ - Class = "fs14") - else: - setDescription = HT.Href(text="ProbeSet/%s/%s" % (self.name,self.cellid),url=\ - "javascript:showDatabase2('%s','%s','%s')" % (self.dataset.name,self.name,self.cellid), \ - Class = "fs14") - else: - if formName: - setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ - "javascript:showDatabase3('%s','%s','%s','')" % (formName, self.dataset.name,self.name), \ - Class = "fs14") - else: - setDescription = HT.Href(text="ProbeSet/%s" % self.name, url=\ - "javascript:showDatabase2('%s','%s','')" % (self.dataset.name,self.name), \ - Class = "fs14") - if self.symbol and self.chr and self.mb: - setDescription.append(' [') - setDescription.append(HT.Italic('%s' % self.symbol,Class="cdg fwb")) - setDescription.append(' on Chr %s @ %s Mb]' % (self.chr,self.mb)) - if self.description: - setDescription.append(': %s' % self.description) - if self.probe_target_description: - setDescription.append('; %s' % self.probe_target_description) - setDescription = HT.Span(setDescription) - - if self.dataset.type != 'Temp' and dispFromDatabase: - setDescription.append( ' --- FROM : ') - setDescription.append(self.dataset.genHTML(Class='cori')) - return setDescription - - @property - def description_fmt(self): - '''Return a text formated description''' - if self.description: - formatted = self.description - if self.probe_target_description: - formatted += "; " + self.probe_target_description - else: - formatted = "Not available" - return formatted.capitalize() - - @property - def alias_fmt(self): - '''Return a text formatted alias''' - if self.alias: - alias = string.replace(self.alias, ";", " ") - alias = string.join(string.split(alias), ", ") - return alias - - - @property - def location_fmt(self): - '''Return a text formatted location - - While we're at it we set self.location in case we need it later (do we?) - - ''' - - if self.chr and self.mb: - self.location = 'Chr %s @ %s Mb' % (self.chr,self.mb) - elif self.chr: - self.location = 'Chr %s @ Unknown position' % (self.chr) - else: - self.location = 'Not available' - - fmt = self.location - ##XZ: deal with direction - if self.strand_probe == '+': - fmt += (' on the plus strand ') - elif self.strand_probe == '-': - fmt += (' on the minus strand ') - - return fmt - - - def get_database(self): - """ - Returns the database, and the url referring to the database if it exists - - We're going to to return two values here, and we don't want to have to call this twice from - the template. So it's not a property called from the template, but instead is called from the view - - """ - if self.cellid: - self.cursor.execute(""" - select ProbeFreeze.Name from ProbeFreeze, ProbeSetFreeze - where - ProbeFreeze.Id = ProbeSetFreeze.ProbeFreezeId AND - ProbeSetFreeze.Id = %d""" % thisTrait.dataset.id) - probeDBName = self.cursor.fetchone()[0] - return dict(name = probeDBName, - url = None) - else: - return dict(name = self.dataset.fullname, - url = webqtlConfig.INFOPAGEHREF % self.dataset.name) - - def calculate_correlation(self, values, method): - """Calculate the correlation value and p value according to the method specified""" - - #ZS: This takes the list of values of the trait our selected trait is being correlated against and removes the values of the samples our trait has no value for - #There's probably a better way of dealing with this, but I'll have to ask Christian - updated_raw_values = [] - updated_values = [] - for i in range(len(values)): - if values[i] != "None": - updated_raw_values.append(self.raw_values[i]) - updated_values.append(values[i]) - - self.raw_values = updated_raw_values - values = updated_values - - if method == METHOD_SAMPLE_PEARSON or method == METHOD_LIT or method == METHOD_TISSUE_PEARSON: - corr, nOverlap = webqtlUtil.calCorrelation(self.raw_values, values, len(values)) - else: - corr, nOverlap = webqtlUtil.calCorrelationRank(self.raw_values, values, len(values)) - - self.correlation = corr - self.overlap = nOverlap - - if self.overlap < 3: - self.p_value = 1.0 - else: - #ZS - This is probably the wrong way to deal with this. Correlation values of 1.0 definitely exist (the trait correlated against itself), so zero division needs to br prevented. - if abs(self.correlation) >= 1.0: - self.p_value = 0.0 - else: - ZValue = 0.5*log((1.0+self.correlation)/(1.0-self.correlation)) - ZValue = ZValue*sqrt(self.overlap-3) - self.p_value = 2.0*(1.0 - reaper.normp(abs(ZValue))) diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 4301fb50..69602748 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -26,10 +26,11 @@ class DoSearch(object): assert search_operator in (None, "=", "<", ">", "<=", ">="), "Bad search operator" self.search_operator = search_operator self.dataset = dataset + print("self.dataset is boo: ", type(self.dataset), pf(self.dataset)) + print("self.dataset.group is: ", pf(self.dataset.group)) #Get group information for dataset and the species id - self.dataset.get_group() - self.species_id = webqtlDatabaseFunction.retrieve_species_id(self.dataset.group) + self.species_id = webqtlDatabaseFunction.retrieve_species_id(self.dataset.group.name) def execute(self, query): """Executes query and returns results""" diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index cd478110..7c50dfeb 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -30,7 +30,7 @@ from base import webqtlConfig from utility.THCell import THCell from utility.TDCell import TDCell from base.data_set import create_dataset -from base.webqtlTrait import GeneralTrait +from base.trait import GeneralTrait from base.templatePage import templatePage from wqflask import parser from wqflask import do_search @@ -99,8 +99,7 @@ class SearchResultPage(templatePage): """ self.trait_list = [] - group = self.dataset.group - species = webqtlDatabaseFunction.retrieve_species(group=group) + species = webqtlDatabaseFunction.retrieve_species(self.dataset.group.name) # result_set represents the results for each search term; a search of # "shh grin2b" would have two sets of results, one for each term diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index aef9219f..2bc4fc9c 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -13,7 +13,8 @@ from base import webqtlConfig from base import webqtlCaseData from wqflask.show_trait.SampleList import SampleList from utility import webqtlUtil, Plot, Bunch -from base.webqtlTrait import GeneralTrait +from base.trait import GeneralTrait +from base.data_set import create_dataset from dbFunction import webqtlDatabaseFunction from base.templatePage import templatePage from basicStatistics import BasicStatisticsFunctions @@ -33,105 +34,111 @@ class ShowTrait(templatePage): def __init__(self, args): print("in ShowTrait, args are:", args) - self.group = args.group - self.trait_id = trait_id - self.dataset = dataset + #self.group = args.group + self.trait_id = args['trait_id'] + self.dataset = create_dataset(args['dataset']) + self.cell_id = None #assert self.openMysql(), "No database!" #print("red3 fd.group:", fd.group) this_trait = self.get_this_trait() - print("red4 fd.group:", fd.group) + #print("red4 fd.group:", fd.group) ##read genotype file - fd.group = this_trait.group + #fd.group = this_trait.group - print("[red5] fd.group is:", fd.group) - fd.readGenotype() + #print("[red5] fd.group is:", fd.group) + self.dataset.group.read_genotype_file() + #fd.readGenotype() - if not fd.genotype: - fd.readData(incf1=1) + if not self.dataset.group.genotype: + self.read_data(incf1=1) - # determine data editing page format - variance_data_page = 0 - if fd.formID == 'varianceChoice': - variance_data_page = 1 - - if variance_data_page: - fmID='dataEditing' - else: - if fd.enablevariance: - fmID='pre_dataEditing' - else: - fmID='dataEditing' - - # Some fields, like method, are defaulted to None; otherwise in IE the field can't be changed using jquery - hddn = OrderedDict( - FormID = fmID, - group = fd.group, - submitID = '', - scale = 'physic', - additiveCheck = 'ON', - showSNP = 'ON', - showGenes = 'ON', - method = None, - parentsf14regression = 'OFF', - stats_method = '1', - chromosomes = '-1', - topten = '', - viewLegend = 'ON', - intervalAnalystCheck = 'ON', - valsHidden = 'OFF', - database = '', - criteria = None, - MDPChoice = None, - bootCheck = None, - permCheck = None, - applyVarianceSE = None, - sampleNames = '_', - sampleVals = '_', - sampleVars = '_', - otherStrainNames = '_', - otherStrainVals = '_', - otherStrainVars = '_', - extra_attributes = '_', - other_extra_attributes = '_', - export_data = None - ) - - if fd.enablevariance: - hddn['enablevariance']='ON' - if fd.incparentsf1: - hddn['incparentsf1']='ON' - - if this_trait: - hddn['fullname'] = str(this_trait) - try: - hddn['normalPlotTitle'] = this_trait.symbol - hddn['normalPlotTitle'] += ": " - hddn['normalPlotTitle'] += this_trait.name - except: - hddn['normalPlotTitle'] = str(this_trait.name) - hddn['fromDataEditingPage'] = 1 - if this_trait.dataset and this_trait.dataset.type and this_trait.dataset.type == 'ProbeSet': - hddn['trait_type'] = this_trait.dataset.type - if this_trait.cellid: - hddn['cellid'] = this_trait.cellid - else: - self.cursor.execute("SELECT h2 from ProbeSetXRef WHERE DataId = %d" % - this_trait.mysqlid) - heritability = self.cursor.fetchone() - hddn['heritability'] = heritability - - hddn['attribute_names'] = "" - - hddn['mappingMethodId'] = webqtlDatabaseFunction.getMappingMethod (cursor=self.cursor, - groupName=fd.group) - - if fd.identification: - hddn['identification'] = fd.identification - else: - hddn['identification'] = "Un-named trait" #If no identification, set identification to un-named + ## determine data editing page format + #variance_data_page = 0 + #if fd.formID == 'varianceChoice': + # variance_data_page = 1 + # + #if variance_data_page: + # fmID='dataEditing' + #else: + # if fd.enablevariance: + # fmID='pre_dataEditing' + # else: + # fmID='dataEditing' + + # Todo: Add back in the ones we actually need from below, as we discover we need them + hddn = OrderedDict() + + + ## Some fields, like method, are defaulted to None; otherwise in IE the field can't be changed using jquery + #hddn = OrderedDict( + # FormID = fmID, + # group = fd.group, + # submitID = '', + # scale = 'physic', + # additiveCheck = 'ON', + # showSNP = 'ON', + # showGenes = 'ON', + # method = None, + # parentsf14regression = 'OFF', + # stats_method = '1', + # chromosomes = '-1', + # topten = '', + # viewLegend = 'ON', + # intervalAnalystCheck = 'ON', + # valsHidden = 'OFF', + # database = '', + # criteria = None, + # MDPChoice = None, + # bootCheck = None, + # permCheck = None, + # applyVarianceSE = None, + # sampleNames = '_', + # sampleVals = '_', + # sampleVars = '_', + # otherStrainNames = '_', + # otherStrainVals = '_', + # otherStrainVars = '_', + # extra_attributes = '_', + # other_extra_attributes = '_', + # export_data = None + # ) + + #if fd.enablevariance: + # hddn['enablevariance']='ON' + #if fd.incparentsf1: + # hddn['incparentsf1']='ON' + + #if this_trait: + # hddn['fullname'] = str(this_trait) + # try: + # hddn['normalPlotTitle'] = this_trait.symbol + # hddn['normalPlotTitle'] += ": " + # hddn['normalPlotTitle'] += this_trait.name + # except: + # hddn['normalPlotTitle'] = str(this_trait.name) + # hddn['fromDataEditingPage'] = 1 + # if this_trait.dataset and this_trait.dataset.type and this_trait.dataset.type == 'ProbeSet': + # hddn['trait_type'] = this_trait.dataset.type + # if this_trait.cellid: + # hddn['cellid'] = this_trait.cellid + # else: + # self.cursor.execute("SELECT h2 from ProbeSetXRef WHERE DataId = %d" % + # this_trait.mysqlid) + # heritability = self.cursor.fetchone() + # hddn['heritability'] = heritability + # + # hddn['attribute_names'] = "" + # + #hddn['mappingMethodId'] = webqtlDatabaseFunction.getMappingMethod (cursor=self.cursor, + # groupName=fd.group) + # + #if fd.identification: + # hddn['identification'] = fd.identification + #else: + # hddn['identification'] = "Un-named trait" #If no identification, set identification to un-named self.dispTraitInformation(fd, "", hddn, this_trait) #Display trait information + function buttons @@ -186,27 +193,109 @@ class ShowTrait(templatePage): #trait_id = self.fd['trait_id'] #cell_id = self.fd.get('CellID') - this_trait = webqtlTrait(dataset=dataset, - name=trait_id, - cellid=cell_id) + this_trait = GeneralTrait(dataset=self.dataset.name, + name=self.trait_id, + cellid=self.cell_id) ##identification, etc. - self.fd.identification = '%s : %s' % (this_trait.dataset.shortname, trait_id) + self.identification = '%s : %s' % (self.dataset.shortname, self.trait_id) this_trait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\ - &ProbeSetID=%s&group=%s&parentsf1=on' %(dataset, trait_id, self.fd['group']) + &ProbeSetID=%s&group=%s&parentsf1=on' %(self.dataset, self.trait_id, self.dataset.group.name) - if cell_id: - self.fd.identification = '%s/%s'%(self.fd.identification, cell_id) - this_trait.returnURL = '%s&CellID=%s' % (this_trait.returnURL, cell_id) + if self.cell_id: + self.identification = '%s/%s'%(self.identification, self.cell_id) + this_trait.returnURL = '%s&CellID=%s' % (this_trait.returnURL, self.cell_id) - print("yellow1:", self.group) - this_trait.retrieveInfo() - print("yellow2:", self.group) - this_trait.retrieveData() - print("yellow3:", self.group) + print("yellow1:", self.dataset.group) + this_trait.retrieve_info() + print("yellow2:", self.dataset.group) + this_trait.retrieve_sample_data() + print("yellow3:", self.dataset.group) return this_trait + def read_data(self): + '''read user input data or from trait data and analysis form''' + + if incf1 == None: + incf1 = [] + + if not self.genotype: + self.readGenotype() + if not samplelist: + if incf1: + samplelist = self.f1list + self.samplelist + else: + samplelist = self.samplelist + + #print("before traitfiledata self.traitfile is:", pf(self.traitfile)) + + traitfiledata = getattr(self, "traitfile", None) + traitpastedata = getattr(self, "traitpaste", None) + variancefiledata = getattr(self, "variancefile", None) + variancepastedata = getattr(self, "variancepaste", None) + Nfiledata = getattr(self, "Nfile", None) + + #### Todo: Rewrite below when we get to someone submitting their own trait ##### + + def to_float(item): + try: + return float(item) + except ValueError: + return None + + print("bottle samplelist is:", samplelist) + if traitfiledata: + tt = traitfiledata.split() + values = map(webqtlUtil.StringAsFloat, tt) + elif traitpastedata: + tt = traitpastedata.split() + values = map(webqtlUtil.StringAsFloat, tt) + else: + print("mapping formdataasfloat") + #values = map(self.FormDataAsFloat, samplelist) + values = [to_float(getattr(self, key)) for key in samplelist] + print("rocket values is:", values) + + + if len(values) < len(samplelist): + values += [None] * (len(samplelist) - len(values)) + elif len(values) > len(samplelist): + values = values[:len(samplelist)] + print("now values is:", values) + + + if variancefiledata: + tt = variancefiledata.split() + variances = map(webqtlUtil.StringAsFloat, tt) + elif variancepastedata: + tt = variancepastedata.split() + variances = map(webqtlUtil.StringAsFloat, tt) + else: + variances = map(self.FormVarianceAsFloat, samplelist) + + if len(variances) < len(samplelist): + variances += [None]*(len(samplelist) - len(variances)) + elif len(variances) > len(samplelist): + variances = variances[:len(samplelist)] + + if Nfiledata: + tt = string.split(Nfiledata) + nsamples = map(webqtlUtil.IntAsFloat, tt) + if len(nsamples) < len(samplelist): + nsamples += [None]*(len(samplelist) - len(nsamples)) + else: + nsamples = map(self.FormNAsFloat, samplelist) + + ##values, variances, nsamples is obsolete + self.allTraitData = {} + for i, _sample in enumerate(samplelist): + if values[i] != None: + self.allTraitData[_sample] = webqtlCaseData( + _sample, values[i], variances[i], nsamples[i]) + print("allTraitData is:", pf(self.allTraitData)) + + def dispTraitInformation(self, fd, title1Body, hddn, this_trait): _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) -- cgit v1.2.3 From 6749d3c9be414ad15b9c39b2d1817ca27566d959 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 6 Dec 2012 14:27:53 -0600 Subject: Made many small changes to show_trait/data_set/search_results/trait to remove use of the formData object and cursor --- wqflask/base/data_set.py | 7 +- wqflask/base/trait.py | 4 +- wqflask/wqflask/search_results.py | 4 +- wqflask/wqflask/show_trait/SampleList.py | 8 +- wqflask/wqflask/show_trait/show_trait.py | 129 ++++++++++++++++--------------- 5 files changed, 78 insertions(+), 74 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 68f5e5ed..14e4055e 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -42,12 +42,13 @@ def create_dataset(dataset_name): #cursor = db_conn.cursor() print("dataset_name:", dataset_name) - dataset_type = g.db.execute(""" + query = """ SELECT DBType.Name FROM DBList, DBType - WHERE DBList.Name = %s and + WHERE DBList.Name = '%s' and DBType.Id = DBList.DBTypeId - """, (dataset_name)).fetchone().Name + """ % (escape(dataset_name)) + dataset_type = g.db.execute(query).fetchone().Name #dataset_type = cursor.fetchone()[0] print("[blubber] dataset_type:", pf(dataset_type)) diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py index d3753fc1..d0158ebd 100755 --- a/wqflask/base/trait.py +++ b/wqflask/base/trait.py @@ -24,7 +24,7 @@ class GeneralTrait: def __init__(self, **kw): print("in GeneralTrait") - self.dataset = kw.get('dataset', None) # database object + self.dataset = kw.get('dataset', None) # database name self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. self.cellid = kw.get('cellid', None) self.identification = kw.get('identification', 'un-named trait') @@ -230,7 +230,7 @@ class GeneralTrait: if samplelist == None: samplelist = [] - assert self.dataset + #assert self.dataset #if self.cellid: # #Probe Data diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index 7c50dfeb..b518ab99 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -112,11 +112,11 @@ class SearchResultPage(templatePage): print("foo locals are:", locals()) trait_id = result[0] - this_trait = GeneralTrait(dataset=self.dataset, name=trait_id) + this_trait = GeneralTrait(dataset=self.dataset.name, name=trait_id) this_trait.retrieve_info(QTL=True) self.trait_list.append(this_trait) - self.dataset.get_trait_info(self.trait_list, species) + self.dataset.get_trait_info(self.trait_list, species) def search(self): diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 25877521..53bb24fb 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -9,7 +9,7 @@ from pprint import pformat as pf class SampleList(object): def __init__(self, cursor, - fd, + dataset, variance_data_page, sample_names, this_trait, @@ -17,7 +17,7 @@ class SampleList(object): header): self.cursor = cursor - self.fd = fd + self.dataset = dataset self.this_trait = this_trait self.sample_group_type = sample_group_type # primary or other self.header = header @@ -42,7 +42,7 @@ class SampleList(object): #if fd.RISet == 'AXBXA' and sampleName in ('AXB18/19/20','AXB13/14','BXA8/17'): # sampleNameAdd = HT.Href(url='/mouseCross.html#AXB/BXA', text=HT.Sup('#'), Class='fs12', target="_blank") sample.extra_info = {} - if self.fd.RISet == 'AXBXA' and sample_name in ('AXB18/19/20','AXB13/14','BXA8/17'): + if self.dataset.group.name == 'AXBXA' and sample_name in ('AXB18/19/20','AXB13/14','BXA8/17'): sample.extra_info['url'] = "/mouseCross.html#AXB/BXA" sample.extra_info['css_class'] = "fs12" @@ -124,7 +124,7 @@ class SampleList(object): WHERE Strain.Name = %s and StrainXRef.StrainId = Strain.Id and InbredSet.Id = StrainXRef.InbredSetId and - InbredSet.Name = %s""", (sample_name, self.fd.RISet)) + InbredSet.Name = %s""", (sample_name, self.dataset.group.name)) sample_id = self.cursor.fetchone()[0] diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 2bc4fc9c..73b438d0 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -7,6 +7,8 @@ import cPickle from collections import OrderedDict +from flask import Flask, g + from htmlgen import HTMLgen2 as HT from base import webqtlConfig @@ -140,10 +142,10 @@ class ShowTrait(templatePage): #else: # hddn['identification'] = "Un-named trait" #If no identification, set identification to un-named - self.dispTraitInformation(fd, "", hddn, this_trait) #Display trait information + function buttons + self.dispTraitInformation(args, "", hddn, this_trait) #Display trait information + function buttons if this_trait == None: - this_trait = webqtlTrait(data=fd.allTraitData, dataset=None) + this_trait = webqtlTrait(data=args['allTraitData'], dataset=None) ## Variance submit page only #if fd.enablevariance and not variance_data_page: @@ -156,15 +158,14 @@ class ShowTrait(templatePage): # print("Calling dispBasicStatistics") # self.dispBasicStatistics(fd, this_trait) - self.build_correlation_tools(fd, this_trait) - + self.build_correlation_tools(args, this_trait) - self.make_sample_lists(fd, variance_data_page, this_trait) + self.make_sample_lists(args, variance_data_page, this_trait) - if fd.allsamplelist: - hddn['allsamplelist'] = string.join(fd.allsamplelist, ' ') + if args['allsamplelist']: + hddn['allsamplelist'] = string.join(args['allsamplelist'], ' ') - if fd.varianceDispName != 'Variance': + if args['varianceDispName'] != 'Variance': hddn['isSE'] = "yes" # We'll need access to this_trait and hddn in the Jinja2 Template, so we put it inside self @@ -172,8 +173,8 @@ class ShowTrait(templatePage): self.hddn = hddn self.sample_group_types = OrderedDict() - self.sample_group_types['samples_primary'] = fd.group + " Only" - self.sample_group_types['samples_other'] = "Non-" + fd.group + self.sample_group_types['samples_primary'] = self.dataset.group.name + " Only" + self.sample_group_types['samples_other'] = "Non-" + self.dataset.group.name self.sample_group_types['samples_all'] = "All Cases" sample_lists = [group.sample_list for group in self.sample_groups] print("sample_lists is:", pf(sample_lists)) @@ -221,7 +222,7 @@ class ShowTrait(templatePage): incf1 = [] if not self.genotype: - self.readGenotype() + self.dataset.read_genotype_file() if not samplelist: if incf1: samplelist = self.f1list + self.samplelist @@ -296,9 +297,9 @@ class ShowTrait(templatePage): print("allTraitData is:", pf(self.allTraitData)) - def dispTraitInformation(self, fd, title1Body, hddn, this_trait): + def dispTraitInformation(self, args, title1Body, hddn, this_trait): - _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) + _Species = webqtlDatabaseFunction.retrieve_species(group=self.dataset.group.name) #tbl = HT.TableLite(cellpadding=2, Class="collap", style="margin-left:20px;", width="840", valign="top", id="target1") @@ -323,31 +324,31 @@ class ShowTrait(templatePage): snpBrowserText = "" updateText = "" - if webqtlConfig.USERDICT[self.privilege] >= webqtlConfig.USERDICT['user']: - - if this_trait==None or this_trait.dataset.type=='Temp': - updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'addPublish');") - updateButton_img = HT.Image("/images/edit_icon.jpg", name="addnew", alt="Add To Publish", title="Add To Publish", style="border:none;") - updateButton.append(updateButton_img) - updateText = "Edit" - elif this_trait.dataset.type != 'Temp': - if this_trait.dataset.type == 'Publish' and this_trait.confidential: #XZ: confidential phenotype trait - if webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=this_trait.authorized_users): - updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'updateRecord');") - updateButton_img = HT.Image("/images/edit_icon.jpg", name="update", alt="Edit", title="Edit", style="border:none;") - updateButton.append(updateButton_img) - updateText = "Edit" - else: - updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'updateRecord');") - updateButton_img = HT.Image("/images/edit_icon.jpg", name="update", alt="Edit", title="Edit", style="border:none;") - updateButton.append(updateButton_img) - updateText = "Edit" - else: - pass + #if webqtlConfig.USERDICT[self.privilege] >= webqtlConfig.USERDICT['user']: + # + # if this_trait==None or this_trait.dataset.type=='Temp': + # updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'addPublish');") + # updateButton_img = HT.Image("/images/edit_icon.jpg", name="addnew", alt="Add To Publish", title="Add To Publish", style="border:none;") + # updateButton.append(updateButton_img) + # updateText = "Edit" + # elif this_trait.dataset.type != 'Temp': + # if this_trait.dataset.type == 'Publish' and this_trait.confidential: #XZ: confidential phenotype trait + # if webqtlUtil.hasAccessToConfidentialPhenotypeTrait(privilege=self.privilege, userName=self.userName, authorized_users=this_trait.authorized_users): + # updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'updateRecord');") + # updateButton_img = HT.Image("/images/edit_icon.jpg", name="update", alt="Edit", title="Edit", style="border:none;") + # updateButton.append(updateButton_img) + # updateText = "Edit" + # else: + # updateButton = HT.Href(url="#redirect", onClick="dataEditingFunc(document.getElementsByName('dataInput')[0],'updateRecord');") + # updateButton_img = HT.Image("/images/edit_icon.jpg", name="update", alt="Edit", title="Edit", style="border:none;") + # updateButton.append(updateButton_img) + # updateText = "Edit" + # else: + # pass - self.cursor.execute('SELECT Name FROM InbredSet WHERE Name="%s"' % fd.group) + g.db.execute("SELECT Name FROM InbredSet WHERE Name=%s", self.dataset.group.name) if this_trait: - addSelectionButton = HT.Href(url="#redirect", onClick="addRmvSelection('%s', document.getElementsByName('%s')[0], 'addToSelection');" % (fd.group, 'dataInput')) + addSelectionButton = HT.Href(url="#redirect", onClick="addRmvSelection('%s', document.getElementsByName('%s')[0], 'addToSelection');" % (self.dataset.group.name, 'dataInput')) addSelectionButton_img = HT.Image("/images/add_icon.jpg", name="addselect", alt="Add To Collection", title="Add To Collection", style="border:none;") #addSelectionButton.append(addSelectionButton_img) addSelectionText = "Add" @@ -373,8 +374,7 @@ class ShowTrait(templatePage): if this_trait.symbol: #XZ: Show SNP Browser only for mouse if _Species == 'mouse': - self.cursor.execute("select geneSymbol from GeneList where geneSymbol = %s", this_trait.symbol) - geneName = self.cursor.fetchone() + geneName = g.db.execute("SELECT geneSymbol FROM GeneList WHERE geneSymbol = %s", this_trait.symbol).fetchone() if geneName: snpurl = os.path.join(webqtlConfig.CGIDIR, "main.py?FormID=SnpBrowserResultPage&submitStatus=1&diffAlleles=True&customStrain=True") + "&geneName=%s" % geneName[0] else: @@ -496,15 +496,14 @@ class ShowTrait(templatePage): if this_trait.dataset.name.find('Liver') >= 0 and this_trait.dataset.name.find('F2') < 0: pass else: + query = """SELECT count(*) + FROM Probe, ProbeSet + WHERE ProbeSet.Name = '%s' AND Probe.ProbeSetId = ProbeSet.Id""" % (this_trait.name) #query database for number of probes associated with trait; if count > 0, set probe tool button and text - self.cursor.execute("""SELECT count(*) - FROM Probe, ProbeSet - WHERE ProbeSet.Name = '%s' AND Probe.ProbeSetId = ProbeSet.Id""" % (this_trait.name)) - - probeResult = self.cursor.fetchone() + probeResult = g.db.execute(query).fetchone() if probeResult[0] > 0: probeurl = "%s?FormID=showProbeInfo&database=%s&ProbeSetID=%s&CellID=%s&group=%s&incparentsf1=ON" \ - % (os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), this_trait.dataset, this_trait.name, this_trait.cellid, fd.group) + % (os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), this_trait.dataset, this_trait.name, this_trait.cellid, self.dataset.group.name) probeButton = HT.Href(url="#", onClick="javascript:openNewWin('%s'); return false;" % probeurl) probeButton_img = HT.Image("/images/probe_icon.jpg", name="probe", alt=" Check sequence of probes ", title=" Check sequence of probes ", style="border:none;") #probeButton.append(probeButton_img) @@ -632,18 +631,21 @@ class ShowTrait(templatePage): #symatlas_species = "Mus musculus" #self.cursor.execute("SELECT chromosome,txStart,txEnd FROM GeneList WHERE geneSymbol = '%s'" % this_trait.symbol) - self.cursor.execute('SELECT chromosome,txStart,txEnd FROM GeneList WHERE geneSymbol = "%s"' % this_trait.symbol) - try: - chr, txst, txen = self.cursor.fetchall()[0] - if chr and txst and txen and this_trait.refseq_transcriptid : - txst = int(txst*1000000) - txen = int(txen*1000000) - tSpan.append(HT.Span(HT.Href(text= 'UCSC',target="mainFrame",\ - title= 'Info from UCSC Genome Browser', url = webqtlConfig.UCSC_REFSEQ % ('mm9',this_trait.refseq_transcriptid,chr,txst,txen), - Class="fs14 fwn"), style=linkStyle) - , " "*2) - except: - pass + #try: + this_chr, txst, txen = g.db.execute("SELECT chromosome,txStart,txEnd FROM GeneList WHERE geneSymbol = %s", (this_trait.symbol)).fetchone() + if this_chr and txst and txen and this_trait.refseq_transcriptid : + txst = int(txst*1000000) + txen = int(txen*1000000) + #tSpan.append(HT.Span(HT.Href(text= 'UCSC',target="mainFrame",\ + # title= 'Info from UCSC Genome Browser', url = webqtlConfig.UCSC_REFSEQ % ('mm9', + # this_trait.refseq_transcriptid, + # this_chr, + # txst, + # txen), + # Class="fs14 fwn"), style=linkStyle) + # , " "*2) + #except: + # pass #XZ, 7/16/2009: The url for SymAtlas (renamed as BioGPS) has changed. We don't need this any more #tSpan.append(HT.Span(HT.Href(text= 'SymAtlas',target="mainFrame",\ @@ -1147,11 +1149,11 @@ class ShowTrait(templatePage): #title2Body.append(submitTable) - def build_correlation_tools(self, fd, this_trait): + def build_correlation_tools(self, this_trait): #species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) - this_group = fd.group + this_group = self.dataset.group.name # We're checking a string here! assert isinstance(this_group, basestring), "We need a string type thing here" @@ -1172,6 +1174,7 @@ class ShowTrait(templatePage): dataset_menu = [] print("[tape4] webqtlConfig.PUBLICTHRESH:", webqtlConfig.PUBLICTHRESH) print("[tape4] type webqtlConfig.PUBLICTHRESH:", type(webqtlConfig.PUBLICTHRESH)) + query = self.cursor.execute('''SELECT PublishFreeze.FullName,PublishFreeze.Name FROM PublishFreeze,InbredSet WHERE PublishFreeze.InbredSetId = InbredSet.Id and InbredSet.Name = %s and PublishFreeze.public > %s''', @@ -1610,10 +1613,10 @@ class ShowTrait(templatePage): def make_sample_lists(self, fd, variance_data_page, this_trait): - if fd.parlist: - all_samples_ordered = fd.parlist + fd.f1list + fd.samplelist + if args['parlist']: + all_samples_ordered = args['parlist'] + args['f1list'] + args['samplelist'] else: - all_samples_ordered = fd.f1list + fd.samplelist + all_samples_ordered = args['f1list'] + args['samplelist'] this_trait_samples = set(this_trait.data.keys()) @@ -1622,12 +1625,12 @@ class ShowTrait(templatePage): print("-*- primary_samplelist is:", pf(primary_sample_names)) primary_samples = SampleList(self.cursor, - fd=fd, + args=args, variance_data_page=variance_data_page, sample_names=primary_sample_names, this_trait=this_trait, sample_group_type='primary', - header="%s Only" % (fd.group)) + header="%s Only" % (self.dataset.group.name)) other_sample_names = [] for sample in this_trait.data.keys(): -- cgit v1.2.3 From 2ec627563efa9fdf4fc74cb4764bfebb5ab90933 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 6 Dec 2012 15:39:28 -0600 Subject: Got show_trait running again for MrnaAssay traits --- wqflask/base/data_set.py | 37 +++++++------- wqflask/wqflask/show_trait/SampleList.py | 48 +++++++++--------- wqflask/wqflask/show_trait/show_trait.py | 81 ++++++++++++++----------------- wqflask/wqflask/templates/show_trait.html | 2 +- 4 files changed, 80 insertions(+), 88 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 14e4055e..9f0f3fac 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -77,6 +77,9 @@ class DatasetGroup(object): self.name = "BXD" self.incparentsf1 = False + self.f1list = None + self.parlist = None + self.allsamples = None #def read_genotype(self): @@ -95,39 +98,39 @@ class DatasetGroup(object): #genotype_1 is Dataset Object without parents and f1 #genotype_2 is Dataset Object with parents and f1 (not for intercross) - self.genotype_1 = reaper.Dataset() + genotype_1 = reaper.Dataset() # reaper barfs on unicode filenames, so here we ensure it's a string full_filename = str(os.path.join(webqtlConfig.GENODIR, self.name + '.geno')) - self.genotype_1.read(full_filename) + genotype_1.read(full_filename) print("Got to after read") try: # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py; - _f1, _f12, _mat, _pat = webqtlUtil.ParInfo[self.name] + f1, f12, maternal, paternal = webqtlUtil.ParInfo[self.name] except KeyError: - _f1 = _f12 = _mat = _pat = None + f1 = f12 = maternal = paternal = None - self.genotype_2 = self.genotype_1 - if self.genotype_1.type == "group" and _mat and _pat: - self.genotype_2 = self.genotype_1.add(Mat=_mat, Pat=_pat) #, F1=_f1) + + if genotype_1.type == "group" and maternal and paternal: + genotype_2 = genotype_1.add(Mat=maternal, Pat=paternal) #, F1=_f1) + else: + genotype_2 = genotype_1 #determine default genotype object - if self.incparentsf1 and self.genotype_1.type != "intercross": - self.genotype = self.genotype_2 + if self.incparentsf1 and genotype_1.type != "intercross": + self.genotype = genotype_2 else: self.incparentsf1 = 0 - self.genotype = self.genotype_1 + self.genotype = genotype_1 self.samplelist = list(self.genotype.prgy) - self.f1list = [] - self.parlist = [] - if _f1 and _f12: - self.f1list = [_f1, _f12] - if _mat and _pat: - self.parlist = [_mat, _pat] + if f1 and f12: + self.f1list = [f1, f12] + if maternal and paternal: + self.parlist = [maternal, paternal] class DataSet(object): @@ -169,8 +172,6 @@ class DataSet(object): # self.get_group() # # return self._group - - diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 53bb24fb..0c752617 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -1,5 +1,7 @@ from __future__ import absolute_import, print_function, division +from flask import Flask, g + from base import webqtlCaseData from utility import webqtlUtil, Plot, Bunch from base.webqtlTrait import GeneralTrait @@ -8,22 +10,19 @@ from pprint import pformat as pf class SampleList(object): def __init__(self, - cursor, dataset, - variance_data_page, sample_names, this_trait, sample_group_type, header): - - self.cursor = cursor + self.dataset = dataset self.this_trait = this_trait self.sample_group_type = sample_group_type # primary or other self.header = header self.sample_list = [] # The actual list - + self.get_attributes() print("camera: attributes are:", pf(self.attributes)) @@ -54,7 +53,7 @@ class SampleList(object): sample.this_id = "Other_" + str(counter) #### For extra attribute columns; currently only used by several datasets - Zach - if self.this_trait and self.this_trait.db and self.this_trait.db.type == 'ProbeSet': + if self.this_trait and self.dataset and self.dataset.type == 'ProbeSet': sample.extra_attributes = self.get_extra_attribute_values(sample_name) print("sample.extra_attributes is", pf(sample.extra_attributes)) self.sample_list.append(sample) @@ -88,27 +87,27 @@ class SampleList(object): def get_attributes(self): """Finds which extra attributes apply to this dataset""" - #ZS: Id and name values for this trait's extra attributes - self.cursor.execute('''SELECT CaseAttribute.Id, CaseAttribute.Name + #ZS: Id and name values for this trait's extra attributes + case_attributes = g.db.execute('''SELECT CaseAttribute.Id, CaseAttribute.Name FROM CaseAttribute, CaseAttributeXRef WHERE CaseAttributeXRef.ProbeSetFreezeId = %s AND CaseAttribute.Id = CaseAttributeXRef.CaseAttributeId group by CaseAttributeXRef.CaseAttributeId''', - (str(self.this_trait.db.id),)) + (str(self.dataset.id),)) self.attributes = {} - for key, value in self.cursor.fetchall(): + for key, value in case_attributes.fetchall(): print("radish: %s - %s" % (key, value)) self.attributes[key] = Bunch() self.attributes[key].name = value - self.cursor.execute('''SELECT DISTINCT CaseAttributeXRef.Value - FROM CaseAttribute, CaseAttributeXRef - WHERE CaseAttribute.Name = %s AND - CaseAttributeXRef.CaseAttributeId = CaseAttribute.Id''', (value,)) + attribute_values = g.db.execute('''SELECT DISTINCT CaseAttributeXRef.Value + FROM CaseAttribute, CaseAttributeXRef + WHERE CaseAttribute.Name = %s AND + CaseAttributeXRef.CaseAttributeId = CaseAttribute.Id''', (value,)) - self.attributes[key].distinct_values = [item[0] for item in self.cursor.fetchall()] + self.attributes[key].distinct_values = [item[0] for item in attribute_values.fetchall()] self.attributes[key].distinct_values.sort(key=natural_sort_key) @@ -119,27 +118,28 @@ class SampleList(object): if self.attributes: #ZS: Get StrainId value for the next query - self.cursor.execute("""SELECT Strain.Id + result = g.db.execute("""SELECT Strain.Id FROM Strain, StrainXRef, InbredSet WHERE Strain.Name = %s and StrainXRef.StrainId = Strain.Id and InbredSet.Id = StrainXRef.InbredSetId and - InbredSet.Name = %s""", (sample_name, self.dataset.group.name)) + InbredSet.Name = %s""", (sample_name, + self.dataset.group.name)) - sample_id = self.cursor.fetchone()[0] + sample_id = result.fetchone().Id for attribute in self.attributes: #ZS: Add extra case attribute values (if any) - self.cursor.execute("""SELECT Value + result = g.db.execute("""SELECT Value FROM CaseAttributeXRef - WHERE ProbeSetFreezeId = '%s' AND - StrainId = '%s' AND - CaseAttributeId = '%s' - group by CaseAttributeXRef.CaseAttributeId""" % ( + WHERE ProbeSetFreezeId = %s AND + StrainId = %s AND + CaseAttributeId = %s + group by CaseAttributeXRef.CaseAttributeId""", ( self.this_trait.db.id, sample_id, str(attribute))) - attribute_value = self.cursor.fetchone()[0] #Trait-specific attributes, if any + attribute_value = result.fetchone().Value #Trait-specific attributes, if any #ZS: If it's an int, turn it into one for sorting #(for example, 101 would be lower than 80 if they're strings instead of ints) diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 73b438d0..cd61d70c 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -55,7 +55,7 @@ class ShowTrait(templatePage): #fd.readGenotype() if not self.dataset.group.genotype: - self.read_data(incf1=1) + self.read_data(include_f1=True) #incf1=1) ## determine data editing page format #variance_data_page = 0 @@ -158,15 +158,15 @@ class ShowTrait(templatePage): # print("Calling dispBasicStatistics") # self.dispBasicStatistics(fd, this_trait) - self.build_correlation_tools(args, this_trait) + self.build_correlation_tools(this_trait) - self.make_sample_lists(args, variance_data_page, this_trait) + self.make_sample_lists(this_trait) - if args['allsamplelist']: - hddn['allsamplelist'] = string.join(args['allsamplelist'], ' ') + if self.dataset.group.allsamples: + hddn['allsamples'] = string.join(self.dataset.group.allsamples, ' ') - if args['varianceDispName'] != 'Variance': - hddn['isSE'] = "yes" + #if args['varianceDispName'] != 'Variance': + # hddn['isSE'] = "yes" # We'll need access to this_trait and hddn in the Jinja2 Template, so we put it inside self self.this_trait = this_trait @@ -215,22 +215,20 @@ class ShowTrait(templatePage): return this_trait - def read_data(self): + def read_data(self, include_f1=False): '''read user input data or from trait data and analysis form''' - if incf1 == None: - incf1 = [] + #if incf1 == None: + # incf1 = [] if not self.genotype: self.dataset.read_genotype_file() if not samplelist: - if incf1: + if include_f1: samplelist = self.f1list + self.samplelist else: samplelist = self.samplelist - #print("before traitfiledata self.traitfile is:", pf(self.traitfile)) - traitfiledata = getattr(self, "traitfile", None) traitpastedata = getattr(self, "traitpaste", None) variancefiledata = getattr(self, "variancefile", None) @@ -1174,39 +1172,34 @@ class ShowTrait(templatePage): dataset_menu = [] print("[tape4] webqtlConfig.PUBLICTHRESH:", webqtlConfig.PUBLICTHRESH) print("[tape4] type webqtlConfig.PUBLICTHRESH:", type(webqtlConfig.PUBLICTHRESH)) - query = - self.cursor.execute('''SELECT PublishFreeze.FullName,PublishFreeze.Name FROM + results = g.db.execute("""SELECT PublishFreeze.FullName,PublishFreeze.Name FROM PublishFreeze,InbredSet WHERE PublishFreeze.InbredSetId = InbredSet.Id - and InbredSet.Name = %s and PublishFreeze.public > %s''', + and InbredSet.Name = %s and PublishFreeze.public > %s""", (this_group, webqtlConfig.PUBLICTHRESH)) - for item in self.cursor.fetchall(): + for item in results.fetchall(): dataset_menu.append(dict(tissue=None, datasets=[item])) - self.cursor.execute('''SELECT GenoFreeze.FullName,GenoFreeze.Name FROM GenoFreeze, + results = g.db.execute("""SELECT GenoFreeze.FullName,GenoFreeze.Name FROM GenoFreeze, InbredSet WHERE GenoFreeze.InbredSetId = InbredSet.Id and InbredSet.Name = - %s and GenoFreeze.public > %s''', + %s and GenoFreeze.public > %s""", (this_group, webqtlConfig.PUBLICTHRESH)) - for item in self.cursor.fetchall(): + for item in results.fetchall(): dataset_menu.append(dict(tissue=None, datasets=[item])) #03/09/2009: Xiaodong changed the SQL query to order by Name as requested by Rob. - self.cursor.execute('SELECT Id, Name FROM Tissue order by Name') - for item in self.cursor.fetchall(): + tissues = g.db.execute("SELECT Id, Name FROM Tissue order by Name") + for item in tissues.fetchall(): tissue_id, tissue_name = item #databaseMenuSub = HT.Optgroup(label = '%s ------' % tissue_name) #dataset_sub_menu = [] - print("phun9") - self.cursor.execute('''SELECT ProbeSetFreeze.FullName,ProbeSetFreeze.Name FROM ProbeSetFreeze, ProbeFreeze, + data_sets = g.db.execute('''SELECT ProbeSetFreeze.FullName,ProbeSetFreeze.Name FROM ProbeSetFreeze, ProbeFreeze, InbredSet WHERE ProbeSetFreeze.ProbeFreezeId = ProbeFreeze.Id and ProbeFreeze.TissueId = %s and ProbeSetFreeze.public > %s and ProbeFreeze.InbredSetId = InbredSet.Id and InbredSet.Name like %s order by ProbeSetFreeze.CreateTime desc, ProbeSetFreeze.AvgId ''', (tissue_id, webqtlConfig.PUBLICTHRESH, "%" + this_group + "%")) - print("phun8") - dataset_sub_menu = [item for item in self.cursor.fetchall() if item] - #for item2 in self.cursor.fetchall(): - # dataset_sub_menu.append(item2) + dataset_sub_menu = [item for item in data_sets.fetchall() if item] if dataset_sub_menu: dataset_menu.append(dict(tissue=tissue_name, datasets=dataset_sub_menu)) @@ -1612,11 +1605,13 @@ class ShowTrait(templatePage): title4Body.append(submitTable) - def make_sample_lists(self, fd, variance_data_page, this_trait): - if args['parlist']: - all_samples_ordered = args['parlist'] + args['f1list'] + args['samplelist'] + def make_sample_lists(self, this_trait): + if self.dataset.group.parlist: + all_samples_ordered = (self.dataset.group.parlist + + self.dataset.group.f1list + + self.dataset.group.samplelist) else: - all_samples_ordered = args['f1list'] + args['samplelist'] + all_samples_ordered = self.dataset.group.f1list + self.dataset.group.samplelist this_trait_samples = set(this_trait.data.keys()) @@ -1624,9 +1619,7 @@ class ShowTrait(templatePage): print("-*- primary_samplelist is:", pf(primary_sample_names)) - primary_samples = SampleList(self.cursor, - args=args, - variance_data_page=variance_data_page, + primary_samples = SampleList(dataset = self.dataset, sample_names=primary_sample_names, this_trait=this_trait, sample_group_type='primary', @@ -1640,18 +1633,16 @@ class ShowTrait(templatePage): other_sample_names.append(sample) if other_sample_names: - par_f1_samples = fd.parlist + fd.f1list + parent_f1_samples = self.dataset.group.parlist + self.dataset.group.f1list other_sample_names.sort() #Sort other samples - other_sample_names = par_f1_samples + other_sample_names + other_sample_names = parent_f1_samples + other_sample_names - other_samples = SampleList(self.cursor, - fd=fd, - variance_data_page=variance_data_page, - sample_names=other_sample_names, - this_trait=this_trait, - sample_group_type='other', - header="Non-%s" % (fd.group)) + other_samples = SampleList(dataset=self.dataset, + sample_names=other_sample_names, + this_trait=this_trait, + sample_group_type='other', + header="Non-%s" % (self.dataset.group.name)) self.sample_groups = (primary_samples, other_samples) else: @@ -1661,4 +1652,4 @@ class ShowTrait(templatePage): #if (other_sample_names or (fd.f1list and this_trait.data.has_key(fd.f1list[0])) # or (fd.f1list and this_trait.data.has_key(fd.f1list[1]))): # print("hjs") - fd.allsamplelist = all_samples_ordered + self.dataset.group.allsamples = all_samples_ordered diff --git a/wqflask/wqflask/templates/show_trait.html b/wqflask/wqflask/templates/show_trait.html index b5c1d6ac..163be69c 100644 --- a/wqflask/wqflask/templates/show_trait.html +++ b/wqflask/wqflask/templates/show_trait.html @@ -19,7 +19,7 @@
    -- cgit v1.2.3 From 41721f0d386c034470fe68e0017295474242ab48 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 6 Dec 2012 16:50:20 -0600 Subject: Added minimum/maximum to basic statistics table Continued bug-shooting related to getting show_trait running with phenotype traits --- wqflask/base/data_set.py | 32 +++++--- wqflask/wqflask/do_search.py | 5 +- wqflask/wqflask/search_results.py | 7 -- wqflask/wqflask/show_trait/show_trait.py | 7 +- .../static/new/javascript/show_trait.coffee | 86 ++++++++++++---------- .../wqflask/static/new/javascript/show_trait.js | 10 ++- wqflask/wqflask/static/new/javascript/stats.coffee | 31 +++++--- wqflask/wqflask/static/new/javascript/stats.js | 18 ++++- wqflask/wqflask/templates/search_result_page.html | 4 +- wqflask/wqflask/templates/show_trait.html | 2 +- wqflask/wqflask/templates/show_trait_details.html | 2 +- .../wqflask/templates/show_trait_edit_data.html | 2 +- 12 files changed, 122 insertions(+), 84 deletions(-) (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 9f0f3fac..2182fe9e 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -30,6 +30,7 @@ from htmlgen import HTMLgen2 as HT import reaper import webqtlConfig +from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil from MySQLdb import escape_string as escape @@ -72,10 +73,12 @@ class DatasetGroup(object): """ def __init__(self, dataset): """This sets self.group and self.group_id""" - self.name, self.group_id = g.db.execute(dataset.query).fetchone() + self.name, self.id = g.db.execute(dataset.query_for_group).fetchone() if self.name == 'BXD300': self.name = "BXD" + self.species = webqtlDatabaseFunction.retrieve_species(self.name) + self.incparentsf1 = False self.f1list = None self.parlist = None @@ -151,14 +154,25 @@ class DataSet(object): self.check_confidentiality() - self.retrieve_name() + self.retrieve_other_names() self.group = DatasetGroup(self) # sets self.group and self.group_id def get_desc(self): """Gets overridden later, at least for Temp...used by trait's get_given_name""" return None - + + #@staticmethod + #def get_by_trait_id(trait_id): + # """Gets the dataset object given the trait id""" + # + # + # + # name = g.db.execute(""" SELECT + # + # """) + # + # return DataSet(name) # Delete this eventually @property @@ -175,7 +189,7 @@ class DataSet(object): - def retrieve_name(self): + def retrieve_other_names(self): """ If the data set name parameter is not found in the 'Name' field of the data set table, check if it is actually the FullName or ShortName instead. @@ -326,7 +340,7 @@ class PhenotypeDataSet(DataSet): def retrieve_sample_data(self, trait): query = """ SELECT - Strain.Name, PublishData.value, PublishSE.error, NStrain.count, PublishData.Id + Strain.Name, PublishData.value, PublishSE.error, NStrain.count FROM (PublishData, Strain, PublishXRef, PublishFreeze) left join PublishSE on @@ -340,7 +354,7 @@ class PhenotypeDataSet(DataSet): PublishFreeze.Id = %d AND PublishData.StrainId = Strain.Id Order BY Strain.Name - """ % (self.trait.name, self.id) + """ % (trait.name, self.id) results = g.db.execute(query).fetchall() return results @@ -368,7 +382,7 @@ class GenotypeDataSet(DataSet): # Todo: Obsolete or rename this field self.type = 'Geno' - self.query = ''' + self.query_for_group = ''' SELECT InbredSet.Name, InbredSet.Id FROM @@ -418,7 +432,7 @@ class GenotypeDataSet(DataSet): GenoData.StrainId = Strain.Id Order BY Strain.Name - """ % (webqtlDatabaseFunction.retrieve_species_id(self.group), trait.name, self.name) + """ % (webqtlDatabaseFunction.retrieve_species_id(self.group.name), trait.name, self.name) results = g.db.execute(query).fetchall() return results @@ -476,7 +490,7 @@ class MrnaAssayDataSet(DataSet): # Todo: Obsolete or rename this field self.type = 'ProbeSet' - self.query = ''' + self.query_for_group = ''' SELECT InbredSet.Name, InbredSet.Id FROM diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index 69602748..403f1b5e 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -177,9 +177,6 @@ class PhenotypeSearch(DoSearch): from_clause = self.normalize_spaces(from_clause) - #Get group information for dataset - self.dataset.get_group() - query = (self.base_query + """%s WHERE %s @@ -189,7 +186,7 @@ class PhenotypeSearch(DoSearch): PublishFreeze.Id = %s""" % ( from_clause, where_clause, - escape(str(self.dataset.group_id)), + escape(str(self.dataset.group.id)), escape(str(self.dataset.id)))) print("query is:", pf(query)) diff --git a/wqflask/wqflask/search_results.py b/wqflask/wqflask/search_results.py index b518ab99..5cb8e314 100644 --- a/wqflask/wqflask/search_results.py +++ b/wqflask/wqflask/search_results.py @@ -4,13 +4,6 @@ from wqflask import app from flask import render_template -################################################### -# # -# This file uses only spaces for indentation # -# # -################################################### - -#import string import os import cPickle import re diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index cd61d70c..d1c60877 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -32,13 +32,16 @@ from pprint import pformat as pf -class ShowTrait(templatePage): +class ShowTrait(object): def __init__(self, args): print("in ShowTrait, args are:", args) #self.group = args.group self.trait_id = args['trait_id'] + self.dataset = create_dataset(args['dataset']) + + #self.dataset = create_dataset(args['dataset']) self.cell_id = None #assert self.openMysql(), "No database!" @@ -817,7 +820,7 @@ class ShowTrait(templatePage): # )) pass - menuTable = HT.TableLite(cellpadding=2, Class="collap", width="150", id="target1") + #menuTable = HT.TableLite(cellpadding=2, Class="collap", width="150", id="target1") #menuTable.append(HT.TR(HT.TD(addSelectionButton, align="center"),HT.TD(updateButton, align="center"), colspan=3, height=50, style="vertical-align:bottom;")) #menuTable.append(HT.TR(HT.TD(addSelectionText, align="center"),HT.TD(updateText, align="center"), colspan=3, height=50, style="vertical-align:bottom;")) diff --git a/wqflask/wqflask/static/new/javascript/show_trait.coffee b/wqflask/wqflask/static/new/javascript/show_trait.coffee index 6e22119f..3d9fcd5a 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait.coffee +++ b/wqflask/wqflask/static/new/javascript/show_trait.coffee @@ -23,15 +23,15 @@ $ -> id = "#" + process_id(category, value_type) console.log("the_id:", id) in_box = $(id).html - + current_value = parseFloat($(in_box)).toFixed(decimal_places) - + console.log("urgh:", category, value_type) the_value = sample_sets[category][value_type]() console.log("After running sample_sets, the_value is:", the_value) if decimal_places > 0 the_value = the_value.toFixed(decimal_places) - + console.log("*-* the_value:", the_value) console.log("*-* current_value:", current_value) if the_value != current_value @@ -40,16 +40,16 @@ $ -> update_stat_values = (sample_sets)-> for category in ['samples_primary', 'samples_other', 'samples_all'] change_stats_value(sample_sets, category, "n_of_samples", 0) - for stat in ["mean", "median", "std_dev", "std_error"] + for stat in ["mean", "median", "std_dev", "std_error", "min", "max"] console.log("Calling change_stats_value") change_stats_value(sample_sets, category, stat, 2) - edit_data_change = -> + edit_data_change = -> sample_sets = samples_primary: new Stats([]) samples_other: new Stats([]) samples_all: new Stats([]) - + console.log("at beginning:", sample_sets) # ########## @@ -114,6 +114,14 @@ $ -> { vn: "std_dev" pretty: "Standard Deviation (SD)" + }, + { + vn: "min" + pretty: "Minimum" + }, + { + vn: "max" + pretty: "Maximum" } ] @@ -150,8 +158,8 @@ $ -> processed += "-" processed += value return processed - - + + show_hide_outliers = -> console.log("FOOBAR in beginning of show_hide_outliers") label = $('#show_hide_outliers').val() @@ -163,10 +171,10 @@ $ -> $('#show_hide_outliers').val("Hide Outliers") console.log("Should be now Hide Outliers") - + ##Calculate Correlations Code - - + + on_corr_method_change = -> console.log("in beginning of on_corr_method_change") corr_method = $('select[name=corr_method]').val() @@ -179,15 +187,15 @@ $ -> $("#corr_sample_method_options").show() $('select[name=corr_method]').change(on_corr_method_change) - - + + ##End Calculate Correlations Code - + ##Populate Samples Attribute Values Code - + create_value_dropdown = (value) -> return """""" - + populate_sample_attributes_values_dropdown = -> console.log("in beginning of psavd") $('#attribute_values').empty() @@ -205,14 +213,14 @@ $ -> if js_data.attribute_names.length > 0 populate_sample_attributes_values_dropdown() $('#exclude_menu').change(populate_sample_attributes_values_dropdown) - + ##End Populate Samples Attribute Values Codess ##Block Samples By Attribute Value Code block_by_attribute_value = -> attribute_name = $('#exclude_menu').val() exclude_by_value = $('#attribute_values').val() - + cell_class = ".column_name-#{attribute_name}" $(cell_class).each (index, element) => if $.trim($(element).text()) == exclude_by_value @@ -220,11 +228,11 @@ $ -> $(row).find(".trait_value_input").val("x") $('#exclude_group').click(block_by_attribute_value) - + ##End Block Samples By Attribute Value Code - + ##Block Samples By Index Code - + block_by_index = -> index_string = $('#remove_samples_field').val() index_list = [] @@ -241,7 +249,7 @@ $ -> index = parseInt(index_set) console.log("index:", index) index_list.push(index) - #catch(erro) + #catch(erro) # alert("Syntax error") console.log("index_list:", index_list) for index in index_list @@ -251,33 +259,33 @@ $ -> $('#Primary_'+index.toString()).find('.trait_value_input').val("x") else if $('#block_group').val() == "other" console.log("block_group:", $('#block_group').val()) - console.log("row:", $('#Other_'+index.toString())) + console.log("row:", $('#Other_'+index.toString())) $('#Other_'+index.toString()).find('.trait_value_input').val("x") - + $('#block_by_index').click(block_by_index) ##End Block Samples By Index Code - + ##Hide Sample Rows With No Value (value of 'x') Code - + hide_no_value = -> $('.value_se').each (_index, element) => if $(element).find('.trait_value_input').val() == 'x' $(element).hide() - + $('#hide_no_value').click(hide_no_value) ##End Hide Sample Rows With No Value Code - + ##Block Outliers Code block_outliers = -> $('.outlier').each (_index, element) => $(element).find('.trait_value_input').val('x') - + $('#block_outliers').click(block_outliers) - + ##End Block Outliers Code - + ##Reset Table Values Code reset_samples_table = -> $('.trait_value_input').each (_index, element) => @@ -289,9 +297,9 @@ $ -> $('#reset').click(reset_samples_table) ##End Reset Table Values Code - + ##Get Sample Data From Table Code - + get_sample_table_data = -> samples = {} primary_samples = [] @@ -315,27 +323,27 @@ $ -> ##End Get Sample Data from Table Code ##Export Sample Table Data Code - + export_sample_table_data = -> sample_data = get_sample_table_data() console.log("sample_data is:", sample_data) json_sample_data = JSON.stringify(sample_data) console.log("json_sample_data is:", json_sample_data) - + $('input[name=export_data]').val(json_sample_data) console.log("export_data is", $('input[name=export_data]').val()) - + format = $('#export_format').val() if format == "excel" $('#trait_data_form').attr('action', '/export_trait_excel') else $('#trait_data_form').attr('action', '/export_trait_csv') console.log("action is:", $('#trait_data_form').attr('action')) - + $('#trait_data_form').submit() $('#export').click(export_sample_table_data) - + ##End Export Sample Table Data Code @@ -344,7 +352,7 @@ $ -> console.log("after registering block_outliers") _.mixin(_.str.exports()); # Add string fuctions directly to underscore - $('#value_table').change(edit_data_change) + $('#edit_sample_lists').change(edit_data_change) console.log("loaded") #console.log("basic_table is:", basic_table) # Add back following two lines later diff --git a/wqflask/wqflask/static/new/javascript/show_trait.js b/wqflask/wqflask/static/new/javascript/show_trait.js index 919bc766..84282aef 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait.js +++ b/wqflask/wqflask/static/new/javascript/show_trait.js @@ -53,7 +53,7 @@ change_stats_value(sample_sets, category, "n_of_samples", 0); _results.push((function() { var _j, _len1, _ref1, _results1; - _ref1 = ["mean", "median", "std_dev", "std_error"]; + _ref1 = ["mean", "median", "std_dev", "std_error", "min", "max"]; _results1 = []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { stat = _ref1[_j]; @@ -126,6 +126,12 @@ }, { vn: "std_dev", pretty: "Standard Deviation (SD)" + }, { + vn: "min", + pretty: "Minimum" + }, { + vn: "max", + pretty: "Maximum" } ]; console.log("rows are:", rows); @@ -362,7 +368,7 @@ $('#block_outliers').click(block_outliers); console.log("after registering block_outliers"); _.mixin(_.str.exports()); - $('#value_table').change(edit_data_change); + $('#edit_sample_lists').change(edit_data_change); console.log("loaded"); make_table(); edit_data_change(); diff --git a/wqflask/wqflask/static/new/javascript/stats.coffee b/wqflask/wqflask/static/new/javascript/stats.coffee index 677dc258..118ee7a8 100644 --- a/wqflask/wqflask/static/new/javascript/stats.coffee +++ b/wqflask/wqflask/static/new/javascript/stats.coffee @@ -1,20 +1,20 @@ class Stats constructor: (@the_values) -> - + add_value: (value) -> @the_values.push(value) - + n_of_samples: -> return @the_values.length - + sum: -> total = 0 total += value for value in @the_values return total - + mean: -> return @sum() / @n_of_samples() - + median: -> is_odd = @the_values.length % 2 median_position = Math.floor(@the_values.length / 2) @@ -24,7 +24,7 @@ class Stats else return (the_values_sorted[median_position] + the_values_sorted[median_position - 1]) / 2 - + std_dev: -> sum = 0 for value in @the_values @@ -32,15 +32,22 @@ class Stats sum += step_a step_b = sum / @the_values.length return Math.sqrt(step_b) - + std_error: -> return @std_dev() / Math.sqrt(@n_of_samples()) + min: -> + return Math.min(@the_values...) + + max: -> + return Math.max(@the_values...) + bxd_only = new Stats([3, 5, 7, 8]) -console.log("[red] bxd_only mean:", bxd_only.mean()) -console.log("[green] bxd_only median:", bxd_only.median()) -console.log("[purple] bxd_only std_dev:", bxd_only.std_dev()) -console.log("[magenta] bxd_only std_error:", bxd_only.std_error()) +console.log("[xred] bxd_only mean:", bxd_only.mean()) +console.log("[xgreen] bxd_only median:", bxd_only.median()) +console.log("[xpurple] bxd_only std_dev:", bxd_only.std_dev()) +console.log("[xmagenta] bxd_only std_error:", bxd_only.std_error()) +console.log("[xyellow] bxd_only min:", bxd_only.min()) -window.Stats = Stats \ No newline at end of file +window.Stats = Stats diff --git a/wqflask/wqflask/static/new/javascript/stats.js b/wqflask/wqflask/static/new/javascript/stats.js index f95d03d4..620f7d5d 100644 --- a/wqflask/wqflask/static/new/javascript/stats.js +++ b/wqflask/wqflask/static/new/javascript/stats.js @@ -62,19 +62,29 @@ return this.std_dev() / Math.sqrt(this.n_of_samples()); }; + Stats.prototype.min = function() { + return Math.min.apply(Math, this.the_values); + }; + + Stats.prototype.max = function() { + return Math.max.apply(Math, this.the_values); + }; + return Stats; })(); bxd_only = new Stats([3, 5, 7, 8]); - console.log("[red] bxd_only mean:", bxd_only.mean()); + console.log("[xred] bxd_only mean:", bxd_only.mean()); + + console.log("[xgreen] bxd_only median:", bxd_only.median()); - console.log("[green] bxd_only median:", bxd_only.median()); + console.log("[xpurple] bxd_only std_dev:", bxd_only.std_dev()); - console.log("[purple] bxd_only std_dev:", bxd_only.std_dev()); + console.log("[xmagenta] bxd_only std_error:", bxd_only.std_error()); - console.log("[magenta] bxd_only std_error:", bxd_only.std_error()); + console.log("[xyellow] bxd_only min:", bxd_only.min()); window.Stats = Stats; diff --git a/wqflask/wqflask/templates/search_result_page.html b/wqflask/wqflask/templates/search_result_page.html index 35ff4e8e..11f68bba 100644 --- a/wqflask/wqflask/templates/search_result_page.html +++ b/wqflask/wqflask/templates/search_result_page.html @@ -51,9 +51,9 @@ + dataset = dataset.name + )}}"> {{ this_trait.name }} diff --git a/wqflask/wqflask/templates/show_trait.html b/wqflask/wqflask/templates/show_trait.html index 163be69c..28341186 100644 --- a/wqflask/wqflask/templates/show_trait.html +++ b/wqflask/wqflask/templates/show_trait.html @@ -18,7 +18,7 @@
    -
    +
    {% for sample_type in sample_groups %}

    {{ sample_type.header }}

    -- cgit v1.2.3 From daea7eb0eb4eef445140275522703e7e2214154b Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Tue, 18 Dec 2012 17:15:09 -0600 Subject: Created new file species.py and species class object TheSpecies Converted html for the mapping tabs to bootstrap and redid html inside of the Interval Mapping tab Added text input for # of permutation tests and bootstrap tests --- wqflask/base/data_set.py | 8 +- wqflask/base/species.py | 16 + wqflask/wqflask/show_trait/show_trait.py | 3 +- .../templates/show_trait_mapping_tools.html | 551 +++++++-------------- wqflask/wqflask/views.py | 8 + 5 files changed, 218 insertions(+), 368 deletions(-) create mode 100644 wqflask/base/species.py (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 2182fe9e..612b9209 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -30,6 +30,7 @@ from htmlgen import HTMLgen2 as HT import reaper import webqtlConfig +from base import species from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil @@ -145,7 +146,7 @@ class DataSet(object): def __init__(self, name): - assert name + assert name, "Need a name" self.name = name self.id = None self.type = None @@ -155,7 +156,10 @@ class DataSet(object): self.check_confidentiality() self.retrieve_other_names() - self.group = DatasetGroup(self) # sets self.group and self.group_id + + self.species = species.TheSpecies(self) + self.group = DatasetGroup(self) # sets self.group and self.group_id and gets genotype + def get_desc(self): diff --git a/wqflask/base/species.py b/wqflask/base/species.py new file mode 100644 index 00000000..98941ce5 --- /dev/null +++ b/wqflask/base/species.py @@ -0,0 +1,16 @@ +from __future__ import print_function, division + + +class TheSpecies(object): + def __init__(self, dataset): + self.dataset = dataset + + @property + def chromosomes(self): + chromosomes = [("All", -1)] + + for counter, genotype in enumerate(self.dataset.group.genotype): + if len(genotype) > 1: + chromosomes.append((genotype.name, counter)) + + return chromosomes diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index 836d37ea..c605cb58 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -1354,7 +1354,8 @@ class ShowTrait(object): return_results_menu_selected = return_results_menu_selected,) - def dispMappingTools(self, fd, title4Body, this_trait): + def build_mapping_tools(self, this_trait): + _Species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, group=fd.group) diff --git a/wqflask/wqflask/templates/show_trait_mapping_tools.html b/wqflask/wqflask/templates/show_trait_mapping_tools.html index 90498e9a..1109ded3 100644 --- a/wqflask/wqflask/templates/show_trait_mapping_tools.html +++ b/wqflask/wqflask/templates/show_trait_mapping_tools.html @@ -1,382 +1,203 @@ -

      Mapping Tools

    - -

    - - - -
    -
    - - -
    - - - - - - +
    +

    Mapping Tools

    + +
    + + +
    +
    + + +
    + {% if dataset.group.genotype.Mbmap %} + + + {% endif %} +
    + + + + +
    + + +
    + +
    +
    +
    - - - - - - - - - - - - -
    Chromosome:
    Mapping Scale:

    - Permutation Test (n=2000)
    - Bootstrap Test (n=2000)
    - Use Parents
    - Use Weighted
    -
    -
    -
    -
    + + + + + + + +
    + + + + + + + + + + + + + + + + + + +
    Display LRS greater than:
    Display all LRS
    Use Parents
    Use Weighted

    +
    +
    +
    Marker regression computes and displays LRS + values for individual markers.
    + This function also lists additive effects (phenotype units per allele) and
    + dominance deviations for some datasets.

    +
    + +
    + + + + + + + + +
    + + + + + + + + + + + + +
    Sort by:
    Return:

    + Permutation Test + (n=500)
    +
    +
    +
    +
    Pair-Scan searches for pairs of chromosomal regions + that are
    + involved in two-locus epistatic interactions.

    +
    +
    + + + + diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 70d8cd20..503b0972 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -160,6 +160,14 @@ def corr_compute_page(): print("Made it to rendering") return render_template("correlation_page.html", **template_vars.__dict__) +@app.route("/int_mapping", methods=('POST',)) +def interval_mapping_page(): + fd = webqtlFormData.webqtlFormData(request.form) + print("Have fd") + template_vars = CorrelationPage.CorrelationPage(fd) + print("Made it to rendering") + return render_template("correlation_page.html", **template_vars.__dict__) + # Todo: Can we simplify this? -Sam def sharing_info_page(): -- cgit v1.2.3 From 25fccfb3447012c3f2a75e3f54520e700d801487 Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 3 Jan 2013 15:54:53 -0600 Subject: Created template for marek regression page and made the compute button direct to it added asbolute_import in data_set.py and trait.py Made several minor changes and deleted commented out code in trait.py --- wqflask/base/data_set.py | 7 +- wqflask/base/trait.py | 78 +++--------- wqflask/wqflask/do_search.py | 2 +- wqflask/wqflask/show_trait/show_trait.py | 133 +++++---------------- .../new/javascript/show_trait_mapping_tools.coffee | 18 ++- .../new/javascript/show_trait_mapping_tools.js | 15 ++- wqflask/wqflask/templates/marker_regression.html | 51 ++++++++ .../templates/show_trait_mapping_tools.html | 8 +- wqflask/wqflask/views.py | 63 +++++----- 9 files changed, 160 insertions(+), 215 deletions(-) create mode 100644 wqflask/wqflask/templates/marker_regression.html (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 612b9209..36d4acaf 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -16,11 +16,11 @@ # Contact Drs. Robert W. Williams and Xiaodong Zhou (2010) # at rwilliams@uthsc.edu and xzhou15@uthsc.edu # -# +#we # # This module is used by GeneNetwork project (www.genenetwork.org) -from __future__ import print_function, division +from __future__ import absolute_import, print_function, division import os from flask import Flask, g @@ -29,7 +29,7 @@ from htmlgen import HTMLgen2 as HT import reaper -import webqtlConfig +from base import webqtlConfig from base import species from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil @@ -50,6 +50,7 @@ def create_dataset(dataset_name): WHERE DBList.Name = '%s' and DBType.Id = DBList.DBTypeId """ % (escape(dataset_name)) + print("query is: ", pf(query)) dataset_type = g.db.execute(query).fetchone().Name #dataset_type = cursor.fetchone()[0] diff --git a/wqflask/base/trait.py b/wqflask/base/trait.py index 8c9e3b10..241bf2ab 100755 --- a/wqflask/base/trait.py +++ b/wqflask/base/trait.py @@ -1,12 +1,12 @@ -from __future__ import division, print_function +from __future__ import absolute_import, division, print_function import string from htmlgen import HTMLgen2 as HT -import webqtlConfig -from webqtlCaseData import webqtlCaseData -from data_set import create_dataset +from base import webqtlConfig +from base.webqtlCaseData import webqtlCaseData +from base.data_set import create_dataset from dbFunction import webqtlDatabaseFunction from utility import webqtlUtil @@ -24,76 +24,28 @@ class GeneralTrait: def __init__(self, **kw): print("in GeneralTrait") - self.dataset = kw.get('dataset', None) # database name - self.name = kw.get('name', None) # Trait ID, ProbeSet ID, Published ID, etc. - self.cellid = kw.get('cellid', None) + self.dataset = kw.get('dataset') # database name + self.name = kw.get('name') # Trait ID, ProbeSet ID, Published ID, etc. + self.cellid = kw.get('cellid') self.identification = kw.get('identification', 'un-named trait') - #self.group = kw.get('group', None) self.haveinfo = kw.get('haveinfo', False) - self.sequence = kw.get('sequence', None) # Blat sequence, available for ProbeSet + self.sequence = kw.get('sequence') # Blat sequence, available for ProbeSet self.data = kw.get('data', {}) - + if kw.get('fullname'): name2 = value.split("::") if len(name2) == 2: self.dataset, self.name = name2 + # self.cellid is set to None above elif len(name2) == 3: self.dataset, self.name, self.cellid = name2 - - #if self.dataset and isinstance(self.dataset, basestring): - self.dataset = create_dataset(self.dataset) - print("self.dataset is:", self.dataset, type(self.dataset)) - #if self.dataset: - - #self.dataset.get_group() + self.dataset = create_dataset(self.dataset) - #if self.dataset.type == "Temp": - # self.cursor.execute(''' - # SELECT - # InbredSet.Name - # FROM - # InbredSet, Temp - # WHERE - # Temp.InbredSetId = InbredSet.Id AND - # Temp.Name = "%s" - # ''', self.name) - # self.group = self.cursor.fetchone()[0] - #else: - # self.group = self.dataset.get_group() - - #print("trinity, self.group is:", self.group) - - # - # In ProbeSet, there are maybe several annotations match one sequence - # so we need use sequence(BlatSeq) as the identification, when we update - # one annotation, we update the others who match the sequence also. - # - # Hongqiang Li, 3/3/2008 - # - - #XZ, 05/08/2009: This block is not neccessary. We can add 'BlatSeq' into disfield. - # The variable self.sequence should be changed to self.BlatSeq - # It also should be changed in other places where it are used. - - #if self.dataset: - #if self.dataset.type == 'ProbeSet': - # print("Doing ProbeSet Query") - # query = ''' - # SELECT - # ProbeSet.BlatSeq - # FROM - # ProbeSet, ProbeSetFreeze, ProbeSetXRef - # WHERE - # ProbeSet.Id=ProbeSetXRef.ProbeSetId and - # ProbeSetFreeze.Id = ProbeSetXRef.ProbeSetFreezeId and - # ProbeSet.Name = %s and - # ProbeSetFreeze.Name = %s - # ''', (self.name, self.dataset.name) - # print("query is:", query) - # self.sequence = g.db.execute(*query).fetchone()[0] - # #self.sequence = self.cursor.fetchone()[0] - # print("self.sequence is:", self.sequence) + # Todo: These two lines are necessary most of the time, but perhaps not all of the time + # So we could add a simple if statement to short-circuit this if necessary + self.retrieve_info() + self.retrieve_sample_data() def get_name(self): diff --git a/wqflask/wqflask/do_search.py b/wqflask/wqflask/do_search.py index fc45395c..a2eddfc6 100644 --- a/wqflask/wqflask/do_search.py +++ b/wqflask/wqflask/do_search.py @@ -63,7 +63,7 @@ class DoSearch(object): class MrnaAssaySearch(DoSearch): """A search within an mRNA expression dataset""" - DoSearch.search_types['ProbeSet'] = "ProbeSetSearch" + DoSearch.search_types['ProbeSet'] = "MrnaAssaySearch" base_query = """SELECT ProbeSet.Name as TNAME, 0 as thistable, diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index c605cb58..807761a2 100755 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -34,45 +34,26 @@ from pprint import pformat as pf class ShowTrait(object): - def __init__(self, args): - print("in ShowTrait, args are:", args) - #self.group = args.group - self.trait_id = args['trait_id'] + def __init__(self, kw): + print("in ShowTrait, kw are:", kw) + self.trait_id = kw['trait_id'] - self.dataset = create_dataset(args['dataset']) - - #self.dataset = create_dataset(args['dataset']) - self.cell_id = None + self.dataset = create_dataset(kw['dataset']) + + #self.cell_id = None - #assert self.openMysql(), "No database!" - #print("red3 fd.group:", fd.group) - this_trait = self.get_this_trait() + this_trait = GeneralTrait(dataset=self.dataset.name, + name=self.trait_id, + cellid=None) - #print("red4 fd.group:", fd.group) - ##read genotype file - #fd.group = this_trait.group - #print("[red5] fd.group is:", fd.group) self.dataset.group.read_genotype_file() - #fd.readGenotype() if not self.dataset.group.genotype: - self.read_data(include_f1=True) #incf1=1) + self.read_data(include_f1=True) - ## determine data editing page format - #variance_data_page = 0 - #if fd.formID == 'varianceChoice': - # variance_data_page = 1 - # - #if variance_data_page: - # fmID='dataEditing' - #else: - # if fd.enablevariance: - # fmID='pre_dataEditing' - # else: - # fmID='dataEditing' - + # Todo: Add back in the ones we actually need from below, as we discover we need them hddn = OrderedDict() @@ -111,55 +92,19 @@ class ShowTrait(object): # export_data = None # ) - #if fd.enablevariance: - # hddn['enablevariance']='ON' - #if fd.incparentsf1: - # hddn['incparentsf1']='ON' - #if this_trait: - # hddn['fullname'] = str(this_trait) - # try: - # hddn['normalPlotTitle'] = this_trait.symbol - # hddn['normalPlotTitle'] += ": " - # hddn['normalPlotTitle'] += this_trait.name - # except: - # hddn['normalPlotTitle'] = str(this_trait.name) - # hddn['fromDataEditingPage'] = 1 # if this_trait.dataset and this_trait.dataset.type and this_trait.dataset.type == 'ProbeSet': - # hddn['trait_type'] = this_trait.dataset.type - # if this_trait.cellid: - # hddn['cellid'] = this_trait.cellid - # else: # self.cursor.execute("SELECT h2 from ProbeSetXRef WHERE DataId = %d" % # this_trait.mysqlid) # heritability = self.cursor.fetchone() - # hddn['heritability'] = heritability - # - # hddn['attribute_names'] = "" - # + #hddn['mappingMethodId'] = webqtlDatabaseFunction.getMappingMethod (cursor=self.cursor, # groupName=fd.group) - # - #if fd.identification: - # hddn['identification'] = fd.identification - #else: - # hddn['identification'] = "Un-named trait" #If no identification, set identification to un-named - - self.dispTraitInformation(args, "", hddn, this_trait) #Display trait information + function buttons - if this_trait == None: - this_trait = webqtlTrait(data=args['allTraitData'], dataset=None) + self.dispTraitInformation(kw, "", hddn, this_trait) #Display trait information + function buttons - ## Variance submit page only - #if fd.enablevariance and not variance_data_page: - # pass - # #title2Body.append("Click the next button to go to the variance submission form.", - # # HT.Center(next,reset)) - #else: - # pass - # # We'll get this part working later - # print("Calling dispBasicStatistics") - # self.dispBasicStatistics(fd, this_trait) + #if this_trait == None: + # this_trait = webqtlTrait(data=kw['allTraitData'], dataset=None) self.build_correlation_tools(this_trait) @@ -168,9 +113,6 @@ class ShowTrait(object): if self.dataset.group.allsamples: hddn['allsamples'] = string.join(self.dataset.group.allsamples, ' ') - #if args['varianceDispName'] != 'Variance': - # hddn['isSE'] = "yes" - # We'll need access to this_trait and hddn in the Jinja2 Template, so we put it inside self self.this_trait = this_trait self.hddn = hddn @@ -188,34 +130,23 @@ class ShowTrait(object): self.js_data = js_data - def get_this_trait(self): - # When is traitInfos used? - #if traitInfos: - # database, ProbeSetID, CellID = traitInfos - #else: - #dataset = self.fd['dataset'] - #trait_id = self.fd['trait_id'] - #cell_id = self.fd.get('CellID') - - this_trait = GeneralTrait(dataset=self.dataset.name, - name=self.trait_id, - cellid=self.cell_id) - - ##identification, etc. - self.identification = '%s : %s' % (self.dataset.shortname, self.trait_id) - this_trait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\ - &ProbeSetID=%s&group=%s&parentsf1=on' %(self.dataset, self.trait_id, self.dataset.group.name) - - if self.cell_id: - self.identification = '%s/%s'%(self.identification, self.cell_id) - this_trait.returnURL = '%s&CellID=%s' % (this_trait.returnURL, self.cell_id) - - print("yellow1:", self.dataset.group) - this_trait.retrieve_info() - print("yellow2:", self.dataset.group) - this_trait.retrieve_sample_data() - print("yellow3:", self.dataset.group) - return this_trait + #def get_this_trait(self): + # this_trait = GeneralTrait(dataset=self.dataset.name, + # name=self.trait_id, + # cellid=self.cell_id) + # + # ###identification, etc. + # #self.identification = '%s : %s' % (self.dataset.shortname, self.trait_id) + # #this_trait.returnURL = webqtlConfig.CGIDIR + webqtlConfig.SCRIPTFILE + '?FormID=showDatabase&database=%s\ + # # &ProbeSetID=%s&group=%s&parentsf1=on' %(self.dataset, self.trait_id, self.dataset.group.name) + # # + # #if self.cell_id: + # # self.identification = '%s/%s'%(self.identification, self.cell_id) + # # this_trait.returnURL = '%s&CellID=%s' % (this_trait.returnURL, self.cell_id) + # + # this_trait.retrieve_info() + # this_trait.retrieve_sample_data() + # return this_trait def read_data(self, include_f1=False): diff --git a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee index b1f5b186..d0fc869d 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee +++ b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.coffee @@ -1,11 +1,17 @@ $ -> - run_marker_regression = -> - console.log("In marker regression") - url = "/marker_regression" + submit_special = -> + # Add submit_special class plus a data-url field to any button + # And it will submit to that url + # No js changes necessary + console.log("In submit_special") + console.log("this is:", this) + console.log("$(this) is:", $(this)) + url = $(this).data("url") + console.log("url is:", url) $("#trait_data_form").attr("action", url); $("#trait_data_form").submit() - $("#do_marker_regression").click(run_marker_regression) + $(".submit_special").click(submit_special) composite_mapping_fields = -> @@ -14,10 +20,10 @@ $ -> $("#use_composite_choice").change(composite_mapping_fields) + #### Todo: Redo below so its like submit_special and requires no js hardcoding toggle_enable_disable = (elem) -> - $(elem).prop("disabled", !$(elem.prop("disabled"))) + $(elem).prop("disabled", !$(elem).prop("disabled")) - $("#choose_closet_control").change(-> toggle_enable_disable("#control_locus") ) diff --git a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js index c8328498..c6766288 100644 --- a/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js +++ b/wqflask/wqflask/static/new/javascript/show_trait_mapping_tools.js @@ -2,21 +2,24 @@ (function() { $(function() { - var composite_mapping_fields, run_marker_regression, toggle_enable_disable; - run_marker_regression = function() { + var composite_mapping_fields, submit_special, toggle_enable_disable; + submit_special = function() { var url; - console.log("In marker regression"); - url = "/marker_regression"; + console.log("In submit_special"); + console.log("this is:", this); + console.log("$(this) is:", $(this)); + url = $(this).data("url"); + console.log("url is:", url); $("#trait_data_form").attr("action", url); return $("#trait_data_form").submit(); }; - $("#do_marker_regression").click(run_marker_regression); + $(".submit_special").click(submit_special); composite_mapping_fields = function() { return $(".composite_fields").toggle(); }; $("#use_composite_choice").change(composite_mapping_fields); toggle_enable_disable = function(elem) { - return $(elem).prop("disabled", !$(elem.prop("disabled"))); + return $(elem).prop("disabled", !$(elem).prop("disabled")); }; $("#choose_closet_control").change(function() { return toggle_enable_disable("#control_locus"); diff --git a/wqflask/wqflask/templates/marker_regression.html b/wqflask/wqflask/templates/marker_regression.html new file mode 100644 index 00000000..db2de604 --- /dev/null +++ b/wqflask/wqflask/templates/marker_regression.html @@ -0,0 +1,51 @@ +{% extends "base.html" %} +{% block title %}Marker Regression{% endblock %} +{% block content %} + +
    +
    +

    Marker Regression

    +

    + {{ this_trait.name }}: {{ this_trait.description_fmt }} +

    +
    +
    + +
    +
    Aliases
    +
    {{ this_trait.alias_fmt }}
    + +
    Location
    +
    {{ this_trait.location_fmt }}
    + +
    Database
    +
    + + {{ dataset.name }} + +
    + + {% if this_trait.probe_set_specificity %} +
    + + BLAT Specifity + +
    +
    {{ "%.1f" % (this_trait.probe_set_specificity) }}
    + {% endif %} + {% if this_trait.probe_set_blat_score %} +
    BLAT Score
    +
    {{ "%i" % (this_trait.probe_set_blat_score) }}
    + {% endif %} +
    + + + +{% endblock %} + +{% block js %} + +{% endblock %} \ No newline at end of file diff --git a/wqflask/wqflask/templates/show_trait_mapping_tools.html b/wqflask/wqflask/templates/show_trait_mapping_tools.html index 8436703d..72b152fa 100644 --- a/wqflask/wqflask/templates/show_trait_mapping_tools.html +++ b/wqflask/wqflask/templates/show_trait_mapping_tools.html @@ -101,7 +101,8 @@
    - @@ -135,8 +136,9 @@
    -
    diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 503b0972..f6c0dfb0 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -18,6 +18,7 @@ from flask import render_template, request, make_response, Response, Flask, g, c from wqflask import search_results from wqflask.show_trait import show_trait from wqflask.show_trait import export_trait_data +from wqflask.marker_regression import marker_regression from wqflask.correlation import CorrelationPage from wqflask.dataSharing import SharingInfo, SharingInfoPage @@ -89,27 +90,6 @@ def whats_new_page(): print("\nnews_item is: %s\n" % (news_item)) return render_template("whats_new.html", news_items=news_items) - -@app.route("/show_trait") -def show_trait_page(): - # Here it's currently too complicated not to use an fd that is a webqtlFormData - #fd = webqtlFormData.webqtlFormData(request.args) - #print("stp y1:", pf(vars(fd))) - template_vars = show_trait.ShowTrait(request.args) - - print("js_data before dump:", template_vars.js_data) - - template_vars.js_data = json.dumps(template_vars.js_data, - default=json_default_handler, - indent=" ") - # Sorting the keys messes up the ordered dictionary, so don't do that - #sort_keys=True) - - print("js_data after dump:", template_vars.js_data) - - print("show_trait template_vars:", pf(template_vars.__dict__)) - return render_template("show_trait.html", **template_vars.__dict__) - @app.route('/export_trait_csv', methods=('POST',)) def export_trait_excel(): """Excel file consisting of the sample data from the trait data and analysis page""" @@ -150,33 +130,52 @@ def export_trait_csv(): mimetype='text/csv', headers={"Content-Disposition":"attachment;filename=test.csv"}) +@app.route("/show_trait") +def show_trait_page(): + # Here it's currently too complicated not to use an fd that is a webqtlFormData + #fd = webqtlFormData.webqtlFormData(request.args) + #print("stp y1:", pf(vars(fd))) + template_vars = show_trait.ShowTrait(request.args) + print("js_data before dump:", template_vars.js_data) + template_vars.js_data = json.dumps(template_vars.js_data, + default=json_default_handler, + indent=" ") + # Sorting the keys messes up the ordered dictionary, so don't do that + #sort_keys=True) + + print("js_data after dump:", template_vars.js_data) + print("show_trait template_vars:", pf(template_vars.__dict__)) + return render_template("show_trait.html", **template_vars.__dict__) + +@app.route("/marker_regression", methods=('POST',)) +def marker_regression_page(): + template_vars = marker_regression.MarkerRegression(request.form) + #print("js_data before dump:", template_vars.js_data) + #template_vars.js_data = json.dumps(template_vars.js_data, + # default=json_default_handler, + # indent=" ") + #print("js_data after dump:", template_vars.js_data) + print("marker_regression template_vars:", pf(template_vars.__dict__)) + return render_template("marker_regression.html", **template_vars.__dict__) @app.route("/corr_compute", methods=('POST',)) def corr_compute_page(): - #print("In corr_compute, request.args is:", pf(request.form)) + print("In corr_compute, request.args is:", pf(request.form)) fd = webqtlFormData.webqtlFormData(request.form) - print("Have fd") template_vars = CorrelationPage.CorrelationPage(fd) - print("Made it to rendering") return render_template("correlation_page.html", **template_vars.__dict__) @app.route("/int_mapping", methods=('POST',)) def interval_mapping_page(): - fd = webqtlFormData.webqtlFormData(request.form) - print("Have fd") - template_vars = CorrelationPage.CorrelationPage(fd) - print("Made it to rendering") - return render_template("correlation_page.html", **template_vars.__dict__) - + template_vars = interval_mapping.IntervalMapping(request.args) + return render_template("interval_mapping.html", **template_vars.__dict__) # Todo: Can we simplify this? -Sam def sharing_info_page(): """Info page displayed when the user clicks the "Info" button next to the dataset selection""" print("In sharing_info_page") fd = webqtlFormData.webqtlFormData(request.args) - print("2Have fd") template_vars = SharingInfoPage.SharingInfoPage(fd) - print("2 Made it to rendering") return template_vars -- cgit v1.2.3 From 91ed29ef68e8ad29b728f7f574ccc83730d9f7ab Mon Sep 17 00:00:00 2001 From: Zachary Sloan Date: Thu, 3 Jan 2013 18:15:32 -0600 Subject: Began working on marker_regression.py and created Chromosomes class in species.py --- wqflask/base/data_set.py | 6 +- wqflask/base/species.py | 52 +- wqflask/wqflask/interval_analyst/GeneUtil.py | 124 ++ .../interval_analyst/IntervalAnalystPage.py | 405 +++++ wqflask/wqflask/interval_analyst/__init__.py | 0 .../marker_regression/MarkerRegressionPage.py | 1648 ++++++++++++++++++++ wqflask/wqflask/marker_regression/__init__.py | 0 .../wqflask/marker_regression/marker_regression.py | 1648 ++++++++++++++++++++ 8 files changed, 3870 insertions(+), 13 deletions(-) create mode 100755 wqflask/wqflask/interval_analyst/GeneUtil.py create mode 100755 wqflask/wqflask/interval_analyst/IntervalAnalystPage.py create mode 100644 wqflask/wqflask/interval_analyst/__init__.py create mode 100644 wqflask/wqflask/marker_regression/MarkerRegressionPage.py create mode 100644 wqflask/wqflask/marker_regression/__init__.py create mode 100755 wqflask/wqflask/marker_regression/marker_regression.py (limited to 'wqflask/base/data_set.py') diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index 36d4acaf..50ef8f57 100755 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -85,8 +85,8 @@ class DatasetGroup(object): self.f1list = None self.parlist = None self.allsamples = None - - + + #def read_genotype(self): # self.read_genotype_file() # @@ -158,8 +158,8 @@ class DataSet(object): self.retrieve_other_names() - self.species = species.TheSpecies(self) self.group = DatasetGroup(self) # sets self.group and self.group_id and gets genotype + self.species = species.TheSpecies(self) diff --git a/wqflask/base/species.py b/wqflask/base/species.py index 98941ce5..1fd76772 100644 --- a/wqflask/base/species.py +++ b/wqflask/base/species.py @@ -1,16 +1,48 @@ -from __future__ import print_function, division +from __future__ import absolute_import, print_function, division +import collections + +from flask import Flask, g + +#from MySQLdb import escape_string as escape + +from pprint import pformat as pf class TheSpecies(object): def __init__(self, dataset): self.dataset = dataset + print("self.dataset is:", pf(self.dataset.__dict__)) + self.chromosomes = Chromosomes(self.dataset.group.name) + + #@property + #def chromosomes(self): + # chromosomes = [("All", -1)] + # + # for counter, genotype in enumerate(self.dataset.group.genotype): + # if len(genotype) > 1: + # chromosomes.append((genotype.name, counter)) + # + # print("chromosomes is: ", pf(chromosomes)) + # + # return chromosomes + + + +class Chromosomes(object): + def __init__(self, group_name): + self.chromosomes = collections.OrderedDict() + + results = g.db.execute(""" + Select + Chr_Length.Name, Length from Chr_Length, InbredSet + where + Chr_Length.SpeciesId = InbredSet.SpeciesId AND + InbredSet.Name = %s + Order by OrderId + """, group_name).fetchall() + print("bike:", results) + + for item in results: + self.chromosomes[item.Name] = item.Length - @property - def chromosomes(self): - chromosomes = [("All", -1)] - - for counter, genotype in enumerate(self.dataset.group.genotype): - if len(genotype) > 1: - chromosomes.append((genotype.name, counter)) - - return chromosomes + print("self.chromosomes:", self.chromosomes) diff --git a/wqflask/wqflask/interval_analyst/GeneUtil.py b/wqflask/wqflask/interval_analyst/GeneUtil.py new file mode 100755 index 00000000..43008ecf --- /dev/null +++ b/wqflask/wqflask/interval_analyst/GeneUtil.py @@ -0,0 +1,124 @@ +# Copyright (C) University of Tennessee Health Science Center, Memphis, TN. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# This program is available from Source Forge: at GeneNetwork Project +# (sourceforge.net/projects/genenetwork/). +# +# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010) +# at rwilliams@uthsc.edu and xzhou15@uthsc.edu +# +# +# +# This module is used by GeneNetwork project (www.genenetwork.org) +# +# Created by GeneNetwork Core Team 2010/08/10 +# +# Last updated by GeneNetwork Core Team 2010/10/20 + +import string + +#Just return a list of dictionaries +#each dictionary contains sub-dictionary +def loadGenes(cursor, chrName, diffCol, startMb, endMb, webqtlDb =None, species='mouse'): + #cursor.execute("desc GeneList") + #results = cursor.fetchall() + #fetchFields = map(lambda X:X[0], results) + fetchFields = ['SpeciesId', 'Id', 'GeneSymbol', 'GeneDescription', 'Chromosome', 'TxStart', 'TxEnd', + 'Strand', 'GeneID', 'NM_ID', 'kgID', 'GenBankID', 'UnigenID', 'ProteinID', 'AlignID', + 'exonCount', 'exonStarts', 'exonEnds', 'cdsStart', 'cdsEnd'] + + ##List All Species in the Gene Table + speciesDict = {} + cursor.execute("select Species.Name, GeneList.SpeciesId from Species, GeneList where \ + GeneList.SpeciesId = Species.Id group by GeneList.SpeciesId") + results = cursor.fetchall() + for item in results: + speciesDict[item[0]] = item[1] + + ##List current Species and other Species + speciesId = speciesDict[species] + otherSpecies = map(lambda X: [X, speciesDict[X]], speciesDict.keys()) + otherSpecies.remove([species, speciesId]) + + cursor.execute("""SELECT %s from GeneList + where + SpeciesId = %d AND Chromosome = '%s' AND + ((TxStart > %f and TxStart <= %f) OR (TxEnd > %f and TxEnd <= %f)) + order by txStart + """ + % (string.join(fetchFields, ", "), speciesId, chrName, startMb, endMb, startMb, endMb)) + results = cursor.fetchall() + GeneList = [] + + if results: + for result in results: + newdict = {} + for j, item in enumerate(fetchFields): + newdict[item] = result[j] + #count SNPs if possible + if diffCol and species=='mouse': + cursor.execute(""" + select + count(*) from BXDSnpPosition + where + Chr = '%s' AND Mb >= %2.6f AND Mb < %2.6f AND + StrainId1 = %d AND StrainId2 = %d + """ % (chrName, newdict["TxStart"], newdict["TxEnd"], diffCol[0], diffCol[1])) + newdict["snpCount"] = cursor.fetchone()[0] + newdict["snpDensity"] = newdict["snpCount"]/(newdict["TxEnd"]-newdict["TxStart"])/1000.0 + else: + newdict["snpDensity"] = newdict["snpCount"] = 0 + + try: + newdict['GeneLength'] = 1000.0*(newdict['TxEnd'] - newdict['TxStart']) + except: + pass + + #load gene from other Species by the same name + for item in otherSpecies: + othSpec, othSpecId = item + newdict2 = {} + + cursor.execute("SELECT %s from GeneList where SpeciesId = %d and geneSymbol= '%s' limit 1" % + (string.join(fetchFields, ", "), othSpecId, newdict["GeneSymbol"])) + resultsOther = cursor.fetchone() + if resultsOther: + for j, item in enumerate(fetchFields): + newdict2[item] = resultsOther[j] + + #count SNPs if possible, could be a separate function + if diffCol and othSpec == 'mouse': + cursor.execute(""" + select + count(*) from BXDSnpPosition + where + Chr = '%s' AND Mb >= %2.6f AND Mb < %2.6f AND + StrainId1 = %d AND StrainId2 = %d + """ % (chrName, newdict["TxStart"], newdict["TxEnd"], diffCol[0], diffCol[1])) + + newdict2["snpCount"] = cursor.fetchone()[0] + newdict2["snpDensity"] = newdict2["snpCount"]/(newdict2["TxEnd"]-newdict2["TxStart"])/1000.0 + else: + newdict2["snpDensity"] = newdict2["snpCount"] = 0 + + try: + newdict2['GeneLength'] = 1000.0*(newdict2['TxEnd'] - newdict2['TxStart']) + except: + pass + + newdict['%sGene' % othSpec] = newdict2 + + GeneList.append(newdict) + + return GeneList + + diff --git a/wqflask/wqflask/interval_analyst/IntervalAnalystPage.py b/wqflask/wqflask/interval_analyst/IntervalAnalystPage.py new file mode 100755 index 00000000..ec9aa29c --- /dev/null +++ b/wqflask/wqflask/interval_analyst/IntervalAnalystPage.py @@ -0,0 +1,405 @@ +# Copyright (C) University of Tennessee Health Science Center, Memphis, TN. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# This program is available from Source Forge: at GeneNetwork Project +# (sourceforge.net/projects/genenetwork/). +# +# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010) +# at rwilliams@uthsc.edu and xzhou15@uthsc.edu +# +# +# +# This module is used by GeneNetwork project (www.genenetwork.org) +# +# Created by GeneNetwork Core Team 2010/08/10 +# +# Last updated by GeneNetwork Core Team 2010/10/20 + +from mod_python import apache, util, Cookie +import os +import time +import pyXLWriter as xl + +from htmlgen import HTMLgen2 as HT + +import GeneUtil +from base.templatePage import templatePage +from utility import webqtlUtil +from base import webqtlConfig + + +class IntervalAnalystPage(templatePage): + filename = webqtlUtil.genRandStr("Itan_") + + _scriptfile = "main.py?FormID=intervalAnalyst" + + #A dictionary that lets us map the html form names "txStart_mm6" -> "Mb Start (mm8)" + #the first item is the short name (column headers) and the second item is the long name (dropdown list) + # [short name, long name, category] + columnNames = {"GeneSymbol" : ["Gene", "Gene Name", 'gene'], + "GeneDescription" : ["Description", "Gene Description", 'species'], + 'GeneNeighborsCount' : ["Neighbors", "Gene Neighbors", 'gene'], + 'GeneNeighborsRange' : ["Neighborhood", "Gene Neighborhood (Mb)", 'gene'], + 'GeneNeighborsDensity' : ["Gene Density", "Gene Density (Neighbors/Mb)", 'gene'], + "ProteinID" : ["Prot ID", "Protein ID", 'protein'], + "Chromosome" : ["Chr", "Chromosome", 'species'], + "TxStart" : ["Start", "Mb Start", 'species'], + "TxEnd" : ["End", "Mb End", 'species'], + "GeneLength" : ["Length", "Kb Length", 'species'], + "cdsStart" : ["CDS Start", "Mb CDS Start", 'species'], + "cdsEnd" : ["CDS End", "Mb CDS End", 'species'], + "exonCount" : ["Num Exons", "Exon Count", 'species'], + "exonStarts" : ["Exon Starts", "Exon Starts", 'species'], + "exonEnds" : ["Exon Ends", "Exon Ends", 'species'], + "Strand" : ["Strand", "Strand", 'species'], + "GeneID" : ["Gene ID", "Gene ID", 'species'], + "GenBankID" : ["GenBank", "GenBank ID", 'species'], + "UnigenID" : ["Unigen", "Unigen ID", 'species'], + "NM_ID" : ["NM ID", "NM ID", 'species'], + "kgID" : ["kg ID", "kg ID", 'species'], + "snpCount" : ["SNPs", "SNP Count", 'species'], + "snpDensity" : ["SNP Density", "SNP Density", 'species'], + "lrs" : ["LRS", "Likelihood Ratio Statistic", 'misc'], + "lod" : ["LOD", "Likelihood Odds Ratio", 'misc'], + "pearson" : ["Pearson", "Pearson Product Moment", 'misc'], + "literature" : ["Lit Corr", "Literature Correlation", 'misc'], + } + + ###Species Freeze + speciesFreeze = {'mouse':'mm9', 'rat':'rn3', 'human':'hg19'} + for key in speciesFreeze.keys(): + speciesFreeze[speciesFreeze[key]] = key + + def __init__(self, fd): + + templatePage.__init__(self, fd) + + fd.formdata['remote_ip'] = fd.remote_ip + if not self.openMysql(): + return + + self.species = fd.formdata.getvalue("species", "mouse") + try: + self.startMb = float(fd.formdata.getvalue("startMb")) + except: + self.startMb = 10 + try: + self.endMb = float(fd.formdata.getvalue("endMb")) + except: + self.endMb = self.startMb + 10 + + self.Chr = fd.formdata.getvalue("chromosome", "1") + self.xls = fd.formdata.getvalue("xls", "1") + try: + s1 = int(fd.formdata.getvalue("s1")) + s2 = int(fd.formdata.getvalue("s2")) + self.diffColDefault = self.diffCol = [s1, s2] + except: + self.diffColDefault = self.diffCol = [] + if self.species != 'mouse': + self.diffColDefault = [2, 3]#default is B6 and D2 for other species + + controlFrm, dispFields = self.genControlForm(fd) + geneTable, filename = self.genGeneTable(fd, dispFields) + + infoTD = HT.TD(width=400, valign= "top") + infoTD.append(HT.Paragraph("Interval Analyst : Chr %s" % self.Chr, Class="title"), + HT.Strong("Species : "), self.species.title(), HT.BR(), + HT.Strong("Database : "), "UCSC %s" % self.speciesFreeze[self.species], HT.BR(), + HT.Strong("Range : "), "%2.6f Mb - %2.6f Mb" % (self.startMb, self.endMb), HT.BR(), + ) + if filename: + infoTD.append(HT.BR(), HT.BR(), HT.Href(text="Download", url = "/tmp/" + filename, Class="normalsize") + , " output in MS excel format.") + + mainTable = HT.TableLite(HT.TR(infoTD, HT.TD(controlFrm, Class="doubleBorder", width=400), HT.TD(" ", width="")), cellpadding=10) + mainTable.append(HT.TR(HT.TD(geneTable, colspan=3))) + self.dict['body'] = HT.TD(mainTable) + self.dict['title'] = "Interval Analyst" + + def genGeneTable(self, fd, dispFields): + filename = "" + if self.xls: + #import pyXLWriter as xl + filename = "IntAn_Chr%s_%2.6f-%2.6f" % (self.Chr, self.startMb, self.endMb) + filename += ".xls" + + # Create a new Excel workbook + workbook = xl.Writer(os.path.join(webqtlConfig.TMPDIR, filename)) + worksheet = workbook.add_worksheet() + titleStyle = workbook.add_format(align = 'left', bold = 0, size=18, border = 1, border_color="gray") + headingStyle = workbook.add_format(align = 'center', bold = 1, size=13, fg_color = 0x1E, color="white", border = 1, border_color="gray") + + ##Write title Info + worksheet.write([0, 0], "GeneNetwork Interval Analyst Table", titleStyle) + worksheet.write([1, 0], "%s%s" % (webqtlConfig.PORTADDR, os.path.join(webqtlConfig.CGIDIR, self._scriptfile))) + # + worksheet.write([2, 0], "Date : %s" % time.strftime("%B %d, %Y", time.gmtime())) + worksheet.write([3, 0], "Time : %s GMT" % time.strftime("%H:%M ", time.gmtime())) + worksheet.write([4, 0], "Search by : %s" % fd.formdata['remote_ip']) + worksheet.write([5, 0], "view region : Chr %s %2.6f - %2.6f Mb" % (self.Chr, self.startMb, self.endMb)) + nTitleRow = 7 + + geneTable = HT.TableLite(Class="collap", cellpadding=5) + headerRow = HT.TR(HT.TD(" ", Class="fs13 fwb ffl b1 cw cbrb", width="1")) + if self.xls: + worksheet.write([nTitleRow, 0], "Index", headingStyle) + + for ncol, column in enumerate(dispFields): + if len(column) == 1: + headerRow.append(HT.TD(self.columnNames[column[0]][0], Class="fs13 fwb ffl b1 cw cbrb", NOWRAP=1,align="Center")) + if self.xls: + colTitle = self.columnNames[column[0]][0] + worksheet.write([nTitleRow, ncol+1], colTitle, headingStyle) + worksheet.set_column([ncol+1, ncol+1], 2*len(colTitle)) + else: + headerRow.append(HT.TD(self.columnNames[column[0]][0], HT.BR(), " (%s)" % self.speciesFreeze[column[1]], + Class="fs13 fwb ffl b1 cw cbrb", NOWRAP=1, align="Center")) + if self.xls: + colTitle = self.columnNames[column[0]][0] + " (%s)" % self.speciesFreeze[column[1]] + worksheet.write([nTitleRow, ncol+1], colTitle, headingStyle) + worksheet.set_column([ncol+1, ncol+1], 2*len(colTitle)) + #headerRow.append(HT.TD(self.columnNames[column[0]][0], HT.BR(), + # "(%s %s)" % (column[1].title(), self.speciesFreeze[column[1]]), + # Class="colorBlue", NOWRAP=1, align="Center")) + geneTable.append(headerRow) + + geneCol = GeneUtil.loadGenes(self.cursor, self.Chr, self.diffColDefault, self.startMb, self.endMb, species=self.species) + for gIndex, theGO in enumerate(geneCol): + geneRow = HT.TR(HT.TD(gIndex+1, Class="fs12 fwn b1", align="right")) + if self.xls: + nTitleRow += 1 + worksheet.write([nTitleRow, 0], gIndex + 1) + + for ncol, column in enumerate(dispFields): + if len(column) == 1 or column[1]== self.species: + keyValue = "" + fieldName = column[0] + curSpecies = self.species + curGO = theGO + if theGO.has_key(fieldName): + keyValue = theGO[fieldName] + else: + fieldName , othSpec = column + curSpecies = othSpec + subGO = '%sGene' % othSpec + keyValue = "" + curGO = theGO[subGO] + if theGO[subGO].has_key(fieldName): + keyValue = theGO[subGO][fieldName] + + if self.xls: + worksheet.write([nTitleRow, ncol+1], keyValue) + geneRow.append(self.formatTD(keyValue, fieldName, curSpecies, curGO)) + + geneTable.append(geneRow) + + if self.xls: + workbook.close() + return geneTable, filename + + def formatTD(self, keyValue, fieldName, Species, theGO): + if keyValue is None: + keyValue = "" + if keyValue != "": + if fieldName in ("exonStarts", "exonEnds"): + keyValue = string.replace(keyValue, ',', ' ') + return HT.TD(HT.Span(keyValue, Class="code", Id="green"), width=350, Class="fs12 fwn b1") + elif fieldName in ("GeneDescription"): + if keyValue == "---": + keyValue = "" + return HT.TD(keyValue, Class="fs12 fwn b1", width=300) + elif fieldName in ("GeneSymbol"): + webqtlLink = HT.Href("./%s?cmd=sch&gene=%s&alias=1&species=%s" % (webqtlConfig.SCRIPTFILE, keyValue, Species), + HT.Image("/images/webqtl_search.gif", border=0, valign="top"), target="_blank") + if theGO['GeneID']: + geneSymbolLink = HT.Href(webqtlConfig.NCBI_LOCUSID % theGO['GeneID'], keyValue, Class="normalsize", target="_blank") + else: + geneSymbolLink = keyValue + return HT.TD(webqtlLink, geneSymbolLink, Class="fs12 fwn b1",NOWRAP=1) + elif fieldName == 'UnigenID': + try: + gurl = HT.Href(webqtlConfig.UNIGEN_ID % tuple(string.split(keyValue,'.')[:2]), keyValue, Class="normalsize", target="_blank") + except: + gurl = keyValue + return HT.TD(gurl, Class="fs12 fwn b1",NOWRAP=1) + elif fieldName in ("exonCount", "Chromosome"): + return HT.TD(keyValue, Class="fs12 fwn b1",align="right") + elif fieldName in ("snpCount"): + if keyValue: + snpString = HT.Href(url="%s&chr=%s&start=%s&end=%s&geneName=%s&s1=%d&s2=%d" % (os.path.join(webqtlConfig.CGIDIR, 'main.py?FormID=snpBrowser'), + theGO["Chromosome"], theGO["TxStart"], theGO["TxEnd"], theGO["GeneSymbol"], self.diffColDefault[0], self.diffColDefault[1]), + text=theGO["snpCount"], target="_blank", Class="normalsize") + else: + snpString = keyValue + return HT.TD(snpString, Class="fs12 fwn b1",align="right") + elif fieldName in ("snpDensity", "GeneLength"): + if keyValue: keyValue = "%2.3f" % keyValue + else: keyValue = "" + return HT.TD(keyValue, Class="fs12 fwn b1",align="right") + elif fieldName in ("TxStart", "TxEnd"): + return HT.TD("%2.6f" % keyValue, Class="fs12 fwn b1",align="right") + else: + return HT.TD(keyValue, Class="fs12 fwn b1",NOWRAP=1) + else: + return HT.TD(keyValue, Class="fs12 fwn b1",NOWRAP=1,align="right") + + def genControlForm(self, fd): + ##desc GeneList + self.cursor.execute("Desc GeneList") + GeneListFields = self.cursor.fetchall() + GeneListFields = map(lambda X: X[0], GeneListFields) + + #group columns by category--used for creating the dropdown list of possible columns + categories = {} + for item in self.columnNames.keys(): + category = self.columnNames[item] + if category[-1] not in categories.keys(): + categories[category[-1]] = [item ] + else: + categories[category[-1]] = categories[category[-1]]+[item] + + ##List All Species in the Gene Table + speciesDict = {} + self.cursor.execute("select Species.Name, GeneList.SpeciesId from Species, GeneList where \ + GeneList.SpeciesId = Species.Id group by GeneList.SpeciesId order by Species.Id") + results = self.cursor.fetchall() + speciesField = categories.pop('species', []) + categoriesOrder = ['gene', 'protein'] + for item in results: + specName, specId = item + categoriesOrder.append(specName) + speciesDict[specName] = specId + AppliedField = [] + for item2 in speciesField: + if item2 in GeneListFields: + self.cursor.execute("select %s from GeneList where SpeciesId = %d and %s is not NULL limit 1 " % (item2, specId, item2)) + columnApply = self.cursor.fetchone() + if not columnApply: + continue + elif specName != 'mouse' and item2 in ('snpCount', 'snpDensity'): + continue + else: + pass + AppliedField.append(item2) + categories[specName] = AppliedField + + categoriesOrder += ['misc'] + + ############################################################ + ## Create the list of possible columns for the dropdown list + ############################################################ + allColumnsList = HT.Select(name="allColumns", Class="snpBrowserDropBox") + + for category in categoriesOrder: + allFields = categories[category] + if allFields: + geneOpt = HT.Optgroup(label=category.title()) + for item in allFields: + if category in self.speciesFreeze.keys(): + geneOpt.append(("%s (%s %s)" % (self.columnNames[item][1], category.title(), self.speciesFreeze[category]), + "%s__%s" % (item, self.speciesFreeze[category]))) + else: + geneOpt.append((self.columnNames[item][1], item)) + geneOpt.sort() + allColumnsList.append(geneOpt) + + ###################################### + ## Create the list of selected columns + ###################################### + + #cols contains the value of all the selected columns + submitCols = cols = fd.formdata.getvalue("columns", "default") + + if cols == "default": + if self.species=="mouse": #these are the same columns that are shown on intervalPage.py + cols = ['GeneSymbol', 'GeneDescription', 'Chromosome', 'TxStart', 'Strand', 'GeneLength', 'GeneID', 'NM_ID', 'snpCount', 'snpDensity'] + elif self.species=="rat": + cols = ['GeneSymbol', 'GeneDescription', 'Chromosome', 'TxStart', 'GeneLength', 'Strand', 'GeneID', 'UnigenID'] + else: + #should not happen + cols = [] + else: + if type(cols)==type(""): + cols = [cols] + + colsLst = [] + dispFields = [] + for column in cols: + if submitCols == "default" and column not in ('GeneSymbol') and (column in GeneListFields or column in speciesField): + colsLst.append(("%s (%s %s)" % (self.columnNames[column][1], self.species.title(), self.speciesFreeze[self.species]), + "%s__%s" % (column, self.speciesFreeze[self.species]))) + dispFields.append([column, self.species]) + else: + column2 = column.split("__") + if len(column2) == 1: + colsLst.append((self.columnNames[column2[0]][1], column)) + dispFields.append([column]) + else: + thisSpecies = self.speciesFreeze[column2[1]] + colsLst.append(("%s (%s %s)" % (self.columnNames[column2[0]][1], thisSpecies.title(), column2[1]), + column)) + dispFields.append((column2[0], thisSpecies)) + selectedColumnsList = HT.Select(name="columns", Class="snpBrowserSelectBox", multiple="true", data=colsLst, size=6) + + ########################## + ## Create the columns form + ########################## + columnsForm = HT.Form(name="columnsForm", submit=HT.Input(type='hidden'), cgi=os.path.join(webqtlConfig.CGIDIR, self._scriptfile), enctype="multipart/form-data") + columnsForm.append(HT.Input(type="hidden", name="fromdatabase", value= fd.formdata.getvalue("fromdatabase", "unknown"))) + columnsForm.append(HT.Input(type="hidden", name="species", value=self.species)) + if self.diffCol: + columnsForm.append(HT.Input(type="hidden", name="s1", value=self.diffCol[0])) + columnsForm.append(HT.Input(type="hidden", name="s2", value=self.diffCol[1])) + startBox = HT.Input(type="text", name="startMb", value=self.startMb, size=10) + endBox = HT.Input(type="text", name="endMb", value=self.endMb, size=10) + addButton = HT.Input(type="button", name="add", value="Add", Class="button", onClick="addToList(this.form.allColumns.options[this.form.allColumns.selectedIndex].text, this.form.allColumns.options[this.form.allColumns.selectedIndex].value, this.form.columns)") + removeButton = HT.Input(type="button", name="remove", value="Remove", Class="button", onClick="removeFromList(this.form.columns.selectedIndex, this.form.columns)") + upButton = HT.Input(type="button", name="up", value="Up", Class="button", onClick="swapOptions(this.form.columns.selectedIndex, this.form.columns.selectedIndex-1, this.form.columns)") + downButton = HT.Input(type="button", name="down", value="Down", Class="button", onClick="swapOptions(this.form.columns.selectedIndex, this.form.columns.selectedIndex+1, this.form.columns)") + clearButton = HT.Input(type="button", name="clear", value="Clear", Class="button", onClick="deleteAllElements(this.form.columns)") + submitButton = HT.Input(type="submit", value="Refresh", Class="button", onClick="selectAllElements(this.form.columns)") + + selectChrBox = HT.Select(name="chromosome") + self.cursor.execute(""" + Select + Chr_Length.Name, Length from Chr_Length, Species + where + Chr_Length.SpeciesId = Species.Id AND + Species.Name = '%s' + Order by + Chr_Length.OrderId + """ % self.species) + + results = self.cursor.fetchall() + for chrInfo in results: + selectChrBox.append((chrInfo[0], chrInfo[0])) + selectChrBox.selected.append(self.Chr) + + innerColumnsTable = HT.TableLite(border=0, Class="collap", cellpadding = 2) + innerColumnsTable.append(HT.TR(HT.TD(selectedColumnsList)), + HT.TR(HT.TD(clearButton, removeButton, upButton, downButton))) + columnsTable = HT.TableLite(border=0, cellpadding=2, cellspacing=0) + columnsTable.append(HT.TR(HT.TD(HT.Font("Chr: ", size=-1)), + HT.TD(selectChrBox, submitButton)), + HT.TR(HT.TD(HT.Font("View: ", size=-1)), + HT.TD(startBox, HT.Font("Mb to ", size=-1), endBox, HT.Font("Mb", size=-1))), + HT.TR(HT.TD(HT.Font("Show: ", size=-1)), + HT.TD(allColumnsList, addButton)), + HT.TR(HT.TD(""), + HT.TD(innerColumnsTable))) + columnsForm.append(columnsTable) + #columnsForm.append(HT.Input(type="hidden", name="sort", value=diffCol), + # HT.Input(type="hidden", name="identification", value=identification), + # HT.Input(type="hidden", name="traitInfo", value=traitInfo)) + + return columnsForm, dispFields diff --git a/wqflask/wqflask/interval_analyst/__init__.py b/wqflask/wqflask/interval_analyst/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wqflask/wqflask/marker_regression/MarkerRegressionPage.py b/wqflask/wqflask/marker_regression/MarkerRegressionPage.py new file mode 100644 index 00000000..d02d80b3 --- /dev/null +++ b/wqflask/wqflask/marker_regression/MarkerRegressionPage.py @@ -0,0 +1,1648 @@ +# Copyright (C) University of Tennessee Health Science Center, Memphis, TN. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU Affero General Public License for more details. +# +# This program is available from Source Forge: at GeneNetwork Project +# (sourceforge.net/projects/genenetwork/). +# +# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010) +# at rwilliams@uthsc.edu and xzhou15@uthsc.edu +# +# This module is used by GeneNetwork project (www.genenetwork.org) +# +# Created by GeneNetwork Core Team 2010/08/10 +# +# Last updated by GeneNetwork Core Team 2010/10/20 + +import time +import string +import math +from math import * +import piddle as pid +import sys,os +import httplib, urllib + +from htmlgen import HTMLgen2 as HT +from utility import Plot +from intervalAnalyst import GeneUtil +from base.webqtlTrait import webqtlTrait +from base.templatePage import templatePage +from utility import webqtlUtil +from base import webqtlConfig +from dbFunction import webqtlDatabaseFunction +from base.GeneralObject import GeneralObject + +import reaper +import cPickle +from utility.THCell import THCell +from utility.TDCell import TDCell + +class MarkerRegressionPage(templatePage): + + def __init__(self, fd): + + templatePage.__init__(self, fd) + + if not self.openMysql(): + return + + self.initializeParameters(fd) + + filename= webqtlUtil.genRandStr("Itvl_") + ChrList,ChrNameOrderIdDict,ChrOrderIdNameDict,ChrLengthMbList= self.getChrNameOrderIdLength(RISet=fd.RISet) + + if self.mappingMethodId == '4': # For PLINK + + traitInfoList = string.split(string.strip(fd.identification),':') + probesetName = string.strip(traitInfoList[-1]) + plinkOutputFileName= webqtlUtil.genRandStr("%s_%s_"%(fd.RISet,probesetName)) + + # get related values from fd.allTraitData; the format of 'allTraitValueDict'is {strainName1: value=-0.2...} + fd.readData() + allTraitValueDict = fd.allTraitData + + #automatically generate pheno txt file for PLINK + self.genPhenoTxtFileForPlink(phenoFileName=plinkOutputFileName,RISetName=fd.RISet,probesetName=probesetName, valueDict=allTraitValueDict) + # os.system full path is required for input and output files; specify missing value is -9999 + plink_command = '%splink/plink --noweb --ped %splink/%s.ped --no-fid --no-parents --no-sex --no-pheno --map %splink/%s.map --pheno %s/%s.txt --pheno-name %s --missing-phenotype -9999 --out %s%s --assoc ' % (webqtlConfig.HTMLPATH, webqtlConfig.HTMLPATH, fd.RISet, webqtlConfig.HTMLPATH, fd.RISet, webqtlConfig.TMPDIR, plinkOutputFileName, probesetName, webqtlConfig.TMPDIR, plinkOutputFileName) + + os.system(plink_command) + + if fd.identification: + heading2 = HT.Paragraph('Trait ID: %s' % fd.identification) + heading2.__setattr__("class","subtitle") + self.dict['title'] = '%s: Genome Association' % fd.identification + else: + heading2 = "" + self.dict['title'] = 'Genome Association' + + if fd.traitInfo: + symbol,chromosome,MB = string.split(fd.traitInfo,'\t') + heading3 = HT.Paragraph('[ ',HT.Strong(HT.Italic('%s' % symbol,id="green")),' on Chr %s @ %s Mb ]' % (chromosome,MB)) + else: + heading3 = "" + + heading = HT.Paragraph('Trait Data Entered for %s Set' % fd.RISet) + heading.__setattr__("class","title") + + # header info part:Trait Data Entered for HLC Set & Trait ID: + headerdiv = HT.TR(HT.TD(heading, heading2,heading3, width='45%',valign='top', align='left', bgColor='#eeeeee')) + + self.ChrList=ChrList # get chr name from '1' to 'X' + self.ChrLengthMbList = ChrLengthMbList + + # build plink result dict based on chr, key is chr name, value is in list type including Snpname, bp and pvalue info + plinkResultDict={} + count,minPvalue,plinkResultDict =self.getPlinkResultDict(outputFileName=plinkOutputFileName,thresholdPvalue=self.pValue,ChrOrderIdNameDict=ChrOrderIdNameDict) + + # if can not find results which are matched with assigned p-value, system info will show up + if count >0: + + #for genome association report table + reportTable="" + # sortable table object + resultstable,tblobj,bottomInfo = self.GenReportForPLINK(ChrNameOrderIdDict=ChrNameOrderIdDict, RISet=fd.RISet,plinkResultDict=plinkResultDict,thresholdPvalue=self.pValue,chrList=self.ChrList) + + # creat object for result table for sort function + objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb') + cPickle.dump(tblobj, objfile) + objfile.close() + + sortby = ("Index", "up") + reportTable =HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID = "sortable", addIndex = "0"), Id="sortable") + + descriptionTable = HT.TableLite(border=0, cellpadding=0, cellspacing=0) + descriptionTable.append(HT.TR(HT.TD(reportTable, colspan=3))) + descriptionTable.append(HT.TR(HT.TD(HT.BR(),HT.BR()))) + descriptionTable.append(bottomInfo) + + # get each chr's length + self.ChrLengthMbList = map(lambda x: x/1000000.0, self.ChrLengthMbList) # change unit from bp to mb + self.ChrLengthMbSum = reduce(lambda x, y:x+y, self.ChrLengthMbList, 0.0)# get total length of all chrs + if self.ChrLengthMbList: + self.GraphInterval = self.ChrLengthMbSum/(len(self.ChrLengthMbList)*12) #Empirical Mb interval + else: + self.GraphInterval = 1 + + # for human data, there's no CM value + self.ChrLengthCMList = [] + self.ChrLengthCMSum = 0 + + # begin: common part with human data + intCanvas = pid.PILCanvas(size=(self.graphWidth,self.graphHeight)) + gifmap = self.plotIntMappingForPLINK(fd, intCanvas, startMb = self.startMb, endMb = self.endMb, plinkResultDict=plinkResultDict) + + intCanvas.save(os.path.join(webqtlConfig.IMGDIR, filename), format='png') + intImg=HT.Image('/image/'+filename+'.png', border=0, usemap='#WebQTLImageMap') + + TD_LR = HT.TR(HT.TD(HT.Blockquote(gifmap,intImg, HT.P()), bgColor='#eeeeee', height = 200)) + self.dict['body'] = str(headerdiv)+str(TD_LR)+str(resultstable)+str(HT.TR(HT.TD(descriptionTable))) + + else: + heading = "Genome Association" + detail = ['There is no association with marker that meets this criteria. Please provide a less stringend threshold. The minimun p-value is %s.'%minPvalue] + self.error(heading=heading,detail=detail) + return + + elif self.mappingMethodId == '1': # QTLreaper result + if not fd.genotype: + fd.readData() + + fd.parentsf14regression = fd.formdata.getvalue('parentsf14regression') + weightedRegression = fd.formdata.getvalue('applyVarianceSE') + + if fd.parentsf14regression and fd.genotype_2: + _genotype = fd.genotype_2 + else: + _genotype = fd.genotype_1 + + _strains, _vals, _vars, N = fd.informativeStrains(_genotype.prgy, weightedRegression) + + if fd.identification: + heading2 = HT.Paragraph('Trait ID: %s' % fd.identification) + heading2.__setattr__("class","subtitle") + self.dict['title'] = '%s: Genome Association' % fd.identification + else: + heading2 = "" + self.dict['title'] = 'Genome Association' + + if fd.traitInfo: + symbol,chromosome,MB = string.split(fd.traitInfo,'\t') + heading3 = HT.Paragraph('[ ',HT.Strong(HT.Italic('%s' % symbol,id="green")),' on Chr %s @ %s Mb ]' % (chromosome,MB)) + else: + heading3 = "" + + if N < webqtlConfig.KMININFORMATIVE: + heading = "Genome Association" + detail = ['Fewer than %d strain data were entered for %s data set. No mapping attempted.' % (webqtlConfig.KMININFORMATIVE, fd.RISet)] + self.error(heading=heading,detail=detail) + return + else: + heading = HT.Paragraph('Trait Data Entered for %s Set' % fd.RISet) + heading.__setattr__("class","title") + + datadiv = HT.TD(heading, heading2,heading3, width='45%',valign='top', align='left', bgColor='#eeeeee') + resultstable,tblobj,bottomInfo = self.GenReport(ChrNameOrderIdDict,fd, _genotype, _strains, _vals, _vars) + #resultstable = self.GenReport(fd, _genotype, _strains, _vals, _vars) + + # creat object for result table for sort function + objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb') + cPickle.dump(tblobj, objfile) + objfile.close() + + sortby = ("Index", "up") + reportTable =HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID = "sortable", addIndex = "0"), Id="sortable") + + descriptionTable = HT.TableLite(border=0, cellpadding=0, cellspacing=0) + descriptionTable.append(HT.TR(HT.TD(reportTable, colspan=3))) + descriptionTable.append(HT.TR(HT.TD(HT.BR(),HT.BR()))) + descriptionTable.append(bottomInfo) + + self.traitList=_vals + + ##########################plot####################### + + ################################################################ + # Generate Chr list and Retrieve Length Information + ################################################################ + self.genotype= _genotype + self.ChrList = [("All", -1)] + + for i, indChr in enumerate(self.genotype): + self.ChrList.append((indChr.name, i)) + + self.cursor.execute(""" + Select + Length from Chr_Length, InbredSet + where + Chr_Length.SpeciesId = InbredSet.SpeciesId AND + InbredSet.Name = '%s' AND + Chr_Length.Name in (%s) + Order by + OrderId + """ % (fd.RISet, string.join(map(lambda X: "'%s'" % X[0], self.ChrList[1:]), ", "))) + + self.ChrLengthMbList = self.cursor.fetchall() + self.ChrLengthMbList = map(lambda x: x[0]/1000000.0, self.ChrLengthMbList) + self.ChrLengthMbSum = reduce(lambda x, y:x+y, self.ChrLengthMbList, 0.0) + if self.ChrLengthMbList: + self.MbGraphInterval = self.ChrLengthMbSum/(len(self.ChrLengthMbList)*12) #Empirical Mb interval + else: + self.MbGraphInterval = 1 + + self.ChrLengthCMList = [] + for i, _chr in enumerate(self.genotype): + self.ChrLengthCMList.append(_chr[-1].cM - _chr[0].cM) + self.ChrLengthCMSum = reduce(lambda x, y:x+y, self.ChrLengthCMList, 0.0)# used for calculate plot scale + + self.GraphInterval = self.MbGraphInterval #Mb + + # begin: common part with human data + intCanvas = pid.PILCanvas(size=(self.graphWidth,self.graphHeight)) + gifmap = self.plotIntMapping(fd, intCanvas, startMb = self.startMb, endMb = self.endMb, showLocusForm= "") + filename= webqtlUtil.genRandStr("Itvl_") + intCanvas.save(os.path.join(webqtlConfig.IMGDIR, filename), format='png') + intImg=HT.Image('/image/'+filename+'.png', border=0, usemap='#WebQTLImageMap') + + ################################################################ + # footnote goes here + ################################################################ + btminfo = HT.Paragraph(Id="smallsize") #Small('More information about this graph is available here.') + + if (self.additiveChecked): + btminfo.append(HT.BR(), 'A positive additive coefficient (', HT.Font('green', color='green'), ' line) indicates that %s alleles increase trait values. In contrast, a negative additive coefficient (' % fd.ppolar, HT.Font('red', color='red'), ' line) indicates that %s alleles increase trait values.' % fd.mpolar) + + + TD_LR = HT.TR(HT.TD(HT.Blockquote(gifmap,intImg, HT.P()), bgColor='#eeeeee', height = 200)) + + self.dict['body'] = str(datadiv)+str(TD_LR)+str(resultstable)+str(HT.TR(HT.TD(descriptionTable))) + + # end: common part with human data + + else: + pass + + + # add by NL 10-2-2011 + def initializeParameters(self, fd): + """ + Initializes all of the MarkerRegressionPage class parameters, + acquiring most values from the formdata (fd) + """ + ################################### + # manhattam plot parameters + ################################### + + self.graphHeight = 600 + self.graphWidth = 1280 + self.plotScale = 'physic' + self.selectedChr = -1 + self.GRAPH_BACK_DARK_COLOR = pid.HexColor(0xF1F1F9) + self.GRAPH_BACK_LIGHT_COLOR = pid.HexColor(0xFBFBFF) + self.LRS_COLOR = pid.HexColor(0x0000FF) + self.LRS_LOD ='LRS' + self.lrsMax = float(fd.formdata.getvalue('lrsMax', 0)) + self.startMb = fd.formdata.getvalue('startMb', "-1") + self.endMb = fd.formdata.getvalue('endMb', "-1") + self.mappingMethodId = fd.formdata.getvalue('mappingMethodId', "0") + self.permChecked=True + self.multipleInterval=False + self.SIGNIFICANT_WIDTH = 5 + self.SUGGESTIVE_WIDTH = 5 + self.SIGNIFICANT_COLOR = pid.HexColor(0xEBC7C7) + self.SUGGESTIVE_COLOR = pid.gainsboro + self.colorCollection = [self.LRS_COLOR] + self.additiveChecked= True + self.ADDITIVE_COLOR_POSITIVE = pid.green + self.legendChecked =False + self.pValue=float(fd.formdata.getvalue('pValue',-1)) + + # allow user to input p-value greater than 1, + # in this case, the value will be treated as -lgP value. so the input value needs to be transferred to power of 10 format + if self.pValue >1: + self.pValue =10**-(self.pValue) + + try: + self.startMb = float(self.startMb) + self.endMb = float(self.endMb) + if self.startMb > self.endMb: + temp = self.startMb + self.startMb = self.endMb + self.endMb = temp + #minimal distance 10bp + if self.endMb - self.startMb < 0.00001: + self.endMb = self.startMb + 0.00001 + except: + self.startMb = self.endMb = -1 + + def GenReportForPLINK(self, ChrNameOrderIdDict={},RISet='',plinkResultDict= {},thresholdPvalue=-1,chrList=[]): + + 'Create an HTML division which reports any loci which are significantly associated with the submitted trait data.' + ######################################### + # Genome Association report + ######################################### + locusFormName = webqtlUtil.genRandStr("fm_") + locusForm = HT.Form(cgi = os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), \ + enctype='multipart/form-data', name=locusFormName, submit=HT.Input(type='hidden')) + hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':RISet+"Geno",'CellID':'_', \ + 'RISet':RISet, 'incparentsf1':'on'} + for key in hddn.keys(): + locusForm.append(HT.Input(name=key, value=hddn[key], type='hidden')) + + regressionHeading = HT.Paragraph('Genome Association Report') + regressionHeading.__setattr__("class","title") + + filename= webqtlUtil.genRandStr("GenomeAsscociation_") + fpText = open('%s.txt' % (webqtlConfig.TMPDIR+filename), 'wb') + fpText.write('The loci meet the criteria of P-Value <= %3.6f.\n'%thresholdPvalue) + pValueInfo =HT.Paragraph('The loci meet the criteria of P-Value <= %3.6f.\n'%thresholdPvalue) + + textUrl = HT.Href(text = 'Download', url= '/tmp/'+filename+'.txt', target = "_blank", Class='fs12 fwn') + bottomInfo = HT.TR(HT.TD(HT.Paragraph(textUrl, ' result in tab-delimited text format.', HT.BR(), HT.BR(),Class="fs12 fwn"), colspan=3)) + + tblobj={} # build dict for genTableObj function; keys include header and body + tblobj_header = [] # value of key 'header' + tblobj_body=[] # value of key 'body' + reportHeaderRow=[] # header row list for tblobj_header (html part) + headerList=['Index','SNP Name','Chr','Mb','-log(P)'] + headerStyle="fs14 fwb ffl b1 cw cbrb" # style of the header + cellColorStyle = "fs13 b1 fwn c222" # style of the cells + + if headerList: + for ncol, item in enumerate(headerList): + reportHeaderRow.append(THCell(HT.TD(item, Class=headerStyle, valign='bottom',nowrap='ON'),text=item, idx=ncol)) + #download file for table headers' names + fpText.write('SNP_Name\tChromosome\tMb\t-log(P)\n') + + tblobj_header.append(reportHeaderRow) + tblobj['header']=tblobj_header + + index=1 + for chr in chrList: + + if plinkResultDict.has_key(chr): + if chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[chr] + else: + chrOrderId=chr + + valueList=plinkResultDict[chr] + + for value in valueList: + reportBodyRow=[] # row list for tblobj_body (html part) + snpName=value[0] + bp=value[1] + mb=int(bp)/1000000.0 + + try: + pValue =float(value[2]) + except: + pValue =1 + formattedPvalue = -math.log10(pValue) + + formattedPvalue = webqtlUtil.SciFloat(formattedPvalue) + dbSnprs=snpName.replace('rs','') + SnpHref = HT.Href(text=snpName, url="http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs=%s"%dbSnprs, target="_blank") + + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(HT.TD(SnpHref, Class=cellColorStyle,nowrap='ON'),snpName, snpName)) + reportBodyRow.append(TDCell(HT.TD(chr, Class=cellColorStyle, align="center",nowrap='ON'),chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%mb, Class=cellColorStyle, align="center",nowrap='ON'),mb, mb)) + reportBodyRow.append(TDCell(HT.TD(formattedPvalue, Class=cellColorStyle, align="center",nowrap='ON'),formattedPvalue, float(formattedPvalue))) + + fpText.write('%s\t%s\t%3.6f\t%s\n' % (snpName, str(chr), mb, formattedPvalue)) + index+=1 + + tblobj_body.append(reportBodyRow) + + tblobj['body']=tblobj_body + rv=HT.TR(HT.TD(regressionHeading,pValueInfo, locusForm, HT.P(), width='55%',valign='top', align='left',bgColor='#eeeeee')) + + return rv, tblobj,bottomInfo + + + def GenReport(self, ChrNameOrderIdDict,fd, _genotype, _strains, _vals, _vars= []): + 'Create an HTML division which reports any loci which are significantly associated with the submitted trait data.' + #calculate QTL for each trait + self.qtlresults = [] + if webqtlUtil.ListNotNull(_vars): + qtlresults = _genotype.regression(strains = _strains, trait = _vals, variance = _vars) + LRSArray = _genotype.permutation(strains = _strains, trait = _vals, variance = _vars, nperm=fd.nperm) + else: + qtlresults = _genotype.regression(strains = _strains, trait = _vals) + LRSArray = _genotype.permutation(strains = _strains, trait = _vals,nperm=fd.nperm) + + self.qtlresults.append(qtlresults) + + filename= webqtlUtil.genRandStr("GenomeAsscociation_") + + # set suggestive, significant and highly significant LRS + if fd.suggestive == None: + fd.suggestive = LRSArray[int(fd.nperm*0.37-1)] + else: + fd.suggestive = float(fd.suggestive) + if fd.significance == None: + fd.significance = LRSArray[int(fd.nperm*0.95-1)] + else: + fd.significance = float(fd.significance) + + self.significance =fd.significance + self.suggestive = fd.suggestive + self.highlysignificant = LRSArray[int(fd.nperm*0.99-1)] + _dispAllLRS = 0 + if fd.formdata.getvalue('displayAllLRS'): + _dispAllLRS = 1 + qtlresults2 = [] + if _dispAllLRS: + filtered = qtlresults[:] + else: + filtered = filter(lambda x, y=fd.suggestive: x.lrs > y, qtlresults) + if len(filtered) == 0: + qtlresults2 = qtlresults[:] + qtlresults2.sort() + filtered = qtlresults2[-10:] + + ######################################### + # Permutation Graph + ######################################### + myCanvas = pid.PILCanvas(size=(400,300)) + #plotBar(myCanvas,10,10,390,290,LRSArray,XLabel='LRS',YLabel='Frequency',title=' Histogram of Permutation Test',identification=fd.identification) + Plot.plotBar(myCanvas, LRSArray,XLabel='LRS',YLabel='Frequency',title=' Histogram of Permutation Test') + filename= webqtlUtil.genRandStr("Reg_") + myCanvas.save(webqtlConfig.IMGDIR+filename, format='gif') + img=HT.Image('/image/'+filename+'.gif',border=0,alt='Histogram of Permutation Test') + + if fd.suggestive == None: + fd.suggestive = LRSArray[int(fd.nperm*0.37-1)] + else: + fd.suggestive = float(fd.suggestive) + if fd.significance == None: + fd.significance = LRSArray[int(fd.nperm*0.95-1)] + else: + fd.significance = float(fd.significance) + + permutationHeading = HT.Paragraph('Histogram of Permutation Test') + permutationHeading.__setattr__("class","title") + + permutation = HT.TableLite() + permutation.append(HT.TR(HT.TD(img))) + + + ######################################### + # Genome Association report + ######################################### + locusFormName = webqtlUtil.genRandStr("fm_") + locusForm = HT.Form(cgi = os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), \ + enctype='multipart/form-data', name=locusFormName, submit=HT.Input(type='hidden')) + hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':fd.RISet+"Geno",'CellID':'_', \ + 'RISet':fd.RISet, 'incparentsf1':'on'} + for key in hddn.keys(): + locusForm.append(HT.Input(name=key, value=hddn[key], type='hidden')) + + regressionHeading = HT.Paragraph('Genome Association Report') + regressionHeading.__setattr__("class","title") + # report is the info part above report table + if qtlresults2 != []: + report = HT.Blockquote(HT.Font('No association ',color="#FF0000"),HT.Font('with a likelihood ratio statistic greater than %3.1f was found. Here are the top 10 LRSs.' % fd.suggestive,color="#000000")) + else: + report = HT.Blockquote('The following loci in the %s data set have associations with the above trait data.\n' % fd.RISet, HT.P()) + report.__setattr__("class","normalsize") + + fpText = open('%s.txt' % (webqtlConfig.TMPDIR+filename), 'wb') + fpText.write('Suggestive LRS =%3.2f\n'%self.suggestive) + fpText.write('Significant LRS =%3.2f\n'%self.significance) + fpText.write('Highly Significant LRS =%3.2f\n'%self.highlysignificant) + LRSInfo =HT.Paragraph('    Suggestive LRS =%3.2f\n'%fd.suggestive, HT.BR(), '    Significant LRS =%3.2f\n'%fd.significance,HT.BR(),'    Highly Significant LRS =%3.2f\n' % self.highlysignificant) + + textUrl = HT.Href(text = 'Download', url= '/tmp/'+filename+'.txt', target = "_blank", Class='fs12 fwn') + + bottomInfo = HT.TR(HT.TD(HT.Paragraph(textUrl, ' result in tab-delimited text format.', HT.BR(), HT.BR(),'LRS values marked with',HT.Font(' * ',color="red"), 'are greater than the significance threshold (specified by you or by permutation test). ' , HT.BR(), HT.BR(), HT.Strong('Additive Effect'), ' is half the difference in the mean phenotype of all cases that are homozygous for one parental allel at this marker minus the mean of all cases that are homozygous for the other parental allele at this marker. ','In the case of %s strains, for example,' % fd.RISet,' A positive additive effect indicates that %s alleles increase trait values. Negative additive effect indicates that %s alleles increase trait values.'% (fd.ppolar,fd.mpolar),Class="fs12 fwn"))) + + tblobj={} # build dict for genTableObj function; keys include header and body + tblobj_header = [] # value of key 'header' + tblobj_body=[] # value of key 'body' + reportHeaderRow=[] # header row list for tblobj_header (html part) + headerStyle="fs14 fwb ffl b1 cw cbrb" # style of the header + cellColorStyle = "fs13 b1 fwn c222" # style of the cells + + headerList=['Index','LRS','Chr','Mb','Locus','Additive Effect'] + for ncol, item in enumerate(headerList): + reportHeaderRow.append(THCell(HT.TD(item, Class=headerStyle, valign='bottom',nowrap='ON'),text=item, idx=ncol)) + + if fd.genotype.type == 'intercross': + ncol =len(headerList) + reportHeaderRow.append(THCell(HT.TD('Dominance Effect', Class=headerStyle, valign='bottom',nowrap='ON'),text='Dominance Effect', idx=ncol)) + + #download file for table headers' names + fpText.write('LRS\tChromosome\tMb\tLocus\tAdditive Effect\tDominance Effect\n') + + index=1 + for ii in filtered: + #add by NL 06-20-2011: set LRS to 460 when LRS is infinite, + if ii.lrs==float('inf') or ii.lrs>webqtlConfig.MAXLRS: + LRS=webqtlConfig.MAXLRS #maximum LRS value + else: + LRS=ii.lrs + + if LRS > fd.significance: + lrs = HT.TD(HT.Font('%3.3f*' % LRS, color='#FF0000'),Class=cellColorStyle) + else: + lrs = HT.TD('%3.3f' % LRS,Class=cellColorStyle) + + if ii.locus.chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[ii.locus.chr] + else: + chrOrderId=ii.locus.chr + + reportBodyRow=[] # row list for tblobj_body (html part) + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(lrs,LRS, LRS)) + reportBodyRow.append(TDCell(HT.TD(ii.locus.chr, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%ii.locus.Mb, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.Mb, ii.locus.Mb)) + reportBodyRow.append(TDCell(HT.TD(HT.Href(text=ii.locus.name, url = "javascript:showTrait('%s','%s');" % (locusFormName, ii.locus.name), Class='normalsize'), Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.name, ii.locus.name)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.additive, Class=cellColorStyle, align="center",nowrap='ON'),ii.additive, ii.additive)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.dominance, Class=cellColorStyle, align="center",nowrap='ON'),ii.dominance, ii.dominance)) + + fpText.write('%2.3f\t%s\t%3.6f\t%s\t%2.3f\t%2.3f\n' % (LRS, ii.locus.chr, ii.locus.Mb, ii.locus.name, ii.additive, ii.dominance)) + index+=1 + tblobj_body.append(reportBodyRow) + else: + #download file for table headers' names + fpText.write('LRS\tChromosome\tMb\tLocus\tAdditive Effect\n') + + index=1 + for ii in filtered: + #add by NL 06-20-2011: set LRS to 460 when LRS is infinite, + if ii.lrs==float('inf') or ii.lrs>webqtlConfig.MAXLRS: + LRS=webqtlConfig.MAXLRS #maximum LRS value + else: + LRS=ii.lrs + + if LRS > fd.significance: + lrs = HT.TD(HT.Font('%3.3f*' % LRS, color='#FF0000'),Class=cellColorStyle) + else: + lrs = HT.TD('%3.3f' % LRS,Class=cellColorStyle) + + if ii.locus.chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[ii.locus.chr] + else: + chrOrderId=ii.locus.chr + + reportBodyRow=[] # row list for tblobj_body (html part) + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(lrs,LRS, LRS)) + reportBodyRow.append(TDCell(HT.TD(ii.locus.chr, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%ii.locus.Mb, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.Mb, ii.locus.Mb)) + reportBodyRow.append(TDCell(HT.TD(HT.Href(text=ii.locus.name, url = "javascript:showTrait('%s','%s');" % (locusFormName, ii.locus.name), Class='normalsize'), Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.name, ii.locus.name)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.additive, Class=cellColorStyle, align="center",nowrap='ON'),ii.additive, ii.additive)) + + fpText.write('%2.3f\t%s\t%3.6f\t%s\t%2.3f\n' % (LRS, ii.locus.chr, ii.locus.Mb, ii.locus.name, ii.additive)) + index+=1 + tblobj_body.append(reportBodyRow) + + tblobj_header.append(reportHeaderRow) + tblobj['header']=tblobj_header + tblobj['body']=tblobj_body + + rv=HT.TD(regressionHeading,LRSInfo,report, locusForm, HT.P(),width='55%',valign='top', align='left', bgColor='#eeeeee') + if fd.genotype.type == 'intercross': + bottomInfo.append(HT.BR(), HT.BR(), HT.Strong('Dominance Effect'),' is the difference between the mean trait value of cases heterozygous at a marker and the average mean for the two groups homozygous at this marker: e.g., BD - (BB+DD)/2]. A positive dominance effect indicates that the average phenotype of BD heterozygotes exceeds the mean of BB and DD homozygotes. No dominance deviation can be computed for a set of recombinant inbred strains or for a backcross.') + return rv,tblobj,bottomInfo + + return rv,tblobj,bottomInfo + + def plotIntMappingForPLINK(self, fd, canvas, offset= (80, 120, 20, 80), zoom = 1, startMb = None, endMb = None, showLocusForm = "",plinkResultDict={}): + #calculating margins + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + xLeftOffset = int(xLeftOffset*fontZoom) + xRightOffset = int(xRightOffset*fontZoom) + yBottomOffset = int(yBottomOffset*fontZoom) + + cWidth = canvas.size[0] + cHeight = canvas.size[1] + plotWidth = cWidth - xLeftOffset - xRightOffset + plotHeight = cHeight - yTopOffset - yBottomOffset + startPixelX = xLeftOffset + endPixelX = (xLeftOffset + plotWidth) + + #Drawing Area Height + drawAreaHeight = plotHeight + if self.plotScale == 'physic' and self.selectedChr > -1: # for single chr + drawAreaHeight -= self.ENSEMBL_BAND_HEIGHT + self.UCSC_BAND_HEIGHT+ self.WEBQTL_BAND_HEIGHT + 3*self.BAND_SPACING+ 10*zoom + if self.geneChecked: + drawAreaHeight -= self.NUM_GENE_ROWS*self.EACH_GENE_HEIGHT + 3*self.BAND_SPACING + 10*zoom + else: + if self.selectedChr > -1: + drawAreaHeight -= 20 + else:# for all chrs + drawAreaHeight -= 30 + + #Image map + gifmap = HT.Map(name='WebQTLImageMap') + + newoffset = (xLeftOffset, xRightOffset, yTopOffset, yBottomOffset) + # Draw the alternating-color background first and get plotXScale + plotXScale = self.drawGraphBackgroundForPLINK(canvas, gifmap, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb,plinkResultDict=plinkResultDict) + + # Draw X axis + self.drawXAxisForPLINK(fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + # Draw manhattam plot + self.drawManhattanPlotForPLINK(canvas, drawAreaHeight, gifmap, plotXScale, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb,plinkResultDict=plinkResultDict,thresholdPvalue=self.pValue) + + return gifmap + + + def plotIntMapping(self, fd, canvas, offset= (80, 120, 20, 80), zoom = 1, startMb = None, endMb = None, showLocusForm = ""): + #calculating margins + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + xLeftOffset = int(xLeftOffset*fontZoom) + xRightOffset = int(xRightOffset*fontZoom) + yBottomOffset = int(yBottomOffset*fontZoom) + + cWidth = canvas.size[0] + cHeight = canvas.size[1] + plotWidth = cWidth - xLeftOffset - xRightOffset + plotHeight = cHeight - yTopOffset - yBottomOffset + startPixelX = xLeftOffset + endPixelX = (xLeftOffset + plotWidth) + + #Drawing Area Height + drawAreaHeight = plotHeight + if self.plotScale == 'physic' and self.selectedChr > -1: # for single chr + drawAreaHeight -= self.ENSEMBL_BAND_HEIGHT + self.UCSC_BAND_HEIGHT+ self.WEBQTL_BAND_HEIGHT + 3*self.BAND_SPACING+ 10*zoom + if self.geneChecked: + drawAreaHeight -= self.NUM_GENE_ROWS*self.EACH_GENE_HEIGHT + 3*self.BAND_SPACING + 10*zoom + else:# for all chrs + if self.selectedChr > -1: + drawAreaHeight -= 20 + else: + drawAreaHeight -= 30 + + #Image map + gifmap = HT.Map(name='WebQTLImageMap') + + newoffset = (xLeftOffset, xRightOffset, yTopOffset, yBottomOffset) + # Draw the alternating-color background first and get plotXScale + plotXScale = self.drawGraphBackground(canvas, gifmap, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + + # Draw X axis + self.drawXAxis(fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + # Draw QTL curve + self.drawQTL(canvas, drawAreaHeight, gifmap, plotXScale, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + + #draw legend + if self.multipleInterval: + self.drawMultiTraitName(fd, canvas, gifmap, showLocusForm, offset=newoffset) + elif self.legendChecked: + self.drawLegendPanel(fd, canvas, offset=newoffset) + else: + pass + + #draw position, no need to use a separate function + if fd.genotype.Mbmap: + self.drawProbeSetPosition(canvas, plotXScale, offset=newoffset) + + return gifmap + + + # functions for manhattam plot of markers + def drawManhattanPlotForPLINK(self, canvas, drawAreaHeight, gifmap, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None,plinkResultDict={},thresholdPvalue=-1): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + # INTERCROSS = (self.genotype.type=="intercross") + INTERCROSS ='' #?????? + + ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + GraphInterval=self.GraphInterval + pvalueHeightThresh = drawAreaHeight - 80 #ZS: Otherwise the plot gets very close to the chromosome labels + + #draw the pvalue scale + #We first determine whether or not we are using a sliding scale. + #If so, we need to compute the maximum pvalue value to determine where the max y-value should be, and call this pvalueMax. + #pvalueTop is then defined to be above the pvalueMax by enough to add one additional pvalueScale increment. + #if we are using a set-scale, then we set pvalueTop to be the user's value, and pvalueMax doesn't matter. + + # for human data we use p value instead of lrs + pValueList=[] + for key in plinkResultDict: + valueList = plinkResultDict[key] + for item in valueList: + pValue = item[-1] + pValueList.append(pValue) + + formattedPValueList=[] + for pValue in pValueList: + try: + pValue=float(pValue) + except: + pValue =1 + formattedpValue = -math.log10(pValue) + formattedPValueList.append(formattedpValue) + + #sliding scale + pvalueMax = max(formattedPValueList) + #pvalueMax =pvalueMax +1 + # no permutation result for plink func: GenReport() + pvalueMin = int(-math.log10(thresholdPvalue)) + + if pvalueMax> 100: + pvalueScale = 20.0 + elif pvalueMax > 20: + pvalueScale = 5.0 + elif pvalueMax > 7.5: + pvalueScale = 2.5 + else: + pvalueScale = 1.0 + + # the base line for x-axis is -log(thresholdPvalue) + pvalueAxisList = Plot.frange(pvalueMin, pvalueMax, pvalueScale) + #make sure the user's value appears on the y-axis + #ZS: There is no way to do this without making the position of the points not directly proportional to a given distance on the y-axis + #tempPvalueMax=round(pvalueMax) + tempPvalueMax = pvalueAxisList[len(pvalueAxisList)-1] + pvalueScale + pvalueAxisList.append(tempPvalueMax) + + #ZS: I don't understand this; the if statement will be true for any number that isn't exactly X.5. + #if abs(tempPvalueMax-pvalueMax) <0.5: + # tempPvalueMax=tempPvalueMax+1 + # pvalueAxisList.append(tempPvalueMax) + + #draw the "pvalue" string to the left of the axis + pvalueScaleFont=pid.Font(ttf="verdana", size=14*fontZoom, bold=0) + pvalueLODFont=pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + yZero = yTopOffset + plotHeight + + #yAxis label display area + yAxis_label ='-log(P)' + canvas.drawString(yAxis_label, xLeftOffset - canvas.stringWidth("999.99", font=pvalueScaleFont) - 10*zoom, \ + yZero - 150, font=pvalueLODFont, color=pid.black, angle=90) + + for i,item in enumerate(pvalueAxisList): + ypvalue = yZero - (float(i)/float(len(pvalueAxisList) - 1)) * pvalueHeightThresh + canvas.drawLine(xLeftOffset, ypvalue, xLeftOffset - 4, ypvalue, color=self.LRS_COLOR, width=1*zoom) + scaleStr = "%2.1f" % item + #added by NL 6-24-2011:Y-axis scale display + canvas.drawString(scaleStr, xLeftOffset-4-canvas.stringWidth(scaleStr, font=pvalueScaleFont)-5, ypvalue+3, font=pvalueScaleFont, color=self.LRS_COLOR) + + ChrList=self.ChrList + startPosX = xLeftOffset + + for i, chr in enumerate(ChrList): + + if plinkResultDict.has_key(chr): + plinkresultList = plinkResultDict[chr] + + m = 0 + #add by NL 06-24-2011: for mahanttam plot + symbolFont = pid.Font(ttf="fnt_bs", size=5,bold=0) + # color for point in each chr + chrCount=len(ChrList) + chrColorDict =self.getColorForMarker(chrCount=chrCount,flag=1) + for j, item in enumerate(plinkresultList): + try : + mb=float(item[1])/1000000.0 + except: + mb=0 + + try : + pvalue =float(item[-1]) + except: + pvalue =1 + + try: + snpName = item[0] + except: + snpName='' + + formattedPvalue = -math.log10(pvalue) + + Xc = startPosX + (mb-startMb)*plotXScale + Yc = yZero - (formattedPvalue-pvalueMin)*pvalueHeightThresh/(tempPvalueMax - pvalueMin) + canvas.drawString("5", Xc-canvas.stringWidth("5",font=symbolFont)/2+1,Yc+2,color=chrColorDict[i], font=symbolFont) + m += 1 + + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset, yTopOffset, color=self.LRS_COLOR, width=1*zoom) #the blue line running up the y axis + + def drawQTL(self, canvas, drawAreaHeight, gifmap, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + INTERCROSS = (self.genotype.type=="intercross") + + ChrLengthDistList = self.ChrLengthMbList + GraphInterval=self.GraphInterval + LRSHeightThresh = drawAreaHeight + AdditiveHeightThresh = drawAreaHeight/2 + DominanceHeightThresh = drawAreaHeight/2 + + #draw the LRS scale + #We first determine whether or not we are using a sliding scale. + #If so, we need to compute the maximum LRS value to determine where the max y-value should be, and call this LRSMax. + #LRSTop is then defined to be above the LRSMax by enough to add one additional LRSScale increment. + #if we are using a set-scale, then we set LRSTop to be the user's value, and LRSMax doesn't matter. + + if self.LRS_LOD == 'LOD': + lodm = self.LODFACTOR + else: + lodm = 1.0 + + if self.lrsMax <= 0: #sliding scale + LRSMax = max(map(max, self.qtlresults)).lrs + #genotype trait will give infinite LRS + LRSMax = min(LRSMax, webqtlConfig.MAXLRS) + LRSMax = max(self.significance, LRSMax) + else: + LRSMax = self.lrsMax*lodm + + if LRSMax/lodm > 100: + LRSScale = 20.0 + elif LRSMax/lodm > 20: + LRSScale = 5.0 + elif LRSMax/lodm > 7.5: + LRSScale = 2.5 + else: + LRSScale = 1.0 + + LRSAxisList = Plot.frange(LRSScale, LRSMax/lodm, LRSScale) + #make sure the user's value appears on the y-axis + #update by NL 6-21-2011: round the LOD value to 100 when LRSMax is equal to 460 + LRSAxisList.append(round(LRSMax/lodm)) + + #draw the "LRS" or "LOD" string to the left of the axis + LRSScaleFont=pid.Font(ttf="verdana", size=14*fontZoom, bold=0) + LRSLODFont=pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + yZero = yTopOffset + plotHeight + + #yAxis label display area + canvas.drawString(self.LRS_LOD, xLeftOffset - canvas.stringWidth("999.99", font=LRSScaleFont) - 10*zoom, \ + yZero - 150, font=LRSLODFont, color=pid.black, angle=90) + + for item in LRSAxisList: + yLRS = yZero - (item*lodm/LRSMax) * LRSHeightThresh + canvas.drawLine(xLeftOffset, yLRS, xLeftOffset - 4, yLRS, color=self.LRS_COLOR, width=1*zoom) + scaleStr = "%2.1f" % item + #added by NL 6-24-2011:Y-axis scale display + canvas.drawString(scaleStr, xLeftOffset-4-canvas.stringWidth(scaleStr, font=LRSScaleFont)-5, yLRS+3, font=LRSScaleFont, color=self.LRS_COLOR) + + + #"Significant" and "Suggestive" Drawing Routine + # ======= Draw the thick lines for "Significant" and "Suggestive" ===== (crowell: I tried to make the SNPs draw over these lines, but piddle wouldn't have it...) + if self.permChecked and not self.multipleInterval: + significantY = yZero - self.significance*LRSHeightThresh/LRSMax + suggestiveY = yZero - self.suggestive*LRSHeightThresh/LRSMax + + + startPosX = xLeftOffset + for i, _chr in enumerate(self.genotype): + rightEdge = int(startPosX + self.ChrLengthDistList[i]*plotXScale - self.SUGGESTIVE_WIDTH/1.5) + #added by NL 6-24-2011:draw suggestive line (grey one) + canvas.drawLine(startPosX+self.SUGGESTIVE_WIDTH/1.5, suggestiveY, rightEdge, suggestiveY, color=self.SUGGESTIVE_COLOR, + width=self.SUGGESTIVE_WIDTH*zoom, clipX=(xLeftOffset, xLeftOffset + plotWidth-2)) + #added by NL 6-24-2011:draw significant line (pink one) + canvas.drawLine(startPosX+self.SUGGESTIVE_WIDTH/1.5, significantY, rightEdge, significantY, color=self.SIGNIFICANT_COLOR, + width=self.SIGNIFICANT_WIDTH*zoom, clipX=(xLeftOffset, xLeftOffset + plotWidth-2)) + sugg_coords = "%d, %d, %d, %d" % (startPosX, suggestiveY-2, rightEdge + 2*zoom, suggestiveY+2) + sig_coords = "%d, %d, %d, %d" % (startPosX, significantY-2, rightEdge + 2*zoom, significantY+2) + if self.LRS_LOD == 'LRS': + sugg_title = "Suggestive LRS = %0.2f" % self.suggestive + sig_title = "Significant LRS = %0.2f" % self.significance + else: + sugg_title = "Suggestive LOD = %0.2f" % (self.suggestive/4.61) + sig_title = "Significant LOD = %0.2f" % (self.significance/4.61) + Areas1 = HT.Area(shape='rect',coords=sugg_coords,title=sugg_title) + Areas2 = HT.Area(shape='rect',coords=sig_coords,title=sig_title) + gifmap.areas.append(Areas1) + gifmap.areas.append(Areas2) + + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + + if self.multipleInterval: + lrsEdgeWidth = 1 + else: + additiveMax = max(map(lambda X : abs(X.additive), self.qtlresults[0])) + if INTERCROSS: + dominanceMax = max(map(lambda X : abs(X.dominance), self.qtlresults[0])) + else: + dominanceMax = -1 + lrsEdgeWidth = 2 + for i, qtlresult in enumerate(self.qtlresults): + m = 0 + startPosX = xLeftOffset + thisLRSColor = self.colorCollection[i] + + #add by NL 06-24-2011: for mahanttam plot + symbolFont = pid.Font(ttf="fnt_bs", size=5,bold=0) + + for j, _chr in enumerate(self.genotype): + chrCount=len(self.genotype) + chrColorDict =self.getColorForMarker(chrCount=chrCount,flag=1) + LRSCoordXY = [] + AdditiveCoordXY = [] + DominanceCoordXY = [] + for k, _locus in enumerate(_chr): + if self.plotScale == 'physic': + Xc = startPosX + (_locus.Mb-startMb)*plotXScale + else: + Xc = startPosX + (_locus.cM-_chr[0].cM)*plotXScale + # updated by NL 06-18-2011: + # fix the over limit LRS graph issue since genotype trait may give infinite LRS; + # for any lrs is over than 460(LRS max in this system), it will be reset to 460 + if qtlresult[m].lrs> 460 or qtlresult[m].lrs=='inf': + Yc = yZero - webqtlConfig.MAXLRS*LRSHeightThresh/LRSMax + else: + Yc = yZero - qtlresult[m].lrs*LRSHeightThresh/LRSMax + + LRSCoordXY.append((Xc, Yc)) + #add by NL 06-24-2011: for mahanttam plot + #self.significance/4.61 consider chr and LOD + # significantY = yZero - self.significance*LRSHeightThresh/LRSMax + # if Yc >significantY: + # canvas.drawString(":", Xc-canvas.stringWidth(":",font=symbolFont)/2+1,Yc+2,color=pid.black, font=symbolFont) + # else: + # canvas.drawString(":", Xc-canvas.stringWidth(":",font=symbolFont)/2+1,Yc+2,color=pid.black, font=symbolFont) + + # add by NL 06-27-2011: eliminate imputed value when locus name is equal to '-' + if (qtlresult[m].locus.name) and (qtlresult[m].locus.name!=' - '): + canvas.drawString("5", Xc-canvas.stringWidth("5",font=symbolFont)/2+1,Yc+2,color=chrColorDict[j], font=symbolFont) + + if not self.multipleInterval and self.additiveChecked: + Yc = yZero - qtlresult[m].additive*AdditiveHeightThresh/additiveMax + AdditiveCoordXY.append((Xc, Yc)) + if not self.multipleInterval and INTERCROSS and self.additiveChecked: + Yc = yZero - qtlresult[m].dominance*DominanceHeightThresh/dominanceMax + DominanceCoordXY.append((Xc, Yc)) + m += 1 + + startPosX += (ChrLengthDistList[j]+GraphInterval)*plotXScale + + + ###draw additive scale + if not self.multipleInterval and self.additiveChecked: + additiveScaleFont=pid.Font(ttf="verdana",size=12*fontZoom,bold=0) + additiveScale = Plot.detScaleOld(0,additiveMax) + additiveStep = (additiveScale[1]-additiveScale[0])/additiveScale[2] + additiveAxisList = Plot.frange(0, additiveScale[1], additiveStep) + maxAdd = additiveScale[1] + addPlotScale = AdditiveHeightThresh/additiveMax + + additiveAxisList.append(additiveScale[1]) + for item in additiveAxisList: + additiveY = yZero - item*addPlotScale + canvas.drawLine(xLeftOffset + plotWidth,additiveY,xLeftOffset+4+ plotWidth,additiveY,color=self.ADDITIVE_COLOR_POSITIVE, width=1*zoom) + scaleStr = "%2.3f" % item + canvas.drawString(scaleStr,xLeftOffset + plotWidth +6,additiveY+5,font=additiveScaleFont,color=self.ADDITIVE_COLOR_POSITIVE) + + canvas.drawLine(xLeftOffset+plotWidth,additiveY,xLeftOffset+plotWidth,yZero,color=self.ADDITIVE_COLOR_POSITIVE, width=1*zoom) + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset, yTopOffset, color=self.LRS_COLOR, width=1*zoom) #the blue line running up the y axis + + def drawGraphBackgroundForPLINK(self, canvas, gifmap, offset= (80, 120, 80, 50), zoom = 1, startMb = None, endMb = None,plinkResultDict={} ): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #calculate plot scale + #XZ: all of these global variables should be passed from function signiture + ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + GraphInterval=self.GraphInterval + ChrList =self.ChrList + + #multiple chromosome view + plotXScale = plotWidth / ((len(ChrList)-1)*GraphInterval + drawRegionDistance) + + startPosX = xLeftOffset + chrLabelFont=pid.Font(ttf="verdana",size=24*fontZoom,bold=0) + + for i, _chr in enumerate(ChrList): + + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + # NL:resize chr width for drawing + if float(ChrLengthDistList[i])<90: + ChrLengthDistList[i]=90 + #draw the shaded boxes and the sig/sug thick lines + canvas.drawRect(startPosX, yTopOffset, startPosX + ChrLengthDistList[i]*plotXScale, \ + yTopOffset+plotHeight, edgeColor=pid.gainsboro,fillColor=theBackColor) + + chrNameWidth = canvas.stringWidth(_chr, font=chrLabelFont) + chrStartPix = startPosX + (ChrLengthDistList[i]*plotXScale -chrNameWidth)/2 + chrEndPix = startPosX + (ChrLengthDistList[i]*plotXScale +chrNameWidth)/2 + + canvas.drawString(_chr, chrStartPix, yTopOffset +20,font = chrLabelFont,color=pid.dimgray) + COORDS = "%d,%d,%d,%d" %(chrStartPix, yTopOffset, chrEndPix,yTopOffset +20) + + #add by NL 09-03-2010 + HREF = "javascript:changeView(%d,%s);" % (i,ChrLengthDistList) + Areas = HT.Area(shape='rect',coords=COORDS,href=HREF) + gifmap.areas.append(Areas) + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + return plotXScale + + + def drawGraphBackground(self, canvas, gifmap, offset= (80, 120, 80, 50), zoom = 1, startMb = None, endMb = None): + ##conditions + ##multiple Chromosome view + ##single Chromosome Physical + ##single Chromosome Genetic + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #calculate plot scale + if self.plotScale != 'physic': + self.ChrLengthDistList = self.ChrLengthCMList + drawRegionDistance = self.ChrLengthCMSum + else: + self.ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + + if self.selectedChr > -1: #single chromosome view + spacingAmt = plotWidth/13.5 + i = 0 + for startPix in Plot.frange(xLeftOffset, xLeftOffset+plotWidth, spacingAmt): + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + i += 1 + canvas.drawRect(startPix, yTopOffset, min(startPix+spacingAmt, xLeftOffset+plotWidth), \ + yTopOffset+plotHeight, edgeColor=theBackColor, fillColor=theBackColor) + + drawRegionDistance = self.ChrLengthDistList[self.selectedChr] + self.ChrLengthDistList = [drawRegionDistance] + if self.plotScale == 'physic': + plotXScale = plotWidth / (endMb-startMb) + else: + plotXScale = plotWidth / drawRegionDistance + + else: #multiple chromosome view + plotXScale = plotWidth / ((len(self.genotype)-1)*self.GraphInterval + drawRegionDistance) + + startPosX = xLeftOffset + chrLabelFont=pid.Font(ttf="verdana",size=24*fontZoom,bold=0) + + for i, _chr in enumerate(self.genotype): + + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + + #draw the shaded boxes and the sig/sug thick lines + canvas.drawRect(startPosX, yTopOffset, startPosX + self.ChrLengthDistList[i]*plotXScale, \ + yTopOffset+plotHeight, edgeColor=pid.gainsboro,fillColor=theBackColor) + + chrNameWidth = canvas.stringWidth(_chr.name, font=chrLabelFont) + chrStartPix = startPosX + (self.ChrLengthDistList[i]*plotXScale -chrNameWidth)/2 + chrEndPix = startPosX + (self.ChrLengthDistList[i]*plotXScale +chrNameWidth)/2 + + canvas.drawString(_chr.name, chrStartPix, yTopOffset +20,font = chrLabelFont,color=pid.dimgray) + COORDS = "%d,%d,%d,%d" %(chrStartPix, yTopOffset, chrEndPix,yTopOffset +20) + + #add by NL 09-03-2010 + HREF = "javascript:changeView(%d,%s);" % (i,self.ChrLengthMbList) + Areas = HT.Area(shape='rect',coords=COORDS,href=HREF) + gifmap.areas.append(Areas) + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + return plotXScale + + # XZ: The only difference of function drawXAxisForPLINK and function drawXAxis are the function name and the self.plotScale condition. + def drawXAxisForPLINK(self, fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #Parameters + ChrLengthDistList = self.ChrLengthMbList + GraphInterval=self.GraphInterval + + NUM_MINOR_TICKS = 5 # Number of minor ticks between major ticks + X_MAJOR_TICK_THICKNESS = 2 + X_MINOR_TICK_THICKNESS = 1 + X_AXIS_THICKNESS = 1*zoom + + # ======= Alex: Draw the X-axis labels (megabase location) + MBLabelFont = pid.Font(ttf="verdana", size=12*fontZoom, bold=0) + xMajorTickHeight = 15 # How high the tick extends below the axis + xMinorTickHeight = 5*zoom + xAxisTickMarkColor = pid.black + xAxisLabelColor = pid.black + fontHeight = 12*fontZoom # How tall the font that we're using is + spacingFromLabelToAxis = 20 + spacingFromLineToLabel = 3 + + if self.plotScale == 'physic': + strYLoc = yZero + spacingFromLabelToAxis + canvas.fontHeight(MBLabelFont) + ###Physical single chromosome view + if self.selectedChr > -1: + graphMbWidth = endMb - startMb + XScale = Plot.detScale(startMb, endMb) + XStart, XEnd, XStep = XScale + if XStep < 8: + XStep *= 2 + spacingAmtX = spacingAmt = (XEnd-XStart)/XStep + + j = 0 + while abs(spacingAmtX -int(spacingAmtX)) >= spacingAmtX/100.0 and j < 6: + j += 1 + spacingAmtX *= 10 + + formatStr = '%%2.%df' % j + + for counter, _Mb in enumerate(Plot.frange(XStart, XEnd, spacingAmt / NUM_MINOR_TICKS)): + if _Mb < startMb or _Mb > endMb: + continue + Xc = xLeftOffset + plotXScale*(_Mb - startMb) + if counter % NUM_MINOR_TICKS == 0: # Draw a MAJOR mark, not just a minor tick mark + canvas.drawLine(Xc, yZero, Xc, yZero+xMajorTickHeight, color=xAxisTickMarkColor, width=X_MAJOR_TICK_THICKNESS) # Draw the MAJOR tick mark + labelStr = str(formatStr % _Mb) # What Mbase location to put on the label + strWidth = canvas.stringWidth(labelStr, font=MBLabelFont) + drawStringXc = (Xc - (strWidth / 2.0)) + canvas.drawString(labelStr, drawStringXc, strYLoc, font=MBLabelFont, color=xAxisLabelColor, angle=0) + else: + canvas.drawLine(Xc, yZero, Xc, yZero+xMinorTickHeight, color=xAxisTickMarkColor, width=X_MINOR_TICK_THICKNESS) # Draw the MINOR tick mark + # end else + + ###Physical genome wide view + else: + distScale = 0 + startPosX = xLeftOffset + for i, distLen in enumerate(ChrLengthDistList): + if distScale == 0: #universal scale in whole genome mapping + if distLen > 75: + distScale = 25 + elif distLen > 30: + distScale = 10 + else: + distScale = 5 + for tickdists in range(distScale, ceil(distLen), distScale): + canvas.drawLine(startPosX + tickdists*plotXScale, yZero, startPosX + tickdists*plotXScale, yZero + 7, color=pid.black, width=1*zoom) + canvas.drawString(str(tickdists), startPosX+tickdists*plotXScale, yZero + 10*zoom, color=pid.black, font=MBLabelFont, angle=270) + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + megabaseLabelFont = pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + canvas.drawString("Megabases", xLeftOffset + (plotWidth -canvas.stringWidth("Megabases", font=megabaseLabelFont))/2, + strYLoc + canvas.fontHeight(MBLabelFont) + 5*zoom, font=megabaseLabelFont, color=pid.black) + pass + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset+plotWidth, yZero, color=pid.black, width=X_AXIS_THICKNESS) # Draw the X axis itself + + def drawXAxis(self, fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #Parameters + NUM_MINOR_TICKS = 5 # Number of minor ticks between major ticks + X_MAJOR_TICK_THICKNESS = 2 + X_MINOR_TICK_THICKNESS = 1 + X_AXIS_THICKNESS = 1*zoom + + # ======= Alex: Draw the X-axis labels (megabase location) + MBLabelFont = pid.Font(ttf="verdana", size=12*fontZoom, bold=0) + xMajorTickHeight = 15 # How high the tick extends below the axis + xMinorTickHeight = 5*zoom + xAxisTickMarkColor = pid.black + xAxisLabelColor = pid.black + fontHeight = 12*fontZoom # How tall the font that we're using is + spacingFromLabelToAxis = 20 + spacingFromLineToLabel = 3 + + if self.plotScale == 'physic': + strYLoc = yZero + spacingFromLabelToAxis + canvas.fontHeight(MBLabelFont) + ###Physical single chromosome view + if self.selectedChr > -1: + graphMbWidth = endMb - startMb + XScale = Plot.detScale(startMb, endMb) + XStart, XEnd, XStep = XScale + if XStep < 8: + XStep *= 2 + spacingAmtX = spacingAmt = (XEnd-XStart)/XStep + + j = 0 + while abs(spacingAmtX -int(spacingAmtX)) >= spacingAmtX/100.0 and j < 6: + j += 1 + spacingAmtX *= 10 + + formatStr = '%%2.%df' % j + + for counter, _Mb in enumerate(Plot.frange(XStart, XEnd, spacingAmt / NUM_MINOR_TICKS)): + if _Mb < startMb or _Mb > endMb: + continue + Xc = xLeftOffset + plotXScale*(_Mb - startMb) + if counter % NUM_MINOR_TICKS == 0: # Draw a MAJOR mark, not just a minor tick mark + canvas.drawLine(Xc, yZero, Xc, yZero+xMajorTickHeight, color=xAxisTickMarkColor, width=X_MAJOR_TICK_THICKNESS) # Draw the MAJOR tick mark + labelStr = str(formatStr % _Mb) # What Mbase location to put on the label + strWidth = canvas.stringWidth(labelStr, font=MBLabelFont) + drawStringXc = (Xc - (strWidth / 2.0)) + canvas.drawString(labelStr, drawStringXc, strYLoc, font=MBLabelFont, color=xAxisLabelColor, angle=0) + else: + canvas.drawLine(Xc, yZero, Xc, yZero+xMinorTickHeight, color=xAxisTickMarkColor, width=X_MINOR_TICK_THICKNESS) # Draw the MINOR tick mark + # end else + + ###Physical genome wide view + else: + distScale = 0 + startPosX = xLeftOffset + for i, distLen in enumerate(self.ChrLengthDistList): + if distScale == 0: #universal scale in whole genome mapping + if distLen > 75: + distScale = 25 + elif distLen > 30: + distScale = 10 + else: + distScale = 5 + for tickdists in range(distScale, ceil(distLen), distScale): + canvas.drawLine(startPosX + tickdists*plotXScale, yZero, startPosX + tickdists*plotXScale, yZero + 7, color=pid.black, width=1*zoom) + canvas.drawString(str(tickdists), startPosX+tickdists*plotXScale, yZero + 10*zoom, color=pid.black, font=MBLabelFont, angle=270) + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + megabaseLabelFont = pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + canvas.drawString("Megabases", xLeftOffset + (plotWidth -canvas.stringWidth("Megabases", font=megabaseLabelFont))/2, + strYLoc + canvas.fontHeight(MBLabelFont) + 5*zoom, font=megabaseLabelFont, color=pid.black) + pass + else: + ChrAInfo = [] + preLpos = -1 + distinctCount = 0.0 + if len(self.genotype) > 1: + for i, _chr in enumerate(self.genotype): + thisChr = [] + Locus0CM = _chr[0].cM + nLoci = len(_chr) + if nLoci <= 8: + for _locus in _chr: + if _locus.name != ' - ': + if _locus.cM != preLpos: + distinctCount += 1 + preLpos = _locus.cM + thisChr.append([_locus.name, _locus.cM-Locus0CM]) + else: + for j in (0, nLoci/4, nLoci/2, nLoci*3/4, -1): + while _chr[j].name == ' - ': + j += 1 + if _chr[j].cM != preLpos: + distinctCount += 1 + preLpos = _chr[j].cM + thisChr.append([_chr[j].name, _chr[j].cM-Locus0CM]) + ChrAInfo.append(thisChr) + else: + for i, _chr in enumerate(self.genotype): + thisChr = [] + Locus0CM = _chr[0].cM + for _locus in _chr: + if _locus.name != ' - ': + if _locus.cM != preLpos: + distinctCount += 1 + preLpos = _locus.cM + thisChr.append([_locus.name, _locus.cM-Locus0CM]) + ChrAInfo.append(thisChr) + + stepA = (plotWidth+0.0)/distinctCount + + LRectWidth = 10 + LRectHeight = 3 + offsetA = -stepA + lineColor = pid.lightblue + startPosX = xLeftOffset + for j, ChrInfo in enumerate(ChrAInfo): + preLpos = -1 + for i, item in enumerate(ChrInfo): + Lname,Lpos = item + if Lpos != preLpos: + offsetA += stepA + differ = 1 + else: + differ = 0 + preLpos = Lpos + Lpos *= plotXScale + if self.selectedChr > -1: + Zorder = i % 5 + else: + Zorder = 0 + if differ: + canvas.drawLine(startPosX+Lpos,yZero,xLeftOffset+offsetA,\ + yZero+25, color=lineColor) + canvas.drawLine(xLeftOffset+offsetA,yZero+25,xLeftOffset+offsetA,\ + yZero+40+Zorder*(LRectWidth+3),color=lineColor) + rectColor = pid.orange + else: + canvas.drawLine(xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3)-3,\ + xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3),color=lineColor) + rectColor = pid.deeppink + canvas.drawRect(xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3),\ + xLeftOffset+offsetA-LRectHeight,yZero+40+Zorder*(LRectWidth+3)+LRectWidth,\ + edgeColor=rectColor,fillColor=rectColor,edgeWidth = 0) + COORDS="%d,%d,%d,%d"%(xLeftOffset+offsetA-LRectHeight, yZero+40+Zorder*(LRectWidth+3),\ + xLeftOffset+offsetA,yZero+40+Zorder*(LRectWidth+3)+LRectWidth) + HREF="javascript:showDatabase3('%s','%s','%s','');" % (showLocusForm,fd.RISet+"Geno", Lname) + Areas=HT.Area(shape='rect',coords=COORDS,href=HREF, title="Locus : " + Lname) + gifmap.areas.append(Areas) + ##piddle bug + if j == 0: + canvas.drawLine(startPosX,yZero,startPosX,yZero+40, color=lineColor) + startPosX += (self.ChrLengthDistList[j]+self.GraphInterval)*plotXScale + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset+plotWidth, yZero, color=pid.black, width=X_AXIS_THICKNESS) # Draw the X axis itself + + def getColorForMarker(self, chrCount,flag):# no change is needed + chrColorDict={} + for i in range(chrCount): + if flag==1: # display blue and lightblue intercross + chrColorDict[i]=pid.black + elif flag==0: + if (i%2==0): + chrColorDict[i]=pid.blue + else: + chrColorDict[i]=pid.lightblue + else:#display different color for different chr + if i in [0,8,16]: + chrColorDict[i]=pid.black + elif i in [1,9,17]: + chrColorDict[i]=pid.red + elif i in [2,10,18]: + chrColorDict[i]=pid.lightgreen + elif i in [3,11,19]: + chrColorDict[i]=pid.blue + elif i in [4,12]: + chrColorDict[i]=pid.lightblue + elif i in [5,13]: + chrColorDict[i]=pid.hotpink + elif i in [6,14]: + chrColorDict[i]=pid.gold + elif i in [7,15]: + chrColorDict[i]=pid.grey + + return chrColorDict + + + def drawProbeSetPosition(self, canvas, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + if len(self.traitList) != 1: + return + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + try: + Chr = self.traitList[0].chr # self.traitListChr =self.traitList[0].chr=_vals need to change to chrList and mbList + Mb = self.traitList[0].mb # self.traitListMb =self.traitList[0].mb=_vals + except: + return + + if self.plotScale == 'physic': + if self.selectedChr > -1: + if self.genotype[0].name != Chr or Mb < self.startMb or Mb > self.endMb: + return + else: + locPixel = xLeftOffset + (Mb-self.startMb)*plotXScale + else: + locPixel = xLeftOffset + for i, _chr in enumerate(self.genotype): + if _chr.name != Chr: + locPixel += (self.ChrLengthDistList[i] + self.GraphInterval)*plotXScale + else: + locPixel += Mb*plotXScale + break + else: + if self.selectedChr > -1: + if self.genotype[0].name != Chr: + return + else: + for i, _locus in enumerate(self.genotype[0]): + #the trait's position is on the left of the first genotype + if i==0 and _locus.Mb >= Mb: + locPixel=-1 + break + + #the trait's position is between two traits + if i > 0 and self.genotype[0][i-1].Mb < Mb and _locus.Mb >= Mb: + locPixel = xLeftOffset + plotXScale*(self.genotype[0][i-1].cM+(_locus.cM-self.genotype[0][i-1].cM)*(Mb -self.genotype[0][i-1].Mb)/(_locus.Mb-self.genotype[0][i-1].Mb)) + break + + #the trait's position is on the right of the last genotype + if i==len(self.genotype[0]) and Mb>=_locus.Mb: + locPixel = -1 + else: + locPixel = xLeftOffset + for i, _chr in enumerate(self.genotype): + if _chr.name != Chr: + locPixel += (self.ChrLengthDistList[i] + self.GraphInterval)*plotXScale + else: + locPixel += (Mb*(_chr[-1].cM-_chr[0].cM)/self.ChrLengthCMList[i])*plotXScale + break + if locPixel >= 0: + traitPixel = ((locPixel, yZero), (locPixel-6, yZero+12), (locPixel+6, yZero+12)) + canvas.drawPolygon(traitPixel, edgeColor=pid.black, fillColor=self.TRANSCRIPT_LOCATION_COLOR, closed=1) + + if self.legendChecked: + startPosY = 15 + nCol = 2 + smallLabelFont = pid.Font(ttf="trebuc", size=12, bold=1) + leftOffset = xLeftOffset+(nCol-1)*200 + canvas.drawPolygon(((leftOffset+6, startPosY-6), (leftOffset, startPosY+6), (leftOffset+12, startPosY+6)), edgeColor=pid.black, fillColor=self.TRANSCRIPT_LOCATION_COLOR, closed=1) + canvas.drawString("Sequence Site", (leftOffset+15), (startPosY+5), smallLabelFont, self.TOP_RIGHT_INFO_COLOR) + + # build dict based on plink result, key is chr, value is list of [snp,BP,pValue] + def getPlinkResultDict(self,outputFileName='',thresholdPvalue=-1,ChrOrderIdNameDict={}): + + ChrList =self.ChrList + plinkResultDict={} + + plinkResultfp = open("%s%s.qassoc"% (webqtlConfig.TMPDIR, outputFileName), "rb") + + headerLine=plinkResultfp.readline()# read header line + line = plinkResultfp.readline() + + valueList=[] # initialize value list, this list will include snp, bp and pvalue info + pValueList=[] + count=0 + + while line: + #convert line from str to list + lineList=self.buildLineList(line=line) + + # only keep the records whose chromosome name is in db + if ChrOrderIdNameDict.has_key(int(lineList[0])) and lineList[-1] and lineList[-1].strip()!='NA': + + chrName=ChrOrderIdNameDict[int(lineList[0])] + snp = lineList[1] + BP = lineList[2] + pValue = float(lineList[-1]) + pValueList.append(pValue) + + if plinkResultDict.has_key(chrName): + valueList=plinkResultDict[chrName] + + # pvalue range is [0,1] + if thresholdPvalue >=0 and thresholdPvalue<=1: + if pValue < thresholdPvalue: + valueList.append((snp,BP,pValue)) + count+=1 + + plinkResultDict[chrName]=valueList + valueList=[] + else: + if thresholdPvalue>=0 and thresholdPvalue<=1: + if pValue < thresholdPvalue: + valueList.append((snp,BP,pValue)) + count+=1 + + if valueList: + plinkResultDict[chrName]=valueList + + valueList=[] + + + line =plinkResultfp.readline() + else: + line=plinkResultfp.readline() + + if pValueList: + minPvalue= min(pValueList) + else: + minPvalue=0 + + return count,minPvalue,plinkResultDict + + + ###################################################### + # input: line: str,one line read from file + # function: convert line from str to list; + # output: lineList list + ####################################################### + def buildLineList(self,line=None): + + lineList = string.split(string.strip(line),' ')# irregular number of whitespaces between columns + lineList =[ item for item in lineList if item <>''] + lineList = map(string.strip, lineList) + + return lineList + + #added by NL: automatically generate pheno txt file for PLINK based on strainList passed from dataEditing page + def genPhenoTxtFileForPlink(self,phenoFileName='', RISetName='', probesetName='', valueDict={}): + pedFileStrainList=self.getStrainNameFromPedFile(RISetName=RISetName) + outputFile = open("%s%s.txt"%(webqtlConfig.TMPDIR,phenoFileName),"wb") + headerLine = 'FID\tIID\t%s\n'%probesetName + outputFile.write(headerLine) + + newValueList=[] + + #if valueDict does not include some strain, value will be set to -9999 as missing value + for item in pedFileStrainList: + try: + value=valueDict[item] + value=str(value).replace('value=','') + value=value.strip() + except: + value=-9999 + + newValueList.append(value) + + + newLine='' + for i, strain in enumerate(pedFileStrainList): + j=i+1 + value=newValueList[i] + newLine+='%s\t%s\t%s\n'%(strain, strain, value) + + if j%1000==0: + outputFile.write(newLine) + newLine='' + + if newLine: + outputFile.write(newLine) + + outputFile.close() + + # get strain name from ped file in order + def getStrainNameFromPedFile(self, RISetName=''): + pedFileopen= open("%splink/%s.ped"%(webqtlConfig.HTMLPATH, RISetName),"r") + line =pedFileopen.readline() + strainNameList=[] + + while line: + lineList=string.split(string.strip(line),'\t') + lineList=map(string.strip,lineList) + + strainName=lineList[0] + strainNameList.append(strainName) + + line =pedFileopen.readline() + + return strainNameList + + ################################################################ + # Generate Chr list, Chr OrderId and Retrieve Length Information + ################################################################ + def getChrNameOrderIdLength(self,RISet=''): + + try: + query = """ + Select + Chr_Length.Name,Chr_Length.OrderId,Length from Chr_Length, InbredSet + where + Chr_Length.SpeciesId = InbredSet.SpeciesId AND + InbredSet.Name = '%s' + Order by OrderId + """ % (RISet) + self.cursor.execute(query) + + results =self.cursor.fetchall() + ChrList=[] + ChrLengthMbList=[] + ChrNameOrderIdDict={} + ChrOrderIdNameDict={} + + for item in results: + ChrList.append(item[0]) + ChrNameOrderIdDict[item[0]]=item[1] # key is chr name, value is orderId + ChrOrderIdNameDict[item[1]]=item[0] # key is orderId, value is chr name + ChrLengthMbList.append(item[2]) + + except: + ChrList=[] + ChrNameOrderIdDict={} + ChrLengthMbList=[] + + return ChrList,ChrNameOrderIdDict,ChrOrderIdNameDict,ChrLengthMbList diff --git a/wqflask/wqflask/marker_regression/__init__.py b/wqflask/wqflask/marker_regression/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wqflask/wqflask/marker_regression/marker_regression.py b/wqflask/wqflask/marker_regression/marker_regression.py new file mode 100755 index 00000000..ed01a3fa --- /dev/null +++ b/wqflask/wqflask/marker_regression/marker_regression.py @@ -0,0 +1,1648 @@ +from __future__ import absolute_import, print_function, division + +from base.trait import GeneralTrait +from base import data_set #import create_dataset + +from pprint import pformat as pf + +import time +import string +import math +#from math import * +#import piddle +import sys +import os +import httplib +import urllib + +from htmlgen import HTMLgen2 as HT +from utility import Plot +from wqflask.interval_analyst import GeneUtil +from base.trait import GeneralTrait +from base.data_set import create_dataset +from base.templatePage import templatePage +from utility import webqtlUtil +from base import webqtlConfig +from dbFunction import webqtlDatabaseFunction +from base.GeneralObject import GeneralObject + +import reaper +import cPickle +from utility.THCell import THCell +from utility.TDCell import TDCell + + +class MarkerRegression(object): + + #def __init__(self, start_vars): + # + # print("[mike] Now start_vars is:", pf(start_vars)) + # + # self.dataset = data_set.create_dataset(start_vars['dataset_name']) + # self.this_trait = GeneralTrait(dataset=self.dataset.name, + # name=start_vars['trait_id'], + # cellid=None) + # + # print("self.this_trait is: ", pf(self.this_trait)) + # print("self.dataset is: ", pf(self.dataset)) + + def __init__(self, start_vars): + #templatePage.__init__(self, fd) + + #if not self.openMysql(): + # return + + self.dataset = create_dataset(start_vars['dataset_name']) + + #self.initializeParameters(start_vars) + + #filename= webqtlUtil.genRandStr("Itvl_") + #ChrList,ChrNameOrderIdDict,ChrOrderIdNameDict,ChrtLengthMbList= self.getChrNameOrderIdLength(RISet=fd.RISet) + + if False: # For PLINK + + traitInfoList = string.split(string.strip(fd.identification),':') + probesetName = string.strip(traitInfoList[-1]) + plinkOutputFileName= webqtlUtil.genRandStr("%s_%s_"%(fd.RISet,probesetName)) + + # get related values from fd.allTraitData; the format of 'allTraitValueDict'is {strainName1: value=-0.2...} + fd.readData() + allTraitValueDict = fd.allTraitData + + #automatically generate pheno txt file for PLINK + self.genPhenoTxtFileForPlink(phenoFileName=plinkOutputFileName,RISetName=fd.RISet,probesetName=probesetName, valueDict=allTraitValueDict) + # os.system full path is required for input and output files; specify missing value is -9999 + plink_command = '%splink/plink --noweb --ped %splink/%s.ped --no-fid --no-parents --no-sex --no-pheno --map %splink/%s.map --pheno %s/%s.txt --pheno-name %s --missing-phenotype -9999 --out %s%s --assoc ' % (webqtlConfig.HTMLPATH, webqtlConfig.HTMLPATH, fd.RISet, webqtlConfig.HTMLPATH, fd.RISet, webqtlConfig.TMPDIR, plinkOutputFileName, probesetName, webqtlConfig.TMPDIR, plinkOutputFileName) + + os.system(plink_command) + + if fd.identification: + heading2 = HT.Paragraph('Trait ID: %s' % fd.identification) + heading2.__setattr__("class","subtitle") + self.dict['title'] = '%s: Genome Association' % fd.identification + else: + heading2 = "" + self.dict['title'] = 'Genome Association' + + if fd.traitInfo: + symbol,chromosome,MB = string.split(fd.traitInfo,'\t') + heading3 = HT.Paragraph('[ ',HT.Strong(HT.Italic('%s' % symbol,id="green")),' on Chr %s @ %s Mb ]' % (chromosome,MB)) + else: + heading3 = "" + + heading = HT.Paragraph('Trait Data Entered for %s Set' % fd.RISet) + heading.__setattr__("class","title") + + # header info part:Trait Data Entered for HLC Set & Trait ID: + headerdiv = HT.TR(HT.TD(heading, heading2,heading3, width='45%',valign='top', align='left', bgColor='#eeeeee')) + + self.ChrList=ChrList # get chr name from '1' to 'X' + self.ChrLengthMbList = ChrLengthMbList + + # build plink result dict based on chr, key is chr name, value is in list type including Snpname, bp and pvalue info + plinkResultDict={} + count,minPvalue,plinkResultDict =self.getPlinkResultDict(outputFileName=plinkOutputFileName,thresholdPvalue=self.pValue,ChrOrderIdNameDict=ChrOrderIdNameDict) + + # if can not find results which are matched with assigned p-value, system info will show up + if count >0: + + #for genome association report table + reportTable="" + # sortable table object + resultstable,tblobj,bottomInfo = self.GenReportForPLINK(ChrNameOrderIdDict=ChrNameOrderIdDict, RISet=fd.RISet,plinkResultDict=plinkResultDict,thresholdPvalue=self.pValue,chrList=self.ChrList) + + # creat object for result table for sort function + objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb') + cPickle.dump(tblobj, objfile) + objfile.close() + + sortby = ("Index", "up") + reportTable =HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID = "sortable", addIndex = "0"), Id="sortable") + + descriptionTable = HT.TableLite(border=0, cellpadding=0, cellspacing=0) + descriptionTable.append(HT.TR(HT.TD(reportTable, colspan=3))) + descriptionTable.append(HT.TR(HT.TD(HT.BR(),HT.BR()))) + descriptionTable.append(bottomInfo) + + # get each chr's length + self.ChrLengthMbList = map(lambda x: x/1000000.0, self.ChrLengthMbList) # change unit from bp to mb + self.ChrLengthMbSum = reduce(lambda x, y:x+y, self.ChrLengthMbList, 0.0)# get total length of all chrs + if self.ChrLengthMbList: + self.GraphInterval = self.ChrLengthMbSum/(len(self.ChrLengthMbList)*12) #Empirical Mb interval + else: + self.GraphInterval = 1 + + # for human data, there's no CM value + self.ChrLengthCMList = [] + self.ChrLengthCMSum = 0 + + # begin: common part with human data + intCanvas = pid.PILCanvas(size=(self.graphWidth,self.graphHeight)) + gifmap = self.plotIntMappingForPLINK(fd, intCanvas, startMb = self.startMb, endMb = self.endMb, plinkResultDict=plinkResultDict) + + intCanvas.save(os.path.join(webqtlConfig.IMGDIR, filename), format='png') + intImg=HT.Image('/image/'+filename+'.png', border=0, usemap='#WebQTLImageMap') + + TD_LR = HT.TR(HT.TD(HT.Blockquote(gifmap,intImg, HT.P()), bgColor='#eeeeee', height = 200)) + self.dict['body'] = str(headerdiv)+str(TD_LR)+str(resultstable)+str(HT.TR(HT.TD(descriptionTable))) + + else: + heading = "Genome Association" + detail = ['There is no association with marker that meets this criteria. Please provide a less stringend threshold. The minimun p-value is %s.'%minPvalue] + self.error(heading=heading,detail=detail) + return + + else: # QTLreaper result + #if not fd.genotype: + # fd.readData() + # + #fd.parentsf14regression = fd.formdata.getvalue('parentsf14regression') + #weightedRegression = fd.formdata.getvalue('applyVarianceSE') + + #if fd.parentsf14regression and fd.genotype_2: + # _genotype = fd.genotype_2 + #else: + genotype = self.dataset.group.read_genotype_file() + print("[black]:", genotype) + + _strains, _vals, _vars, N = fd.informativeStrains(_genotype.prgy, weightedRegression) + + if fd.identification: + heading2 = HT.Paragraph('Trait ID: %s' % fd.identification) + heading2.__setattr__("class","subtitle") + self.dict['title'] = '%s: Genome Association' % fd.identification + else: + heading2 = "" + self.dict['title'] = 'Genome Association' + + if fd.traitInfo: + symbol,chromosome,MB = string.split(fd.traitInfo,'\t') + heading3 = HT.Paragraph('[ ',HT.Strong(HT.Italic('%s' % symbol,id="green")),' on Chr %s @ %s Mb ]' % (chromosome,MB)) + else: + heading3 = "" + + if N < webqtlConfig.KMININFORMATIVE: + heading = "Genome Association" + detail = ['Fewer than %d strain data were entered for %s data set. No mapping attempted.' % (webqtlConfig.KMININFORMATIVE, fd.RISet)] + self.error(heading=heading,detail=detail) + return + else: + heading = HT.Paragraph('Trait Data Entered for %s Set' % fd.RISet) + heading.__setattr__("class","title") + + datadiv = HT.TD(heading, heading2,heading3, width='45%',valign='top', align='left', bgColor='#eeeeee') + resultstable,tblobj,bottomInfo = self.GenReport(ChrNameOrderIdDict,fd, _genotype, _strains, _vals, _vars) + #resultstable = self.GenReport(fd, _genotype, _strains, _vals, _vars) + + # creat object for result table for sort function + objfile = open('%s.obj' % (webqtlConfig.TMPDIR+filename), 'wb') + cPickle.dump(tblobj, objfile) + objfile.close() + + sortby = ("Index", "up") + reportTable =HT.Div(webqtlUtil.genTableObj(tblobj=tblobj, file=filename, sortby=sortby, tableID = "sortable", addIndex = "0"), Id="sortable") + + descriptionTable = HT.TableLite(border=0, cellpadding=0, cellspacing=0) + descriptionTable.append(HT.TR(HT.TD(reportTable, colspan=3))) + descriptionTable.append(HT.TR(HT.TD(HT.BR(),HT.BR()))) + descriptionTable.append(bottomInfo) + + self.traitList=_vals + + ##########################plot####################### + + ################################################################ + # Generate Chr list and Retrieve Length Information + ################################################################ + self.genotype= _genotype + self.ChrList = [("All", -1)] + + for i, indChr in enumerate(self.genotype): + self.ChrList.append((indChr.name, i)) + + self.cursor.execute(""" + Select + Length from Chr_Length, InbredSet + where + Chr_Length.SpeciesId = InbredSet.SpeciesId AND + InbredSet.Name = '%s' AND + Chr_Length.Name in (%s) + Order by + OrderId + """ % (fd.RISet, string.join(map(lambda X: "'%s'" % X[0], self.ChrList[1:]), ", "))) + + self.ChrLengthMbList = self.cursor.fetchall() + self.ChrLengthMbList = map(lambda x: x[0]/1000000.0, self.ChrLengthMbList) + self.ChrLengthMbSum = reduce(lambda x, y:x+y, self.ChrLengthMbList, 0.0) + if self.ChrLengthMbList: + self.MbGraphInterval = self.ChrLengthMbSum/(len(self.ChrLengthMbList)*12) #Empirical Mb interval + else: + self.MbGraphInterval = 1 + + self.ChrLengthCMList = [] + for i, _chr in enumerate(self.genotype): + self.ChrLengthCMList.append(_chr[-1].cM - _chr[0].cM) + self.ChrLengthCMSum = reduce(lambda x, y:x+y, self.ChrLengthCMList, 0.0)# used for calculate plot scale + + self.GraphInterval = self.MbGraphInterval #Mb + + # begin: common part with human data + intCanvas = pid.PILCanvas(size=(self.graphWidth,self.graphHeight)) + gifmap = self.plotIntMapping(fd, intCanvas, startMb = self.startMb, endMb = self.endMb, showLocusForm= "") + filename= webqtlUtil.genRandStr("Itvl_") + intCanvas.save(os.path.join(webqtlConfig.IMGDIR, filename), format='png') + intImg=HT.Image('/image/'+filename+'.png', border=0, usemap='#WebQTLImageMap') + + ################################################################ + # footnote goes here + ################################################################ + btminfo = HT.Paragraph(Id="smallsize") #Small('More information about this graph is available here.') + + if (self.additiveChecked): + btminfo.append(HT.BR(), 'A positive additive coefficient (', HT.Font('green', color='green'), ' line) indicates that %s alleles increase trait values. In contrast, a negative additive coefficient (' % fd.ppolar, HT.Font('red', color='red'), ' line) indicates that %s alleles increase trait values.' % fd.mpolar) + + + TD_LR = HT.TR(HT.TD(HT.Blockquote(gifmap,intImg, HT.P()), bgColor='#eeeeee', height = 200)) + + self.dict['body'] = str(datadiv)+str(TD_LR)+str(resultstable)+str(HT.TR(HT.TD(descriptionTable))) + + # end: common part with human data + + + + + # add by NL 10-2-2011 + def initializeParameters(self, fd): + """ + Initializes all of the MarkerRegressionPage class parameters, + acquiring most values from the formdata (fd) + """ + ################################### + # manhattam plot parameters + ################################### + + self.graphHeight = 600 + self.graphWidth = 1280 + self.plotScale = 'physic' + self.selectedChr = -1 + self.GRAPH_BACK_DARK_COLOR = pid.HexColor(0xF1F1F9) + self.GRAPH_BACK_LIGHT_COLOR = pid.HexColor(0xFBFBFF) + self.LRS_COLOR = pid.HexColor(0x0000FF) + self.LRS_LOD ='LRS' + self.lrsMax = float(fd.formdata.getvalue('lrsMax', 0)) + self.startMb = fd.formdata.getvalue('startMb', "-1") + self.endMb = fd.formdata.getvalue('endMb', "-1") + self.mappingMethodId = fd.formdata.getvalue('mappingMethodId', "0") + self.permChecked=True + self.multipleInterval=False + self.SIGNIFICANT_WIDTH = 5 + self.SUGGESTIVE_WIDTH = 5 + self.SIGNIFICANT_COLOR = pid.HexColor(0xEBC7C7) + self.SUGGESTIVE_COLOR = pid.gainsboro + self.colorCollection = [self.LRS_COLOR] + self.additiveChecked= True + self.ADDITIVE_COLOR_POSITIVE = pid.green + self.legendChecked =False + self.pValue=float(fd.formdata.getvalue('pValue',-1)) + + # allow user to input p-value greater than 1, + # in this case, the value will be treated as -lgP value. so the input value needs to be transferred to power of 10 format + if self.pValue >1: + self.pValue =10**-(self.pValue) + + try: + self.startMb = float(self.startMb) + self.endMb = float(self.endMb) + if self.startMb > self.endMb: + temp = self.startMb + self.startMb = self.endMb + self.endMb = temp + #minimal distance 10bp + if self.endMb - self.startMb < 0.00001: + self.endMb = self.startMb + 0.00001 + except: + self.startMb = self.endMb = -1 + + def GenReportForPLINK(self, ChrNameOrderIdDict={},RISet='',plinkResultDict= {},thresholdPvalue=-1,chrList=[]): + + 'Create an HTML division which reports any loci which are significantly associated with the submitted trait data.' + ######################################### + # Genome Association report + ######################################### + locusFormName = webqtlUtil.genRandStr("fm_") + locusForm = HT.Form(cgi = os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), \ + enctype='multipart/form-data', name=locusFormName, submit=HT.Input(type='hidden')) + hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':RISet+"Geno",'CellID':'_', \ + 'RISet':RISet, 'incparentsf1':'on'} + for key in hddn.keys(): + locusForm.append(HT.Input(name=key, value=hddn[key], type='hidden')) + + regressionHeading = HT.Paragraph('Genome Association Report') + regressionHeading.__setattr__("class","title") + + filename= webqtlUtil.genRandStr("GenomeAsscociation_") + fpText = open('%s.txt' % (webqtlConfig.TMPDIR+filename), 'wb') + fpText.write('The loci meet the criteria of P-Value <= %3.6f.\n'%thresholdPvalue) + pValueInfo =HT.Paragraph('The loci meet the criteria of P-Value <= %3.6f.\n'%thresholdPvalue) + + textUrl = HT.Href(text = 'Download', url= '/tmp/'+filename+'.txt', target = "_blank", Class='fs12 fwn') + bottomInfo = HT.TR(HT.TD(HT.Paragraph(textUrl, ' result in tab-delimited text format.', HT.BR(), HT.BR(),Class="fs12 fwn"), colspan=3)) + + tblobj={} # build dict for genTableObj function; keys include header and body + tblobj_header = [] # value of key 'header' + tblobj_body=[] # value of key 'body' + reportHeaderRow=[] # header row list for tblobj_header (html part) + headerList=['Index','SNP Name','Chr','Mb','-log(P)'] + headerStyle="fs14 fwb ffl b1 cw cbrb" # style of the header + cellColorStyle = "fs13 b1 fwn c222" # style of the cells + + if headerList: + for ncol, item in enumerate(headerList): + reportHeaderRow.append(THCell(HT.TD(item, Class=headerStyle, valign='bottom',nowrap='ON'),text=item, idx=ncol)) + #download file for table headers' names + fpText.write('SNP_Name\tChromosome\tMb\t-log(P)\n') + + tblobj_header.append(reportHeaderRow) + tblobj['header']=tblobj_header + + index=1 + for chr in chrList: + + if plinkResultDict.has_key(chr): + if chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[chr] + else: + chrOrderId=chr + + valueList=plinkResultDict[chr] + + for value in valueList: + reportBodyRow=[] # row list for tblobj_body (html part) + snpName=value[0] + bp=value[1] + mb=int(bp)/1000000.0 + + try: + pValue =float(value[2]) + except: + pValue =1 + formattedPvalue = -math.log10(pValue) + + formattedPvalue = webqtlUtil.SciFloat(formattedPvalue) + dbSnprs=snpName.replace('rs','') + SnpHref = HT.Href(text=snpName, url="http://www.ncbi.nlm.nih.gov/projects/SNP/snp_ref.cgi?rs=%s"%dbSnprs, target="_blank") + + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(HT.TD(SnpHref, Class=cellColorStyle,nowrap='ON'),snpName, snpName)) + reportBodyRow.append(TDCell(HT.TD(chr, Class=cellColorStyle, align="center",nowrap='ON'),chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%mb, Class=cellColorStyle, align="center",nowrap='ON'),mb, mb)) + reportBodyRow.append(TDCell(HT.TD(formattedPvalue, Class=cellColorStyle, align="center",nowrap='ON'),formattedPvalue, float(formattedPvalue))) + + fpText.write('%s\t%s\t%3.6f\t%s\n' % (snpName, str(chr), mb, formattedPvalue)) + index+=1 + + tblobj_body.append(reportBodyRow) + + tblobj['body']=tblobj_body + rv=HT.TR(HT.TD(regressionHeading,pValueInfo, locusForm, HT.P(), width='55%',valign='top', align='left',bgColor='#eeeeee')) + + return rv, tblobj,bottomInfo + + + def GenReport(self, ChrNameOrderIdDict,fd, _genotype, _strains, _vals, _vars= []): + 'Create an HTML division which reports any loci which are significantly associated with the submitted trait data.' + #calculate QTL for each trait + self.qtlresults = [] + if webqtlUtil.ListNotNull(_vars): + qtlresults = _genotype.regression(strains = _strains, trait = _vals, variance = _vars) + LRSArray = _genotype.permutation(strains = _strains, trait = _vals, variance = _vars, nperm=fd.nperm) + else: + qtlresults = _genotype.regression(strains = _strains, trait = _vals) + LRSArray = _genotype.permutation(strains = _strains, trait = _vals,nperm=fd.nperm) + + self.qtlresults.append(qtlresults) + + filename= webqtlUtil.genRandStr("GenomeAsscociation_") + + # set suggestive, significant and highly significant LRS + if fd.suggestive == None: + fd.suggestive = LRSArray[int(fd.nperm*0.37-1)] + else: + fd.suggestive = float(fd.suggestive) + if fd.significance == None: + fd.significance = LRSArray[int(fd.nperm*0.95-1)] + else: + fd.significance = float(fd.significance) + + self.significance =fd.significance + self.suggestive = fd.suggestive + self.highlysignificant = LRSArray[int(fd.nperm*0.99-1)] + _dispAllLRS = 0 + if fd.formdata.getvalue('displayAllLRS'): + _dispAllLRS = 1 + qtlresults2 = [] + if _dispAllLRS: + filtered = qtlresults[:] + else: + filtered = filter(lambda x, y=fd.suggestive: x.lrs > y, qtlresults) + if len(filtered) == 0: + qtlresults2 = qtlresults[:] + qtlresults2.sort() + filtered = qtlresults2[-10:] + + ######################################### + # Permutation Graph + ######################################### + myCanvas = pid.PILCanvas(size=(400,300)) + #plotBar(myCanvas,10,10,390,290,LRSArray,XLabel='LRS',YLabel='Frequency',title=' Histogram of Permutation Test',identification=fd.identification) + Plot.plotBar(myCanvas, LRSArray, XLabel='LRS',YLabel='Frequency',title=' Histogram of Permutation Test') + filename= webqtlUtil.genRandStr("Reg_") + myCanvas.save(webqtlConfig.IMGDIR+filename, format='gif') + img=HT.Image('/image/'+filename+'.gif',border=0,alt='Histogram of Permutation Test') + + if fd.suggestive == None: + fd.suggestive = LRSArray[int(fd.nperm*0.37-1)] + else: + fd.suggestive = float(fd.suggestive) + if fd.significance == None: + fd.significance = LRSArray[int(fd.nperm*0.95-1)] + else: + fd.significance = float(fd.significance) + + permutationHeading = HT.Paragraph('Histogram of Permutation Test') + permutationHeading.__setattr__("class","title") + + permutation = HT.TableLite() + permutation.append(HT.TR(HT.TD(img))) + + + ######################################### + # Genome Association report + ######################################### + locusFormName = webqtlUtil.genRandStr("fm_") + locusForm = HT.Form(cgi = os.path.join(webqtlConfig.CGIDIR, webqtlConfig.SCRIPTFILE), \ + enctype='multipart/form-data', name=locusFormName, submit=HT.Input(type='hidden')) + hddn = {'FormID':'showDatabase','ProbeSetID':'_','database':fd.RISet+"Geno",'CellID':'_', \ + 'RISet':fd.RISet, 'incparentsf1':'on'} + for key in hddn.keys(): + locusForm.append(HT.Input(name=key, value=hddn[key], type='hidden')) + + regressionHeading = HT.Paragraph('Genome Association Report') + regressionHeading.__setattr__("class","title") + # report is the info part above report table + if qtlresults2 != []: + report = HT.Blockquote(HT.Font('No association ',color="#FF0000"),HT.Font('with a likelihood ratio statistic greater than %3.1f was found. Here are the top 10 LRSs.' % fd.suggestive,color="#000000")) + else: + report = HT.Blockquote('The following loci in the %s data set have associations with the above trait data.\n' % fd.RISet, HT.P()) + report.__setattr__("class","normalsize") + + fpText = open('%s.txt' % (webqtlConfig.TMPDIR+filename), 'wb') + fpText.write('Suggestive LRS =%3.2f\n'%self.suggestive) + fpText.write('Significant LRS =%3.2f\n'%self.significance) + fpText.write('Highly Significant LRS =%3.2f\n'%self.highlysignificant) + LRSInfo =HT.Paragraph('    Suggestive LRS =%3.2f\n'%fd.suggestive, HT.BR(), '    Significant LRS =%3.2f\n'%fd.significance,HT.BR(),'    Highly Significant LRS =%3.2f\n' % self.highlysignificant) + + textUrl = HT.Href(text = 'Download', url= '/tmp/'+filename+'.txt', target = "_blank", Class='fs12 fwn') + + bottomInfo = HT.TR(HT.TD(HT.Paragraph(textUrl, ' result in tab-delimited text format.', HT.BR(), HT.BR(),'LRS values marked with',HT.Font(' * ',color="red"), 'are greater than the significance threshold (specified by you or by permutation test). ' , HT.BR(), HT.BR(), HT.Strong('Additive Effect'), ' is half the difference in the mean phenotype of all cases that are homozygous for one parental allel at this marker minus the mean of all cases that are homozygous for the other parental allele at this marker. ','In the case of %s strains, for example,' % fd.RISet,' A positive additive effect indicates that %s alleles increase trait values. Negative additive effect indicates that %s alleles increase trait values.'% (fd.ppolar,fd.mpolar),Class="fs12 fwn"))) + + tblobj={} # build dict for genTableObj function; keys include header and body + tblobj_header = [] # value of key 'header' + tblobj_body=[] # value of key 'body' + reportHeaderRow=[] # header row list for tblobj_header (html part) + headerStyle="fs14 fwb ffl b1 cw cbrb" # style of the header + cellColorStyle = "fs13 b1 fwn c222" # style of the cells + + headerList=['Index','LRS','Chr','Mb','Locus','Additive Effect'] + for ncol, item in enumerate(headerList): + reportHeaderRow.append(THCell(HT.TD(item, Class=headerStyle, valign='bottom',nowrap='ON'),text=item, idx=ncol)) + + if fd.genotype.type == 'intercross': + ncol =len(headerList) + reportHeaderRow.append(THCell(HT.TD('Dominance Effect', Class=headerStyle, valign='bottom',nowrap='ON'),text='Dominance Effect', idx=ncol)) + + #download file for table headers' names + fpText.write('LRS\tChromosome\tMb\tLocus\tAdditive Effect\tDominance Effect\n') + + index=1 + for ii in filtered: + #add by NL 06-20-2011: set LRS to 460 when LRS is infinite, + if ii.lrs==float('inf') or ii.lrs>webqtlConfig.MAXLRS: + LRS=webqtlConfig.MAXLRS #maximum LRS value + else: + LRS=ii.lrs + + if LRS > fd.significance: + lrs = HT.TD(HT.Font('%3.3f*' % LRS, color='#FF0000'),Class=cellColorStyle) + else: + lrs = HT.TD('%3.3f' % LRS,Class=cellColorStyle) + + if ii.locus.chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[ii.locus.chr] + else: + chrOrderId=ii.locus.chr + + reportBodyRow=[] # row list for tblobj_body (html part) + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(lrs,LRS, LRS)) + reportBodyRow.append(TDCell(HT.TD(ii.locus.chr, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%ii.locus.Mb, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.Mb, ii.locus.Mb)) + reportBodyRow.append(TDCell(HT.TD(HT.Href(text=ii.locus.name, url = "javascript:showTrait('%s','%s');" % (locusFormName, ii.locus.name), Class='normalsize'), Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.name, ii.locus.name)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.additive, Class=cellColorStyle, align="center",nowrap='ON'),ii.additive, ii.additive)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.dominance, Class=cellColorStyle, align="center",nowrap='ON'),ii.dominance, ii.dominance)) + + fpText.write('%2.3f\t%s\t%3.6f\t%s\t%2.3f\t%2.3f\n' % (LRS, ii.locus.chr, ii.locus.Mb, ii.locus.name, ii.additive, ii.dominance)) + index+=1 + tblobj_body.append(reportBodyRow) + else: + #download file for table headers' names + fpText.write('LRS\tChromosome\tMb\tLocus\tAdditive Effect\n') + + index=1 + for ii in filtered: + #add by NL 06-20-2011: set LRS to 460 when LRS is infinite, + if ii.lrs==float('inf') or ii.lrs>webqtlConfig.MAXLRS: + LRS=webqtlConfig.MAXLRS #maximum LRS value + else: + LRS=ii.lrs + + if LRS > fd.significance: + lrs = HT.TD(HT.Font('%3.3f*' % LRS, color='#FF0000'),Class=cellColorStyle) + else: + lrs = HT.TD('%3.3f' % LRS,Class=cellColorStyle) + + if ii.locus.chr in ChrNameOrderIdDict.keys(): + chrOrderId =ChrNameOrderIdDict[ii.locus.chr] + else: + chrOrderId=ii.locus.chr + + reportBodyRow=[] # row list for tblobj_body (html part) + selectCheck=HT.Input(type="checkbox", Class="checkbox", name="index",value=index, onClick="highlight(this)") + reportBodyRow.append(TDCell(HT.TD(str(index),selectCheck, align='right',Class=cellColorStyle,nowrap='ON'),str(index),index)) + reportBodyRow.append(TDCell(lrs,LRS, LRS)) + reportBodyRow.append(TDCell(HT.TD(ii.locus.chr, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.chr, chrOrderId)) + reportBodyRow.append(TDCell(HT.TD('%3.6f'%ii.locus.Mb, Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.Mb, ii.locus.Mb)) + reportBodyRow.append(TDCell(HT.TD(HT.Href(text=ii.locus.name, url = "javascript:showTrait('%s','%s');" % (locusFormName, ii.locus.name), Class='normalsize'), Class=cellColorStyle, align="center",nowrap='ON'),ii.locus.name, ii.locus.name)) + reportBodyRow.append(TDCell(HT.TD('%3.3f' % ii.additive, Class=cellColorStyle, align="center",nowrap='ON'),ii.additive, ii.additive)) + + fpText.write('%2.3f\t%s\t%3.6f\t%s\t%2.3f\n' % (LRS, ii.locus.chr, ii.locus.Mb, ii.locus.name, ii.additive)) + index+=1 + tblobj_body.append(reportBodyRow) + + tblobj_header.append(reportHeaderRow) + tblobj['header']=tblobj_header + tblobj['body']=tblobj_body + + rv=HT.TD(regressionHeading,LRSInfo,report, locusForm, HT.P(),width='55%',valign='top', align='left', bgColor='#eeeeee') + if fd.genotype.type == 'intercross': + bottomInfo.append(HT.BR(), HT.BR(), HT.Strong('Dominance Effect'),' is the difference between the mean trait value of cases heterozygous at a marker and the average mean for the two groups homozygous at this marker: e.g., BD - (BB+DD)/2]. A positive dominance effect indicates that the average phenotype of BD heterozygotes exceeds the mean of BB and DD homozygotes. No dominance deviation can be computed for a set of recombinant inbred strains or for a backcross.') + return rv,tblobj,bottomInfo + + return rv,tblobj,bottomInfo + + def plotIntMappingForPLINK(self, fd, canvas, offset= (80, 120, 20, 80), zoom = 1, startMb = None, endMb = None, showLocusForm = "",plinkResultDict={}): + #calculating margins + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + xLeftOffset = int(xLeftOffset*fontZoom) + xRightOffset = int(xRightOffset*fontZoom) + yBottomOffset = int(yBottomOffset*fontZoom) + + cWidth = canvas.size[0] + cHeight = canvas.size[1] + plotWidth = cWidth - xLeftOffset - xRightOffset + plotHeight = cHeight - yTopOffset - yBottomOffset + startPixelX = xLeftOffset + endPixelX = (xLeftOffset + plotWidth) + + #Drawing Area Height + drawAreaHeight = plotHeight + if self.plotScale == 'physic' and self.selectedChr > -1: # for single chr + drawAreaHeight -= self.ENSEMBL_BAND_HEIGHT + self.UCSC_BAND_HEIGHT+ self.WEBQTL_BAND_HEIGHT + 3*self.BAND_SPACING+ 10*zoom + if self.geneChecked: + drawAreaHeight -= self.NUM_GENE_ROWS*self.EACH_GENE_HEIGHT + 3*self.BAND_SPACING + 10*zoom + else: + if self.selectedChr > -1: + drawAreaHeight -= 20 + else:# for all chrs + drawAreaHeight -= 30 + + #Image map + gifmap = HT.Map(name='WebQTLImageMap') + + newoffset = (xLeftOffset, xRightOffset, yTopOffset, yBottomOffset) + # Draw the alternating-color background first and get plotXScale + plotXScale = self.drawGraphBackgroundForPLINK(canvas, gifmap, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb,plinkResultDict=plinkResultDict) + + # Draw X axis + self.drawXAxisForPLINK(fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + # Draw manhattam plot + self.drawManhattanPlotForPLINK(canvas, drawAreaHeight, gifmap, plotXScale, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb,plinkResultDict=plinkResultDict,thresholdPvalue=self.pValue) + + return gifmap + + + def plotIntMapping(self, fd, canvas, offset= (80, 120, 20, 80), zoom = 1, startMb = None, endMb = None, showLocusForm = ""): + #calculating margins + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + xLeftOffset = int(xLeftOffset*fontZoom) + xRightOffset = int(xRightOffset*fontZoom) + yBottomOffset = int(yBottomOffset*fontZoom) + + cWidth = canvas.size[0] + cHeight = canvas.size[1] + plotWidth = cWidth - xLeftOffset - xRightOffset + plotHeight = cHeight - yTopOffset - yBottomOffset + startPixelX = xLeftOffset + endPixelX = (xLeftOffset + plotWidth) + + #Drawing Area Height + drawAreaHeight = plotHeight + if self.plotScale == 'physic' and self.selectedChr > -1: # for single chr + drawAreaHeight -= self.ENSEMBL_BAND_HEIGHT + self.UCSC_BAND_HEIGHT+ self.WEBQTL_BAND_HEIGHT + 3*self.BAND_SPACING+ 10*zoom + if self.geneChecked: + drawAreaHeight -= self.NUM_GENE_ROWS*self.EACH_GENE_HEIGHT + 3*self.BAND_SPACING + 10*zoom + else:# for all chrs + if self.selectedChr > -1: + drawAreaHeight -= 20 + else: + drawAreaHeight -= 30 + + #Image map + gifmap = HT.Map(name='WebQTLImageMap') + + newoffset = (xLeftOffset, xRightOffset, yTopOffset, yBottomOffset) + # Draw the alternating-color background first and get plotXScale + plotXScale = self.drawGraphBackground(canvas, gifmap, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + + # Draw X axis + self.drawXAxis(fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + # Draw QTL curve + self.drawQTL(canvas, drawAreaHeight, gifmap, plotXScale, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) + + #draw legend + if self.multipleInterval: + self.drawMultiTraitName(fd, canvas, gifmap, showLocusForm, offset=newoffset) + elif self.legendChecked: + self.drawLegendPanel(fd, canvas, offset=newoffset) + else: + pass + + #draw position, no need to use a separate function + if fd.genotype.Mbmap: + self.drawProbeSetPosition(canvas, plotXScale, offset=newoffset) + + return gifmap + + + # functions for manhattam plot of markers + def drawManhattanPlotForPLINK(self, canvas, drawAreaHeight, gifmap, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None,plinkResultDict={},thresholdPvalue=-1): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + # INTERCROSS = (self.genotype.type=="intercross") + INTERCROSS ='' #?????? + + ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + GraphInterval=self.GraphInterval + pvalueHeightThresh = drawAreaHeight - 80 #ZS: Otherwise the plot gets very close to the chromosome labels + + #draw the pvalue scale + #We first determine whether or not we are using a sliding scale. + #If so, we need to compute the maximum pvalue value to determine where the max y-value should be, and call this pvalueMax. + #pvalueTop is then defined to be above the pvalueMax by enough to add one additional pvalueScale increment. + #if we are using a set-scale, then we set pvalueTop to be the user's value, and pvalueMax doesn't matter. + + # for human data we use p value instead of lrs + pValueList=[] + for key in plinkResultDict: + valueList = plinkResultDict[key] + for item in valueList: + pValue = item[-1] + pValueList.append(pValue) + + formattedPValueList=[] + for pValue in pValueList: + try: + pValue=float(pValue) + except: + pValue =1 + formattedpValue = -math.log10(pValue) + formattedPValueList.append(formattedpValue) + + #sliding scale + pvalueMax = max(formattedPValueList) + #pvalueMax =pvalueMax +1 + # no permutation result for plink func: GenReport() + pvalueMin = int(-math.log10(thresholdPvalue)) + + if pvalueMax> 100: + pvalueScale = 20.0 + elif pvalueMax > 20: + pvalueScale = 5.0 + elif pvalueMax > 7.5: + pvalueScale = 2.5 + else: + pvalueScale = 1.0 + + # the base line for x-axis is -log(thresholdPvalue) + pvalueAxisList = Plot.frange(pvalueMin, pvalueMax, pvalueScale) + #make sure the user's value appears on the y-axis + #ZS: There is no way to do this without making the position of the points not directly proportional to a given distance on the y-axis + #tempPvalueMax=round(pvalueMax) + tempPvalueMax = pvalueAxisList[len(pvalueAxisList)-1] + pvalueScale + pvalueAxisList.append(tempPvalueMax) + + #ZS: I don't understand this; the if statement will be true for any number that isn't exactly X.5. + #if abs(tempPvalueMax-pvalueMax) <0.5: + # tempPvalueMax=tempPvalueMax+1 + # pvalueAxisList.append(tempPvalueMax) + + #draw the "pvalue" string to the left of the axis + pvalueScaleFont=pid.Font(ttf="verdana", size=14*fontZoom, bold=0) + pvalueLODFont=pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + yZero = yTopOffset + plotHeight + + #yAxis label display area + yAxis_label ='-log(P)' + canvas.drawString(yAxis_label, xLeftOffset - canvas.stringWidth("999.99", font=pvalueScaleFont) - 10*zoom, \ + yZero - 150, font=pvalueLODFont, color=pid.black, angle=90) + + for i,item in enumerate(pvalueAxisList): + ypvalue = yZero - (float(i)/float(len(pvalueAxisList) - 1)) * pvalueHeightThresh + canvas.drawLine(xLeftOffset, ypvalue, xLeftOffset - 4, ypvalue, color=self.LRS_COLOR, width=1*zoom) + scaleStr = "%2.1f" % item + #added by NL 6-24-2011:Y-axis scale display + canvas.drawString(scaleStr, xLeftOffset-4-canvas.stringWidth(scaleStr, font=pvalueScaleFont)-5, ypvalue+3, font=pvalueScaleFont, color=self.LRS_COLOR) + + ChrList=self.ChrList + startPosX = xLeftOffset + + for i, chr in enumerate(ChrList): + + if plinkResultDict.has_key(chr): + plinkresultList = plinkResultDict[chr] + + m = 0 + #add by NL 06-24-2011: for mahanttam plot + symbolFont = pid.Font(ttf="fnt_bs", size=5,bold=0) + # color for point in each chr + chrCount=len(ChrList) + chrColorDict =self.getColorForMarker(chrCount=chrCount,flag=1) + for j, item in enumerate(plinkresultList): + try : + mb=float(item[1])/1000000.0 + except: + mb=0 + + try : + pvalue =float(item[-1]) + except: + pvalue =1 + + try: + snpName = item[0] + except: + snpName='' + + formattedPvalue = -math.log10(pvalue) + + Xc = startPosX + (mb-startMb)*plotXScale + Yc = yZero - (formattedPvalue-pvalueMin)*pvalueHeightThresh/(tempPvalueMax - pvalueMin) + canvas.drawString("5", Xc-canvas.stringWidth("5",font=symbolFont)/2+1,Yc+2,color=chrColorDict[i], font=symbolFont) + m += 1 + + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset, yTopOffset, color=self.LRS_COLOR, width=1*zoom) #the blue line running up the y axis + + def drawQTL(self, canvas, drawAreaHeight, gifmap, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + INTERCROSS = (self.genotype.type=="intercross") + + ChrLengthDistList = self.ChrLengthMbList + GraphInterval=self.GraphInterval + LRSHeightThresh = drawAreaHeight + AdditiveHeightThresh = drawAreaHeight/2 + DominanceHeightThresh = drawAreaHeight/2 + + #draw the LRS scale + #We first determine whether or not we are using a sliding scale. + #If so, we need to compute the maximum LRS value to determine where the max y-value should be, and call this LRSMax. + #LRSTop is then defined to be above the LRSMax by enough to add one additional LRSScale increment. + #if we are using a set-scale, then we set LRSTop to be the user's value, and LRSMax doesn't matter. + + if self.LRS_LOD == 'LOD': + lodm = self.LODFACTOR + else: + lodm = 1.0 + + if self.lrsMax <= 0: #sliding scale + LRSMax = max(map(max, self.qtlresults)).lrs + #genotype trait will give infinite LRS + LRSMax = min(LRSMax, webqtlConfig.MAXLRS) + LRSMax = max(self.significance, LRSMax) + else: + LRSMax = self.lrsMax*lodm + + if LRSMax/lodm > 100: + LRSScale = 20.0 + elif LRSMax/lodm > 20: + LRSScale = 5.0 + elif LRSMax/lodm > 7.5: + LRSScale = 2.5 + else: + LRSScale = 1.0 + + LRSAxisList = Plot.frange(LRSScale, LRSMax/lodm, LRSScale) + #make sure the user's value appears on the y-axis + #update by NL 6-21-2011: round the LOD value to 100 when LRSMax is equal to 460 + LRSAxisList.append(round(LRSMax/lodm)) + + #draw the "LRS" or "LOD" string to the left of the axis + LRSScaleFont=pid.Font(ttf="verdana", size=14*fontZoom, bold=0) + LRSLODFont=pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + yZero = yTopOffset + plotHeight + + #yAxis label display area + canvas.drawString(self.LRS_LOD, xLeftOffset - canvas.stringWidth("999.99", font=LRSScaleFont) - 10*zoom, \ + yZero - 150, font=LRSLODFont, color=pid.black, angle=90) + + for item in LRSAxisList: + yLRS = yZero - (item*lodm/LRSMax) * LRSHeightThresh + canvas.drawLine(xLeftOffset, yLRS, xLeftOffset - 4, yLRS, color=self.LRS_COLOR, width=1*zoom) + scaleStr = "%2.1f" % item + #added by NL 6-24-2011:Y-axis scale display + canvas.drawString(scaleStr, xLeftOffset-4-canvas.stringWidth(scaleStr, font=LRSScaleFont)-5, yLRS+3, font=LRSScaleFont, color=self.LRS_COLOR) + + + #"Significant" and "Suggestive" Drawing Routine + # ======= Draw the thick lines for "Significant" and "Suggestive" ===== (crowell: I tried to make the SNPs draw over these lines, but piddle wouldn't have it...) + if self.permChecked and not self.multipleInterval: + significantY = yZero - self.significance*LRSHeightThresh/LRSMax + suggestiveY = yZero - self.suggestive*LRSHeightThresh/LRSMax + + + startPosX = xLeftOffset + for i, _chr in enumerate(self.genotype): + rightEdge = int(startPosX + self.ChrLengthDistList[i]*plotXScale - self.SUGGESTIVE_WIDTH/1.5) + #added by NL 6-24-2011:draw suggestive line (grey one) + canvas.drawLine(startPosX+self.SUGGESTIVE_WIDTH/1.5, suggestiveY, rightEdge, suggestiveY, color=self.SUGGESTIVE_COLOR, + width=self.SUGGESTIVE_WIDTH*zoom, clipX=(xLeftOffset, xLeftOffset + plotWidth-2)) + #added by NL 6-24-2011:draw significant line (pink one) + canvas.drawLine(startPosX+self.SUGGESTIVE_WIDTH/1.5, significantY, rightEdge, significantY, color=self.SIGNIFICANT_COLOR, + width=self.SIGNIFICANT_WIDTH*zoom, clipX=(xLeftOffset, xLeftOffset + plotWidth-2)) + sugg_coords = "%d, %d, %d, %d" % (startPosX, suggestiveY-2, rightEdge + 2*zoom, suggestiveY+2) + sig_coords = "%d, %d, %d, %d" % (startPosX, significantY-2, rightEdge + 2*zoom, significantY+2) + if self.LRS_LOD == 'LRS': + sugg_title = "Suggestive LRS = %0.2f" % self.suggestive + sig_title = "Significant LRS = %0.2f" % self.significance + else: + sugg_title = "Suggestive LOD = %0.2f" % (self.suggestive/4.61) + sig_title = "Significant LOD = %0.2f" % (self.significance/4.61) + Areas1 = HT.Area(shape='rect',coords=sugg_coords,title=sugg_title) + Areas2 = HT.Area(shape='rect',coords=sig_coords,title=sig_title) + gifmap.areas.append(Areas1) + gifmap.areas.append(Areas2) + + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + + if self.multipleInterval: + lrsEdgeWidth = 1 + else: + additiveMax = max(map(lambda X : abs(X.additive), self.qtlresults[0])) + if INTERCROSS: + dominanceMax = max(map(lambda X : abs(X.dominance), self.qtlresults[0])) + else: + dominanceMax = -1 + lrsEdgeWidth = 2 + for i, qtlresult in enumerate(self.qtlresults): + m = 0 + startPosX = xLeftOffset + thisLRSColor = self.colorCollection[i] + + #add by NL 06-24-2011: for mahanttam plot + symbolFont = pid.Font(ttf="fnt_bs", size=5,bold=0) + + for j, _chr in enumerate(self.genotype): + chrCount=len(self.genotype) + chrColorDict =self.getColorForMarker(chrCount=chrCount,flag=1) + LRSCoordXY = [] + AdditiveCoordXY = [] + DominanceCoordXY = [] + for k, _locus in enumerate(_chr): + if self.plotScale == 'physic': + Xc = startPosX + (_locus.Mb-startMb)*plotXScale + else: + Xc = startPosX + (_locus.cM-_chr[0].cM)*plotXScale + # updated by NL 06-18-2011: + # fix the over limit LRS graph issue since genotype trait may give infinite LRS; + # for any lrs is over than 460(LRS max in this system), it will be reset to 460 + if qtlresult[m].lrs> 460 or qtlresult[m].lrs=='inf': + Yc = yZero - webqtlConfig.MAXLRS*LRSHeightThresh/LRSMax + else: + Yc = yZero - qtlresult[m].lrs*LRSHeightThresh/LRSMax + + LRSCoordXY.append((Xc, Yc)) + #add by NL 06-24-2011: for mahanttam plot + #self.significance/4.61 consider chr and LOD + # significantY = yZero - self.significance*LRSHeightThresh/LRSMax + # if Yc >significantY: + # canvas.drawString(":", Xc-canvas.stringWidth(":",font=symbolFont)/2+1,Yc+2,color=pid.black, font=symbolFont) + # else: + # canvas.drawString(":", Xc-canvas.stringWidth(":",font=symbolFont)/2+1,Yc+2,color=pid.black, font=symbolFont) + + # add by NL 06-27-2011: eliminate imputed value when locus name is equal to '-' + if (qtlresult[m].locus.name) and (qtlresult[m].locus.name!=' - '): + canvas.drawString("5", Xc-canvas.stringWidth("5",font=symbolFont)/2+1,Yc+2,color=chrColorDict[j], font=symbolFont) + + if not self.multipleInterval and self.additiveChecked: + Yc = yZero - qtlresult[m].additive*AdditiveHeightThresh/additiveMax + AdditiveCoordXY.append((Xc, Yc)) + if not self.multipleInterval and INTERCROSS and self.additiveChecked: + Yc = yZero - qtlresult[m].dominance*DominanceHeightThresh/dominanceMax + DominanceCoordXY.append((Xc, Yc)) + m += 1 + + startPosX += (ChrLengthDistList[j]+GraphInterval)*plotXScale + + + ###draw additive scale + if not self.multipleInterval and self.additiveChecked: + additiveScaleFont=pid.Font(ttf="verdana",size=12*fontZoom,bold=0) + additiveScale = Plot.detScaleOld(0,additiveMax) + additiveStep = (additiveScale[1]-additiveScale[0])/additiveScale[2] + additiveAxisList = Plot.frange(0, additiveScale[1], additiveStep) + maxAdd = additiveScale[1] + addPlotScale = AdditiveHeightThresh/additiveMax + + additiveAxisList.append(additiveScale[1]) + for item in additiveAxisList: + additiveY = yZero - item*addPlotScale + canvas.drawLine(xLeftOffset + plotWidth,additiveY,xLeftOffset+4+ plotWidth,additiveY,color=self.ADDITIVE_COLOR_POSITIVE, width=1*zoom) + scaleStr = "%2.3f" % item + canvas.drawString(scaleStr,xLeftOffset + plotWidth +6,additiveY+5,font=additiveScaleFont,color=self.ADDITIVE_COLOR_POSITIVE) + + canvas.drawLine(xLeftOffset+plotWidth,additiveY,xLeftOffset+plotWidth,yZero,color=self.ADDITIVE_COLOR_POSITIVE, width=1*zoom) + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset, yTopOffset, color=self.LRS_COLOR, width=1*zoom) #the blue line running up the y axis + + def drawGraphBackgroundForPLINK(self, canvas, gifmap, offset= (80, 120, 80, 50), zoom = 1, startMb = None, endMb = None,plinkResultDict={} ): + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #calculate plot scale + #XZ: all of these global variables should be passed from function signiture + ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + GraphInterval=self.GraphInterval + ChrList =self.ChrList + + #multiple chromosome view + plotXScale = plotWidth / ((len(ChrList)-1)*GraphInterval + drawRegionDistance) + + startPosX = xLeftOffset + chrLabelFont=pid.Font(ttf="verdana",size=24*fontZoom,bold=0) + + for i, _chr in enumerate(ChrList): + + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + # NL:resize chr width for drawing + if float(ChrLengthDistList[i])<90: + ChrLengthDistList[i]=90 + #draw the shaded boxes and the sig/sug thick lines + canvas.drawRect(startPosX, yTopOffset, startPosX + ChrLengthDistList[i]*plotXScale, \ + yTopOffset+plotHeight, edgeColor=pid.gainsboro,fillColor=theBackColor) + + chrNameWidth = canvas.stringWidth(_chr, font=chrLabelFont) + chrStartPix = startPosX + (ChrLengthDistList[i]*plotXScale -chrNameWidth)/2 + chrEndPix = startPosX + (ChrLengthDistList[i]*plotXScale +chrNameWidth)/2 + + canvas.drawString(_chr, chrStartPix, yTopOffset +20,font = chrLabelFont,color=pid.dimgray) + COORDS = "%d,%d,%d,%d" %(chrStartPix, yTopOffset, chrEndPix,yTopOffset +20) + + #add by NL 09-03-2010 + HREF = "javascript:changeView(%d,%s);" % (i,ChrLengthDistList) + Areas = HT.Area(shape='rect',coords=COORDS,href=HREF) + gifmap.areas.append(Areas) + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + return plotXScale + + + def drawGraphBackground(self, canvas, gifmap, offset= (80, 120, 80, 50), zoom = 1, startMb = None, endMb = None): + ##conditions + ##multiple Chromosome view + ##single Chromosome Physical + ##single Chromosome Genetic + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #calculate plot scale + if self.plotScale != 'physic': + self.ChrLengthDistList = self.ChrLengthCMList + drawRegionDistance = self.ChrLengthCMSum + else: + self.ChrLengthDistList = self.ChrLengthMbList + drawRegionDistance = self.ChrLengthMbSum + + if self.selectedChr > -1: #single chromosome view + spacingAmt = plotWidth/13.5 + i = 0 + for startPix in Plot.frange(xLeftOffset, xLeftOffset+plotWidth, spacingAmt): + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + i += 1 + canvas.drawRect(startPix, yTopOffset, min(startPix+spacingAmt, xLeftOffset+plotWidth), \ + yTopOffset+plotHeight, edgeColor=theBackColor, fillColor=theBackColor) + + drawRegionDistance = self.ChrLengthDistList[self.selectedChr] + self.ChrLengthDistList = [drawRegionDistance] + if self.plotScale == 'physic': + plotXScale = plotWidth / (endMb-startMb) + else: + plotXScale = plotWidth / drawRegionDistance + + else: #multiple chromosome view + plotXScale = plotWidth / ((len(self.genotype)-1)*self.GraphInterval + drawRegionDistance) + + startPosX = xLeftOffset + chrLabelFont=pid.Font(ttf="verdana",size=24*fontZoom,bold=0) + + for i, _chr in enumerate(self.genotype): + + if (i % 2 == 0): + theBackColor = self.GRAPH_BACK_DARK_COLOR + else: + theBackColor = self.GRAPH_BACK_LIGHT_COLOR + + #draw the shaded boxes and the sig/sug thick lines + canvas.drawRect(startPosX, yTopOffset, startPosX + self.ChrLengthDistList[i]*plotXScale, \ + yTopOffset+plotHeight, edgeColor=pid.gainsboro,fillColor=theBackColor) + + chrNameWidth = canvas.stringWidth(_chr.name, font=chrLabelFont) + chrStartPix = startPosX + (self.ChrLengthDistList[i]*plotXScale -chrNameWidth)/2 + chrEndPix = startPosX + (self.ChrLengthDistList[i]*plotXScale +chrNameWidth)/2 + + canvas.drawString(_chr.name, chrStartPix, yTopOffset +20,font = chrLabelFont,color=pid.dimgray) + COORDS = "%d,%d,%d,%d" %(chrStartPix, yTopOffset, chrEndPix,yTopOffset +20) + + #add by NL 09-03-2010 + HREF = "javascript:changeView(%d,%s);" % (i,self.ChrLengthMbList) + Areas = HT.Area(shape='rect',coords=COORDS,href=HREF) + gifmap.areas.append(Areas) + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + return plotXScale + + # XZ: The only difference of function drawXAxisForPLINK and function drawXAxis are the function name and the self.plotScale condition. + def drawXAxisForPLINK(self, fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #Parameters + ChrLengthDistList = self.ChrLengthMbList + GraphInterval=self.GraphInterval + + NUM_MINOR_TICKS = 5 # Number of minor ticks between major ticks + X_MAJOR_TICK_THICKNESS = 2 + X_MINOR_TICK_THICKNESS = 1 + X_AXIS_THICKNESS = 1*zoom + + # ======= Alex: Draw the X-axis labels (megabase location) + MBLabelFont = pid.Font(ttf="verdana", size=12*fontZoom, bold=0) + xMajorTickHeight = 15 # How high the tick extends below the axis + xMinorTickHeight = 5*zoom + xAxisTickMarkColor = pid.black + xAxisLabelColor = pid.black + fontHeight = 12*fontZoom # How tall the font that we're using is + spacingFromLabelToAxis = 20 + spacingFromLineToLabel = 3 + + if self.plotScale == 'physic': + strYLoc = yZero + spacingFromLabelToAxis + canvas.fontHeight(MBLabelFont) + ###Physical single chromosome view + if self.selectedChr > -1: + graphMbWidth = endMb - startMb + XScale = Plot.detScale(startMb, endMb) + XStart, XEnd, XStep = XScale + if XStep < 8: + XStep *= 2 + spacingAmtX = spacingAmt = (XEnd-XStart)/XStep + + j = 0 + while abs(spacingAmtX -int(spacingAmtX)) >= spacingAmtX/100.0 and j < 6: + j += 1 + spacingAmtX *= 10 + + formatStr = '%%2.%df' % j + + for counter, _Mb in enumerate(Plot.frange(XStart, XEnd, spacingAmt / NUM_MINOR_TICKS)): + if _Mb < startMb or _Mb > endMb: + continue + Xc = xLeftOffset + plotXScale*(_Mb - startMb) + if counter % NUM_MINOR_TICKS == 0: # Draw a MAJOR mark, not just a minor tick mark + canvas.drawLine(Xc, yZero, Xc, yZero+xMajorTickHeight, color=xAxisTickMarkColor, width=X_MAJOR_TICK_THICKNESS) # Draw the MAJOR tick mark + labelStr = str(formatStr % _Mb) # What Mbase location to put on the label + strWidth = canvas.stringWidth(labelStr, font=MBLabelFont) + drawStringXc = (Xc - (strWidth / 2.0)) + canvas.drawString(labelStr, drawStringXc, strYLoc, font=MBLabelFont, color=xAxisLabelColor, angle=0) + else: + canvas.drawLine(Xc, yZero, Xc, yZero+xMinorTickHeight, color=xAxisTickMarkColor, width=X_MINOR_TICK_THICKNESS) # Draw the MINOR tick mark + # end else + + ###Physical genome wide view + else: + distScale = 0 + startPosX = xLeftOffset + for i, distLen in enumerate(ChrLengthDistList): + if distScale == 0: #universal scale in whole genome mapping + if distLen > 75: + distScale = 25 + elif distLen > 30: + distScale = 10 + else: + distScale = 5 + for tickdists in range(distScale, ceil(distLen), distScale): + canvas.drawLine(startPosX + tickdists*plotXScale, yZero, startPosX + tickdists*plotXScale, yZero + 7, color=pid.black, width=1*zoom) + canvas.drawString(str(tickdists), startPosX+tickdists*plotXScale, yZero + 10*zoom, color=pid.black, font=MBLabelFont, angle=270) + startPosX += (ChrLengthDistList[i]+GraphInterval)*plotXScale + + megabaseLabelFont = pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + canvas.drawString("Megabases", xLeftOffset + (plotWidth -canvas.stringWidth("Megabases", font=megabaseLabelFont))/2, + strYLoc + canvas.fontHeight(MBLabelFont) + 5*zoom, font=megabaseLabelFont, color=pid.black) + pass + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset+plotWidth, yZero, color=pid.black, width=X_AXIS_THICKNESS) # Draw the X axis itself + + def drawXAxis(self, fd, canvas, drawAreaHeight, gifmap, plotXScale, showLocusForm, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + #Parameters + NUM_MINOR_TICKS = 5 # Number of minor ticks between major ticks + X_MAJOR_TICK_THICKNESS = 2 + X_MINOR_TICK_THICKNESS = 1 + X_AXIS_THICKNESS = 1*zoom + + # ======= Alex: Draw the X-axis labels (megabase location) + MBLabelFont = pid.Font(ttf="verdana", size=12*fontZoom, bold=0) + xMajorTickHeight = 15 # How high the tick extends below the axis + xMinorTickHeight = 5*zoom + xAxisTickMarkColor = pid.black + xAxisLabelColor = pid.black + fontHeight = 12*fontZoom # How tall the font that we're using is + spacingFromLabelToAxis = 20 + spacingFromLineToLabel = 3 + + if self.plotScale == 'physic': + strYLoc = yZero + spacingFromLabelToAxis + canvas.fontHeight(MBLabelFont) + ###Physical single chromosome view + if self.selectedChr > -1: + graphMbWidth = endMb - startMb + XScale = Plot.detScale(startMb, endMb) + XStart, XEnd, XStep = XScale + if XStep < 8: + XStep *= 2 + spacingAmtX = spacingAmt = (XEnd-XStart)/XStep + + j = 0 + while abs(spacingAmtX -int(spacingAmtX)) >= spacingAmtX/100.0 and j < 6: + j += 1 + spacingAmtX *= 10 + + formatStr = '%%2.%df' % j + + for counter, _Mb in enumerate(Plot.frange(XStart, XEnd, spacingAmt / NUM_MINOR_TICKS)): + if _Mb < startMb or _Mb > endMb: + continue + Xc = xLeftOffset + plotXScale*(_Mb - startMb) + if counter % NUM_MINOR_TICKS == 0: # Draw a MAJOR mark, not just a minor tick mark + canvas.drawLine(Xc, yZero, Xc, yZero+xMajorTickHeight, color=xAxisTickMarkColor, width=X_MAJOR_TICK_THICKNESS) # Draw the MAJOR tick mark + labelStr = str(formatStr % _Mb) # What Mbase location to put on the label + strWidth = canvas.stringWidth(labelStr, font=MBLabelFont) + drawStringXc = (Xc - (strWidth / 2.0)) + canvas.drawString(labelStr, drawStringXc, strYLoc, font=MBLabelFont, color=xAxisLabelColor, angle=0) + else: + canvas.drawLine(Xc, yZero, Xc, yZero+xMinorTickHeight, color=xAxisTickMarkColor, width=X_MINOR_TICK_THICKNESS) # Draw the MINOR tick mark + # end else + + ###Physical genome wide view + else: + distScale = 0 + startPosX = xLeftOffset + for i, distLen in enumerate(self.ChrLengthDistList): + if distScale == 0: #universal scale in whole genome mapping + if distLen > 75: + distScale = 25 + elif distLen > 30: + distScale = 10 + else: + distScale = 5 + for tickdists in range(distScale, ceil(distLen), distScale): + canvas.drawLine(startPosX + tickdists*plotXScale, yZero, startPosX + tickdists*plotXScale, yZero + 7, color=pid.black, width=1*zoom) + canvas.drawString(str(tickdists), startPosX+tickdists*plotXScale, yZero + 10*zoom, color=pid.black, font=MBLabelFont, angle=270) + startPosX += (self.ChrLengthDistList[i]+self.GraphInterval)*plotXScale + + megabaseLabelFont = pid.Font(ttf="verdana", size=14*zoom*1.5, bold=0) + canvas.drawString("Megabases", xLeftOffset + (plotWidth -canvas.stringWidth("Megabases", font=megabaseLabelFont))/2, + strYLoc + canvas.fontHeight(MBLabelFont) + 5*zoom, font=megabaseLabelFont, color=pid.black) + pass + else: + ChrAInfo = [] + preLpos = -1 + distinctCount = 0.0 + if len(self.genotype) > 1: + for i, _chr in enumerate(self.genotype): + thisChr = [] + Locus0CM = _chr[0].cM + nLoci = len(_chr) + if nLoci <= 8: + for _locus in _chr: + if _locus.name != ' - ': + if _locus.cM != preLpos: + distinctCount += 1 + preLpos = _locus.cM + thisChr.append([_locus.name, _locus.cM-Locus0CM]) + else: + for j in (0, nLoci/4, nLoci/2, nLoci*3/4, -1): + while _chr[j].name == ' - ': + j += 1 + if _chr[j].cM != preLpos: + distinctCount += 1 + preLpos = _chr[j].cM + thisChr.append([_chr[j].name, _chr[j].cM-Locus0CM]) + ChrAInfo.append(thisChr) + else: + for i, _chr in enumerate(self.genotype): + thisChr = [] + Locus0CM = _chr[0].cM + for _locus in _chr: + if _locus.name != ' - ': + if _locus.cM != preLpos: + distinctCount += 1 + preLpos = _locus.cM + thisChr.append([_locus.name, _locus.cM-Locus0CM]) + ChrAInfo.append(thisChr) + + stepA = (plotWidth+0.0)/distinctCount + + LRectWidth = 10 + LRectHeight = 3 + offsetA = -stepA + lineColor = pid.lightblue + startPosX = xLeftOffset + for j, ChrInfo in enumerate(ChrAInfo): + preLpos = -1 + for i, item in enumerate(ChrInfo): + Lname,Lpos = item + if Lpos != preLpos: + offsetA += stepA + differ = 1 + else: + differ = 0 + preLpos = Lpos + Lpos *= plotXScale + if self.selectedChr > -1: + Zorder = i % 5 + else: + Zorder = 0 + if differ: + canvas.drawLine(startPosX+Lpos,yZero,xLeftOffset+offsetA,\ + yZero+25, color=lineColor) + canvas.drawLine(xLeftOffset+offsetA,yZero+25,xLeftOffset+offsetA,\ + yZero+40+Zorder*(LRectWidth+3),color=lineColor) + rectColor = pid.orange + else: + canvas.drawLine(xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3)-3,\ + xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3),color=lineColor) + rectColor = pid.deeppink + canvas.drawRect(xLeftOffset+offsetA, yZero+40+Zorder*(LRectWidth+3),\ + xLeftOffset+offsetA-LRectHeight,yZero+40+Zorder*(LRectWidth+3)+LRectWidth,\ + edgeColor=rectColor,fillColor=rectColor,edgeWidth = 0) + COORDS="%d,%d,%d,%d"%(xLeftOffset+offsetA-LRectHeight, yZero+40+Zorder*(LRectWidth+3),\ + xLeftOffset+offsetA,yZero+40+Zorder*(LRectWidth+3)+LRectWidth) + HREF="javascript:showDatabase3('%s','%s','%s','');" % (showLocusForm,fd.RISet+"Geno", Lname) + Areas=HT.Area(shape='rect',coords=COORDS,href=HREF, title="Locus : " + Lname) + gifmap.areas.append(Areas) + ##piddle bug + if j == 0: + canvas.drawLine(startPosX,yZero,startPosX,yZero+40, color=lineColor) + startPosX += (self.ChrLengthDistList[j]+self.GraphInterval)*plotXScale + + canvas.drawLine(xLeftOffset, yZero, xLeftOffset+plotWidth, yZero, color=pid.black, width=X_AXIS_THICKNESS) # Draw the X axis itself + + def getColorForMarker(self, chrCount,flag):# no change is needed + chrColorDict={} + for i in range(chrCount): + if flag==1: # display blue and lightblue intercross + chrColorDict[i]=pid.black + elif flag==0: + if (i%2==0): + chrColorDict[i]=pid.blue + else: + chrColorDict[i]=pid.lightblue + else:#display different color for different chr + if i in [0,8,16]: + chrColorDict[i]=pid.black + elif i in [1,9,17]: + chrColorDict[i]=pid.red + elif i in [2,10,18]: + chrColorDict[i]=pid.lightgreen + elif i in [3,11,19]: + chrColorDict[i]=pid.blue + elif i in [4,12]: + chrColorDict[i]=pid.lightblue + elif i in [5,13]: + chrColorDict[i]=pid.hotpink + elif i in [6,14]: + chrColorDict[i]=pid.gold + elif i in [7,15]: + chrColorDict[i]=pid.grey + + return chrColorDict + + + def drawProbeSetPosition(self, canvas, plotXScale, offset= (40, 120, 80, 10), zoom = 1, startMb = None, endMb = None): + if len(self.traitList) != 1: + return + + xLeftOffset, xRightOffset, yTopOffset, yBottomOffset = offset + plotWidth = canvas.size[0] - xLeftOffset - xRightOffset + plotHeight = canvas.size[1] - yTopOffset - yBottomOffset + yZero = canvas.size[1] - yBottomOffset + fontZoom = zoom + if zoom == 2: + fontZoom = 1.5 + + try: + Chr = self.traitList[0].chr # self.traitListChr =self.traitList[0].chr=_vals need to change to chrList and mbList + Mb = self.traitList[0].mb # self.traitListMb =self.traitList[0].mb=_vals + except: + return + + if self.plotScale == 'physic': + if self.selectedChr > -1: + if self.genotype[0].name != Chr or Mb < self.startMb or Mb > self.endMb: + return + else: + locPixel = xLeftOffset + (Mb-self.startMb)*plotXScale + else: + locPixel = xLeftOffset + for i, _chr in enumerate(self.genotype): + if _chr.name != Chr: + locPixel += (self.ChrLengthDistList[i] + self.GraphInterval)*plotXScale + else: + locPixel += Mb*plotXScale + break + else: + if self.selectedChr > -1: + if self.genotype[0].name != Chr: + return + else: + for i, _locus in enumerate(self.genotype[0]): + #the trait's position is on the left of the first genotype + if i==0 and _locus.Mb >= Mb: + locPixel=-1 + break + + #the trait's position is between two traits + if i > 0 and self.genotype[0][i-1].Mb < Mb and _locus.Mb >= Mb: + locPixel = xLeftOffset + plotXScale*(self.genotype[0][i-1].cM+(_locus.cM-self.genotype[0][i-1].cM)*(Mb -self.genotype[0][i-1].Mb)/(_locus.Mb-self.genotype[0][i-1].Mb)) + break + + #the trait's position is on the right of the last genotype + if i==len(self.genotype[0]) and Mb>=_locus.Mb: + locPixel = -1 + else: + locPixel = xLeftOffset + for i, _chr in enumerate(self.genotype): + if _chr.name != Chr: + locPixel += (self.ChrLengthDistList[i] + self.GraphInterval)*plotXScale + else: + locPixel += (Mb*(_chr[-1].cM-_chr[0].cM)/self.ChrLengthCMList[i])*plotXScale + break + if locPixel >= 0: + traitPixel = ((locPixel, yZero), (locPixel-6, yZero+12), (locPixel+6, yZero+12)) + canvas.drawPolygon(traitPixel, edgeColor=pid.black, fillColor=self.TRANSCRIPT_LOCATION_COLOR, closed=1) + + if self.legendChecked: + startPosY = 15 + nCol = 2 + smallLabelFont = pid.Font(ttf="trebuc", size=12, bold=1) + leftOffset = xLeftOffset+(nCol-1)*200 + canvas.drawPolygon(((leftOffset+6, startPosY-6), (leftOffset, startPosY+6), (leftOffset+12, startPosY+6)), edgeColor=pid.black, fillColor=self.TRANSCRIPT_LOCATION_COLOR, closed=1) + canvas.drawString("Sequence Site", (leftOffset+15), (startPosY+5), smallLabelFont, self.TOP_RIGHT_INFO_COLOR) + + # build dict based on plink result, key is chr, value is list of [snp,BP,pValue] + def getPlinkResultDict(self,outputFileName='',thresholdPvalue=-1,ChrOrderIdNameDict={}): + + ChrList =self.ChrList + plinkResultDict={} + + plinkResultfp = open("%s%s.qassoc"% (webqtlConfig.TMPDIR, outputFileName), "rb") + + headerLine=plinkResultfp.readline()# read header line + line = plinkResultfp.readline() + + valueList=[] # initialize value list, this list will include snp, bp and pvalue info + pValueList=[] + count=0 + + while line: + #convert line from str to list + lineList=self.buildLineList(line=line) + + # only keep the records whose chromosome name is in db + if ChrOrderIdNameDict.has_key(int(lineList[0])) and lineList[-1] and lineList[-1].strip()!='NA': + + chrName=ChrOrderIdNameDict[int(lineList[0])] + snp = lineList[1] + BP = lineList[2] + pValue = float(lineList[-1]) + pValueList.append(pValue) + + if plinkResultDict.has_key(chrName): + valueList=plinkResultDict[chrName] + + # pvalue range is [0,1] + if thresholdPvalue >=0 and thresholdPvalue<=1: + if pValue < thresholdPvalue: + valueList.append((snp,BP,pValue)) + count+=1 + + plinkResultDict[chrName]=valueList + valueList=[] + else: + if thresholdPvalue>=0 and thresholdPvalue<=1: + if pValue < thresholdPvalue: + valueList.append((snp,BP,pValue)) + count+=1 + + if valueList: + plinkResultDict[chrName]=valueList + + valueList=[] + + + line =plinkResultfp.readline() + else: + line=plinkResultfp.readline() + + if pValueList: + minPvalue= min(pValueList) + else: + minPvalue=0 + + return count,minPvalue,plinkResultDict + + + ###################################################### + # input: line: str,one line read from file + # function: convert line from str to list; + # output: lineList list + ####################################################### + def buildLineList(self,line=None): + + lineList = string.split(string.strip(line),' ')# irregular number of whitespaces between columns + lineList =[ item for item in lineList if item <>''] + lineList = map(string.strip, lineList) + + return lineList + + #added by NL: automatically generate pheno txt file for PLINK based on strainList passed from dataEditing page + def genPhenoTxtFileForPlink(self,phenoFileName='', RISetName='', probesetName='', valueDict={}): + pedFileStrainList=self.getStrainNameFromPedFile(RISetName=RISetName) + outputFile = open("%s%s.txt"%(webqtlConfig.TMPDIR,phenoFileName),"wb") + headerLine = 'FID\tIID\t%s\n'%probesetName + outputFile.write(headerLine) + + newValueList=[] + + #if valueDict does not include some strain, value will be set to -9999 as missing value + for item in pedFileStrainList: + try: + value=valueDict[item] + value=str(value).replace('value=','') + value=value.strip() + except: + value=-9999 + + newValueList.append(value) + + + newLine='' + for i, strain in enumerate(pedFileStrainList): + j=i+1 + value=newValueList[i] + newLine+='%s\t%s\t%s\n'%(strain, strain, value) + + if j%1000==0: + outputFile.write(newLine) + newLine='' + + if newLine: + outputFile.write(newLine) + + outputFile.close() + + # get strain name from ped file in order + def getStrainNameFromPedFile(self, RISetName=''): + pedFileopen= open("%splink/%s.ped"%(webqtlConfig.HTMLPATH, RISetName),"r") + line =pedFileopen.readline() + strainNameList=[] + + while line: + lineList=string.split(string.strip(line),'\t') + lineList=map(string.strip,lineList) + + strainName=lineList[0] + strainNameList.append(strainName) + + line =pedFileopen.readline() + + return strainNameList + + ################################################################# + ## Generate Chr list, Chr OrderId and Retrieve Length Information + ################################################################# + #def getChrNameOrderIdLength(self,RISet=''): + # + # try: + # query = """ + # Select + # Chr_Length.Name,Chr_Length.OrderId,Length from Chr_Length, InbredSet + # where + # Chr_Length.SpeciesId = InbredSet.SpeciesId AND + # InbredSet.Name = '%s' + # Order by OrderId + # """ % (RISet) + # self.cursor.execute(query) + # + # results =self.cursor.fetchall() + # ChrList=[] + # ChrLengthMbList=[] + # ChrNameOrderIdDict={} + # ChrOrderIdNameDict={} + # + # for item in results: + # ChrList.append(item[0]) + # ChrNameOrderIdDict[item[0]]=item[1] # key is chr name, value is orderId + # ChrOrderIdNameDict[item[1]]=item[0] # key is orderId, value is chr name + # ChrLengthMbList.append(item[2]) + # + # except: + # ChrList=[] + # ChrNameOrderIdDict={} + # ChrLengthMbList=[] + # + # return ChrList,ChrNameOrderIdDict,ChrOrderIdNameDict,ChrLengthMbList -- cgit v1.2.3