File: buildblastdatabaseworker.cpp

package info (click to toggle)
bandage 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,684 kB
  • sloc: cpp: 45,359; sh: 491; makefile: 12
file content (102 lines) | stat: -rw-r--r-- 3,242 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Copyright 2017 Ryan Wick

//This file is part of Bandage

//Bandage is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//Bandage is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with Bandage.  If not, see <http://www.gnu.org/licenses/>.


#include "buildblastdatabaseworker.h"
#include <QProcess>
#include "../program/globals.h"
#include "../program/settings.h"
#include <QFile>
#include <QTextStream>
#include <QMapIterator>
#include "../graph/debruijnnode.h"
#include "../graph/assemblygraph.h"
#include "blastsearch.h"

BuildBlastDatabaseWorker::BuildBlastDatabaseWorker(QString makeblastdbCommand) :
    m_makeblastdbCommand(makeblastdbCommand)
{
}

void BuildBlastDatabaseWorker::buildBlastDatabase()
{
    g_blastSearch->m_cancelBuildBlastDatabase = false;

    QFile file(g_blastSearch->m_tempDirectory + "all_nodes.fasta");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);

    QMapIterator<QString, DeBruijnNode*> i(g_assemblyGraph->m_deBruijnGraphNodes);
    while (i.hasNext())
    {
        if (g_blastSearch->m_cancelBuildBlastDatabase)
        {
            emit finishedBuild("Build cancelled.");
            return;
        }

        i.next();
        DeBruijnNode * node = i.value();
        out << node->getFasta(true, false, false);
    }
    file.close();

    // Make sure the graph has sequences to BLAST.
    bool atLeastOneSequence = false;
    QMapIterator<QString, DeBruijnNode*> j(g_assemblyGraph->m_deBruijnGraphNodes);
    while (j.hasNext())
    {
        j.next();
        DeBruijnNode * node = j.value();
        if (!node->sequenceIsMissing())
        {
            atLeastOneSequence = true;
            break;
        }
    }
    if (!atLeastOneSequence)
    {
        m_error = "Cannot build the BLAST database as this graph contains no sequences";
        emit finishedBuild(m_error);
        return;
    }

    QStringList makeblastdbArguments = { "-in", g_blastSearch->m_tempDirectory + "all_nodes.fasta", "-dbtype", "nucl" };
    g_blastSearch->m_makeblastdb = new QProcess();
    g_blastSearch->m_makeblastdb->start(m_makeblastdbCommand, makeblastdbArguments);

    bool finished = g_blastSearch->m_makeblastdb->waitForFinished(-1);

    if (g_blastSearch->m_makeblastdb->exitCode() != 0 || !finished)
    {
        m_error = "There was a problem building the BLAST database";
        QString stdErr = g_blastSearch->m_makeblastdb->readAllStandardError();
        if (stdErr.length() > 0)
            m_error += ":\n\n" + stdErr;
        else
            m_error += ".";
    }
    else if (g_blastSearch->m_cancelBuildBlastDatabase)
        m_error = "Build cancelled.";
    else
        m_error = "";

    emit finishedBuild(m_error);

    g_blastSearch->m_makeblastdb->deleteLater();
    g_blastSearch->m_makeblastdb = 0;
}