1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef _U2_SW_ALGORITHM_TASK_H_
23 #define _U2_SW_ALGORITHM_TASK_H_
24 
25 #include <QMutex>
26 #include <QVector>
27 
28 #include <U2Algorithm/PairwiseAlignmentTask.h>
29 #include <U2Algorithm/SmithWatermanSettings.h>
30 
31 #include <U2Core/DNASequence.h>
32 #include <U2Core/SMatrix.h>
33 #include <U2Core/SequenceWalkerTask.h>
34 #include <U2Core/Task.h>
35 
36 #include "PairAlignSequences.h"
37 #include "SmithWatermanAlgorithm.h"
38 
39 namespace U2 {
40 
41 enum SW_AlgType { SW_classic,
42                   SW_sse2,
43                   SW_cuda,
44                   SW_opencl };
45 
46 class CudaGpuModel;
47 class OpenCLGpuModel;
48 
49 class SWAlgorithmTask : public Task, public SequenceWalkerCallback {
50     Q_OBJECT
51 public:
52     SWAlgorithmTask(const SmithWatermanSettings &s,
53                     const QString &taskName,
54                     SW_AlgType algType);
55     ~SWAlgorithmTask();
56 
57     void prepare();
58 
59     virtual void onRegion(SequenceWalkerSubtask *t, TaskStateInfo &ti);
60 
61     QList<PairAlignSequences> &getResult();
62     ReportResult report();
63 
64     QList<Task *> onSubTaskFinished(Task *subTask);
65 
66 private:
67     void addResult(QList<PairAlignSequences> &res);
68     int calculateMatrixLength(int searchSeqLen, int patternLen, int gapOpen, int gapExtension, int maxScore, int minScore);
69     void removeResultFromOverlap(QList<PairAlignSequences> &res);
70     int calculateMaxScore(const QByteArray &seq, const SMatrix &substitutionMatrix);
71 
72     void setupTask(int maxScore);
73 
74     QList<PairAlignSequences> pairAlignSequences;
75     int minScore;
76 
77     QMutex lock;
78 
79     SW_AlgType algType;
80 
81     QList<SmithWatermanResult> resultList;
82     SmithWatermanSettings sWatermanConfig;
83     SequenceWalkerTask *t;
84 
85     CudaGpuModel *cudaGpu;
86     OpenCLGpuModel *openClGpu;
87 };
88 
89 class SWResultsPostprocessingTask : public Task {
90     Q_OBJECT
91 public:
92     SWResultsPostprocessingTask(SmithWatermanSettings &_sWatermanConfig, QList<SmithWatermanResult> &_resultList, QList<PairAlignSequences> &_resPAS);
~SWResultsPostprocessingTask()93     ~SWResultsPostprocessingTask() {
94     }
95 
96     void prepare();
97     void run();
report()98     ReportResult report() {
99         return ReportResult_Finished;
100     }
101 
102 private:
103     SmithWatermanSettings sWatermanConfig;
104     QList<SmithWatermanResult> resultList;
105     QList<PairAlignSequences> resPAS;
106 };
107 
108 class PairwiseAlignmentSmithWatermanTaskSettings : public PairwiseAlignmentTaskSettings {
109 public:
110     PairwiseAlignmentSmithWatermanTaskSettings(const PairwiseAlignmentTaskSettings &s);
111     virtual ~PairwiseAlignmentSmithWatermanTaskSettings();
112 
113     virtual bool convertCustomSettings();
114 
115     // all settings except sMatrix and pointers must be set up through customSettings and then must be converted by convertCustomSettings().
116     SmithWatermanReportCallbackMAImpl *reportCallback;
117     SmithWatermanResultListener *resultListener;
118     SmithWatermanResultFilter *resultFilter;
119 
120     int gapOpen;
121     int gapExtd;
122     int percentOfScore;
123     QString sMatrixName;
124     SMatrix sMatrix;  // initialized by convertCustomSettings()
125 
126     static const QString PA_SW_GAP_OPEN;
127     static const QString PA_SW_GAP_EXTD;
128     static const QString PA_SW_SCORING_MATRIX_NAME;
129     static const QString PA_SW_REALIZATION_NAME;
130     static const qint64 PA_SW_DEFAULT_PERCENT_OF_SCORE = 0;
131     static const QString PA_SW_DEFAULT_RESULT_FILTER;
132 };
133 
134 class PairwiseAlignmentSmithWatermanTask : public PairwiseAlignmentTask, public SequenceWalkerCallback {
135     Q_OBJECT
136 public:
137     PairwiseAlignmentSmithWatermanTask(PairwiseAlignmentSmithWatermanTaskSettings *_settings, SW_AlgType algType);
138     ~PairwiseAlignmentSmithWatermanTask();
139     virtual void onRegion(SequenceWalkerSubtask *t, TaskStateInfo &ti);
140     void prepare();
141     QList<PairAlignSequences> &getResult();
142     ReportResult report();
143 
144     QList<Task *> onSubTaskFinished(Task *subTask);
145 
146 protected:
147     void addResult(QList<PairAlignSequences> &res);
148     int calculateMaxScore(const QByteArray &seq, const SMatrix &substitutionMatrix);
149     void setupTask();
150     int calculateMatrixLength(const QByteArray &searchSeq, const QByteArray &patternSeq, int gapOpen, int gapExtension, int maxScore, int minScore);
151     void removeResultFromOverlap(QList<PairAlignSequences> &res);
152     QList<PairAlignSequences> expandResults(QList<PairAlignSequences> &res);
153 
154 protected:
155     QMutex lock;
156     PairwiseAlignmentSmithWatermanTaskSettings *settings;
157     SW_AlgType algType;
158     QList<PairAlignSequences> pairAlignSequences;
159     QList<SmithWatermanResult> resultList;
160     int minScore;
161     int maxScore;
162     QByteArray *sqnc;
163     QByteArray *ptrn;
164     SequenceWalkerTask *t;
165 
166     CudaGpuModel *cudaGpu;
167     OpenCLGpuModel *openClGpu;
168 };
169 
170 class PairwiseAlignmentSWResultsPostprocessingTask : public Task {
171     Q_OBJECT
172 
173 public:
174     PairwiseAlignmentSWResultsPostprocessingTask(SmithWatermanResultFilter *rf, SmithWatermanResultListener *rl, QList<SmithWatermanResult> &_resultList, QList<PairAlignSequences> &_resPAS);
~PairwiseAlignmentSWResultsPostprocessingTask()175     ~PairwiseAlignmentSWResultsPostprocessingTask() {
176     }
177 
178     void run();
179     void prepare();
report()180     ReportResult report() {
181         return ReportResult_Finished;
182     }
183 
184 private:
185     SmithWatermanResultFilter *rf;
186     SmithWatermanResultListener *rl;
187     QList<SmithWatermanResult> resultList;
188     QList<PairAlignSequences> resPAS;
189 };
190 
191 }  // namespace U2
192 
193 #endif  //_U2_SW_ALGORITHM_TASK_H_
194