def findSNRCutoff(self):
     self.varsP.updateInfoReport( '  Finding SNR cutoff %s\n' % self.expTag, printalso=True ) #put this in info report
     usedScanCount = 0
     #print "\nnscans = ", len(self.scans), "\n" #debug
     deviceDset = molecule.moleculeDataset(self.curExp.basesPerPixel,numLabelChannels=self.numLabelChannels)
     self.varsP.updateInfoReport("               ID  "+deviceDset.makeExperimentHeader(), printalso=True)
     for i, scan in enumerate(self.scans):
         #if i == 0:
             #self.scanReportHeader += deviceDset.makeExperimentHeader() +'\n'
             #if self.varsP.lambdaRef:
             #    deviceDsetLambda = molecule.moleculeDataset(self.curExp.basesPerPixel,numLabelChannels=1)
         molFile = scan.molFile
         if not os.path.exists(molFile) :
             self.varsP.updatePipeReport("Device.findSNRCutoff: MISSING FILE: " + molFile + "\n") 
             continue
         usedScanCount += 1
         scanDset = molecule.moleculeDataset(self.curExp.basesPerPixel, molTag=int(scan.molTag),numLabelChannels=self.numLabelChannels)
         scanDset.readMolFile(molFile)
         lab2File = molFile.replace('.mol', '.0.lab')
         scanDset.annotateLabels(lab2File)
         if self.numLabelChannels > 1:
             lab2File = molFile.replace('.mol', '.1.lab')
             scanDset.annotateLabels(lab2File, labelChannel=1)
         self.varsP.updateInfoReport( "   SNR cutoff: " + scan.molTag + "  " + scanDset.makeExperimentReport(), printalso=True)
         deviceDset.addDset(scanDset)
     if not usedScanCount :
         return
     self.roughMassProfileHeader = deviceDset.getRoughMassProfile(headerOnly=True)
     self.roughMassProfile = deviceDset.getRoughMassProfile()
     deviceDset.getLogSnrCutoff()
     self.SnrCutoff = deviceDset.LogSnrSimpleThresh
     self.lambdaSnrCutoff = self.SnrCutoff
     self.varsP.updateInfoReport( "   SNR total :       " + deviceDset.makeExperimentReport(), printalso=True )
     snrstr = " ".join( map(lambda x: "%.3f"%x, self.SnrCutoff) )
     self.varsP.updateInfoReport( "   SNR Cutoff: " + snrstr + "\n", printalso=True )
 def getCorrectedDataset(self, bpp, snrCutoff, swapchannels=False, verbose=False):
     '''Load mol and lab files into molecule.moleculeDataset,
     call scaleTo500 and filteredSubset in order to apply SNR filter as previously calculated.'''
     scanDset = molecule.moleculeDataset(bpp, molTag=int(self.molTag),numLabelChannels=self.numLabelChannels)
     ret = scanDset.readMolFile(self.molFile)
     if ret != 0 :
         return
     suff = '.0.lab'
     if swapchannels :
         suff = '.1.lab'
     lab2File = self.molFile.replace('.mol', suff)
     scanDset.annotateLabels(lab2File)
     self.Occupancy = scanDset.CalculateOccupancy(1)
     self.Mb = scanDset.MegabasesDetected
     if verbose :
         self.varsP.updateInfoReport("   Prefilter : %s %s" % (self.molTag, scanDset.makeExperimentReport()), printalso=True)
     if self.numLabelChannels > 1:
         lab2File = self.molFile.replace('.mol', '.1.lab')
         scanDset.annotateLabels(lab2File, labelChannel=1)
     scanDset.scaleTo500()
     if verbose :
         self.varsP.updateInfoReport("   Postscale : %s %s" % (self.molTag, scanDset.makeExperimentReport()), printalso=True) #this one is ok
     filt = molecule.filteredSubset(scanDset,minLabSnr=snrCutoff,minLabels=5,minLen=self.minMolLen)
     if verbose :
         self.varsP.updateInfoReport("   Postfilter: %s %s" % (self.molTag, filt.makeExperimentReport()), printalso=True) 
     return filt
    def getLambdaMapJob(self, snrCutoff=0, verbose=False):
        #Note, verbose will print once per job, so use for debugging only
        # add lambda alignment band to this
        lambdaFilter = self.varsP.argData['lambdaFilter']
        lamMinLen = float(lambdaFilter[lambdaFilter.index('-minlen')  +1]) if '-minlen'   in lambdaFilter else 40.
        lamMaxLen = float(lambdaFilter[lambdaFilter.index('-maxlen')  +1]) if '-maxlen'   in lambdaFilter else 60.
        lamMinLab = float(lambdaFilter[lambdaFilter.index('-minsites')+1]) if '-minsites' in lambdaFilter else 6.
        lamMaxLab = float(lambdaFilter[lambdaFilter.index('-maxsites')+1]) if '-maxsites' in lambdaFilter else 10.
        #old format below (dict, not list)
        #lamMinLen = int(lambdaFilter['-minlen'  ]) # 40
        #lamMaxLen = int(lambdaFilter['-maxlen'  ]) # 60
        #lamMinLab = int(lambdaFilter['-minsites']) # 6
        #lamMaxLab = int(lambdaFilter['-maxsites']) # 10
        if verbose :
            self.varsP.updateInfoReport("lamMinLen = %.0f\n" % lamMinLen, printalso=True)
            self.varsP.updateInfoReport("lamMaxLen = %.0f\n" % lamMaxLen, printalso=True)
            self.varsP.updateInfoReport("lamMinLab = %.0f\n" % lamMinLab, printalso=True)
            self.varsP.updateInfoReport("lamMaxLab = %.0f\n" % lamMaxLab, printalso=True)
        
        #need mol file to do this; if doesn't exist, return with warning
        if not(os.path.exists(self.molFile)):
            print "Skipping map lambda job", self.molTag, "because mol file missing:", self.molFile
            self.lambdaErrFile = None
            return

        bnxFileLambda = '%s_lambda.bnx' % self.molTag
        bnxFileLambda = os.path.join(os.path.split(self.molFile)[0], bnxFileLambda)
        #if lambda bnx exists, skip the isolation step
        if os.path.exists(bnxFileLambda) :
            print "Using lambda bnx", bnxFileLambda
        else :
            print '  Isolating Lambda %s' % self.molTag
            lab2File = self.molFile.replace('.mol', '.0.lab')
            scanDset = molecule.moleculeDataset(self.curExp.basesPerPixel, molTag=int(self.molTag))
            scanDset.readMolFile(self.molFile)
            scanDset.annotateLabels(lab2File)
            # Introduce optArguments for Lambda Band
            scanDsetLambda = molecule.filteredSubset(scanDset,snrCutoff,lamMinLab,lamMaxLab,lamMinLen,lamMaxLen,True)
            scanDsetLambda.writeBnxFile(bnxFileLambda, quality=self.quality)

        self.lambdaBnx = bnxFileLambda
        baseArgs = self.varsP.argsListed('mapLambda')
        outputTarget = bnxFileLambda.replace('.bnx', '')
        curArgs = [self.varsP.RefAlignerBin, '-i', bnxFileLambda, '-o', outputTarget, '-ref', self.varsP.lambdaRef] + baseArgs
        if self.varsP.stdoutlog :
            curArgs.extend( ['-stdout', '-stderr'] )
        jobTag = self.molTag + '_lambda'
        self.lambdaErrFile = outputTarget + '.err'

        #if the err file exists, no need to process
        if os.path.exists(self.lambdaErrFile) :
            print "Skipping map lambda job ", jobTag, "because err file exists", self.lambdaErrFile
            return
        
        return mthread.singleJob(curArgs, jobTag, self.lambdaErrFile, jobTag)
 def writeCorrectedBnx(self):
     self.varsP.updateInfoReport("  Correcting %s\n" % self.expTag, printalso=True)
     deviceDset = molecule.moleculeDataset(self.curExp.basesPerPixel,numLabelChannels=self.numLabelChannels)
     self.varsP.updateInfoReport("               ID  "+deviceDset.makeExperimentHeader(), printalso=True)
     goodscans = 0
     for i,scan in enumerate(self.scans):
         #print scan.localTiff #debug
         #print self.bppCorrection[i] #debug
         #if self.bppCorrection != 1.0 :
         correctedBpp = self.bppCorrection[i]*self.curExp.basesPerPixel
         curDataset = scan.getCorrectedDataset(correctedBpp, self.SnrCutoff)
         if not curDataset :
             continue
         goodscans += 1
         #this is redundant with the info report at the end of performImageAnalysis
         # partially, since that comes from the scan object, this is from the moleculeDataset
         self.varsP.updateInfoReport('   Corrected : %s %s' % (scan.molTag, curDataset.makeExperimentReport()), printalso=True)
         #self.varsP.updateInfoReport('\n', printalso=True) #debug
         deviceDset.addDset(curDataset)
     if not goodscans :
         self.bnxFile = "" #if no good scans, null the bnxFile
         return
     self.labPer100kb = deviceDset.calculateLabPer100Kb()
     deviceDset.writeBnxFile(self.bnxFile, quality=self.quality)