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 #include "SqliteUpgraderFrom_1_13_To_1_25.h"
23
24 #include <U2Core/L10n.h>
25 #include <U2Core/U2AssemblyUtils.h>
26 #include <U2Core/U2AttributeUtils.h>
27 #include <U2Core/U2CoreAttributes.h>
28 #include <U2Core/U2Dbi.h>
29 #include <U2Core/U2SafePoints.h>
30 #include <U2Core/U2SqlHelpers.h>
31
32 #include "../SQLiteAssemblyDbi.h"
33 #include "../SQLiteDbi.h"
34 #include "../SQLiteObjectRelationsDbi.h"
35
36 namespace U2 {
37
SqliteUpgraderFrom_1_13_To_1_25(SQLiteDbi * dbi)38 SqliteUpgraderFrom_1_13_To_1_25::SqliteUpgraderFrom_1_13_To_1_25(SQLiteDbi *dbi)
39 : SqliteUpgrader(Version::parseVersion("1.13.0"), Version::parseVersion("1.25.0"), dbi) {
40 }
41
upgrade(U2OpStatus & os) const42 void SqliteUpgraderFrom_1_13_To_1_25::upgrade(U2OpStatus &os) const {
43 SQLiteTransaction t(dbi->getDbRef(), os);
44
45 upgradeCoverageAttribute(os);
46 CHECK_OP(os, );
47
48 dbi->setProperty(U2DbiOptions::APP_MIN_COMPATIBLE_VERSION, versionTo.text, os);
49 }
50
upgradeCoverageAttribute(U2OpStatus & os) const51 void SqliteUpgraderFrom_1_13_To_1_25::upgradeCoverageAttribute(U2OpStatus &os) const {
52 // get assembly ids
53 QList<U2DataId> assemblyIds = dbi->getObjectDbi()->getObjects(U2Type::Assembly, 0, U2DbiOptions::U2_DBI_NO_LIMIT, os);
54 CHECK_OP(os, );
55 CHECK(!assemblyIds.isEmpty(), );
56 U2AttributeDbi *attributeDbi = dbi->getAttributeDbi();
57 CHECK_EXT(attributeDbi != nullptr, os.setError("Attribute dbi is NULL"), );
58
59 foreach (const U2DataId &id, assemblyIds) {
60 // find and remove coverage attribute from ByteArrayAttribute table
61 U2ByteArrayAttribute attr = U2AttributeUtils::findByteArrayAttribute(attributeDbi, id, U2BaseAttributeName::coverage_statistics, os);
62
63 if (!attr.value.isEmpty()) { // if empty, then nothing to remove
64 U2AttributeUtils::removeAttribute(attributeDbi, attr.id, os);
65 }
66
67 // calculate new coverage
68 U2AssemblyDbi *assemblyDbi = dbi->getAssemblyDbi();
69 CHECK_EXT(attributeDbi != nullptr, os.setError("Assembly dbi is NULL"), );
70 U2Assembly assembly = assemblyDbi->getAssemblyObject(id, os);
71 CHECK_OP(os, );
72
73 U2IntegerAttribute lengthAttr = U2AttributeUtils::findIntegerAttribute(attributeDbi, id, U2BaseAttributeName::reference_length, os);
74 CHECK_OP(os, );
75 if (lengthAttr.value == 0) { // Nothing to calculate
76 continue;
77 }
78 static const qint64 MAX_COVERAGE_CACHE_SIZE = 1000 * 1000;
79 int coverageSize = (int)qMin(MAX_COVERAGE_CACHE_SIZE, lengthAttr.value);
80 U2AssemblyCoverageStat coverageStat;
81 coverageStat.resize(coverageSize);
82
83 assemblyDbi->calculateCoverage(id, U2Region(0, lengthAttr.value), coverageStat, os);
84 CHECK_OP(os, );
85
86 // write new coverage attribute to ByteArrayAttribute table
87 U2ByteArrayAttribute attribute;
88 attribute.objectId = id;
89 attribute.name = U2BaseAttributeName::coverage_statistics;
90 attribute.value = U2AssemblyUtils::serializeCoverageStat(coverageStat);
91 attribute.version = assembly.version;
92 attributeDbi->createByteArrayAttribute(attribute, os);
93 CHECK_OP(os, );
94 }
95 }
96
97 } // namespace U2
98