def __init__(self, **kwargs):
     OrderedObject.__init__(self, **kwargs)
     self.name = String('default')
     self.maxCores = Integer()
     self.allowMPI = Boolean()
     self.allowThreads = Boolean()
     self.maxHours = Integer()
Beispiel #2
0
    def _createOutputStep(self):
        # New Output would be an Integer
        boxSize = Integer(10)

        if self.iBoxSize.hasValue():
            boxSize.set(2 * int(self.iBoxSize.get()))

        self._defineOutputs(oBoxSize=boxSize)
Beispiel #3
0
def rowToParticle(partRow, **kwargs):
    """ Create a Particle from a row of a meta """
    img = em.Particle()
    
    # Provide a hook to be used if something is needed to be 
    # done for special cases before converting image to row
    preprocessImageRow = kwargs.get('preprocessImageRow', None)
    if preprocessImageRow:
        preprocessImageRow(img, partRow)
    
    # Decompose Relion filename
    index, filename = relionToLocation(partRow.getValue(md.RLN_IMAGE_NAME))
    img.setLocation(index, filename)
    
    if partRow.containsLabel(md.RLN_PARTICLE_CLASS):
        img.setClassId(partRow.getValue(md.RLN_PARTICLE_CLASS))
    
    if kwargs.get('readCtf', True):
        img.setCTF(rowToCtfModel(partRow))
        
    # alignment is mandatory at this point, it shoud be check
    # and detected defaults if not passed at readSetOf.. level
    alignType = kwargs.get('alignType') 
    
    if alignType != em.ALIGN_NONE:
        img.setTransform(rowToAlignment(partRow, alignType))
        
    if kwargs.get('readAcquisition', True):
        img.setAcquisition(rowToAcquisition(partRow))
        
    if kwargs.get('magnification', None):
        img.getAcquisition().setMagnification(kwargs.get("magnification"))
    
    setObjId(img, partRow)
    # Read some extra labels
    rowToObject(partRow, img, {},
                extraLabels=IMAGE_EXTRA_LABELS + kwargs.get('extraLabels', []))

    img.setCoordinate(rowToCoordinate(partRow))
    
    # copy micId if available from row to particle
    if partRow.hasLabel(md.RLN_MICROGRAPH_ID):
        img.setMicId(partRow.getValue(md.RLN_MICROGRAPH_ID))
    
    # copy particleId if available from row to particle
    if partRow.hasLabel(md.RLN_PARTICLE_ID):
        img._rlnParticleId = Integer(partRow.getValue(md.RLN_PARTICLE_ID))
    
    # copy particleId if available from row to particle
    if partRow.hasLabel(md.RLN_PARTICLE_RANDOM_SUBSET):
        img._rln_halfId = Integer(partRow.getValue(md.RLN_PARTICLE_RANDOM_SUBSET))
    
    # Provide a hook to be used if something is needed to be 
    # done for special cases before converting image to row
    postprocessImageRow = kwargs.get('postprocessImageRow', None)
    if postprocessImageRow:
        postprocessImageRow(img, partRow)
    return img
Beispiel #4
0
class PKInhalationDissolution(EMObject):
    # Hartung2020_MATLAB/functions/get_disol.m
    # All implemented dissolution models are based on an adapted version of
    # the Noyes - Whitney equation, but differ in whether they
    # 1) describe saturable or unsaturable dissolution
    # 2) truncate or not the dissolution speed for small particles (for  numeric stability)
    # 3) truncate or not the dissolution speed for large particles
    #    (based on the assumption that only a part of the particle surface
    #    is in contact with the dissolution medium)

    UNSAT       = 0
    TRUNC_UNSAT = 1
    SAT         = 2 # Only this one is implemented
    TRUNC_SAT   = 3
    TRUNC2_SAT  = 4
    CAP         = 5
    XCAP        = 6
    UNSOL       = 7

    def __init__(self, **args):
        EMObject.__init__(self, **args)
        self.type = Integer()
        self.type.set(self.SAT)
        self.substanceParams = None

    def prepare(self, substanceParams, part):
        self.substanceParams = substanceParams
        substanceData = substanceParams.getData()

        if part=="bronchi":
            self.kdiss = substanceData['kdiss_br']
            self.Cs = substanceData['Cs_br']
        else:
            self.kdiss = substanceData['kdiss_alv']
            self.Cs = substanceData['Cs_alv']
        self.rho = substanceData['rho']

        print("Substance in %s ==================="%part)
        print("Maximum dissolution rate [nmol/(cm*min)]",self.kdiss)
        print("Solubility in [nmol/cm3]=[uM]",self.Cs)
        print("Density in [nmol/cm3]",self.rho)

        D = self.kdiss / self.Cs
        K = 4 * math.pi * D / (self.rho * np.power(4.0/3.0 * math.pi, 1.0/3.0))

        if self.type.get()==self.SAT:
            self.dissol = lambda s, Cf: np.multiply(np.reshape(K*(self.Cs-Cf),(Cf.size,1)),
                                                    np.reshape(np.where(s>0, np.power(s,1.0/3.0), 0.0),(1,s.size)))
                          # [cm^3/min]

    def getDissolution(self, s, Cf, h):
        return self.dissol(s,Cf)
    def _getMetrics(self):
        """ Internal method to compute some metrics. """
        # mean values of FSC-Q

        mtd = md.MetaData()
        mtd.read(self._getFileName(MD_MEANS))

        mean = mtd.getValue(MDL_VOLUME_SCORE1, 1)
        meanA = mtd.getValue(MDL_VOLUME_SCORE2, 1)

        # means value for map divided by resolution (FSC-Qr)
        mtd2 = md.MetaData()
        mtd2.read(self._getFileName(MD2_MEANS))

        mean2 = mtd2.getValue(MDL_VOLUME_SCORE1, 1)
        meanA2 = mtd2.getValue(MDL_VOLUME_SCORE2, 1)

        # statistic from fnal pdb with fsc-q
        # Number of atoms greater or less than 0.5
        total_atom = 0
        fscq_greater = 0
        fscq_less = 0
        with open(self._getFileName(PDB_VALUE_FILE)) as f:
            lines_data = f.readlines()
            for j, lin in enumerate(lines_data):

                if (lin.startswith('ATOM') or lin.startswith('HETATM')):

                    total_atom = total_atom + 1
                    fscq_atom = float(lin[54:60])

                    if (fscq_atom > 0.5):
                        fscq_greater = fscq_greater + 1

                    if (fscq_atom < -0.5):
                        fscq_less = fscq_less + 1

        porc_greater = (fscq_greater * 100) / total_atom
        porc_less = (fscq_less * 100) / total_atom

        return {
            'mean': Float(mean),
            'meanA': Float(meanA),
            'mean2': Float(mean2),
            'meanA2': Float(meanA2),
            'total_atom': Integer(total_atom),
            'fscq_greater': Integer(fscq_greater),
            'fscq_less': Integer(fscq_less),
            'porc_greater': Float(porc_greater),
            'porc_less': Float(porc_less)
        }
Beispiel #6
0
    def compareClassesStep(self, i1, i2):
        set1 = self.inputClasses1.get()
        set2 = self.inputClasses2.get()

        # Compare each pair of class from set1 and set2
        # compute the Jaccard index for each (J = len(intersection) / len(union))
        # Create a list will all pairs indexes and the sort them
        jaccardList = []
        f = open(self._getPath('jaccard.txt'), 'w')
        f.write(
            '; class1 class2 intersection(i) union(i) jaccard index = len(i)/len(u)\n'
        )
        for cls1 in set1:
            ids1 = cls1.getIdSet()
            for cls2 in set2:
                ids2 = cls2.getIdSet()
                inter = len(ids1.intersection(ids2))
                union = len(ids1.union(ids2))
                jaccardIndex = float(inter) / union
                jaccardTuple = (cls1.getObjId(), cls2.getObjId(), inter, union,
                                jaccardIndex)
                f.write('%d %d %d %d %0.3f\n' % jaccardTuple)
                jaccardList.append(jaccardTuple)
        f.close()

        jaccardList.sort(key=lambda e: e[4], reverse=True)
        visitedClasses = set()
        outputFn = self._getPath('consensus.sqlite')
        cleanPath(outputFn)
        outputSet = EMSet(filename=outputFn)

        for clsId1, clsId2, inter, union, jaccardIndex in jaccardList:
            if clsId1 not in visitedClasses:
                visitedClasses.add(clsId1)  # mark as visited
                cls1 = set1[clsId1]
                cls2 = set2[clsId2]
                o = Object()
                o.setObjLabel('classes %d - %d' % (clsId1, clsId2))
                o.class1 = cls1.clone()
                o.class1.id = Integer(clsId1)
                o.class2 = cls2.clone()
                o.class2.id = Integer(clsId2)
                o.jaccard = Float(jaccardIndex)
                o.intersection = Integer(inter)
                o.union = Integer(union)
                outputSet.append(o)

        self._defineOutputs(outputConsensus=outputSet)
Beispiel #7
0
    def createOutputStep(self):
        from sklearn.manifold import TSNE
        Xdim = self.inputParticles.get().getXDim()
        self.Ts = self.inputParticles.get().getSamplingRate()
        newTs = self.targetResolution.get() * 1.0 / 3.0
        self.newTs = max(self.Ts, newTs)
        self.newXdim = int(Xdim * self.Ts / newTs)
        fnOut = self._getFileName('fnOut')
        mdOut = md.MetaData(fnOut)
        coeffMatrix = np.vstack(mdOut.getColumnValues(md.MDL_SPH_COEFFICIENTS))
        X_tsne_1d = TSNE(n_components=1).fit_transform(coeffMatrix)
        X_tsne_2d = TSNE(n_components=2).fit_transform(coeffMatrix)

        newMdOut = md.MetaData()
        i = 0
        for row in md.iterRows(mdOut):
            newRow = row
            newRow.setValue(md.MDL_SPH_TSNE_COEFF1D, float(X_tsne_1d[i, 0]))
            newRow.setValue(md.MDL_SPH_TSNE_COEFF2D,
                            [float(X_tsne_2d[i, 0]),
                             float(X_tsne_2d[i, 1])])
            if self.newTs != self.Ts:
                coeffs = mdOut.getValue(md.MDL_SPH_COEFFICIENTS,
                                        row.getObjId())
                correctionFactor = self.inputVolume.get().getDim(
                )[0] / self.newXdim
                coeffs = [correctionFactor * coeff for coeff in coeffs]
                newRow.setValue(md.MDL_SPH_COEFFICIENTS, coeffs)
            newRow.addToMd(newMdOut)
            i += 1
        newMdOut.write(fnOut)

        inputSet = self.inputParticles.get()
        partSet = self._createSetOfParticles()

        partSet.copyInfo(inputSet)
        partSet.setAlignmentProj()
        partSet.copyItems(inputSet,
                          updateItemCallback=self._updateParticle,
                          itemDataIterator=md.iterRows(
                              fnOut, sortByLabel=md.MDL_ITEM_ID))
        partSet.L1 = Integer(self.l1.get())
        partSet.L2 = Integer(self.l2.get())
        partSet.Rmax = Integer(self.inputVolume.get().getDim()[0] / 2)

        self._defineOutputs(outputParticles=partSet)
        self._defineTransformRelation(self.inputParticles, partSet)
Beispiel #8
0
 def createOutputStep(self):
     """ The output is just an Integer. Other protocols can use it in those
         IntParam if it has set allowsPointer=True
     """
     micSet = self.inputMicrographs.get()
     boxSize = Integer(self.particleBoxsize)
     self._defineOutputs(boxsize=boxSize)
     self._defineSourceRelation(micSet, boxSize)
Beispiel #9
0
    def _defineParams(self, form):
        form.addSection('Input')
        form.addParam('inputExperiment',
                      params.PointerParam,
                      label="Input experiment",
                      pointerClass='PKPDExperiment',
                      help='Select an experiment with samples')
        form.addParam(
            'protElimination',
            params.PointerParam,
            label="Elimination rate",
            pointerClass='ProtPKPDEliminationRate',
            help=
            'Select an execution of a protocol estimating the elimination rate'
        )
        form.addParam(
            "absorptionF",
            params.FloatParam,
            label="Absorption fraction",
            default=1,
            help="Between 0 (=no absorption) and 1 (=full absorption)")

        form.addParam(
            'bounds',
            params.StringParam,
            label="Ka, V, [tlag] bounds",
            default="",
            expertLevel=LEVEL_ADVANCED,
            help=
            'Bounds for Ka (absorption constant), V (distribution volume) and optionally tlag.\nExample 1: (0,1e-3);(30,50);(0.1,0.5) -> Ka in (0,1e-3), V in (30,50) and tlag in (0.1,0.5)\n'
        )
        form.addParam('confidenceInterval',
                      params.FloatParam,
                      label="Confidence interval",
                      default=95,
                      expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        form.addParam(
            'includeTlag',
            params.BooleanParam,
            label="Include tlag",
            default=True,
            expertLevel=LEVEL_ADVANCED,
            help='Calculate the delay between administration and absorption')
        self.fitType = Integer()  # Logarithmic fit
        self.fitType.set(1)
Beispiel #10
0
    def _updateItem(self, particle, row):
        self.reader.setParticleTransform(particle, row)
        # FIXME: check if other attrs need saving
        particle._rlnImageOriginalName = String(row.rlnImageOriginalName)
        particle._rlnRandomSubset = Integer(row.rlnRandomSubset)

        newLoc = convert.relionToLocation(row.rlnImageName)
        particle.setLocation(newLoc)
Beispiel #11
0
 def __init__(self, **kwargs):
     OrderedObject.__init__(self, **kwargs)
     self.name = String()
     # Number of cores from which the queue is mandatory
     # 0 means no mandatory at all
     # 1 will force to launch all jobs through the queue
     self.mandatory = Integer()
     self.queues = None  # List for queue configurations
     self.submitCommand = String()
     # Allow to change the prefix of submission scripts
     # we used by default the ID.job, but in some clusters
     # the job script should start by a letter
     self.submitPrefix = String()
     self.checkCommand = String()
     self.cancelCommand = String()
     self.submitTemplate = String()
     self.jobDoneRegex = String()
    def readPartsFromMics(self, micList, outputParts):
        """ Read the particles extract for the given list of micrographs
        and update the outputParts set with new items.
        """
        relionToLocation = relion.convert.relionToLocation
        p = Particle()
        p._rlnOpticsGroup = Integer()
        acq = self.getInputMicrographs().getAcquisition()
        # JMRT: Ideally I would like to disable the whole Acquisition for each
        #       particle row, but the SetOfImages will set it again.
        #       Another option could be to disable in the set, but then in
        #       streaming, other protocols might get the wrong optics info
        pAcq = Acquisition(magnification=acq.getMagnification(),
                           voltage=acq.getVoltage(),
                           amplitudeContrast=acq.getAmplitudeContrast(),
                           sphericalAberration=acq.getSphericalAberration())
        p.setAcquisition(pAcq)

        tmp = self._getTmpPath()
        extra = self._getExtraPath()

        for mic in micList:
            posSet = set()
            coordDict = {self._getPos(c): c
                         for c in self.coordDict[mic.getObjId()]}
            del self.coordDict[mic.getObjId()]

            ogNumber = mic.getAttributeValue('_rlnOpticsGroup', 1)

            partsStar = self.__getMicFile(mic, '_extract.star', folder=tmp)
            partsTable = relion.convert.Table(fileName=partsStar)
            stackFile = self.__getMicFile(mic, '.mrcs', folder=tmp)
            endStackFile = self.__getMicFile(mic, '.mrcs', folder=extra)
            pwutils.moveFile(stackFile, endStackFile)

            for part in partsTable:
                pos = (int(float(part.rlnCoordinateX)),
                       int(float(part.rlnCoordinateY)))

                if pos in posSet:
                    print("Duplicate coordinate at: %s, IGNORED. " % str(pos))
                    coord = None
                else:
                    coord = coordDict.get(pos, None)

                if coord is not None:
                    # scale the coordinates according to particles dimension.
                    coord.scale(self.getBoxScale())
                    p.copyObjId(coord)
                    idx, fn = relionToLocation(part.rlnImageName)
                    p.setLocation(idx, endStackFile)
                    p.setCoordinate(coord)
                    p.setMicId(mic.getObjId())
                    p.setCTF(mic.getCTF())
                    p._rlnOpticsGroup.set(ogNumber)
                    outputParts.append(p)
                    posSet.add(pos)
    def createOutputStep(self):
        posDir = self._getExtraPath()
        coordSet = self._createSetOfCoordinates(self.inputMics)
        readSetOfCoordinates(posDir, self.inputMics, coordSet)
        self._defineOutputs(outputCoordinates=coordSet)
        self._defineSourceRelation(self.inputMicrographs, coordSet)

        boxSize = Integer(coordSet.getBoxSize())
        self._defineOutputs(boxsize=boxSize)
        self._defineSourceRelation(self.inputMicrographs.get(), boxSize)
    def show(self, form, *params):
        patchValues = [Integer(i) for i in range(32, 130, 8)]

        # Get a data provider from the patchValues to be used in the tree (dialog)
        provider = ListTreeProviderString(patchValues)

        dlg = dialog.ListDialog(form.root, "Paych shape values", provider,
                                "Select one of the size values)")

        # Set the chosen value back to the form
        form.setVar(PATCH_SHAPE, dlg.values[0].get())
Beispiel #15
0
class ProtPKPDExponentialFit(ProtPKPDFitBase):
    """ Fit a set of exponentials. The observed measurement is modelled as Y=sum_{i=1}^N c_i exp(-lambda_i * X).\n
Confidence intervals calculated by this fitting may be pessimistic because it assumes that all model parameters
are independent, which are not. Use Bootstrap estimates instead.\n
        Protocol created by http://www.kinestatpharma.com\n"""
    _label = 'fit exponentials'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineParams(self, form, fullForm=True):
        self._defineParams1(form,"t","Cp")
        if fullForm:
            form.addParam('fitType', params.EnumParam, choices=["Linear","Logarithmic","Relative"], label="Fit mode", default=1,
                          help='Linear: sum (Cobserved-Cpredicted)^2\nLogarithmic: sum(log10(Cobserved)-log10(Cpredicted))^2\n'\
                               "Relative: sum ((Cobserved-Cpredicted)/Cobserved)^2")
            form.addParam('Nexp', params.IntParam, label="Number of exponentials", default=1,
                          help='Number of exponentials to fit')
        else:
            self.fitType=Integer()
            self.fitType.set(1)
            self.Nexp=Integer()
            self.Nexp.set(1)
        form.addParam('bounds', params.StringParam, label="Amplitude and time constant bounds", default="", expertLevel=LEVEL_ADVANCED,
                      help='Bounds for the c_i amplitudes and lambdas.\nExample 1: (0,10);(0,1e-2) -> c1 in (0,10), lambda1 in (0,1e-2)\n'\
                           'Example 2: (0,10);(0,1e-2);(0,1);(0,1e-1) -> c1 in (0,10), lambda1 in (0,1e-2), c2 in (0,1), lambda2 in (0,1e-1)')
        form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval=", default=95, expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        if fullForm:
            form.addParam('reportX', params.StringParam, label="Evaluate at X=", default="", expertLevel=LEVEL_ADVANCED,
                          help='Evaluate the model at these X values\nExample 1: [0,5,10,20,40,100]\nExample 2: 0:0.55:10, from 0 to 10 in steps of 0.5')
        else:
            self.reportX=String()
            self.reportX.set("")

    def getListOfFormDependencies(self):
        return [self.predictor.get(), self.predicted.get(), self.fitType.get(), self.bounds.get()]

    def createModel(self):
        return PKPDExponentialModel()

    def setupFromFormParameters(self):
        self.model.Nexp=self.Nexp.get()

    #--------------------------- INFO functions --------------------------------------------
    def _warnings(self):
        warnings = []
        experiment = self.readExperiment(self.getInputExperiment().fnPKPD,show=False)
        incorrectList = experiment.getNonBolusDoses()
        if len(incorrectList)>0:
            warnings.append("This protocol is meant only for intravenous bolus regimens. Check the doses for %s"%(','.join(incorrectList)))
        return warnings

    def _validate(self):
        errors=ProtPKPDFitBase._validate(self)
        if self.Nexp.get()<1:
            errors.append("The number of exponentials has to be larger than 0")
        return errors
    def launchParticlePickGUIStep(self, micFn):
        # Launch the particle picking GUI
        extraDir = self._getExtraPath()
        process = launchSupervisedPickerGUI(micFn, extraDir, self)
        process.wait()
        # generate the discarded output only if there is a good output
        if self.saveDiscarded and exists(self._getPath('coordinates.sqlite')):
            self.createDiscardedStep()

        coordSet = self.getCoords()
        if coordSet:
            boxSize = Integer(coordSet.getBoxSize())
            self._defineOutputs(boxsize=boxSize)
            self._defineSourceRelation(self.inputMicrographs.get(), boxSize)
Beispiel #17
0
 def initializeRejDict(self):
     self.discDict = {'defocus': 0,
                           'astigmatism': 0,
                           'singleResolution': 0,
                           '_xmipp_ctfCritFirstZero': 0,
                           '_xmipp_ctfCritfirstZeroRatio': 0,
                           '_xmipp_ctfCritCorr13': 0,
                           '_xmipp_ctfIceness': 0,
                           '_xmipp_ctfCritCtfMargin': 0,
                           '_xmipp_ctfCritNonAstigmaticValidty': 0,
                           'consensusResolution': 0
                           }
     for k in self.discDict:
         setattr(self, "rejBy"+k, Integer(0))
     self._store()
Beispiel #18
0
 def _defineParams(self, form, fullForm=True):
     self._defineParams1(form,"t","Cp")
     if fullForm:
         form.addParam('fitType', params.EnumParam, choices=["Linear","Logarithmic","Relative"], label="Fit mode", default=1,
                       help='Linear: sum (Cobserved-Cpredicted)^2\nLogarithmic: sum(log10(Cobserved)-log10(Cpredicted))^2\n'\
                            "Relative: sum ((Cobserved-Cpredicted)/Cobserved)^2")
         form.addParam('Nexp', params.IntParam, label="Number of exponentials", default=1,
                       help='Number of exponentials to fit')
     else:
         self.fitType=Integer()
         self.fitType.set(1)
         self.Nexp=Integer()
         self.Nexp.set(1)
     form.addParam('bounds', params.StringParam, label="Amplitude and time constant bounds", default="", expertLevel=LEVEL_ADVANCED,
                   help='Bounds for the c_i amplitudes and lambdas.\nExample 1: (0,10);(0,1e-2) -> c1 in (0,10), lambda1 in (0,1e-2)\n'\
                        'Example 2: (0,10);(0,1e-2);(0,1);(0,1e-1) -> c1 in (0,10), lambda1 in (0,1e-2), c2 in (0,1), lambda2 in (0,1e-1)')
     form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval=", default=95, expertLevel=LEVEL_ADVANCED,
                   help='Confidence interval for the fitted parameters')
     if fullForm:
         form.addParam('reportX', params.StringParam, label="Evaluate at X=", default="", expertLevel=LEVEL_ADVANCED,
                       help='Evaluate the model at these X values\nExample 1: [0,5,10,20,40,100]\nExample 2: 0:0.55:10, from 0 to 10 in steps of 0.5')
     else:
         self.reportX=String()
         self.reportX.set("")
Beispiel #19
0
            def updateItem(item, row):
                micName = getMicName(item)

                if micName not in micDict:
                    raise Exception("Micrograph name (aka micName) '%s' was "
                                    "not found in the 'data_micrographs' table of "
                                    "the input star file: %s"
                                    % (micName, inputStar))

                ogNumber = micDict[micName]

                if not hasattr(item, '_rlnOpticsGroup'):
                    item._rlnOpticsGroup = Integer()

                item._rlnOpticsGroup.set(ogNumber)
Beispiel #20
0
def saveConfig(filename):
    from pyworkflow.mapper import SqliteMapper
    from pyworkflow.object import String, Integer

    mapper = SqliteMapper(filename)
    o = Config()
    for k, v in globals().iteritems():
        if k.startswith('cfg'):
            if type(v) is str:
                value = String(v)
            else:
                value = Integer(v)
            setattr(o, k, value)
    mapper.insert(o)
    mapper.commit()
    def _readValidationPklFile(self, fileName):
        self.SUMMARYFILENAME = self._getTmpPath(self.SUMMARYFILENAME)
        command = """import pickle
import collections
import json

def pickleData(file):
    with open(file,"r") as f:
        return pickle.load(f)

# process file {VALIDATIONCRYOEMPKLFILENAME}"
data = pickleData('{VALIDATIONCRYOEMPKLFILENAME}')
dictSummary = collections.OrderedDict()

dictSummary['Rhama_Outliers'] = data.model.geometry.ramachandran.outliers
dictSummary['Rhama_Favored'] = data.model.geometry.ramachandran.favored
dictSummary['Rota_Outliers'] = data.model.geometry.rotamer.outliers
dictSummary['Cbeta_Outliers_n'] = data.model.geometry.c_beta.cbetadev.n_outliers
dictSummary['Clash_score'] = data.model.geometry.clash.score
dictSummary['MolProbity_score'] = data.model.geometry.molprobity_score
""".format(VALIDATIONCRYOEMPKLFILENAME=fileName)

        command += """with open('%s',"w") as f:
    f.write(json.dumps(dictSummary))
""" % (self.SUMMARYFILENAME)

        pythonFileName = self.SUMMARYFILENAME.replace('.txt', '.py')
        # write script file
        with open(pythonFileName, "w") as f:
            f.write(command)

        # execute file with phenix.python
        Plugin.runPhenixProgram("", pythonFileName)

        # read file in scipion python
        with open(self.SUMMARYFILENAME, "r") as f:
            dictSummary = f.read()

        dictSummary = json.loads(dictSummary,
                                 object_pairs_hook=collections.OrderedDict)

        self.ramachandranOutliers = Float(dictSummary['Rhama_Outliers'])
        self.ramachandranFavored = Float(dictSummary['Rhama_Favored'])
        self.rotamerOutliers = Float(dictSummary['Rota_Outliers'])
        self.cbetaOutliers = Integer(dictSummary['Cbeta_Outliers_n'])
        self.clashscore = Float(dictSummary['Clash_score'])
        self.overallScore = Float(dictSummary['MolProbity_score'])
    def _defineParams(self, form):
        form.addSection('Input')
        form.addParam('inputExperiment', params.PointerParam, label="Input experiment",
                      pointerClass='PKPDExperiment',
                      help='Select an experiment with samples')
        form.addParam('protElimination', params.PointerParam, label="Elimination rate",
                      pointerClass='ProtPKPDEliminationRate',
                      help='Select an execution of a protocol estimating the elimination rate')
        form.addParam("absorptionF", params.FloatParam, label="Absorption fraction", default=1,
                      help="Between 0 (=no absorption) and 1 (=full absorption)")

        form.addParam('bounds', params.StringParam, label="Ka, V, [tlag] bounds", default="", expertLevel=LEVEL_ADVANCED,
                      help='Bounds for Ka (absorption constant), V (distribution volume) and optionally tlag.\nExample 1: (0,1e-3);(30,50);(0.1,0.5) -> Ka in (0,1e-3), V in (30,50) and tlag in (0.1,0.5)\n')
        form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval", default=95, expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        form.addParam('includeTlag', params.BooleanParam, label="Include tlag", default=True, expertLevel=LEVEL_ADVANCED,
                      help='Calculate the delay between administration and absorption')
        self.fitType=Integer() # Logarithmic fit
        self.fitType.set(1)
 def _parseFile(self, fileName):
     with open(fileName, encoding="ISO-8859-1") as f:
         line = f.readline()
         while line:
             words = line.strip().split()
             if len(words) > 1:
                 if (words[0] == 'Ramachandran' and words[1] == 'outliers'):
                     self.ramachandranOutliers = Float(words[3])
                 elif (words[0] == 'favored' and words[1] == '='):
                     self.ramachandranFavored = Float(words[2])
                 elif (words[0] == 'Rotamer' and words[1] == 'outliers'):
                     self.rotamerOutliers = Float(words[3])
                 elif (words[0] == 'C-beta' and words[1] == 'deviations'):
                     self.cbetaOutliers = Integer(words[3])
                 elif (words[0] == 'Clashscore' and words[1] == '='):
                     self.clashscore = Float(words[2])
                 elif (words[0] == 'MolProbity' and words[1] == 'score'):
                     self.overallScore = Float(words[3])
             line = f.readline()
Beispiel #24
0
class PKCiliarySpeed(EMObject):
    # Hartung2020_MATLAB/functions/get_cilspeed.m
    # Only 'interp' model is implemented here

    EXPON = 0
    FIT = 1
    INTERP = 2

    def __init__(self, **args):
        EMObject.__init__(self, **args)
        self.type = Integer()
        self.type.set(self.INTERP)

        self.lungParams = None

    def prepare(self, lungParams, ciliarySpeedType):
        self.type.set(ciliarySpeedType)

        self.lungParams = lungParams
        lungData = lungParams.getBronchial()

        self.Lx = np.sum(lungData['length_cm'])
        self.pos = lungData['pos']
        self.diam_cm = lungData['diam_cm']

        print("Ciliary speed ================")
        print("Total length of the lung [cm]",self.Lx)
        print("Location of branches [cm]",self.pos)
        print("Diameter of the branches [cm]",self.diam_cm)

        if self.type.get()==self.EXPON:
            # Exponentially increasing ciliary speed
            self.cilspeed = lambda x: np.where(x>self.Lx, 0.0, np.exp(np.log(4e-3) + x * (np.log(50) - np.log(4e-3))))
        elif self.type.get()==self.FIT:
            # Simple fitted empirical ciliary transport model
            self.cilspeed = lambda x: np.where(x<0.0, 0.0, np.exp(5*np.divide(self.Lx-x-3,np.power(3+self.Lx-x,0.92)))/200)
        elif self.type.get()==self.INTERP:
            # Hofmann - Sturm model for mucociliary transport velocity
            # Reference: Hofmann / Sturm (2004) J Aerosol Med, Vol 17(1)
            self.v = 0.1 * 1.2553 * np.power(lungData['diam_cm'],2.808) # in [cm / min]
            self.cilspeed = interp1d(self.pos, self.v, bounds_error=False, fill_value=(self.v[0], self.v[-1]))
    def _updateItem(self, item, row):
        """ Implement this function to do some
        update actions over each single item
        that will be stored in the output Set.
        """
        # Add alignment info from corresponding item on inputAlignment
        inputAlignment = self.inputAlignment.get()
        scale = inputAlignment.getSamplingRate()/self.inputParticles.get().getSamplingRate()

        alignedParticle = inputAlignment[item.getObjId()]
        # If alignment is found for this particle set the alignment info
        # on the output particle, if not do not write that item
        if alignedParticle is not None:
            alignment = alignedParticle.getTransform()
            alignment.scaleShifts(scale, shiftsAppliedBefore=self.shiftsAppliedBefore.get())
            item.setTransform(alignment)

            if self.assignRandomSubsets:
                subset = alignedParticle.getAttributeValue('_rlnRandomSubset', None)
                if subset is not None:
                    item._rlnRandomSubset = Integer(subset)
        else:
            item._appendItem = False
Beispiel #26
0
class QueueConfig(OrderedObject):
    def __init__(self, **kwargs):
        OrderedObject.__init__(self, **kwargs)
        self.name = String('default')
        self.maxCores = Integer()
        self.allowMPI = Boolean()
        self.allowThreads = Boolean()
        self.maxHours = Integer()

    def getName(self):
        return self.name.get()

    def getMaxCores(self):
        return self.maxCores.get()

    def getAllowMPI(self):
        return self.allowMPI.get()

    def getAllowThreads(self):
        return self.allowThreads.get()

    def getMaxHours(self):
        return self.maxHours.get()

    def setName(self, name):
        self.name.set(name)

    def setMaxCores(self, maxCores):
        self.maxCores.set(maxCores)

    def setAllowMPI(self, allowMPI):
        self.allowMPI.set(allowMPI)

    def setAllowThreads(self, allowThreads):
        self.allowThreads.set(allowThreads)

    def setMaxHours(self, maxHours):
        self.maxHours.set(maxHours)
    def _updateOutput(self, tsIdList):
        """ Update the output set with the finished Tilt-series.
        Params:
            :param tsIdList: list of ids of finished tasks.
        """
        ts = self._getTiltSeries(tsIdList[0])
        tsId = ts.getTsId()
        objId = ts.getObjId()
        # Flag to check the first time we save output
        self._createOutput = getattr(self, '_createOutput', True)

        outputSet = self._getOutputSet()

        if outputSet is None:
            # Special case just to update the outputSet status
            # but it only makes sense when there is outputSet
            if not tsIdList:
                return
            outputSet = self._createOutputSet()
        else:
            outputSet.enableAppend()
            self._createOutput = False

        newCTFTomoSeries = CTFTomoSeries()
        newCTFTomoSeries.copyInfo(ts)
        newCTFTomoSeries.setTiltSeries(ts)
        newCTFTomoSeries.setTsId(tsId)
        newCTFTomoSeries.setObjId(objId)

        outputSet.append(newCTFTomoSeries)

        index = 1
        for ti in self._tsDict.getTiList(tsId):
            newCTFTomo = ti._ctfModel
            newCTFTomo.setIndex(Integer(index))
            index += 1
            newCTFTomoSeries.append(newCTFTomo)

        newCTFTomoSeries.calculateDefocusUDeviation()
        newCTFTomoSeries.calculateDefocusVDeviation()

        if not (newCTFTomoSeries.getIsDefocusUDeviationInRange() and
                newCTFTomoSeries.getIsDefocusVDeviationInRange()):
            newCTFTomoSeries.setEnabled(False)

        newCTFTomoSeries.write(properties=False)
        outputSet.update(newCTFTomoSeries)

        if self._createOutput:
            self._defineOutputs(**{self._getOutputName(): outputSet})
            self._defineSourceRelation(self._getInputTs(pointer=True),
                                       outputSet)
            self._createOutput = False
        else:
            outputSet.write()
            self._store(outputSet)

        outputSet.close()
        self._store()

        if self._tsDict.allDone():
            self._coStep.setStatus(STATUS_NEW)
 def __init__(self, **args):        
     ProtRefine3D.__init__(self, **args)
     ProtClassify3D.__init__(self, **args)
     self.numberOfCtfGroups = Integer(1)
     self._lastIter = Integer(0)
class XmippProtProjMatch(ProtRefine3D, ProtClassify3D):
    """ 3D reconstruction and classification using multireference projection matching"""

    _label = 'projection matching'
    
    FILENAMENUMBERLENGTH = 6

    def __init__(self, **args):        
        ProtRefine3D.__init__(self, **args)
        ProtClassify3D.__init__(self, **args)
        self.numberOfCtfGroups = Integer(1)
        self._lastIter = Integer(0)
        
    def _initialize(self):
        """ This function is mean to be called after the 
        working dir for the protocol have been set. (maybe after recovery from mapper)
        """
        self._loadInputInfo()
        # Setup the dictionary with filenames templates to 
        # be used by _getFileName
        createFilenameTemplates(self)
        # Load the values from several params generating a list
        # of values per iteration or references
        initializeLists(self)

    def _loadInputInfo(self):
        from pyworkflow.em.packages.xmipp3 import getImageLocation
        
        reference = self.input3DReferences.get() # Input can be either a single volume or a set of volumes.
        
        if isinstance(reference, Volume): # Treat the case of a single volume
            self.referenceFileNames = [getImageLocation(reference)]
        else:
            self.referenceFileNames = [getImageLocation(vol) for vol in reference]
            
        self.numberOfReferences = len(self.referenceFileNames)
        self.resolSam = reference.getSamplingRate()

        
    #--------------------------- DEFINE param functions --------------------------------------------   
        
    def _defineParams(self, form):
        """ Since the form definition is very very large,
        we have do it in a separated function.
        """
        _defineProjectionMatchingParams(self, form)
         
         
    #--------------------------- INSERT steps functions --------------------------------------------  
    
    def _insertAllSteps(self):
        self._initialize()
        # Insert initial steps
        self._insertFunctionStep('convertInputStep')
        self._insertFunctionStep('executeCtfGroupsStep')
#         insertExecuteCtfGroupsStep(self)
#         insertInitAngularReferenceFileStep(self)
        self._insertFunctionStep('initAngularReferenceFileStep')
        # Steps per iteration
        self._insertItersSteps()
        # Final steps
        self._insertFunctionStep('createOutputStep')
        
    def _insertItersSteps(self):
        """ Insert several steps needed per iteration. """
        
        for iterN in self.allIters():
            dirsStep = self._insertFunctionStep('createIterDirsStep', iterN)
            # Insert some steps per reference volume
            projMatchSteps = []
            for refN in self.allRefs():
                # Mask the references in the iteration
                insertMaskReferenceStep(self, iterN, refN, prerequisites=[dirsStep])
                # Create the library of projections
                insertAngularProjectLibraryStep(self, iterN, refN)
                # Projection matching steps
                projMatchStep = self._insertProjectionMatchingStep(iterN, refN)
                projMatchSteps.append(projMatchStep)
                
            # Select the reference that best fits each image
            self._insertFunctionStep('assignImagesToReferencesStep', iterN, prerequisites=projMatchSteps)
            
            insertAngularClassAverageStep(self, iterN, refN)
    
            # Reconstruct each reference with new averages
            for refN in self.allRefs():
                # Create new class averages with images assigned
                insertReconstructionStep(self, iterN, refN)
                
                if self.doComputeResolution and self._doSplitReferenceImages[iterN]:
                    # Reconstruct two halves of the data
                    insertReconstructionStep(self, iterN, refN, 'Split1')
                    insertReconstructionStep(self, iterN, refN, 'Split2')
                    # Compute the resolution
                    insertComputeResolutionStep(self, iterN, refN)
                    
                insertFilterVolumeStep(self, iterN, refN)

            # Calculate both angles and shifts devitations for this iteration
            self._insertFunctionStep('calculateDeviationsStep', iterN)

    
    def _insertProjectionMatchingStep(self, iterN, refN):
        args = getProjectionMatchingArgs(self, iterN)
        return self._insertFunctionStep('projectionMatchingStep', iterN, refN, args)
    
    #--------------------------- STEPS functions --------------------------------------------       

    def convertInputStep(self):
        """ Generated the input particles metadata expected 
        by projection matching. And copy the generated file to be
        used as initial docfile for further iterations.
        """
        from pyworkflow.em.packages.xmipp3 import writeSetOfParticles
        writeSetOfParticles(self.inputParticles.get(), self.selFileName, 
                            blockName=self.blockWithAllExpImages)
        #copyFile(self.selFileName, self._getFileName('inputParticlesDoc'))
        
    def createIterDirsStep(self, iterN):
        """ Create the necessary directory for a given iteration. """
        iterDirs = [self._getFileName(k, iter=iterN) for k in ['iterDir', 'projMatchDirs', 'libraryDirs']]
    
        for d in iterDirs:
            makePath(d)
            
        return iterDirs
    
    def volumeConvertStep(self, reconstructedFilteredVolume, maskedFileName):
        runVolumeConvertStep(self, reconstructedFilteredVolume, maskedFileName)
    
    def executeCtfGroupsStep(self, **kwargs):
        runExecuteCtfGroupsStep(self, **kwargs)
    
    def transformMaskStep(self, args, **kwargs):
        runTransformMaskStep(self, args, **kwargs)
    
    def angularProjectLibraryStep(self, iterN, refN, args, stepParams, **kwargs):
        runAngularProjectLibraryStep(self, iterN, refN, args, stepParams, **kwargs)
        
    def initAngularReferenceFileStep(self):
        runInitAngularReferenceFileStep(self)
        
    def projectionMatchingStep(self, iterN, refN, args):
        runProjectionMatching(self, iterN, refN, args)
    
    def assignImagesToReferencesStep(self, iterN):
        runAssignImagesToReferences(self, iterN)
        
    def cleanVolumeStep(self, vol1, vol2):
        cleanPath(vol1, vol2)
    
    def reconstructionStep(self, iterN, refN, program, method, args, suffix, mpi, threads, **kwargs):
        runReconstructionStep(self, iterN, refN, program, method, args, suffix, mpi, threads, **kwargs)
    
    def storeResolutionStep(self, resolIterMd, resolIterMaxMd, sampling):
        runStoreResolutionStep(self, resolIterMd, resolIterMaxMd, sampling)
    
    def calculateFscStep(self, iterN, refN, args, constantToAdd, **kwargs):
        runCalculateFscStep(self, iterN, refN, args, constantToAdd, **kwargs)
    
    def filterVolumeStep(self, iterN, refN, constantToAddToFiltration, **kwargs):
        runFilterVolumeStep(self, iterN, refN, constantToAddToFiltration, **kwargs)
    
    def createOutputStep(self):
        runCreateOutputStep(self)

    #--------------------------- INFO functions -------------------------------------------- 
    
    def _validate(self):
        errors = []
        if self.doCTFCorrection and not self.doAutoCTFGroup and not exists(self.setOfDefocus.get()):
            errors.append("Error: for non-automated ctf grouping, please provide a docfile!")
        if self.numberOfMpi<=1:
            errors.append("The number of MPI processes has to be larger than 1")
        
        xDimImg = self.inputParticles.get().getXDim()
        xDimVol, _, _ = self.input3DReferences.get().getDim()
        if xDimImg != xDimVol:
            errors.append("The dimensions of the volume(s) and particles must be equal!!!!")
        return errors
    
    def _citations(self):
        cites = []
        return cites
    
    def _summary(self):
        summary = []
        return summary
    
    def _methods(self):
        return self._summary()  # summary is quite explicit and serve as methods
    
    #--------------------------- UTILS functions --------------------------------------------
    
    def allIters(self):
        """ Iterate over all iterations. """
        for i in range(1, self.numberOfIterations.get()+1):
            yield i
            
    def allRefs(self):
        """ Iterate over all references. """
        for i in range(1, self.numberOfReferences+1):
            yield i
            
    def allCtfGroups(self):
        """ Iterate over all CTF groups. """
        for i in range(1, self.numberOfCtfGroups.get() + 1):
            yield i
            
    def itersFloatValues(self, attributeName, firstValue=-1):
        """ Take the string of a given attribute and
        create a list of floats that will be used by 
        the iteratioins. An special first value will be
        added to the list for iteration 0.
        """
        valuesStr = self.getAttributeValue(attributeName)
        if valuesStr is None:
            raise Exception('None value for attribute: %s' % attributeName)
        return [firstValue] + getFloatListFromValues(valuesStr, length=self.numberOfIterations.get())
    
    def itersBoolValues(self, attributeName, firstValue=False):
        """ Take the string of a given attribute and
        create a list of booleans that will be used by 
        the iteratioins. An special first value will be
        added to the list for iteration 0.
        """
        valuesStr = self.getAttributeValue(attributeName)
        if valuesStr is None:
            raise Exception('None value for attribute: %s' % attributeName)
        return [firstValue] + getBoolListFromValues(valuesStr, length=self.numberOfIterations.get())
        
    def itersStringValues(self, attributeName, firstValue='c1'):
        """ Take the string of a given attribute and
        create a list of strings that will be used by 
        the iteratioins. An special first value will be
        added to the list for iteration 0.
        """
        valuesStr = self.getAttributeValue(attributeName)
        if valuesStr is None:
            raise Exception('None value for attribute: %s' % attributeName)
        return [firstValue] + getStringListFromValues(valuesStr, length=self.numberOfIterations.get())
        
    def _getBlockFileName(self, blockName, blockNumber, filename, length=None):
        l = length or self.FILENAMENUMBERLENGTH
        
        return blockName + str(blockNumber).zfill(l) + '@' + filename
    
    def _getExpImagesFileName(self, filename):
        return self.blockWithAllExpImages + '@' + filename
    
    def _getRefBlockFileName(self, ctfBlName, ctfBlNumber, refBlName, refBlNumber, filename, length=None):
        l = length or self.FILENAMENUMBERLENGTH
        
        return ctfBlName + str(ctfBlNumber).zfill(l) + '_' + refBlName + str(refBlNumber).zfill(l) + '@' + filename

    def _getFourierMaxFrequencyOfInterest(self, iterN, refN):
        """ Read the corresponding resolution metadata and return the
        desired resolution.
        """
        md = xmipp.MetaData(self._getFileName('resolutionXmdMax', iter=iterN, ref=refN))
        return md.getValue(xmipp.MDL_RESOLUTION_FREQREAL, md.firstObject())
    
    def calculateDeviationsStep(self, it):
        """ Calculate both angles and shifts devitations for all iterations
        """
    
        SL = xmipp.SymList()
        mdIter = xmipp.MetaData()
        #for it in self.allIters():
        mdIter.clear()
        SL.readSymmetryFile(self._symmetry[it])
        md1 = xmipp.MetaData(self.docFileInputAngles[it])
        md2 = xmipp.MetaData(self.docFileInputAngles[it-1])
        #ignore disabled,
        md1.removeDisabled()
        md2.removeDisabled()

        #first metadata file may not have shiftx and shifty
        if not md2.containsLabel(xmipp.MDL_SHIFT_X):
            md2.addLabel(xmipp.MDL_SHIFT_X)
            md2.addLabel(xmipp.MDL_SHIFT_Y)
            md2.fillConstant(xmipp.MDL_SHIFT_X,0.)
            md2.fillConstant(xmipp.MDL_SHIFT_Y,0.)
        oldLabels=[xmipp.MDL_ANGLE_ROT,
                   xmipp.MDL_ANGLE_TILT,
                   xmipp.MDL_ANGLE_PSI,
                   xmipp.MDL_SHIFT_X,
                   xmipp.MDL_SHIFT_Y]
        newLabels=[xmipp.MDL_ANGLE_ROT2,
                   xmipp.MDL_ANGLE_TILT2,
                   xmipp.MDL_ANGLE_PSI2,
                   xmipp.MDL_SHIFT_X2,
                   xmipp.MDL_SHIFT_Y2]
        md2.renameColumn(oldLabels,newLabels)
        md2.addLabel(xmipp.MDL_SHIFT_X_DIFF)
        md2.addLabel(xmipp.MDL_SHIFT_Y_DIFF)
        md2.addLabel(xmipp.MDL_SHIFT_DIFF)
        mdIter.join1(md1, md2, xmipp.MDL_IMAGE, xmipp.INNER_JOIN)
        SL.computeDistance(mdIter,False,False,False)
        xmipp.activateMathExtensions()
        #operate in sqlite
        shiftXLabel     = xmipp.label2Str(xmipp.MDL_SHIFT_X)
        shiftX2Label    = xmipp.label2Str(xmipp.MDL_SHIFT_X2)
        shiftXDiff      = xmipp.label2Str(xmipp.MDL_SHIFT_X_DIFF)
        shiftYLabel     = xmipp.label2Str(xmipp.MDL_SHIFT_Y)
        shiftY2Label    = xmipp.label2Str(xmipp.MDL_SHIFT_Y2)
        shiftYDiff      = xmipp.label2Str(xmipp.MDL_SHIFT_Y_DIFF)
        shiftDiff       = xmipp.label2Str(xmipp.MDL_SHIFT_DIFF)
        #timeStr = str(dtBegin)
        operateString   =       shiftXDiff+"="+shiftXLabel+"-"+shiftX2Label
        operateString  += "," + shiftYDiff+"="+shiftYLabel+"-"+shiftY2Label
        mdIter.operate(operateString)
        operateString  =  shiftDiff+"=sqrt(" \
                          +shiftXDiff+"*"+shiftXDiff+"+" \
                          +shiftYDiff+"*"+shiftYDiff+");"
        mdIter.operate(operateString)
        iterFile = self._mdDevitationsFn(it)
        mdIter.write(iterFile,xmipp.MD_APPEND)

        self._setLastIter(it)

    
    def _mdDevitationsFn(self, it):
        mdFn = self._getPath('deviations.xmd')
        return "iter_%03d@" % it + mdFn

    def _setLastIter(self, iterN):
        self._lastIter.set(iterN)
        self._store(self._lastIter)

    def getLastIter(self):
        return self._lastIter.get()
    
    def _createItemMatrix(self, item, row):
        from pyworkflow.em.packages.xmipp3.convert import createItemMatrix
        import pyworkflow.em as em
        
        createItemMatrix(item, row, align=em.ALIGN_PROJ)
class ProtPKPDAbsorptionRate(ProtPKPDFitBase):
    """ Estimation of the absorption rate for a non-intravenous route. The estimation is performed after estimating
        the elimination rate. The experiment is determined by the\n
        Protocol created by http://www.kinestatpharma.com\n.
        See the theory at http://www.pharmpress.com/files/docs/Basic%20Pharmacokinetics%20sample.pdf"""
    _label = 'absorption rate'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineParams(self, form):
        form.addSection('Input')
        form.addParam('inputExperiment', params.PointerParam, label="Input experiment",
                      pointerClass='PKPDExperiment',
                      help='Select an experiment with samples')
        form.addParam('protElimination', params.PointerParam, label="Elimination rate",
                      pointerClass='ProtPKPDEliminationRate',
                      help='Select an execution of a protocol estimating the elimination rate')
        form.addParam("absorptionF", params.FloatParam, label="Absorption fraction", default=1,
                      help="Between 0 (=no absorption) and 1 (=full absorption)")

        form.addParam('bounds', params.StringParam, label="Ka, V, [tlag] bounds", default="", expertLevel=LEVEL_ADVANCED,
                      help='Bounds for Ka (absorption constant), V (distribution volume) and optionally tlag.\nExample 1: (0,1e-3);(30,50);(0.1,0.5) -> Ka in (0,1e-3), V in (30,50) and tlag in (0.1,0.5)\n')
        form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval", default=95, expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        form.addParam('includeTlag', params.BooleanParam, label="Include tlag", default=True, expertLevel=LEVEL_ADVANCED,
                      help='Calculate the delay between administration and absorption')
        self.fitType=Integer() # Logarithmic fit
        self.fitType.set(1)

    def getListOfFormDependencies(self):
        return [self.protElimination.get().getObjId(), self.absorptionF.get(), self.bounds.get(), self.confidenceInterval.get()]

    #--------------------------- STEPS functions --------------------------------------------
    def setupFromFormParameters(self):
        self.eliminationFitting = self.readFitting(self.protElimination.get().outputFitting.fnFitting.get())
        self.model.F = self.absorptionF.get()

    def getXYvars(self):
        self.varNameX = self.protElimination.get().predictor.get()
        self.varNameY = self.protElimination.get().predicted.get()

    def createModel(self):
        return PKPDSimpleEVModel(self.includeTlag.get())

    def prepareForSampleAnalysis(self, sampleName):
        sample = self.experiment.samples[sampleName]
        sampleFit = self.eliminationFitting.getSampleFit(sampleName)
        sample.interpretDose()
        self.model.D = sample.getDoseAt(0.0)
        self.model.Dunits = sample.getDoseUnits()
        if sampleFit == None:
            print("  Cannot process %s because its elimination rate cannot be found\n\n"%sampleName)
            return False
        self.model.C0 = sampleFit.parameters[0]
        self.model.C0units = self.eliminationFitting.modelParameterUnits[0]
        self.model.Ke = sampleFit.parameters[1]
        self.model.KeUnits = self.eliminationFitting.modelParameterUnits[1]
        print("Concentration at t=0 = %f [%s]"%(self.model.C0,strUnit(self.model.C0units)))
        print("Elimination rate = %f [%s]"%(self.model.Ke,strUnit(self.model.KeUnits)))

        self.experiment.addParameterToSample(sampleName, "Ke", self.model.KeUnits, "Automatically estimated elimination rate", self.model.Ke)

        return True

    def postSampleAnalysis(self, sampleName):
        xunits = self.experiment.getVarUnits(self.varNameX)
        Cunits = self.experiment.getVarUnits(self.varNameY)
        Ka = self.model.parameters[0]
        Ke = self.model.Ke
        tmax = math.log(Ka/Ke)/(Ka-Ke)

        self.experiment.addParameterToSample(sampleName, "tmax", xunits, "Estimated time of the Maximum of the non-iv peak", tmax)
        Cmax = self.model.forwardModel(self.model.parameters,x=[np.atleast_1d(np.array(tmax))])[0][0]
        self.experiment.addParameterToSample(sampleName, "Cmax", Cunits, "Estimated concentration of the Maximum of the non-iv peak", Cmax)
        print("tmax = %f [%s]"%(tmax,strUnit(xunits)))
        print("Cmax = %f [%s]"%(Cmax,strUnit(Cunits)))

    #--------------------------- INFO functions --------------------------------------------
    def _summary(self):
        msg=[]
        msg.append("Non-compartmental analysis for the observations of the variable %s"%self.protElimination.get().predicted.get())
        return msg

    def _warnings(self):
        msg = []
        experiment = self.readExperiment(self.getInputExperiment().fnPKPD,show=False)
        incorrectList = experiment.getNonBolusDoses()
        if len(incorrectList)!=0:
            msg.append("This protocol is meant only for bolus regimens. Check the doses for %s"%(','.join(incorrectList)))

        return msg
Beispiel #31
0
class ProtPKPDAbsorptionRate(ProtPKPDFitBase):
    """ Estimation of the absorption rate for a non-intravenous route. The estimation is performed after estimating
        the elimination rate. The experiment is determined by the\n
        Protocol created by http://www.kinestatpharma.com\n.
        See the theory at http://www.pharmpress.com/files/docs/Basic%20Pharmacokinetics%20sample.pdf"""
    _label = 'absorption rate'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineParams(self, form):
        form.addSection('Input')
        form.addParam('inputExperiment',
                      params.PointerParam,
                      label="Input experiment",
                      pointerClass='PKPDExperiment',
                      help='Select an experiment with samples')
        form.addParam(
            'protElimination',
            params.PointerParam,
            label="Elimination rate",
            pointerClass='ProtPKPDEliminationRate',
            help=
            'Select an execution of a protocol estimating the elimination rate'
        )
        form.addParam(
            "absorptionF",
            params.FloatParam,
            label="Absorption fraction",
            default=1,
            help="Between 0 (=no absorption) and 1 (=full absorption)")

        form.addParam(
            'bounds',
            params.StringParam,
            label="Ka, V, [tlag] bounds",
            default="",
            expertLevel=LEVEL_ADVANCED,
            help=
            'Bounds for Ka (absorption constant), V (distribution volume) and optionally tlag.\nExample 1: (0,1e-3);(30,50);(0.1,0.5) -> Ka in (0,1e-3), V in (30,50) and tlag in (0.1,0.5)\n'
        )
        form.addParam('confidenceInterval',
                      params.FloatParam,
                      label="Confidence interval",
                      default=95,
                      expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        form.addParam(
            'includeTlag',
            params.BooleanParam,
            label="Include tlag",
            default=True,
            expertLevel=LEVEL_ADVANCED,
            help='Calculate the delay between administration and absorption')
        self.fitType = Integer()  # Logarithmic fit
        self.fitType.set(1)

    def getListOfFormDependencies(self):
        return [
            self.protElimination.get().getObjId(),
            self.absorptionF.get(),
            self.bounds.get(),
            self.confidenceInterval.get()
        ]

    #--------------------------- STEPS functions --------------------------------------------
    def setupFromFormParameters(self):
        self.eliminationFitting = self.readFitting(
            self.protElimination.get().outputFitting.fnFitting.get())
        self.model.F = self.absorptionF.get()

    def getXYvars(self):
        self.varNameX = self.protElimination.get().predictor.get()
        self.varNameY = self.protElimination.get().predicted.get()

    def createModel(self):
        return PKPDSimpleEVModel(self.includeTlag.get())

    def prepareForSampleAnalysis(self, sampleName):
        sample = self.experiment.samples[sampleName]
        sampleFit = self.eliminationFitting.getSampleFit(sampleName)
        sample.interpretDose()
        self.model.D = sample.getDoseAt(0.0)
        self.model.Dunits = sample.getDoseUnits()
        if sampleFit == None:
            print(
                "  Cannot process %s because its elimination rate cannot be found\n\n"
                % sampleName)
            return False
        self.model.C0 = sampleFit.parameters[0]
        self.model.C0units = self.eliminationFitting.modelParameterUnits[0]
        self.model.Ke = sampleFit.parameters[1]
        self.model.KeUnits = self.eliminationFitting.modelParameterUnits[1]
        print("Concentration at t=0 = %f [%s]" %
              (self.model.C0, strUnit(self.model.C0units)))
        print("Elimination rate = %f [%s]" %
              (self.model.Ke, strUnit(self.model.KeUnits)))

        self.experiment.addParameterToSample(
            sampleName, "Ke", self.model.KeUnits,
            "Automatically estimated elimination rate", self.model.Ke)

        return True

    def postSampleAnalysis(self, sampleName):
        xunits = self.experiment.getVarUnits(self.varNameX)
        Cunits = self.experiment.getVarUnits(self.varNameY)
        Ka = self.model.parameters[0]
        Ke = self.model.Ke
        tmax = math.log(Ka / Ke) / (Ka - Ke)

        self.experiment.addParameterToSample(
            sampleName, "tmax", xunits,
            "Estimated time of the Maximum of the non-iv peak", tmax)
        Cmax = self.model.forwardModel(self.model.parameters,
                                       x=[np.atleast_1d(np.array(tmax))])[0][0]
        self.experiment.addParameterToSample(
            sampleName, "Cmax", Cunits,
            "Estimated concentration of the Maximum of the non-iv peak", Cmax)
        print("tmax = %f [%s]" % (tmax, strUnit(xunits)))
        print("Cmax = %f [%s]" % (Cmax, strUnit(Cunits)))

    #--------------------------- INFO functions --------------------------------------------
    def _summary(self):
        msg = []
        msg.append(
            "Non-compartmental analysis for the observations of the variable %s"
            % self.protElimination.get().predicted.get())
        return msg

    def _warnings(self):
        msg = []
        experiment = self.readExperiment(self.getInputExperiment().fnPKPD,
                                         show=False)
        incorrectList = experiment.getNonBolusDoses()
        if len(incorrectList) != 0:
            msg.append(
                "This protocol is meant only for bolus regimens. Check the doses for %s"
                % (','.join(incorrectList)))

        return msg
Beispiel #32
0
    def __init__(self, **args):
        EMObject.__init__(self, **args)
        self.type = Integer()
        self.type.set(self.INTERP)

        self.lungParams = None
    def operateStep(self):

        outputSet = self.inputSet.get().create(self._getPath())

        if self.operation.get() == 0:
            # Filter columns
            referenceValue = self.filterValue.get()
            value = self.inputSet.get().getFirstItem().getAttributeValue(
                self.filterColumn.get())

            if isinstance(value, float):
                referenceValue = float(referenceValue)
            elif isinstance(value, int):
                referenceValue = int(referenceValue)

            filterOp = self.filterOp.get()
            if filterOp == 6:
                referenceValue2 = self.filterValue2.get()

                if isinstance(value, float):
                    referenceValue2 = float(referenceValue2)
                elif isinstance(value, int):
                    referenceValue2 = int(referenceValue2)

            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())
                if isinstance(value, Float):
                    value = float(value)
                elif isinstance(value, Integer):
                    value = int(value)

                add = False
                if filterOp == 0:  # ==
                    add = value == referenceValue
                elif filterOp == 1:  # >
                    add = value > referenceValue
                elif filterOp == 2:  # >=
                    add = value > referenceValue
                elif filterOp == 3:  # <
                    add = value < referenceValue
                elif filterOp == 4:  # <=
                    add = value <= referenceValue
                elif filterOp == 5:  # !=
                    add = value != referenceValue
                elif filterOp == 6:  # between
                    add = (value <= referenceValue
                           and value >= referenceValue2)
                elif filterOp == 7:  #startswith
                    add = value.startswith(referenceValue)
                elif filterOp == 8:  # endswith
                    add = value.endswith(referenceValue)
                elif filterOp == 9:  # contains
                    add = referenceValue in value
                elif filterOp == 10:  # does not startswith
                    add = not (value.startswith(referenceValue))
                elif filterOp == 11:  # does not endswith
                    add = not (value.endswith(referenceValue))
                elif filterOp == 12:  # does not contains
                    add = not (referenceValue in value)
                if add:
                    newEntry = self.inputSet.get().ITEM_TYPE()
                    newEntry.copy(oldEntry)
                    outputSet.append(newEntry)

        elif self.operation.get() == 1:
            # Keep columns
            keepList = [x.strip() for x in self.keepColumns.get().split()]

            ignoreList = []
            for name, _ in self.inputSet.get().getFirstItem().getAttributes():
                if not name in keepList:
                    ignoreList.append(name)
            for oldEntry in self.inputSet.get():
                newEntry = self.inputSet.get().ITEM_TYPE()
                newEntry.copy(oldEntry, ignoreAttrs=ignoreList)
                outputSet.append(newEntry)

        elif self.operation.get() == 2:
            # Unique
            found = {}
            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())
                if not value in found:
                    found[value] = True
                    newEntry = self.inputSet.get().ITEM_TYPE()
                    newEntry.copy(oldEntry)
                    outputSet.append(newEntry)

        elif self.operation.get() >= 3 and self.operation.get() <= 6:
            # Top N, Bottom N,Top %, Bottom %
            V = []
            for entry in self.inputSet.get():
                V.append(entry.getAttributeValue(self.filterColumn.get()))
            V.sort()
            op = self.operation.get()
            if op == 3:
                threshold = V[-self.N.get()]
            elif op == 4:
                threshold = V[self.N.get() - 1]
            elif op == 5:
                threshold = V[-ceil(self.percentile.get() / 100 * len(V))]
            elif op == 6:
                threshold = V[ceil(self.percentile.get() / 100 * len(V)) - 1]

            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())
                if (op == 3 or op == 5) and value >= threshold:
                    newEntry = self.inputSet.get().ITEM_TYPE()
                    newEntry.copy(oldEntry)
                    outputSet.append(newEntry)
                elif (op == 4 or op == 6) and value <= threshold:
                    newEntry = self.inputSet.get().ITEM_TYPE()
                    newEntry.copy(oldEntry)
                    outputSet.append(newEntry)

        elif self.operation.get() == 7:
            # Count the number of entries that are the same
            count = {}
            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())
                if not value in count:
                    count[value] = 0
                count[value] += 1

            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())

                newEntry = self.inputSet.get().ITEM_TYPE()
                newEntry.copy(oldEntry)
                newEntry.count = Integer(count[value])
                outputSet.append(newEntry)

        elif self.operation.get() == 8:
            # Intersection between 2 Sets
            secondSet = {}
            for entry in self.secondSet.get():
                value = entry.getAttributeValue(self.filterColumn.get())
                if not value in secondSet:
                    secondSet[value] = True

            for oldEntry in self.inputSet.get():
                value = oldEntry.getAttributeValue(self.filterColumn.get())

                if value in secondSet:
                    newEntry = self.inputSet.get().ITEM_TYPE()
                    newEntry.copy(oldEntry)
                    outputSet.append(newEntry)

        elif self.operation.get() == 9:
            # Sort
            V = []
            newEntries = []
            for entry in self.inputSet.get():
                V.append(entry.getAttributeValue(self.filterColumn.get()))
                newEntry = self.inputSet.get().ITEM_TYPE()
                newEntry.copy(entry)
                newEntry.cleanObjId()
                newEntries.append(newEntry)
            if self.direction.get() == 0:
                idxSort = np.argsort(V)
            else:
                idxSort = np.argsort(-np.asarray(V))

            for idx in idxSort:
                outputSet.append(newEntries[idx])

        if len(outputSet) > 0:
            self._defineOutputs(output=outputSet)
            self._defineSourceRelation(self.inputSet, outputSet)
Beispiel #34
0
 def __init__(self, **args):
     EMObject.__init__(self, **args)
     self.type = Integer()
     self.type.set(self.SAT)
     self.substanceParams = None