File: querypaths.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 (319 lines) | stat: -rw-r--r-- 11,343 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//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 "querypaths.h"
#include "commoncommandlinefunctions.h"
#include "../program/settings.h"
#include "../graph/assemblygraph.h"
#include "../blast/blastsearch.h"
#include <QDateTime>

int bandageQueryPaths(QStringList arguments)
{
    QTextStream out(stdout);
    QTextStream err(stderr);

    if (checkForHelp(arguments))
    {
        printQueryPathsUsage(&out, false);
        return 0;
    }

    if (checkForHelpAll(arguments))
    {
        printQueryPathsUsage(&out, true);
        return 0;
    }

    if (arguments.size() < 3)
    {
        printQueryPathsUsage(&err, false);
        return 1;
    }

    QString graphFilename = arguments.at(0);
    arguments.pop_front();
    if (!checkIfFileExists(graphFilename))
    {
        outputText("Bandage error: " + graphFilename, &err);
        return 1;
    }

    QString queriesFilename = arguments.at(0);
    arguments.pop_front();
    if (!checkIfFileExists(queriesFilename))
    {
        outputText("Bandage error: " + queriesFilename, &err);
        return 1;
    }
    g_settings->blastQueryFilename = queriesFilename;

    //Ensure that the --query option isn't used, as that would overwrite the
    //queries file that is a positional argument.
    if (isOptionPresent("--query", &arguments))
    {
        err << "Bandage error: the --query option cannot be used with Bandage querypaths." << Qt::endl;
        return 1;
    }

    QString outputPrefix = arguments.at(0);
    QString tableFilename = outputPrefix + ".tsv";
    QString pathFastaFilename = outputPrefix + "_paths.fasta";
    QString hitsFastaFilename = outputPrefix + "_hits.fasta";
    arguments.pop_front();

    QString error = checkForInvalidQueryPathsOptions(arguments);
    if (error.length() > 0)
    {
        outputText("Bandage error: " + error, &err);
        return 1;
    }

    bool pathFasta = false;
    bool hitsFasta = false;
    parseQueryPathsOptions(arguments, &pathFasta, &hitsFasta);

    //Check to make sure the output files don't already exist.
    QFile tableFile(tableFilename);
    QFile pathsFile(pathFastaFilename);
    QFile hitsFile(hitsFastaFilename);
    if (tableFile.exists())
    {
        outputText("Bandage error: " + tableFilename + " already exists.", &err);
        return 1;
    }
    if (pathFasta && pathsFile.exists())
    {
        outputText("Bandage error: " + pathFastaFilename + " already exists.", &err);
        return 1;
    }
    if (hitsFasta && hitsFile.exists())
    {
        outputText("Bandage error: " + hitsFastaFilename + " already exists.", &err);
        return 1;
    }

    QDateTime startTime = QDateTime::currentDateTime();

    out << Qt::endl << "(" << QDateTime::currentDateTime().toString("dd MMM yyyy hh:mm:ss") << ") Loading graph...        " << Qt::flush;

    bool loadSuccess = g_assemblyGraph->loadGraphFromFile(graphFilename);
    if (!loadSuccess)
        return 1;
    out << "done" << Qt::endl;

    if (!createBlastTempDirectory())
    {
        err << "Error creating temporary directory for BLAST files" << Qt::endl;
        return 1;
    }

    out << "(" << QDateTime::currentDateTime().toString("dd MMM yyyy hh:mm:ss") << ") Running BLAST search... " << Qt::flush;
    QString blastError = g_blastSearch->doAutoBlastSearch();
    if (blastError != "")
    {
        err << Qt::endl << blastError << Qt::endl;
        return 1;
    }
    out << "done" << Qt::endl;
    out << "(" << QDateTime::currentDateTime().toString("dd MMM yyyy hh:mm:ss") << ") Saving results...       " << Qt::flush;

    //Create the table file.
    tableFile.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream tableOut(&tableFile);

    //Write the TSV header line.
    tableOut << "Query\t"
                "Path\t"
                "Length\t"
                "Query covered by path\t"
                "Query covered by hits\t"
                "Mean hit identity\t"
                "Total hit mismatches\t"
                "Total hit gap opens\t"
                "Relative length\t"
                "Length discrepancy\t"
                "E-value product\t";

    //If the user asked for a separate path sequence file, then the last column
    //will be a reference to that sequence ID.  If not, the sequence will go in
    //the table.
    if (pathFasta)
        tableOut << "Sequence ID\n";
    else
        tableOut << "Sequence\n";

    //If a path sequence FASTA file is used, these will store the sequences
    //that will go there.
    QList<QString> pathSequenceIDs;
    QList<QByteArray> pathSequences;

    //If a hits sequence FASTA file is used, these will store the sequences
    //that will go there.
    QList<QString> hitSequenceIDs;
    QList<QByteArray> hitSequences;

    for (size_t i = 0; i < g_blastSearch->m_blastQueries.m_queries.size(); ++i)
    {
        BlastQuery * query = g_blastSearch->m_blastQueries.m_queries[i];
        QList<BlastQueryPath> queryPaths = query->getPaths();

        for (int j = 0; j < queryPaths.size(); ++j)
        {
            BlastQueryPath queryPath = queryPaths[j];
            Path path = queryPath.getPath();

            tableOut << query->getName() << "\t";
            tableOut << path.getString(true) << "\t";
            tableOut << QString::number(path.getLength()) << "\t";
            tableOut << QString::number(100.0 * queryPath.getPathQueryCoverage()) << "%\t";
            tableOut << QString::number(100.0 * queryPath.getHitsQueryCoverage()) << "%\t";
            tableOut << QString::number(queryPath.getMeanHitPercIdentity()) << "%\t";
            tableOut << QString::number(queryPath.getTotalHitMismatches()) << "\t";
            tableOut << QString::number(queryPath.getTotalHitGapOpens()) << "\t";
            tableOut << QString::number(100.0 * queryPath.getRelativePathLength()) << "%\t";
            tableOut << queryPath.getAbsolutePathLengthDifferenceString(false) << "\t";
            tableOut << queryPath.getEvalueProduct().asString(false) << "\t";

            //If we are using a separate file for the path sequences, save the
            //sequence along with its ID to save later, and store the ID here.
            //Otherwise, just include the sequence in this table.
            QByteArray sequence = path.getPathSequence();
            QString pathSequenceID = query->getName() + "_" + QString::number(j+1);
            if (pathFasta)
            {
                pathSequenceIDs.push_back(pathSequenceID);
                pathSequences.push_back(sequence);
                tableOut << pathSequenceID << "\n";
            }
            else
                tableOut << sequence << "\n";

            //If we are also saving the hit sequences, save the hit sequence
            //along with its ID to save later.
            if (hitsFasta)
            {
                QList<BlastHit *> hits = queryPath.getHits();
                for (int k = 0; k < hits.size(); ++k)
                {
                    BlastHit * hit = hits[k];
                    QString hitSequenceID = pathSequenceID + "_" + QString::number(k+1);
                    QByteArray hitSequence = hit->getNodeSequence();
                    hitSequenceIDs.push_back(hitSequenceID);
                    hitSequences.push_back(hitSequence);
                }
            }
        }
    }

    //Write the path sequence FASTA file, if appropriate.
    if (pathFasta)
    {
        pathsFile.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream pathsOut(&pathsFile);

        for (int i = 0; i < pathSequenceIDs.size(); ++i)
        {
            pathsOut << ">" + pathSequenceIDs[i] + "\n";
            pathsOut << AssemblyGraph::addNewlinesToSequence(pathSequences[i]);
        }
    }

    //Write the hits sequence FASTA file, if appropriate.
    if (hitsFasta)
    {
        hitsFile.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream hitsOut(&hitsFile);

        for (int i = 0; i < hitSequenceIDs.size(); ++i)
        {
            hitsOut << ">" << hitSequenceIDs[i] << "\n";
            hitsOut << AssemblyGraph::addNewlinesToSequence(hitSequences[i]);
        }
    }

    out << "done" << Qt::endl;

    out << Qt::endl << "Results:      " + tableFilename << Qt::endl;
    if (pathFasta)
        out << "              " + pathFastaFilename << Qt::endl;
    if (hitsFasta)
        out << "              " + hitsFastaFilename << Qt::endl;

    out << Qt::endl << "Summary: Total BLAST queries:           " << g_blastSearch->m_blastQueries.getQueryCount() << Qt::endl;
    out << "         Queries with found paths:      " << g_blastSearch->m_blastQueries.getQueryCountWithAtLeastOnePath() << Qt::endl;
    out << "         Total query paths:             " << g_blastSearch->m_blastQueries.getQueryPathCount() << Qt::endl;

    out << Qt::endl << "Elapsed time: " << getElapsedTime(startTime, QDateTime::currentDateTime()) << Qt::endl;

    deleteBlastTempDirectory();
    return 0;
}


void printQueryPathsUsage(QTextStream * out, bool all)
{
    QStringList text;

    text << "Bandage querypaths searches for queries in the graph using BLAST and outputs the results to a tab-delimited file.";
    text << "";
    text << "Usage:    Bandage querypaths <graph> <queries> <output_prefix> [options]";
    text << "";
    text << "Positional parameters:";
    text << "<graph>             A graph file of any type supported by Bandage";
    text << "<queries>           A FASTA file of one or more BLAST queries";
    text << "<output_prefix>     The output file prefix (used to create the '.tsv' output file, and possibly FASTA files as well, depending on options)";
    text << "";
    text << "Options:  --pathfasta         Put all query path sequences in a multi-FASTA file, not in the TSV file";
    text << "--hitsfasta         Produce a multi-FASTA file of all BLAST hits in the query paths";
    text << "";

    getCommonHelp(&text);
    if (all)
        getSettingsUsage(&text);
    getOnlineHelpMessage(&text);

    outputText(text, out);
}



QString checkForInvalidQueryPathsOptions(QStringList arguments)
{
    checkOptionWithoutValue("--pathfasta", &arguments);
    checkOptionWithoutValue("--hitsfasta", &arguments);

    QString error = checkForInvalidOrExcessSettings(&arguments);
    if (error.length() > 0) return error;

    return checkForInvalidOrExcessSettings(&arguments);
}



void parseQueryPathsOptions(QStringList arguments, bool * pathFasta,
                            bool * hitsFasta)
{
    int pathFastaIndex = arguments.indexOf("--pathfasta");
    *pathFasta = (pathFastaIndex > -1);

    int hitsFastaIndex = arguments.indexOf("--hitsfasta");
    *hitsFasta = (hitsFastaIndex > -1);

    parseSettings(arguments);
}