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_CALCULATE_COVERAGE_PER_BASE_TASK_H_
23 #define _U2_CALCULATE_COVERAGE_PER_BASE_TASK_H_
24 
25 #include <QMap>
26 
27 #include <U2Core/Task.h>
28 #include <U2Core/U2Assembly.h>
29 #include <U2Core/U2Region.h>
30 #include <U2Core/U2Type.h>
31 
32 namespace U2 {
33 
34 class CoveragePerBaseInfo {
35 public:
CoveragePerBaseInfo()36     CoveragePerBaseInfo()
37         : coverage(0) {
38     }
39 
40     int coverage;
41     QMap<char, int> basesCount;
42 };
43 class GetAssemblyLengthTask : public Task {
44     Q_OBJECT
45 public:
GetAssemblyLengthTask(const U2DbiRef & dbiRef,const U2DataId & assemblyId)46     GetAssemblyLengthTask(const U2DbiRef &dbiRef, const U2DataId &assemblyId)
47         : Task(tr("Get length of Assembly"), TaskFlag_None), dbiRef(dbiRef), assemblyId(assemblyId) {
48     }
49 
50     void run();
51 
getAssemblyLength()52     qint64 getAssemblyLength() const {
53         return length;
54     }
55 
56 private:
57     const U2DbiRef dbiRef;
58     const U2DataId assemblyId;
59     qint64 length;
60 };
61 
62 class CalculateCoveragePerBaseOnRegionTask : public Task {
63     Q_OBJECT
64 public:
65     CalculateCoveragePerBaseOnRegionTask(const U2DbiRef &dbiRef, const U2DataId &assemblyId, const U2Region &region);
66     ~CalculateCoveragePerBaseOnRegionTask();
67 
68     void run();
69 
70     const U2Region &getRegion() const;
71     QVector<CoveragePerBaseInfo> *takeResult();
72 
73 private:
74     void processRead(const U2AssemblyRead &read);
75     U2CigarOp nextCigarOp(const QVector<U2CigarOp> &cigarVector, int &index, int &insertionsCount);
76 
77     const U2DbiRef dbiRef;
78     const U2DataId assemblyId;
79     const U2Region region;
80     QVector<CoveragePerBaseInfo> *results;
81 };
82 
83 class CalculateCoveragePerBaseTask : public Task {
84     Q_OBJECT
85 public:
86     CalculateCoveragePerBaseTask(const U2DbiRef &dbiRef, const U2DataId &assemblyId);
87     ~CalculateCoveragePerBaseTask();
88 
89     void prepare();
90     QList<Task *> onSubTaskFinished(Task *subTask);
91 
92     bool isResultReady(qint64 startPos) const;
93     bool areThereUnprocessedResults() const;
94     QVector<CoveragePerBaseInfo> *takeResult(qint64 startPos);
95 
96 signals:
97     void si_regionIsProcessed(qint64 startPos);
98 
99 private slots:
100 
101 private:
102     const U2DbiRef dbiRef;
103     const U2DataId assemblyId;
104     QHash<qint64, QVector<CoveragePerBaseInfo> *> results;
105 
106     static const qint64 MAX_REGION_LENGTH = 100000;
107     GetAssemblyLengthTask *getLengthTask;
108 };
109 
110 }  // namespace U2
111 
112 #endif  // _U2_CALCULATE_COVERAGE_PER_BASE_TASK_H_
113