File: blastqueries.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 (249 lines) | stat: -rw-r--r-- 6,235 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
//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 "blastqueries.h"
#include "../program/globals.h"
#include "../program/settings.h"
#include <QTextStream>
#include "blastsearch.h"
#include "../program/memory.h"

BlastQueries::BlastQueries() :
    m_tempNuclFile(0), m_tempProtFile(0)
{
    m_presetColours = getPresetColours();
}


BlastQueries::~BlastQueries()
{
    clearAllQueries();
}


void BlastQueries::createTempQueryFiles()
{
    m_tempNuclFile.reset(new QFile(g_blastSearch->m_tempDirectory + "nucl_queries.fasta"));
    m_tempProtFile.reset(new QFile(g_blastSearch->m_tempDirectory + "prot_queries.fasta"));
}

BlastQuery * BlastQueries::getQueryFromName(QString queryName)
{
    for (size_t i = 0; i < m_queries.size(); ++i)
    {
        if (m_queries[i]->getName() == queryName)
            return m_queries[i];
    }
    return 0;
}


void BlastQueries::addQuery(BlastQuery * newQuery)
{
    newQuery->setName(getUniqueName(newQuery->getName()));

    //Give the new query a colour
    int colourIndex = int(m_queries.size());
    colourIndex %= m_presetColours.size();
    newQuery->setColour(m_presetColours[colourIndex]);

    m_queries.push_back(newQuery);
    updateTempFiles();
}


//This function renames the query.  It returns the name given, because that
//might not be exactly the same as the name passed to the function if it
//wasn't unique.
QString BlastQueries::renameQuery(BlastQuery * newQuery, QString newName)
{
    newQuery->setName(getUniqueName(newName));
    updateTempFiles();
    return newQuery->getName();
}


//This function looks at the name, and if it is not unique, it adds a suffix
//to make it unique.  Also make sure it's not "all" or "none", as those will
//conflict with viewing all queries at once or no queries.
QString BlastQueries::getUniqueName(QString name)
{
    //If the query name ends in a semicolon, remove it.  Ending semicolons
    //mess with BLAST.
    if (name.endsWith(';'))
        name.chop(1);

    //The name can't be empty.
    if (name == "")
        name = g_settings->unnamedQueryDefaultName;

    int queryNumber = 2;
    QString finalName = name;
    while (getQueryFromName(finalName) != 0 ||
           finalName == "all" || finalName == "none")
        finalName = name + "_" + QString::number(queryNumber++);
    return finalName;
}

void BlastQueries::clearAllQueries()
{
    for (size_t i = 0; i < m_queries.size(); ++i)
        delete m_queries[i];
    m_queries.clear();
    deleteTempFiles();
}

void BlastQueries::clearSomeQueries(std::vector<BlastQuery *> queriesToRemove)
{
    for (size_t i = 0; i < queriesToRemove.size(); ++i)
    {
        m_queries.erase(std::remove(m_queries.begin(), m_queries.end(), queriesToRemove[i]), m_queries.end());
        delete queriesToRemove[i];
    }

    updateTempFiles();
}

void BlastQueries::deleteTempFiles()
{
    if (tempNuclFileExists())
        m_tempNuclFile->remove();
    if (tempProtFileExists())
        m_tempProtFile->remove();
}

void BlastQueries::updateTempFiles()
{
    deleteTempFiles();

    if (getQueryCount(NUCLEOTIDE) > 0)
        writeTempFile(m_tempNuclFile, NUCLEOTIDE);

    if (getQueryCount(PROTEIN) > 0)
        writeTempFile(m_tempProtFile, PROTEIN);
}


void BlastQueries::writeTempFile(QSharedPointer<QFile> file, SequenceType sequenceType)
{
    file->open(QIODevice::Append | QIODevice::Text);
    QTextStream out(file.data());
    for (size_t i = 0; i < m_queries.size(); ++i)
    {
        if (m_queries[i]->getSequenceType() == sequenceType)
        {
            out << ">" << m_queries[i]->getName() << "\n";
            out << m_queries[i]->getSequence();
            out << "\n";
        }
    }
    file->close();
}


void BlastQueries::searchOccurred()
{
    for (size_t i = 0; i < m_queries.size(); ++i)
        m_queries[i]->setAsSearchedFor();
}


void BlastQueries::clearSearchResults()
{
    for (size_t i = 0; i < m_queries.size(); ++i)
        m_queries[i]->clearSearchResults();
}


int BlastQueries::getQueryCount()
{
    return int(m_queries.size());
}

int BlastQueries::getQueryCountWithAtLeastOnePath()
{
    int count = 0;

    for (size_t i = 0; i < m_queries.size(); ++i)
    {
        if (m_queries[i]->getPathCount() > 0)
            ++count;
    }

    return count;
}

int BlastQueries::getQueryPathCount()
{
    int count = 0;

    for (size_t i = 0; i < m_queries.size(); ++i)
        count += m_queries[i]->getPathCount();
    return count;
}

int BlastQueries::getQueryCount(SequenceType sequenceType)
{
    int count = 0;
    for (size_t i = 0; i < m_queries.size(); ++i)
    {
        if (m_queries[i]->getSequenceType() == sequenceType)
            ++count;
    }
    return count;
}


bool BlastQueries::tempNuclFileExists()
{
    if (m_tempNuclFile.isNull())
        return false;
    return m_tempNuclFile->exists();
}
bool BlastQueries::tempProtFileExists()
{
    if (m_tempProtFile.isNull())
        return false;
    return m_tempProtFile->exists();
}


//This function looks to see if a query pointer is in the list
//of queries.  The query pointer given may or may not still
//actually exist, so it can't be dereferenced.
bool BlastQueries::isQueryPresent(BlastQuery * query)
{
    for (size_t i = 0; i < m_queries.size(); ++i)
    {
        if (query == m_queries[i])
            return true;
    }

    return false;
}


//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 BlastQueries::findQueryPaths()
{
    for (size_t i = 0; i < m_queries.size(); ++i)
        m_queries[i]->findQueryPaths();
}