Esempio n. 1
0
void SandCMapABF::CalculateForces( int stepNum, double *masses, double *positions, double *forces )
{
    /* Change the masses and positions array to the STL vectors */
    vector<double> vMasses   (masses   , masses   +_nAtoms);
    vector<double> vPositions(positions, positions+_hdim  );

    /* Compute the CV value of the current configuration and its related Jacobian */
    try
    {
        _bDistance ? _sandcv->CalculateCVdist(vPositions) : _sandcv->CalculateCV(vPositions);

        /* Calculate the Abf forces and return it */
        vector<double> forceABF( _abf->CalculateForce( stepNum, vMasses, vPositions, _sandcv->GetValue(), _sandcv->GetJacobian() ) );

        /* Copy the calculated forces to pass to the MD engine */
        copy( forceABF.begin(), forceABF.end(), forces );
    }
    catch (const runtime_error &e)
    {
        fill( forces, forces+_hdim, 0.0 );
        cout<<"NO FORCE is applied because "<<e.what()<<endl;
    }

}
/**
 * @brief Run the analysis.
 */
void CNAnalysisMethodMosaicism::run()
{
    Verbose::out(1, "CNAnalysisMethodMosaicism::run(...) start");
    isSetup();
        determineLocalProbeSets();

    m_vSegments.deleteAll();
    vector<int> vChromosomes = getChromosomes(getProbeSets());
    int iLastChromosome = vChromosomes[vChromosomes.size() - 1];
    Verbose::progressBegin(1,"CNAnalysisMethodMosaicism::run(...) ", iLastChromosome, 1, iLastChromosome);

    for (int i = 0; (i < vChromosomes.size()); i++)
    {
        int iChromosome = vChromosomes[i];
        if (iChromosome == 255) {continue;}
        if (iChromosome == m_iYChromosome && !m_bRunYChromosome) {
          continue;
        }
        int iProbeSetCount = getProbeSetCount(iChromosome, getProbeSets());
        if (iProbeSetCount == 0) continue; // probably 23 or Y
        Verbose::progressStep(1);

        // debug: for later use
        m_current_experiment=getExperiment()->getExperimentName();
        m_current_chr=ToStr(vChromosomes[i]);

        std::vector<int> vPositions(iProbeSetCount);
        double* pLog2Ratios = new double[iProbeSetCount];
        int iStartChromosome = getChrBounds(iChromosome, getProbeSets()).first;
        m_iStartIndex = 0;
        m_iEndIndex = (iProbeSetCount - 1);
        CNProbeSetArray* psets = getProbeSets();
        for (int j = 0; (j < iProbeSetCount); j++)
        {
            vPositions[j] = psets->at(iStartChromosome + j)->getPosition();
            pLog2Ratios[j] = psets->at(iStartChromosome + j)->getLog2Ratio();
        }
        double* pRunMean = new double[iProbeSetCount];
        int iMarkerBandwidth = Min(iProbeSetCount, m_iMarkerBandwidth);
        runmean(pLog2Ratios, pRunMean, &iProbeSetCount, &iMarkerBandwidth);

        // debug - this has been ruled out.
#ifdef MOSAICISM_DEBUG_MEANS
        writeRunningMeans(m_current_experiment+".chr"+m_current_chr+".means.tsv",
                          iProbeSetCount,
                          pLog2Ratios,
                          pRunMean);
#endif

        CNSegmentArray vSegments;
        newSegments((unsigned char)iChromosome, pRunMean, iProbeSetCount, vPositions, vSegments);
        // dont need this anymore.
        delete[] pRunMean;

        // debug
#ifdef MOSAICISM_DEBUG
        vSegments.writeToTsv(m_current_experiment+"-chr"+m_current_chr+".newseg1.tsv");
#endif
        //
        cleanSegments(vSegments);

        // Set marker count and median marker distance.
        for (int iSegmentIndex = 0; (iSegmentIndex < vSegments.getCount()); iSegmentIndex++)
        {
            CNSegment* p = vSegments.getAt(iSegmentIndex);
            p->setStartPosition(vPositions[p->getStartIndex()]);
            p->setEndPosition(vPositions[p->getEndIndex()]);
            calculateSegmentConfidence(pLog2Ratios, p);
            int iMarkerCount = 0;
            for (unsigned int iProbeSetIndex = 0; (iProbeSetIndex < vPositions.size()); iProbeSetIndex++)
            {
                if ((vPositions[iProbeSetIndex] >= p->getStartPosition()) && (vPositions[iProbeSetIndex] <= p->getEndPosition()))
                {
                    iMarkerCount++;
                }
            }
            if (iMarkerCount < 2)
            {
                p->setMarkerCount(iMarkerCount);
                p->setMeanMarkerDistance(0);
            }
            else
            {
                AffxMultiDimensionalArray<int> vMarkerDistances(iMarkerCount - 1);
                int iIndex = 0;
                for (unsigned int iProbeSetIndex = 1; (iProbeSetIndex < vPositions.size()); iProbeSetIndex++)
                {
                    int iPrevPosition = vPositions[iProbeSetIndex - 1];
                    int iNextPosition = vPositions[iProbeSetIndex];
                    if ((iPrevPosition >= p->getStartPosition()) && (iNextPosition <= p->getEndPosition()))
                    {
                        getProbeSets()->getAt(iStartChromosome + iProbeSetIndex - 1)->setMosaicismMixture(p->getMixtureAsDouble());
                        getProbeSets()->getAt(iStartChromosome + iProbeSetIndex)->setMosaicismMixture(p->getMixtureAsDouble());
                        vMarkerDistances.set(iIndex, (iNextPosition - iPrevPosition));
                        iIndex++;
                    }
                }
                p->setMarkerCount(iMarkerCount);
                p->setMeanMarkerDistance(vMarkerDistances.mean());
            }
            // transfer of ownership from vSegments to m_vSegments
            m_vSegments.add(p);
        }

#ifdef MOSAICISM_DEBUG
        vSegments.writeToTsv(m_current_experiment+"-chr"+m_current_chr+".seg2.tsv");
#endif
        // why "nullAll()" instead of "deleteAll()"?
        // because the pointers were copied to m_vSegments above and
        // AffxArray will delete the pointers in vSegments when it goes out of scope.
        vSegments.nullAll();
        delete[] pLog2Ratios;
    }
    Verbose::progressEnd(1, "Done");
    Verbose::out(1, "CNAnalysisMethodMosaicism::run(...) end");
}