Ejemplo n.º 1
0
void ContigsPrinter::run()
{
    SortedReferenceMetadata::Contigs originalContigs;
    if (!originalSortedReferenceXml_.empty())
    {
        SortedReferenceMetadata inXml(reference::loadSortedReferenceXml(originalSortedReferenceXml_));
        originalContigs = inXml.getContigs();
    }

    SortedReferenceMetadata::Contigs outputContigs;

    std::ifstream is(genomeFile_.string().c_str());
    if (!is) {
        BOOST_THROW_EXCEPTION(isaac::common::IoException(errno, "Failed to open reference file " + genomeFile_.string()));
    }

    int64_t lastAcgtCount(0), lastBasesCount(0), lastContigByteStart(0), lineNumber(0), lastContigGenomicStart(0), streamPos(0);
    unsigned index = 0;
    std::string lastHeader, line;

    SortedReferenceMetadata outXml;
    isaac::common::MD5Sum md5Sum;
    // TODO: use the MultiFastaReader component to ensure consistency (particularly for the index)
    while (std::getline(is, line))
    {
        streamPos = is.tellg();
        ++lineNumber;
        if ('>' == line[0])
        {
            std::cerr << "header at:" << streamPos << "\n";
            if (!lastHeader.empty())
            {
                const std::string contigName = lastHeader.substr(1, lastHeader.find_first_of(" \t\r") - 1);
                SortedReferenceMetadata::Contigs::const_iterator originalContigIt =
                    std::find_if(originalContigs.begin(), originalContigs.end(), boost::bind(&SortedReferenceMetadata::Contig::name_, _1) == contigName);
                outXml.putContig(lastContigGenomicStart,
                                 contigName,
                                 genomeFile_,
                                 lastContigByteStart,
                                 streamPos - lastContigByteStart - (1 + line.length()), //byte length (assuming newline is one byte long)
                                 lastBasesCount, lastAcgtCount, index, index,
                                 (originalContigs.end() == originalContigIt ? "" : originalContigIt->bamSqAs_),
                                 (originalContigs.end() == originalContigIt ? "" : originalContigIt->bamSqUr_),
                                 isaac::common::MD5Sum::toHexString( md5Sum.getDigest().data, 16 ));

                ++index;
            }
            // Reset counters
            lastAcgtCount = 0;
            lastHeader = line;
            lastContigByteStart = streamPos;
            lastContigGenomicStart += lastBasesCount;
            lastBasesCount = 0;
            md5Sum.clear();
        }
        else
        {
            lastAcgtCount += std::count_if(line.begin(), line.end(), boost::bind(&oligo::getValue, _1) != oligo::INVALID_OLIGO);
            // ignore untranslated '\r'
            lastBasesCount += std::count_if(line.begin(), line.end(),
                                            [](char c){return c != '\r';});
//                                            boost::bind(&boost::ref<char>, _1) != '\r');
        
            // MD5 with format characters stripped out
            std::string lineStd = line;
            boost::to_upper(lineStd);
            lineStd.erase(std::remove_if(lineStd.begin(), lineStd.end(), &isspace), lineStd.end());  // isspace should remove ' ', '\n', '\r', '\t'
    
            md5Sum.update( lineStd.c_str(), lineStd.size() );
        }
    }
    if (!is.eof()) {
        BOOST_THROW_EXCEPTION(
                isaac::common::IoException(errno, "Failed while reading sequence."
                                             " Line:" + boost::lexical_cast<std::string>(lineNumber)
                                           + " Position:" + boost::lexical_cast<std::string>(is.tellg())
                                           + " Buffer:" + line + "\n"));
    }
    std::cerr << "end of stream at:" << streamPos << "\n";
    if (!lastHeader.empty())
    {
        const std::string contigName = lastHeader.substr(1, lastHeader.find_first_of(" \t\r") - 1);
        SortedReferenceMetadata::Contigs::const_iterator originalContigIt =
            std::find_if(originalContigs.begin(), originalContigs.end(), boost::bind(&SortedReferenceMetadata::Contig::name_, _1) == contigName);
        outXml.putContig(lastContigGenomicStart,
                         contigName,
                         genomeFile_,
                         lastContigByteStart,
                         streamPos - lastContigByteStart, //byte length
                         lastBasesCount, lastAcgtCount, index, index,
                         (originalContigs.end() == originalContigIt ? "" : originalContigIt->bamSqAs_),
                         (originalContigs.end() == originalContigIt ? "" : originalContigIt->bamSqUr_),
                         isaac::common::MD5Sum::toHexString( md5Sum.getDigest().data, 16 ));
        ++index;
    }

    saveSortedReferenceXml(std::cout, outXml);
}
Ejemplo n.º 2
0
//
// This stream buffer does not support seeking.
//
EXPORT_C TStreamPos MStreamBuf::DoSeekL(TMark,TStreamLocation,TInt)
	{
	Panic(EStreamCannotSeek);
	TStreamPos streamPos(-1);
	return streamPos;
	}
Ejemplo n.º 3
0
void Plots::changeOrder(QList<std::tuple<int, int> > orderedFilterInfo)
{
    if(orderedFilterInfo.empty())
    {
        qDebug() << "orderedFilterInfo is empty, do not reorder..";
        return;
    }

    qDebug() << "changeOrder: items = " << orderedFilterInfo.count();

    auto gridLayout = static_cast<QGridLayout*> (layout());
    auto rowsCount = gridLayout->rowCount();

    Q_ASSERT(m_plotsCount <= rowsCount);

    qDebug() << "plotsCount: " << m_plotsCount;

    QList <std::tuple<size_t, size_t, size_t>> currentOrderedPlotsInfo;
    QList <std::tuple<size_t, size_t, size_t>> expectedOrderedPlotsInfo;

    for(auto row = 0; row < m_plotsCount; ++row)
    {
        auto plotItem = gridLayout->itemAtPosition(row, 0);
        auto legendItem = gridLayout->itemAtPosition(row, 1);

        Q_ASSERT(plotItem);
        Q_ASSERT(legendItem);

        auto plot = qobject_cast<Plot*> (plotItem->widget());
        Q_ASSERT(plot);

        currentOrderedPlotsInfo.push_back(std::make_tuple(plot->group(), plot->type(), plot->streamPos()));
    }

    for(auto filterInfo : orderedFilterInfo)
    {
        for(auto plotInfo : currentOrderedPlotsInfo)
        {
            if(std::get<0>(plotInfo) == std::get<0>(filterInfo) && std::get<1>(plotInfo) == std::get<1>(filterInfo))
            {
                expectedOrderedPlotsInfo.push_back(plotInfo);
            }
        }
    }

    Q_ASSERT(currentOrderedPlotsInfo.length() == expectedOrderedPlotsInfo.length());
    if(currentOrderedPlotsInfo.length() != expectedOrderedPlotsInfo.length())
        return;

    for(auto i = 0; i < expectedOrderedPlotsInfo.length(); ++i)
    {
        qDebug() << "cg: " << std::get<0>(currentOrderedPlotsInfo[i])
                 << ", "
                 << "ct: " << std::get<1>(currentOrderedPlotsInfo[i])
                 << ", "
                 << "cp: " << std::get<2>(currentOrderedPlotsInfo[i])
                 << ", "
                 << "eg: " << std::get<0>(expectedOrderedPlotsInfo[i])
                 << ", "
                 << "et: " << std::get<1>(expectedOrderedPlotsInfo[i])
                 << ", "
                 << "ep: " << std::get<2>(expectedOrderedPlotsInfo[i]);
    }

    for(auto i = 0; i < expectedOrderedPlotsInfo.length(); ++i)
    {
        if(expectedOrderedPlotsInfo[i] != currentOrderedPlotsInfo[i])
        {
            // search current item which we should put at expected position
            for(auto j = 0; j < expectedOrderedPlotsInfo.length(); ++j)
            {
                if(expectedOrderedPlotsInfo[i] == currentOrderedPlotsInfo[j])
                {
                    qDebug() << "i: " << i << ", j: " << j;

                    auto plotWidget = gridLayout->itemAtPosition(j, 0)->widget();
                    auto legendWidget = gridLayout->itemAtPosition(j, 1)->widget();

                    {
                        auto plot = qobject_cast<Plot*> (plotWidget);
                        qDebug() << "jg: " << plot->group() << ", t: " << plot->type() << ", p: " << plot->streamPos() << ", ptr = " << plot;
                    }

                    auto swapPlotWidget = gridLayout->itemAtPosition(i, 0)->widget();
                    auto swapLegendWidget = gridLayout->itemAtPosition(i, 1)->widget();

                    {
                        auto plot = qobject_cast<Plot*> (swapPlotWidget);
                        qDebug() << "ig: " << plot->group() << ", t: " << plot->type() << ", p: " << plot->streamPos() << ", ptr = " << plot;
                    }

                    gridLayout->removeWidget(plotWidget);
                    gridLayout->removeWidget(legendWidget);

                    gridLayout->removeWidget(swapPlotWidget);
                    gridLayout->removeWidget(swapLegendWidget);

                    gridLayout->addWidget(plotWidget, i, 0);
                    gridLayout->addWidget(legendWidget, i, 1);

                    gridLayout->addWidget(swapPlotWidget, j, 0);
                    gridLayout->addWidget(swapLegendWidget, j, 1);

                    currentOrderedPlotsInfo[j] = currentOrderedPlotsInfo[i];
                    currentOrderedPlotsInfo[i] = expectedOrderedPlotsInfo[i];

                    break;
                }
            }
        }
    }

    Q_ASSERT(rowsCount == gridLayout->rowCount());

    for(auto row = 0; row < m_plotsCount; ++row)
    {
        auto plotItem = gridLayout->itemAtPosition(row, 0);
        auto legendItem = gridLayout->itemAtPosition(row, 1);

        Q_ASSERT(plotItem);
        Q_ASSERT(legendItem);

        auto plot = qobject_cast<Plot*> (plotItem->widget());
        Q_ASSERT(plot);

        Q_ASSERT(plot->group() == std::get<0>(expectedOrderedPlotsInfo[row]) &&
                 plot->type() == std::get<1>(expectedOrderedPlotsInfo[row]));
    }
}