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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
//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 "blastsearch.h"
#include "../graph/assemblygraph.h"
#include <QDir>
#include <QRegularExpression>
#include "buildblastdatabaseworker.h"
#include "runblastsearchworker.h"
#include "../program/settings.h"
#include <QApplication>
#include "../graph/debruijnnode.h"
#include "../program/memory.h"
#include <math.h>
BlastSearch::BlastSearch() :
m_blastQueries(), m_tempDirectory("bandage_temp/")
{
}
BlastSearch::~BlastSearch()
{
cleanUp();
}
void BlastSearch::clearBlastHits()
{
m_allHits.clear();
m_blastQueries.clearSearchResults();
m_blastOutput = "";
}
void BlastSearch::cleanUp()
{
clearBlastHits();
m_blastQueries.clearAllQueries();
emptyTempDirectory();
}
//This function uses the contents of m_blastOutput (the raw output from the
//BLAST search) to construct the BlastHit objects.
//It looks at the filters to possibly exclude hits which fail to meet user-
//defined thresholds.
void BlastSearch::buildHitsFromBlastOutput()
{
QStringList blastHitList = m_blastOutput.split("\n", Qt::SkipEmptyParts);
for (int i = 0; i < blastHitList.size(); ++i)
{
QString hitString = blastHitList[i];
QStringList alignmentParts = hitString.split('\t');
if (alignmentParts.size() < 12)
continue;
QString queryName = alignmentParts[0];
QString nodeLabel = alignmentParts[1];
double percentIdentity = alignmentParts[2].toDouble();
int alignmentLength = alignmentParts[3].toInt();
int numberMismatches = alignmentParts[4].toInt();
int numberGapOpens = alignmentParts[5].toInt();
int queryStart = alignmentParts[6].toInt();
int queryEnd = alignmentParts[7].toInt();
int nodeStart = alignmentParts[8].toInt();
int nodeEnd = alignmentParts[9].toInt();
SciNot eValue(alignmentParts[10]);
double bitScore = alignmentParts[11].toDouble();
//Only save BLAST hits that are on forward strands.
if (nodeStart > nodeEnd)
continue;
QString nodeName = getNodeNameFromString(nodeLabel);
DeBruijnNode * node;
if (g_assemblyGraph->m_deBruijnGraphNodes.contains(nodeName))
node = g_assemblyGraph->m_deBruijnGraphNodes[nodeName];
else
continue;
BlastQuery * query = g_blastSearch->m_blastQueries.getQueryFromName(queryName);
if (query == 0)
continue;
QSharedPointer<BlastHit> hit(new BlastHit(query, node, percentIdentity, alignmentLength,
numberMismatches, numberGapOpens, queryStart, queryEnd,
nodeStart, nodeEnd, eValue, bitScore));
//Check the user-defined filters.
if (g_settings->blastAlignmentLengthFilter.on &&
alignmentLength < g_settings->blastAlignmentLengthFilter)
continue;
if (g_settings->blastQueryCoverageFilter.on)
{
double hitCoveragePercentage = 100.0 * hit->getQueryCoverageFraction();
if (hitCoveragePercentage < g_settings->blastQueryCoverageFilter)
continue;
}
if (g_settings->blastIdentityFilter.on &&
percentIdentity < g_settings->blastIdentityFilter)
continue;
if (g_settings->blastEValueFilter.on &&
eValue > g_settings->blastEValueFilter)
continue;
if (g_settings->blastBitScoreFilter.on &&
bitScore < g_settings->blastBitScoreFilter)
continue;
m_allHits.push_back(hit);
query->addHit(hit);
}
}
//This function looks at each BLAST query and tries to find a path through
//the graph which covers the maximal amount of the query.
void BlastSearch::findQueryPaths()
{
m_blastQueries.findQueryPaths();
}
QString BlastSearch::getNodeNameFromString(QString nodeString)
{
QStringList nodeStringParts = nodeString.split("_");
//The node string format should look like this:
//NODE_nodename_length_123_cov_1.23
if (nodeStringParts.size() < 6)
return "";
if (nodeStringParts.size() == 6)
return nodeStringParts[1];
//If the code got here, there are more than 6 parts. This means there are
//underscores in the node name (happens a lot with Trinity graphs). So we
//need to pull out the parts which consitute the name.
int underscoreCount = nodeStringParts.size() - 6;
QString nodeName = "";
for (int i = 0; i <= underscoreCount; ++i)
{
nodeName += nodeStringParts[1+i];
if (i < underscoreCount)
nodeName += "_";
}
return nodeName;
}
#ifdef Q_OS_WIN32
//On Windows, we use the WHERE command to find a program.
bool BlastSearch::findProgram(QString programName, QString * command)
{
QProcess find;
find.start("WHERE", QStringList(programName));
find.waitForFinished();
*command = programName;
return (find.exitCode() == 0);
}
#else
//On Mac/Linux we use the which command to find a program.
bool BlastSearch::findProgram(QString programName, QString * command)
{
QProcess find;
//On Mac, it's necessary to adjust the PATH variable in order
//for which to work.
#ifdef Q_OS_MAC
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QStringList envlist = env.toStringList();
//Add some paths to the process environment
envlist.replaceInStrings(QRegularExpression("^(?i)PATH=(.*)"), "PATH="
"/usr/bin:"
"/bin:"
"/usr/sbin:"
"/sbin:"
"/opt/local/bin:"
"/usr/local/bin:"
"/opt/homebrew/bin:"
"$HOME/bin:"
"$HOME/.local/bin:"
"$HOME/miniconda3/bin:"
"/usr/local/ncbi/blast/bin:"
"\\1");
find.setEnvironment(envlist);
#endif
find.start("which", QStringList(programName));
find.waitForFinished();
//On a Mac, we need to use the full path to the program.
#ifdef Q_OS_MAC
*command = QString(find.readAll()).simplified();
#else
*command = programName;
#endif
return (find.exitCode() == 0);
}
#endif
void BlastSearch::clearSomeQueries(std::vector<BlastQuery *> queriesToRemove)
{
//Remove any hits that are for queries that will be deleted.
QList< QSharedPointer<BlastHit> >::iterator i = m_allHits.begin();
while (i != m_allHits.end())
{
BlastQuery * hitQuery = (*i)->m_query;
bool hitIsForDeletedQuery = (std::find(queriesToRemove.begin(), queriesToRemove.end(), hitQuery) != queriesToRemove.end());
if (hitIsForDeletedQuery)
i = m_allHits.erase(i);
else
++i;
}
//Now actually delete the queries.
m_blastQueries.clearSomeQueries(queriesToRemove);
}
void BlastSearch::emptyTempDirectory()
{
//Safety checks
if (g_blastSearch->m_tempDirectory == "")
return;
if (!g_blastSearch->m_tempDirectory.contains("bandage_temp"))
return;
QDir tempDirectory(m_tempDirectory);
tempDirectory.setNameFilters(QStringList() << "*.*");
tempDirectory.setFilter(QDir::Files);
foreach(QString dirFile, tempDirectory.entryList())
tempDirectory.remove(dirFile);
}
//This function carries out the entire BLAST search procedure automatically, without user input.
//It returns an error string which is empty if all goes well.
QString BlastSearch::doAutoBlastSearch()
{
cleanUp();
QString makeblastdbCommand;
if (!findProgram("makeblastdb", &makeblastdbCommand))
return "Error: The program makeblastdb was not found. Please install NCBI BLAST to use this feature.";
BuildBlastDatabaseWorker buildBlastDatabaseWorker(makeblastdbCommand);
buildBlastDatabaseWorker.buildBlastDatabase();
if (buildBlastDatabaseWorker.m_error != "")
return buildBlastDatabaseWorker.m_error;
loadBlastQueriesFromFastaFile(g_settings->blastQueryFilename);
QString blastnCommand;
if (!findProgram("blastn", &blastnCommand))
return "Error: The program blastn was not found. Please install NCBI BLAST to use this feature.";
QString tblastnCommand;
if (!findProgram("tblastn", &tblastnCommand))
return "Error: The program tblastn was not found. Please install NCBI BLAST to use this feature.";
RunBlastSearchWorker runBlastSearchWorker(blastnCommand, tblastnCommand, g_settings->blastSearchParameters);
runBlastSearchWorker.runBlastSearch();
if (runBlastSearchWorker.m_error != "")
return runBlastSearchWorker.m_error;
blastQueryChanged("all");
return "";
}
//This function returns the number of queries loaded from the FASTA file.
int BlastSearch::loadBlastQueriesFromFastaFile(QString fullFileName)
{
int queriesBefore = int(g_blastSearch->m_blastQueries.m_queries.size());
std::vector<QString> queryNames;
std::vector<QByteArray> querySequences;
AssemblyGraph::readFastaOrFastqFile(fullFileName, &queryNames, &querySequences);
for (size_t i = 0; i < queryNames.size(); ++i)
{
QApplication::processEvents();
//We only use the part of the query name up to the first space.
QStringList queryNameParts = queryNames[i].split(" ");
QString queryName;
if (queryNameParts.size() > 0)
queryName = cleanQueryName(queryNameParts[0]);
g_blastSearch->m_blastQueries.addQuery(new BlastQuery(queryName,
querySequences[i]));
}
int queriesAfter = int(g_blastSearch->m_blastQueries.m_queries.size());
return queriesAfter - queriesBefore;
}
QString BlastSearch::cleanQueryName(QString queryName)
{
//Replace whitespace with underscores
queryName = queryName.replace(QRegularExpression("\\s"), "_");
//Remove any dots from the end of the query name. BLAST doesn't
//include them in its results, so if we don't remove them, then
//we won't be able to find a match between the query name and
//the BLAST hit.
while (queryName.length() > 0 && queryName[queryName.size() - 1] == '.')
queryName = queryName.left(queryName.size() - 1);
return queryName;
}
void BlastSearch::blastQueryChanged(QString queryName)
{
g_assemblyGraph->clearAllBlastHitPointers();
std::vector<BlastQuery *> queries;
//If "all" is selected, then we'll display each of the BLAST queries
if (queryName == "all")
queries = g_blastSearch->m_blastQueries.m_queries;
//If only one query is selected, then just display that one.
else
{
BlastQuery * query = g_blastSearch->m_blastQueries.getQueryFromName(queryName);
if (query != 0)
queries.push_back(query);
}
//We now filter out any queries that have been hidden by the user.
std::vector<BlastQuery *> shownQueries;
for (size_t i = 0; i < queries.size(); ++i)
{
BlastQuery * query = queries[i];
if (query->isShown())
shownQueries.push_back(query);
}
//Add the blast hit pointers to nodes that have a hit for
//the selected target(s).
for (size_t i = 0; i < shownQueries.size(); ++i)
{
BlastQuery * query = shownQueries[i];
for (int j = 0; j < g_blastSearch->m_allHits.size(); ++j)
{
BlastHit * hit = g_blastSearch->m_allHits[j].data();
if (hit->m_query == query)
hit->m_node->addBlastHit(hit);
}
}
}
|