From 77bc01663a38b413d52d39de754b470a804afd97 Mon Sep 17 00:00:00 2001 From: zsloan Date: Tue, 5 Apr 2016 21:46:06 +0000 Subject: Bootstrap test should now work correctly for qtl reaper mapping method on full genome view There seems to be some issue with the way it does single chromosome bootstrap tests compared with GN1, though. It seems like GN1 completely re-runs the test for that specific chromosome, while GN2 just takes a subset from the previously calculated bootstraps --- .../wqflask/marker_regression/marker_regression.py | 64 +++++++++++++++++++--- .../marker_regression/marker_regression_gn1.py | 64 +++++++++++++++------- .../wqflask/templates/marker_regression_gn1.html | 2 + .../templates/show_trait_mapping_tools.html | 20 +++++-- wqflask/wqflask/views.py | 4 ++ 5 files changed, 120 insertions(+), 34 deletions(-) diff --git a/wqflask/wqflask/marker_regression/marker_regression.py b/wqflask/wqflask/marker_regression/marker_regression.py index 6ce35a6c..f08e47ca 100644 --- a/wqflask/wqflask/marker_regression/marker_regression.py +++ b/wqflask/wqflask/marker_regression/marker_regression.py @@ -80,6 +80,8 @@ class MarkerRegression(object): self.score_type = "LRS" #ZS: LRS or LOD self.mapping_scale = "physic" self.num_perm = 0 + self.bootstrap_results = [] + #ZS: This is passed to GN1 code for single chr mapping self.selected_chr = -1 @@ -130,7 +132,7 @@ class MarkerRegression(object): self.num_perm = 0 else: self.num_perm = start_vars['num_perm'] - self.control = start_vars['control_marker'] + self.control_marker = start_vars['control_marker'] self.do_control = start_vars['do_control'] self.method = start_vars['mapmethod_rqtl_geno'] self.model = start_vars['mapmodel_rqtl_geno'] @@ -143,17 +145,35 @@ class MarkerRegression(object): if start_vars['num_perm'] == "": self.num_perm = 0 else: - self.num_perm = int(start_vars['num_perm']) + self.num_perm = int(start_vars['num_perm']) if "startMb" in start_vars: #ZS: Check if first time page loaded, so it can default to ON + if "bootCheck" in start_vars: + self.bootCheck = "ON" + self.num_bootstrap = int(start_vars['num_bootstrap']) + else: + self.bootCheck = False + self.num_bootstrap = int(start_vars['num_bootstrap']) if "additiveCheck" in start_vars: self.additiveCheck = start_vars['additiveCheck'] else: self.additiveCheck = False else: + try: + if int(start_vars['num_bootstrap']) > 0: + self.bootCheck = "ON" + self.num_bootstrap = int(start_vars['num_bootstrap']) + else: + self.bootCheck = False + self.num_bootstrap = 0 + #ZS: If some string that can't be converted to int is input for num_bootstrap + except: + self.num_bootstrap = 0 + self.bootCheck = False + self.additiveCheck = "ON" - self.control = start_vars['control_marker'] + self.control_marker = start_vars['control_marker'] self.do_control = start_vars['do_control'] results = self.gen_reaper_results() elif self.mapping_method == "plink": @@ -447,7 +467,7 @@ class MarkerRegression(object): def create_covariates(self, cross): ro.globalenv["the_cross"] = cross ro.r('genotypes <- pull.geno(the_cross)') # Get the genotype matrix - userinputS = self.control.replace(" ", "").split(",") # TODO: sanitize user input, Never Ever trust a user + userinputS = self.control_marker.replace(" ", "").split(",") # TODO: sanitize user input, Never Ever trust a user covariate_names = ', '.join('"{0}"'.format(w) for w in userinputS) #print("Marker names of selected covariates:", covariate_names) ro.r('covnames <- c(' + covariate_names + ')') @@ -656,14 +676,40 @@ class MarkerRegression(object): #print("samples:", trimmed_samples) - if self.control != "" and self.do_control == "true": - #print("CONTROL IS:", self.control) + if self.control_marker != "" and self.do_control == "true": reaper_results = genotype.regression(strains = trimmed_samples, - trait = trimmed_values, - control = str(self.control)) + trait = trimmed_values, + control = str(self.control_marker)) + if self.bootCheck: + control_geno = [] + control_geno2 = [] + _FIND = 0 + for _chr in genotype: + for _locus in _chr: + if _locus.name == self.control_marker: + control_geno2 = _locus.genotype + _FIND = 1 + break + if _FIND: + break + if control_geno2: + _prgy = list(genotype.prgy) + for _strain in trimmed_samples: + _idx = _prgy.index(_strain) + control_geno.append(control_geno2[_idx]) + + self.bootstrap_results = genotype.bootstrap(strains = trimmed_samples, + trait = trimmed_values, + control = control_geno, + nboot = self.num_bootstrap) else: reaper_results = genotype.regression(strains = trimmed_samples, - trait = trimmed_values) + trait = trimmed_values) + + if self.bootCheck: + self.bootstrap_results = genotype.bootstrap(strains = trimmed_samples, + trait = trimmed_values, + nboot = self.num_bootstrap) self.json_data['chr'] = [] self.json_data['pos'] = [] diff --git a/wqflask/wqflask/marker_regression/marker_regression_gn1.py b/wqflask/wqflask/marker_regression/marker_regression_gn1.py index 739dc569..99f0ac99 100644 --- a/wqflask/wqflask/marker_regression/marker_regression_gn1.py +++ b/wqflask/wqflask/marker_regression/marker_regression_gn1.py @@ -227,24 +227,34 @@ class MarkerRegression(object): self.nperm = int(start_vars['num_perm']) else: self.nperm = 0 + if 'num_bootstrap' in start_vars.keys(): + self.nboot = int(start_vars['num_bootstrap']) + else: + self.nboot = 0 if (start_vars['num_perm'] == "") or (start_vars['num_perm'] < 1): self.permChecked = False else: self.permChecked = True #self.permChecked = fd.formdata.getvalue('permCheck', True) - #self.bootChecked = fd.formdata.getvalue('bootCheck', '') - self.bootChecked = False #ZS: For now setting to False, I'll add this option later once rest of figure works + + if 'bootCheck' in start_vars.keys(): + self.bootChecked = start_vars['bootCheck'] + else: + self.bootChecked = False + if 'bootstrap_results' in start_vars.keys(): + self.bootResult = start_vars['bootstrap_results'] + else: + self.bootResult = [] + if 'do_control' in start_vars.keys(): self.doControl = start_vars['do_control'] else: self.doControl = "false" - if 'control' in start_vars.keys(): - self.controlLocus = start_vars['control'] + if 'control_marker' in start_vars.keys(): + self.controlLocus = start_vars['control_marker'] else: self.controlLocus = "" - - #self.controlLocus = fd.formdata.getvalue('controlLocus', '') #try: self.selectedChr = int(start_vars['selected_chr']) @@ -795,8 +805,8 @@ class MarkerRegression(object): plotXScale = self.drawGraphBackground(canvas, gifmap, offset=newoffset, zoom= zoom, startMb=startMb, endMb = endMb) #draw bootstap - #if self.bootChecked and not self.multipleInterval: - # self.drawBootStrapResult(canvas, fd.nboot, drawAreaHeight, plotXScale, offset=newoffset) + if self.bootChecked and not self.multipleInterval: + self.drawBootStrapResult(canvas, self.nboot, drawAreaHeight, plotXScale, offset=newoffset) # Draw clickable region and gene band if selected if self.plotScale == 'physic' and self.selectedChr > -1: @@ -843,17 +853,31 @@ class MarkerRegression(object): BootCoord = [] i = 0 startX = xLeftOffset - for j, _chr in enumerate(self.genotype): - BootCoord.append( []) - for _locus in _chr: - if self.plotScale == 'physic': - Xc = startX + (_locus.Mb-self.startMb)*plotXScale - else: - Xc = startX + (_locus.cM-_chr[0].cM)*plotXScale - BootCoord[-1].append([Xc, self.bootResult[i]]) - i += 1 - startX += (self.ChrLengthDistList[j] + self.GraphInterval)*plotXScale - + + if self.selectedChr == -1: #ZS: If viewing full genome/all chromosomes + for j, _chr in enumerate(self.genotype): + BootCoord.append( []) + for _locus in _chr: + if self.plotScale == 'physic': + Xc = startX + (_locus.Mb-self.startMb)*plotXScale + else: + Xc = startX + (_locus.cM-_chr[0].cM)*plotXScale + BootCoord[-1].append([Xc, self.bootResult[i]]) + i += 1 + startX += (self.ChrLengthDistList[j] + self.GraphInterval)*plotXScale + else: + for j, _chr in enumerate(self.genotype): + if _chr.name == self.ChrList[self.selectedChr][0]: + BootCoord.append( []) + for _locus in _chr: + if _chr.name == self.ChrList[self.selectedChr][0]: + if self.plotScale == 'physic': + Xc = startX + (_locus.Mb-self.startMb)*plotXScale + else: + Xc = startX + (_locus.cM-_chr[0].cM)*plotXScale + BootCoord[-1].append([Xc, self.bootResult[i]]) + i += 1 + #reduce bootResult if self.selectedChr > -1: maxBootBar = 80.0 @@ -1134,7 +1158,7 @@ class MarkerRegression(object): string1 = 'Mapping for Dataset: %s, mapping on All Chromosomes' % self.dataset.group.name else: string1 = 'Mapping for Dataset: %s, mapping on Chromosome %s' % (self.dataset.group.name, self.ChrList[self.selectedChr][0]) - if self.controlLocus: + if self.controlLocus and self.doControl != "false": string2 = 'Using %s as control' % self.controlLocus else: string2 = 'Using Haldane mapping function with no control for other QTLs' diff --git a/wqflask/wqflask/templates/marker_regression_gn1.html b/wqflask/wqflask/templates/marker_regression_gn1.html index 107b1869..88fb0e89 100644 --- a/wqflask/wqflask/templates/marker_regression_gn1.html +++ b/wqflask/wqflask/templates/marker_regression_gn1.html @@ -22,6 +22,7 @@ + @@ -69,6 +70,7 @@