Ejemplo n.º 1
0
def report(fst):
    global numAttempts
    global fda, fdc, fdRequest, runState, selRec2, splitSize
    global chartPanel, simsDonePanel, systemPanel, empiricalPanel
    global empiricalPanel, menuHandles, statusPanel, frame
    global myFst, maxRun, minRun # This is really a private var to be reused
    global isTemporal, tempSamples, fdt
    if isTemporal:
        fdt.acquire()
    else:
        fda.acquire()
        if myFst < 0:
            myFst = empiricalPanel.getFst()
        if maxRun < 0:
            maxRun = 1.0
            minRun = 0.0
    ext = FDistExtra.getExt()
    fdc = FDistController('.', ext)
    ci = systemPanel.getCI()
    chartPanel.drawCI = True
    confLines = changeChartCI(False)
    simsDonePanel.increment(splitSize / 1000.0)
    if isTemporal:
        desiredNe = empiricalPanel.getNe()
    else:
        desiredFst = empiricalPanel.getFst()
    if simsDonePanel.getRange() == simsDonePanel.getValue():
        #print runState
        if isTemporal:
            fdt.release()  # We are the last one, this is safe
        else:
            fda.release()  # We are the last one, this is safe
        if runState == 'ForceBeforeNeutral' or runState == 'Force':
            os.remove(lpath + os.sep + 'out.dat') #careful, not for 5000 case
            #print "max", maxRun, "min", minRun
            nextFst, maxRun, minRun = approximate_fst(desiredFst, fst, myFst,
                maxRun, minRun)
            #print "obtained", fst, "desired", desiredFst, "next", nextFst, "max", maxRun, "min", minRun
            numAttempts += 1
            if nextFst == myFst or numAttempts == 20:
                numSims = systemPanel.getNumSims()
                if runState == 'Force':
                    statusPanel.setStatus('Running final simulation', Color.YELLOW)
                    runState = 'Final'
                else:
                    runState = 'Neutral'
                    statusPanel.setStatus('Simulation pass to determine initial neutral set', Color.CYAN)
            else:
                statusPanel.setStatus('Forcing correct mean Fst, current error is ' +
                                str(round(abs(fst - desiredFst), 3)), Color.RED)
                numSims = 50000
                myFst = nextFst
            npops = empiricalPanel.getTotalPops()
            nsamples = countPops(selRec2)
            numCores = systemPanel.getNumCores()
            sampSize = empiricalPanel.getSampleSize()
            if isDominant:
                theta = empiricalPanel.getTheta()
                beta = empiricalPanel.getBeta()
                crit = empiricalPanel.getCrit()
                mut = None
            else:
                theta = beta = crit = None
                mutStr = empiricalPanel.mut.getSelectedItem()
                mut = getMut(mutStr)
            if isTemporal:
                pass #XXX
            else:
                runFDistPart(False, selRec2, mut, numSims, npops, nsamples,
                    myFst, sampSize, theta, beta, crit, numCores)
        elif runState == 'Neutral':
            maxRun = -1
            minRun = -1
            myFst  = -1
            if isDominant:
                pv = fdc.run_pv(data_dir =  lpath, version=2)
            else:
                pv = fdc.run_pv(data_dir =  lpath, version=1)
            #pv = get_pv(data_dir = lpath)
            selLoci = getSelLoci(pv)
            if fdRequest == 'Neutral':
                runState = 'Final'
                numSims  = systemPanel.getNumSims()
                statusPanel.setStatus('Running final simulation', Color.YELLOW)
            else:
                runState = 'Force'
                numAttempts = 0
                statusPanel.setStatus('Forcing correct mean Fst for final pass', Color.RED)
                numSims  = 50000
            neutralRec = FileParser.read(selRec2.fname)
            #for locus in selLoci:
            neutralRec.remove_loci_by_name(selLoci, lpath+os.sep+"nr.tmp")
            shutil.copyfile(lpath + os.sep + "nr.tmp",
                    lpath + os.sep + "nr")
            neutralRec = FileParser.read(lpath + os.sep + "nr")
            createInfile(convert_genepop_to_fdist(neutralRec))
            if isTemporal:
                dc = Datacal()
                dc.compute(lpath + os.sep + "infile", lpath + os.sep + "data_fst_outfile")
                dc.computeNe(tempSamples[-1] - tempSamples[0])
                myNe = dc.getNe()
            elif isDominant:
                crit = empiricalPanel.getCrit()
                beta = empiricalPanel.getBeta()
                myFst, _sampSize, _loci, _pops, _F, _obs = \
                    fdc.run_datacal(data_dir = lpath,
                    version=2, crit_freq=crit, p=0.5, beta=beta)
            else:
                myFst, _sampSize = fdc.run_datacal(data_dir = lpath)
            #if myFst < 0.005:
            #    myFst = 0.005
            if isTemporal:
                empiricalPanel.setNe(myNe) #actually it is Ne
            else:
                empiricalPanel.setFst(myFst)
                if not isDominant:
                    mutStr = empiricalPanel.mut.getSelectedItem()
                    mut = getMut(mutStr)
                else:
                    mut = None
            npops = empiricalPanel.getTotalPops()
            nsamples = countPops(selRec2)
            numCores = systemPanel.getNumCores()
            sampSize = empiricalPanel.getSampleSize()
            if isDominant:
                theta = empiricalPanel.getTheta()
                beta = empiricalPanel.getBeta()
                crit = empiricalPanel.getCrit()
            else:
                theta = beta = crit = None
            os.remove(lpath + os.sep + 'out.dat') #careful, not for 5000 case
            createInfile(convert_genepop_to_fdist(selRec2))
            if isTemporal:
                dc = Datacal()
                dc.compute(lpath + os.sep + "infile", lpath + os.sep + "data_fst_outfile")
                dc.computeNe(tempSamples[-1] - tempSamples[0])
                ne = dc.getNe()
            elif isDominant:
                _fst, _sampSize, _loci, _pops, _F, _obs = \
                    fdc.run_datacal(data_dir = lpath, version=2,
                        crit_freq = crit, p=0.5, beta=beta)
            else:
                _fst, _sampSize = fdc.run_datacal(data_dir = lpath)
            if isTemporal:
                runFtempPart(False, selRec2, numSims, npops, nsamples, ne,
                    sampSize, numCores)
            else:
                runFDistPart(False, selRec2, mut, numSims, npops, nsamples,
                    myFst, sampSize, theta, beta, crit, numCores)
        elif runState == 'Final':
            maxRun = -1
            minRun = -1
            myFst  = -1
            statusPanel.setStatus('Done (preparing selection table, please wait...)', Color.GRAY)
            if isDominant:
                pv = fdc.run_pv(data_dir =  lpath, version=2)
            else:
                pv = fdc.run_pv(data_dir =  lpath, version=1)
            selLoci = getSelLoci(pv)
            chartPanel.setSelLoci(pv, selRec2.loci_list, selLoci)
            sp = SelPanel(frame, chartPanel, selRec2.loci_list, pv,
                    systemPanel.getCI(), confLines, locusFst, isDominant,
                    systemPanel.getFDR())
            if isTemporal:
                info(frame, "Done")
            else:
                info(frame, "Simulated Fst: %f" % (fst,))
            statusPanel.setStatus('Done')
            sp.show()
            enablePanel(empiricalPanel)
            enablePanel(systemPanel)
            enableAllMenus(True)
            systemPanel.enableChartFun = True
    else:
        if isTemporal:
            fdt.release()
        else:
            fda.release()
Ejemplo n.º 2
0
def endRunDatacal(after):
    global fdc, selRec2, sampSize, locusFst, lpath
    global empiricalPanel, isDominant, systemPanel
    global tempSamples, isTemporal
    #createInfile(convert_genepop_to_fdist(selRec2))
    createInfile(convert_genepop_to_fdist(selRec2, update_load_status))

    if isDominant:
        crit = empiricalPanel.getCrit()
        beta = empiricalPanel.getBeta()
        fst, sampSize, loci, pops, F, obs = fdc.run_datacal(
            data_dir = lpath, version = 2,
            crit_freq=crit, p=0.5, beta=beta)
    elif isTemporal:
        dc = Datacal()
        dc.compute(lpath + os.sep + "infile", lpath + os.sep + "data_fst_outfile")
        sampSize = dc.getSampleSize()
        dc.computeNe(tempSamples[-1] - tempSamples[0])
        ne = dc.getNe()
    else:
        fst, sampSize = fdc.run_datacal(data_dir = lpath)
        print(fst, sampSize)
    if not isTemporal:
        if fst < 0.0:
            systemPanel.force.setEnabled(False)
            systemPanel.force.setSelected(False)
        else:
            systemPanel.force.setEnabled(True)
    f = open(lpath + os.sep + 'data_fst_outfile')
    locusFst = []
    l = f.readline()
    myPos = 0
    while l != '':
        if isDominant:
            lhe, lfst, lheold, llocus = l.rstrip().split(' ')
            while int(llocus)>myPos:
                locusFst.append(None)
                myPos += 1
            myPos += 1
        else:
            lhe, lfst = l.rstrip().split(' ')
            if lhe == "-nan":
                lhe = "nan"
            if lhe == "-nan":
                lhe = "nan"
            if lfst == "-nan":
                lfst = "nan"
            try:
                if float(lfst) < -10.0:
                    lfst = "nan"
            except ValueError:
                lfst = 'nan'
            try:
                if float(lhe) < -10.0:
                    lhe = "nan"
            except ValueError:
                lhe = 'nan'
        locusFst.append((float(lhe), float(lfst)))
        l = f.readline()
    while len(locusFst) < len(selRec2.loci_list):
        locusFst.append(None)
    f.close()
    if sampSize > 50:
        sampSize = 50
    if isTemporal:
        empiricalPanel.setNe(ne)
    else:
        empiricalPanel.setFst(fst)
    empiricalPanel.setSampleSize(sampSize)
    if isTemporal:
       info (frame, "Dataset Ne: %d" % int(ne))
    else:
       info (frame, "Dataset Fst: %f" % fst)
    after()
    loadPanel.dispose()