1 #ifndef SEQ_TABLE_INFO__HPP
2 #define SEQ_TABLE_INFO__HPP
3 
4 /*  $Id: seq_table_info.hpp 502681 2016-05-26 16:04:36Z vasilche $
5 * ===========================================================================
6 *
7 *                            PUBLIC DOMAIN NOTICE
8 *               National Center for Biotechnology Information
9 *
10 *  This software/database is a "United States Government Work" under the
11 *  terms of the United States Copyright Act.  It was written as part of
12 *  the author's official duties as a United States Government employee and
13 *  thus cannot be copyrighted.  This software/database is freely available
14 *  to the public for use. The National Library of Medicine and the U.S.
15 *  Government have not placed any restriction on its use or reproduction.
16 *
17 *  Although all reasonable efforts have been taken to ensure the accuracy
18 *  and reliability of the software and data, the NLM and the U.S.
19 *  Government do not and cannot warrant the performance or results that
20 *  may be obtained by using this software or data. The NLM and the U.S.
21 *  Government disclaim all warranties, express or implied, including
22 *  warranties of performance, merchantability or fitness for any particular
23 *  purpose.
24 *
25 *  Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 *   Seq-table information
33 *
34 */
35 
36 #include <corelib/ncbiobj.hpp>
37 #include <objmgr/impl/seq_table_setter.hpp>
38 #include <objects/seq/seq_id_handle.hpp>
39 #include <objects/seqloc/Na_strand.hpp>
40 #include <objects/seqtable/Seq_table.hpp>
41 #include <objects/seqtable/SeqTable_column.hpp>
42 #include <objects/seqtable/SeqTable_column_info.hpp>
43 #include <util/range.hpp>
44 #include <vector>
45 
46 BEGIN_NCBI_SCOPE
47 BEGIN_SCOPE(objects)
48 
49 class CDataSource;
50 class CSeq_table;
51 class CSeq_loc;
52 class CSeq_interval;
53 class CSeq_point;
54 class CSeq_feat;
55 struct SAnnotObject_Key;
56 struct SAnnotObject_Index;
57 struct SAnnotTypeSelector;
58 struct SAnnotSelector;
59 
60 /////////////////////////////////////////////////////////////////////////////
61 // information about Seq-table column
62 /////////////////////////////////////////////////////////////////////////////
63 
64 class CSeqTableColumnInfo
65 {
66 public:
CSeqTableColumnInfo(void)67     CSeqTableColumnInfo(void)
68         {
69         }
CSeqTableColumnInfo(const CSeqTable_column & column)70     CSeqTableColumnInfo(const CSeqTable_column& column)
71         : m_Column(&column)
72         {
73         }
~CSeqTableColumnInfo()74     ~CSeqTableColumnInfo()
75         {
76         }
77 
78     DECLARE_OPERATOR_BOOL_REF(m_Column);
79 
Get(void) const80     const CSeqTable_column* Get(void) const
81         {
82             return m_Column.GetPointer();
83         }
operator ->(void) const84     const CSeqTable_column* operator->(void) const
85         {
86             return m_Column.GetPointer();
87         }
88 
89     bool IsSingular(void) const;
90 
91     bool IsSet(size_t row) const;
92 
GetBool(size_t row) const93     bool GetBool(size_t row) const
94         {
95             bool val = false;
96             return *this && m_Column->TryGetBool(row, val) && val;
97         }
98     template<class Value>
GetValue(size_t row,Value & v,bool force=false) const99     bool GetValue(size_t row, Value& v, bool force = false) const
100         {
101             return m_Column->TryGetValue(row, v) ||
102                 (force && x_ThrowUnsetValue());
103         }
104     const string* GetStringPtr(size_t row, bool force = false) const;
105     const vector<char>* GetBytesPtr(size_t row, bool force = false) const;
106     CConstRef<CSeq_id> GetSeq_id(size_t row, bool force = false) const;
107     CConstRef<CSeq_loc> GetSeq_loc(size_t row, bool force = false) const;
108 
109     void UpdateSeq_loc(CSeq_loc& loc, size_t row,
110                        const CSeqTableSetLocField& setter) const;
111     void UpdateSeq_loc(CSeq_loc& loc,
112                        const CSeqTable_single_data& data,
113                        const CSeqTableSetLocField& setter) const;
114     bool UpdateSeq_loc(CSeq_loc& loc,
115                        const CSeqTable_multi_data& data,
116                        size_t index,
117                        const CSeqTableSetLocField& setter) const;
118     void UpdateSeq_feat(CSeq_feat& feat, size_t row,
119                         const CSeqTableSetFeatField& setter) const;
120     void UpdateSeq_feat(CSeq_feat& feat,
121                         const CSeqTable_single_data& data,
122                         const CSeqTableSetFeatField& setter) const;
123     bool UpdateSeq_feat(CSeq_feat& feat,
124                         const CSeqTable_multi_data& data,
125                         size_t index,
126                         const CSeqTableSetFeatField& setter) const;
127 
128 protected:
129     // report unset value with exception
130     bool x_ThrowUnsetValue(void) const;
131 
132 private:
133     CConstRef<CSeqTable_column> m_Column;
134 };
135 
136 class CSeqTableLocColumns
137 {
138 public:
139     CSeqTableLocColumns(const char* field_name,
140                         CSeqTable_column_info::EField_id base_value);
141     ~CSeqTableLocColumns();
142 
143     bool AddColumn(const CSeqTable_column& column);
144 
145     void SetColumn(CSeqTableColumnInfo& field,
146                    const CSeqTable_column& column);
147     void AddExtraColumn(const CSeqTable_column& column,
148                         const CSeqTableSetLocField* setter);
149 
150     void ParseDefaults(void);
151 
152     void UpdateSeq_loc(size_t row,
153                        CRef<CSeq_loc>& seq_loc,
154                        CRef<CSeq_point>& seq_pnt,
155                        CRef<CSeq_interval>& seq_int) const;
156 
157     CConstRef<CSeq_loc> GetLoc(size_t row) const;
158     CConstRef<CSeq_id> GetId(size_t row) const;
159     CSeq_id_Handle GetIdHandle(size_t row) const;
160     TSeqPos GetFrom(size_t row) const;
161     CRange<TSeqPos> GetRange(size_t row) const;
162     ENa_strand GetStrand(size_t row) const;
163 
IsSet(void) const164     bool IsSet(void) const {
165         return m_Is_set;
166     }
IsRealLoc(void) const167     bool IsRealLoc(void) const {
168         return m_Is_real_loc;
169     }
170 
171 
172     void SetTableKeyAndIndex(size_t row,
173                              SAnnotObject_Key& key,
174                              SAnnotObject_Index& index) const;
175 
176 private:
177     friend class CSeqTableInfo;
178 
179     CTempString m_FieldName;
180     CSeqTable_column_info::EField_id m_BaseValue;
181 
182     bool m_Is_set, m_Is_real_loc;
183     bool m_Is_simple, m_Is_probably_simple;
184     bool m_Is_simple_point, m_Is_simple_interval, m_Is_simple_whole;
185 
186     typedef CSeqTableSetLocField TSetter;
187     typedef pair<CSeqTableColumnInfo, CConstRef<TSetter> > TColumnInfo;
188     typedef vector<TColumnInfo> TExtraColumns;
189 
190     CSeqTableColumnInfo m_Loc;
191     CSeqTableColumnInfo m_Id;
192     CSeqTableColumnInfo m_Gi;
193     CSeqTableColumnInfo m_From;
194     CSeqTableColumnInfo m_To;
195     CSeqTableColumnInfo m_Strand;
196     TExtraColumns m_ExtraColumns;
197 
198     CSeq_id_Handle m_DefaultIdHandle;
199 
200 private:
201     CSeqTableLocColumns(const CSeqTableLocColumns&);
202     void operator=(const CSeqTableLocColumns&);
203 };
204 
205 
206 class CSeqTableInfo : public CObject
207 {
208 public:
209     explicit CSeqTableInfo(const CSeq_table& feat_table, bool is_feat);
210     explicit CSeqTableInfo(const CSeq_table& feat_table);
211     ~CSeqTableInfo();
212 
IsFeatTable(void) const213     bool IsFeatTable(void) const {
214         return m_IsFeatTable;
215     }
216 
GetNumRows(void) const217     size_t GetNumRows(void) const {
218         return m_Seq_table->GetNum_rows();
219     }
220 
221     void UpdateSeq_feat(size_t row,
222                         CRef<CSeq_feat>& seq_feat,
223                         CRef<CSeq_point>& seq_pnt,
224                         CRef<CSeq_interval>& seq_int) const;
225 
226     CConstRef<CSeq_loc> GetTableLocation(void) const;
227     TSeqPos GetSortedMaxLength(void) const;
228 
IsSorted(void) const229     bool IsSorted(void) const {
230         return m_IsSorted;
231     }
232 
233     SAnnotTypeSelector GetType(void) const;
234 
GetLocation(void) const235     const CSeqTableLocColumns& GetLocation(void) const {
236         return m_Location;
237     }
GetProduct(void) const238     const CSeqTableLocColumns& GetProduct(void) const {
239         return m_Product;
240     }
RowIsDisabled(size_t row) const241     bool RowIsDisabled(size_t row) const {
242         return m_Disabled.GetBool(row);
243     }
IsPartial(size_t row) const244     bool IsPartial(size_t row) const {
245         return m_Partial.GetBool(row);
246     }
247 
GetLocationId(size_t row) const248     CConstRef<CSeq_id> GetLocationId(size_t row) const {
249         return GetLocation().GetId(row);
250     }
GetLocationFrom(size_t row) const251     TSeqPos GetLocationFrom(size_t row) const {
252         return GetLocation().GetFrom(row);
253     }
GetLocationRange(size_t row) const254     CRange<TSeqPos> GetLocationRange(size_t row) const {
255         return GetLocation().GetRange(row);
256     }
GetLocationStrand(size_t row) const257     ENa_strand GetLocationStrand(size_t row) const {
258         return GetLocation().GetStrand(row);
259     }
260 
261     bool HasLabel(size_t row) const;
262     string GetLabel(size_t row) const;
263 
264     bool MatchBitFilter(const SAnnotSelector& sel, size_t row) const;
265 
266     // returns null if column not found
267     const CSeqTableColumnInfo* FindColumn(int field_id) const;
268     const CSeqTableColumnInfo* FindColumn(const string& field_name) const;
269     // throws an exception if column not found
270     const CSeqTableColumnInfo& GetColumn(int field_id) const;
271     const CSeqTableColumnInfo& GetColumn(const string& field_name) const;
272 
273     static bool IsGoodFeatTable(const CSeq_table& table);
274 
275 private:
276     typedef CSeqTableSetFeatField TSetter;
277     typedef pair<CSeqTableColumnInfo, CConstRef<TSetter> > TColumnInfo;
278     typedef vector<TColumnInfo> TExtraColumns;
279 
280     void x_Initialize(const CSeq_table& table);
281     bool x_IsSorted(void) const;
282 
283     CConstRef<CSeq_table> m_Seq_table;
284     bool m_IsFeatTable;
285     bool m_IsSorted;
286     CSeqTableColumnInfo m_Disabled;
287     CSeqTableLocColumns m_Location;
288     CSeqTableLocColumns m_Product;
289     CSeqTableColumnInfo m_Partial;
290     TExtraColumns m_ExtraColumns;
291 
292     CConstRef<CSeq_loc> m_TableLocation;
293     TSeqPos m_SortedMaxLength;
294 
295     typedef map<int, CSeqTableColumnInfo> TColumnsById;
296     typedef map<string, CSeqTableColumnInfo> TColumnsByName;
297     TColumnsById m_ColumnsById;
298     TColumnsByName m_ColumnsByName;
299 
300 private:
301     CSeqTableInfo(const CSeqTableInfo&);
302     void operator=(const CSeqTableInfo&);
303 };
304 
305 
306 END_SCOPE(objects)
307 END_NCBI_SCOPE
308 
309 #endif  // SEQ_TABLE_INFO__HPP
310