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_FEATURE_DBI_H_
23 #define _U2_FEATURE_DBI_H_
24 
25 #include <U2Core/U2Dbi.h>
26 #include <U2Core/U2Feature.h>
27 #include <U2Core/U2Type.h>
28 
29 namespace U2 {
30 
31 enum ComparisonOp {
32     ComparisonOp_Invalid,
33     ComparisonOp_EQ,
34     ComparisonOp_NEQ,
35     ComparisonOp_GT,
36     ComparisonOp_GET,
37     ComparisonOp_LT,
38     ComparisonOp_LET
39 };
40 
41 enum OrderOp {
42     OrderOp_None,
43     OrderOp_Asc,
44     OrderOp_Desc
45 };
46 
47 enum StrandQuery {
48     Strand_Both,
49     Strand_Direct,
50     Strand_Compl
51 };
52 
53 class FeatureQuery {
54 public:
FeatureQuery()55     FeatureQuery()
56         : topLevelOnly(false), featureNameOrderOp(OrderOp_None), keyNameOrderOp(OrderOp_None),
57           keyValueCompareOp(ComparisonOp_Invalid), keyValueOrderOp(OrderOp_None),
58           intersectRegion(-1, 0), startPosOrderOp(OrderOp_None),
59           closestFeature(ComparisonOp_Invalid), strandQuery(Strand_Both),
60           featureClass(U2Feature::Annotation),
61           featureType(U2FeatureTypes::Invalid) {
62     }
63 
64     bool operator==(const FeatureQuery &other) const {
65         return sequenceId == other.sequenceId && parentFeatureId == other.parentFeatureId && rootFeatureId == other.rootFeatureId && topLevelOnly == other.topLevelOnly && featureName == other.featureName && featureNameOrderOp == other.featureNameOrderOp && keyName == other.keyName && keyNameOrderOp == other.keyNameOrderOp && keyValue == other.keyValue && keyValueCompareOp == other.keyValueCompareOp && keyValueOrderOp == other.keyValueOrderOp && intersectRegion == other.intersectRegion && startPosOrderOp == other.startPosOrderOp && closestFeature == other.closestFeature && strandQuery == other.strandQuery && featureType == other.featureType;
66     }
67 
68     U2DataId sequenceId;
69 
70     U2DataId parentFeatureId;
71     U2DataId rootFeatureId;
72     bool topLevelOnly;
73 
74     QString featureName;
75     OrderOp featureNameOrderOp;
76 
77     QString keyName;
78     OrderOp keyNameOrderOp;
79 
80     QString keyValue;
81     ComparisonOp keyValueCompareOp;
82     OrderOp keyValueOrderOp;
83 
84     /**
85      * Indicates the region to intersect
86      * Special values:
87      * startPos = -1 and len = 0 :   filter is disabled
88      */
89     U2Region intersectRegion;
90     OrderOp startPosOrderOp;
91 
92     /**
93      * Get one feature which:
94      * ComparisonOp_EQ - has the same start position as @intersectRegion
95      * ComparisonOp_GT - to the right of intersectRegion
96      * ComparisonOp_LT - to the left of intersectRegion
97      * ComparisonOp_Invalid - disables filter
98      */
99     ComparisonOp closestFeature;
100 
101     StrandQuery strandQuery;
102 
103     U2Feature::FeatureClass featureClass;
104     U2FeatureType featureType;
105 };
106 
107 struct FeatureAndKey {
108     U2Feature feature;
109     U2FeatureKey key;
110 };
111 
112 /**
113  * An interface to obtain 'read' access to sequence features
114  */
115 class U2FeatureDbi : public U2ChildDbi {
116 public:
117     /**
118      * Creates a DB representation of AnnotationTableObject.
119      * @table has to be fully initialized except of the id field.
120      */
121     virtual void createAnnotationTableObject(U2AnnotationTable &table, const QString &folder, U2OpStatus &os) = 0;
122     /**
123      * Returns a DB representation of AnnotationTableObject having supplied @tableId.
124      */
125     virtual U2AnnotationTable getAnnotationTableObject(const U2DataId &tableId, U2OpStatus &os) = 0;
126     /**
127      * Removes a DB representation of AnnotationTableObject having supplied @tableId along with all its data
128      */
129     virtual void removeAnnotationTableData(const U2DataId &tableId, U2OpStatus &os) = 0;
130     /**
131      * Reads feature data by id
132      */
133     virtual U2Feature getFeature(const U2DataId &featureId, U2OpStatus &os) = 0;
134     /**
135      * Counts features that matched the query.
136      */
137     virtual qint64 countFeatures(const FeatureQuery &q, U2OpStatus &os) = 0;
138     /**
139      * Returns features that matched the query. Returns NULL if error occurs
140      */
141     virtual U2DbiIterator<U2Feature> *getFeatures(const FeatureQuery &q, U2OpStatus &os) = 0;
142     /**
143      * Returns all keys of a specified feature
144      */
145     virtual QList<U2FeatureKey> getFeatureKeys(const U2DataId &featureId, U2OpStatus &os) = 0;
146     /**
147      * Returns all the features and keys belonging to the same annotation table with @rootFeatureId as a root feature
148      */
149     virtual QList<FeatureAndKey> getFeatureTable(const U2DataId &rootFeatureId, U2OpStatus &os) = 0;
150     /**
151      * Creates new feature in database. Uses all fields in 'feature' param
152      * and assign database id to it as the result
153      * Requires: U2DbiFeature_WriteFeature feature support
154      */
155     virtual void createFeature(U2Feature &feature, const QList<U2FeatureKey> &keys, U2OpStatus &os) = 0;
156     /**
157      * Adds key to feature
158      * Requires: U2DbiFeature_WriteFeature feature support
159      */
160     virtual void addKey(const U2DataId &featureId, const U2FeatureKey &key, U2OpStatus &os) = 0;
161     /**
162      * Removes all feature keys with a specified name
163      * Requires: U2DbiFeature_WriteFeature feature support
164      */
165     virtual void removeAllKeys(const U2DataId &featureId, const QString &keyName, U2OpStatus &os) = 0;
166     /**
167      * Removes all feature keys with a specified name and value
168      * Requires: U2DbiFeature_WriteFeature feature support
169      */
170     virtual void removeKey(const U2DataId &featureId, const U2FeatureKey &key, U2OpStatus &os) = 0;
171     /**
172      * Updates feature key.
173      * Requires: U2DbiFeature_WriteFeature feature support
174      */
175     virtual void updateKeyValue(const U2DataId &featureId, const U2FeatureKey &key, U2OpStatus &os) = 0;
176     /**
177      * After the invocation @key.value contains the value of a feature's key with name @key.name.
178      * Returning value specifies whether the key with name @key.name exists for a given feature.
179      */
180     virtual bool getKeyValue(const U2DataId &featureId, U2FeatureKey &key, U2OpStatus &os) = 0;
181     /**
182      * Updates feature location. Features with U2Region(0,0) have no specified location
183      * Requires: U2DbiFeature_WriteFeature feature support
184      */
185     virtual void updateLocation(const U2DataId &featureId, const U2FeatureLocation &location, U2OpStatus &os) = 0;
186     /**
187      * Updates feature type
188      * Requires: U2DbiFeature_WriteFeature feature support
189      */
190     virtual void updateType(const U2DataId &featureId, U2FeatureType newType, U2OpStatus &os) = 0;
191     /**
192      * Updates feature name
193      * Requires: U2DbiFeature_WriteFeature feature support
194      */
195     virtual void updateName(const U2DataId &featureId, const QString &newName, U2OpStatus &os) = 0;
196     /**
197      * Updates feature parent
198      * Requires: U2DbiFeature_WriteFeature feature support
199      */
200     virtual void updateParentId(const U2DataId &featureId, const U2DataId &parentId, U2OpStatus &os) = 0;
201     /**
202      * Updates feature sequence
203      * Requires: U2DbiFeature_WriteFeature feature support
204      */
205     virtual void updateSequenceId(const U2DataId &featureId, const U2DataId &seqId, U2OpStatus &os) = 0;
206     /**
207      * Removes the feature from database
208      * Requires: U2DbiFeature_WriteFeature feature support
209      */
210     virtual void removeFeature(const U2DataId &featureId, U2OpStatus &os) = 0;
211     /**
212      * Removes subfeatures along with their parent feature from database
213      * Requires: U2DbiFeature_WriteFeature feature support
214      */
215     virtual void removeFeaturesByParent(const U2DataId &parentId, U2OpStatus &os, SubfeatureSelectionMode mode = SelectParentFeature) = 0;
216 
217     virtual void removeFeaturesByParents(const QList<U2DataId> &parentIds, U2OpStatus &os) = 0;
218     /**
219      * Removes subfeatures along with their root feature from database
220      * Requires: U2DbiFeature_WriteFeature feature support
221      */
222     virtual void removeFeaturesByRoot(const U2DataId &rootId, U2OpStatus &os, SubfeatureSelectionMode mode = SelectParentFeature) = 0;
223     /**
224      * Returns features that matched the query. Returns NULL if error occurs
225      */
226     virtual U2DbiIterator<U2Feature> *getFeaturesByRegion(const U2Region &reg, const U2DataId &rootId, const QString &featureName, const U2DataId &seqId, U2OpStatus &os, bool contains = false) = 0;
227 
228     virtual U2DbiIterator<U2Feature> *getFeaturesByParent(const U2DataId &parentId, const QString &featureName, const U2DataId &seqId, U2OpStatus &os, SubfeatureSelectionMode mode = NotSelectParentFeature) = 0;
229 
230     virtual U2DbiIterator<U2Feature> *getFeaturesByRoot(const U2DataId &rootId, const FeatureFlags &types, U2OpStatus &os) = 0;
231 
232     virtual U2DbiIterator<U2Feature> *getFeaturesBySequence(const QString &featureName, const U2DataId &seqId, U2OpStatus &os) = 0;
233 
234     virtual U2DbiIterator<U2Feature> *getFeaturesByName(const U2DataId &rootId, const QString &name, const FeatureFlags &types, U2OpStatus &os) = 0;
235 
236     /**
237      * Returns the map where a key corresponds to a annotation table and a value is the subset of the @values
238      * that occurs within a single qualifier value belonging to the annotation table.
239      */
240     virtual QMap<U2DataId, QStringList> getAnnotationTablesByFeatureKey(const QStringList &values, U2OpStatus &os) = 0;
241 
242 protected:
U2FeatureDbi(U2Dbi * rootDbi)243     U2FeatureDbi(U2Dbi *rootDbi)
244         : U2ChildDbi(rootDbi) {
245     }
246 };
247 
248 }  // namespace U2
249 
250 #endif
251