NCBI C++ ToolKit
dblink_field.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: dblink_field.cpp 46979 2022-03-29 20:23:37Z asztalos $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Colleen Bollin
27  */
28 
29 
30 #include <ncbi_pch.hpp>
31 
34 #include <objmgr/seqdesc_ci.hpp>
35 #include <objmgr/bioseq_ci.hpp>
37 
38 #include <gui/objutils/utils.hpp>
43 
47 
51 
52 
53 bool CDBLinkField::SetVal(CObject& object, const string & newValue, EExistingText existing_text)
54 {
55  bool rval = false;
56  CSeqdesc* seqdesc = dynamic_cast<CSeqdesc*>(&object);
57  CUser_object* user = dynamic_cast<CUser_object*>(&object);
58 
59  if (seqdesc && seqdesc->IsUser()) {
60  user = &(seqdesc->SetUser());
61  }
62  if (user && IsDBLink(*user)) {
63  bool found = false;
64  if (user->IsSetData()) {
65  CUser_object::TData::iterator it = user->SetData().begin();
66  while (it != user->SetData().end()) {
67  if ((*it)->IsSetLabel() && (*it)->GetLabel().IsStr()) {
69  if (check == m_FieldType) {
70  rval |= SetVal(**it, newValue, existing_text);
71  found = true;
72  }
73  }
74  if (!(*it)->IsSetData()) {
75  it = user->SetData().erase(it);
76  } else {
77  it++;
78  }
79  }
80  }
81  if (!found && (m_ConstraintFieldType != m_FieldType || !m_StringConstraint)) {
82  CRef<CUser_field> new_field(new CUser_field());
84  if (SetVal(*new_field, newValue, eExistingText_replace_old)) {
85  user->SetData().push_back(new_field);
86  rval = true;
87  }
88  }
89 
90  // if User object now has no fields, reset so it will be detected as empty
91  if (user->GetData().empty()) {
92  user->ResetData();
93  }
94  }
95  return rval;
96 }
97 
98 
99 bool CDBLinkField::SetVal(CUser_field& field, const string & newValue, EExistingText existing_text)
100 {
101  bool rval = false;
102 
103  if (field.IsSetData()) {
104  if (field.GetData().IsStr()) {
105  string curr_val = field.GetData().GetStr();
107  || m_StringConstraint->DoesTextMatch(curr_val)) {
108  if (existing_text == eExistingText_add_qual) {
109  field.SetData().SetStrs().push_back(curr_val);
110  field.SetData().SetStrs().push_back(newValue);
111  rval = true;
112  } else if (AddValueToString(curr_val, newValue, existing_text)) {
113  field.SetData().SetStrs().push_back(curr_val);
114  rval = true;
115  }
116  }
117  } else if (field.GetData().IsStrs()) {
118  if ((existing_text == eExistingText_add_qual || field.GetData().GetStrs().empty())
120  field.SetData().SetStrs().push_back(newValue);
121  rval = true;
122  } else {
123  NON_CONST_ITERATE(CUser_field::TData::TStrs, s, field.SetData().SetStrs()) {
125  || m_StringConstraint->DoesTextMatch(*s)) {
126  rval |= AddValueToString(*s, newValue, existing_text);
127  }
128  }
129  }
130  } else if (field.GetData().Which() == CUser_field::TData::e_not_set) {
132  field.SetData().SetStrs().push_back(newValue);
133  rval = true;
134  }
135  }
137  field.SetData().SetStrs().push_back(newValue);
138  rval = true;
139  }
140 
141  return rval;
142 }
143 
144 
145 string CDBLinkField::GetVal( const CObject& object)
146 {
147  vector<string> vals = GetVals(object);
148  if (vals.size() > 0) {
149  return vals[0];
150  } else {
151  return "";
152  }
153 
154 }
155 
156 
157 vector<string> CDBLinkField::GetVals(const CObject& object)
158 {
159  vector<string> vals;
160  const CSeqdesc* seqdesc = dynamic_cast<const CSeqdesc*>(&object);
161  const CUser_object* user = dynamic_cast<const CUser_object*>(&object);
162 
163  if (seqdesc && seqdesc->IsUser()) {
164  user = &(seqdesc->GetUser());
165  }
166  if (IsDBLink(*user) && user->IsSetData()) {
167  CUser_object::TData::const_iterator it = user->GetData().begin();
168  while (it != user->GetData().end()) {
169  if ((*it)->IsSetLabel() && (*it)->GetLabel().IsStr() && (*it)->IsSetData()) {
171  if (check == m_FieldType) {
172  switch((*it)->GetData().Which()) {
174  vals.push_back((*it)->GetData().GetStr());
175  break;
177  ITERATE(CUser_field::TData::TStrs, s, (*it)->GetData().GetStrs()) {
178  vals.push_back(*s);
179  }
180  break;
181  default:
182  //not handling other types of fields
183  break;
184  }
185  }
186  }
187  ++it;
188  }
189  }
190  return vals;
191 }
192 
193 
195 {
197 }
198 
199 
200 void CDBLinkField::SetConstraint(const string& field_name, CConstRef<edit::CStringConstraint> string_constraint)
201 {
202  // constraints only apply for authors, since there could be more than one
205  string_constraint.Reset(NULL);
206  } else {
208  m_StringConstraint->Assign(*string_constraint);
209  }
210 }
211 
212 vector<CConstRef<CObject> > CDBLinkField::GetObjects(CBioseq_Handle bsh)
213 {
214  vector<CConstRef<CObject> > objects;
215 
216  CSeqdesc_CI desc_ci(bsh, CSeqdesc::e_User);
217  while (desc_ci) {
218  if (IsDBLink(desc_ci->GetUser())) {
219  CConstRef<CObject> object;
220  object.Reset(&(*desc_ci));
221  objects.push_back(object);
222  }
223  ++desc_ci;
224  }
225 
226  return objects;
227 }
228 
229 
230 vector<CRef<CApplyObject> > CDBLinkField::GetApplyObjects(CBioseq_Handle bsh)
231 {
232  vector<CRef<CApplyObject> > objects;
233 
234  // add existing descriptors
235  CSeqdesc_CI desc_ci(bsh, CSeqdesc::e_User);
236  while (desc_ci) {
237  if (IsDBLink(desc_ci->GetUser())) {
238  CRef<CApplyObject> obj(new CApplyObject(bsh, *desc_ci));
239  objects.push_back(obj);
240  }
241  ++desc_ci;
242  }
243 
244  if (objects.empty()) {
245  CRef<CApplyObject> new_obj(new CApplyObject(bsh, "DBLink"));
246  objects.push_back(new_obj);
247  }
248 
249  return objects;
250 }
251 
252 
253 vector<CConstRef<CObject> > CDBLinkField::GetObjects(CSeq_entry_Handle seh, const string& constraint_field, CRef<edit::CStringConstraint> string_constraint)
254 {
255  vector<CConstRef<CObject> > objs;
256  CRef<CScope> scope(&seh.GetScope());
257 
258  CBioseq_CI bi (seh, objects::CSeq_inst::eMol_na);
259  while (bi) {
260  if (NStr::EqualNocase(constraint_field, kFieldTypeSeqId)) {
261  if (edit::CSeqIdGuesser::DoesSeqMatchConstraint(*bi, string_constraint)) {
262  vector<CConstRef<CObject> > these_objs = GetObjects(*bi);
263  objs.insert(objs.end(), these_objs.begin(), these_objs.end());
264  }
265  } else {
266  vector<CConstRef<CObject> > these_objs = GetObjects(*bi);
267  ITERATE (vector<CConstRef<CObject> >, it, these_objs) {
268  if (DoesObjectMatchFieldConstraint (**it, constraint_field, string_constraint, scope)) {
269  objs.push_back (*it);
270  }
271  }
272  }
273  ++bi;
274  }
275 
276  return objs;
277 }
278 
279 
280 bool CDBLinkField::IsEmpty(const CObject& object) const
281 {
282  bool rval = false;
283  const CSeqdesc* seqdesc = dynamic_cast<const CSeqdesc*>(&object);
284  const CUser_object* user = dynamic_cast<const CUser_object*>(&object);
285  if (seqdesc && seqdesc->IsUser()) {
286  user = &(seqdesc->GetUser());
287  }
288  if (user && IsDBLink(*user)) {
289  if (!user->IsSetData() || user->GetData().empty()) {
290  rval = true;
291  }
292  }
293 
294  return rval;
295 }
296 
297 
299 {
300  CSeqdesc* seqdesc = dynamic_cast<CSeqdesc*>(&object);
301  CUser_object* user = dynamic_cast<CUser_object*>(&object);
302 
303  if (seqdesc && seqdesc->IsUser()) {
304  user = &(seqdesc->SetUser());
305  }
306  if (user && user->IsSetData()) {
307  CUser_object::TData::iterator it = user->SetData().begin();
308  while (it != user->SetData().end()) {
309  bool do_erase = false;
310  if ((*it)->IsSetLabel() && (*it)->GetLabel().IsStr()) {
312  if (check == m_FieldType) {
313  do_erase = true;
314  }
315  }
316  if (do_erase) {
317  it = user->SetData().erase(it);
318  } else {
319  it++;
320  }
321  }
322  if (user->GetData().empty()) {
323  user->ResetData();
324  }
325  }
326 }
327 
328 
329 vector<CConstRef<CObject> > CDBLinkField::GetRelatedObjects(const CObject& object, CRef<CScope> scope)
330 {
331  vector<CConstRef<CObject> > related;
332 
333  const CSeqdesc * obj_desc = dynamic_cast<const CSeqdesc *>(&object);
334  const CSeq_feat * obj_feat = dynamic_cast<const CSeq_feat *>(&object);
335 
336  if (obj_feat) {
337  // find closest related DBLink User Objects
338  CBioseq_Handle bsh = scope->GetBioseqHandle(obj_feat->GetLocation());
339  related = GetObjects(bsh);
340  } else if (obj_desc) {
341  if (obj_desc->IsUser() && IsDBLink(obj_desc->GetUser())) {
342  CConstRef<CObject> obj(obj_desc);
343  related.push_back(obj);
344  } else {
345  CSeq_entry_Handle seh = edit::GetSeqEntryForSeqdesc(scope, *obj_desc);
347  }
348  }
349 
350  return related;
351 }
352 
353 
354 vector<CConstRef<CObject> > CDBLinkField::GetRelatedObjects(const CApplyObject& object)
355 {
356  vector<CConstRef<CObject> > related;
357 
358  const CSeqdesc * obj_desc = dynamic_cast<const CSeqdesc *>(&(object.GetObject()));
359  const CSeq_feat * obj_feat = dynamic_cast<const CSeq_feat *>(&(object.GetObject()));
360 
361  if (obj_feat) {
362  related = GetObjects(object.GetSEH(), "", CRef<edit::CStringConstraint>(NULL));
363  } else if (obj_desc) {
364  if (obj_desc->IsUser() && IsDBLink(obj_desc->GetUser())) {
365  CConstRef<CObject> obj(obj_desc);
366  related.push_back(obj);
367  } else {
369  }
370  }
371 
372  return related;
373 }
374 
376 {
377  if (user.IsSetType()
378  && user.GetType().IsStr()
379  && NStr::EqualNocase(user.GetType().GetStr(),"DBLink")) {
380  return true;
381  } else {
382  return false;
383  }
384 }
385 
CSeq_entry_Handle GetSeqEntryForSeqdesc(CRef< CScope > scope, const CSeqdesc &seq_desc)
CBioseq_CI –.
Definition: bioseq_ci.hpp:69
CBioseq_Handle –.
CConstRef –.
Definition: ncbiobj.hpp:1266
static EDBLinkFieldType GetTypeForLabel(string label)
static string GetLabelForType(EDBLinkFieldType field_type)
virtual bool IsEmpty(const CObject &object) const
CDBLinkFieldType::EDBLinkFieldType m_FieldType
CRef< objects::edit::CStringConstraint > m_StringConstraint
static bool IsDBLink(const CUser_object &user)
virtual string GetLabel() const
virtual void SetConstraint(const string &field_name, CConstRef< edit::CStringConstraint > string_constraint)
virtual vector< CRef< objects::edit::CApplyObject > > GetApplyObjects(CBioseq_Handle bsh)
virtual string GetVal(const CObject &object)
CDBLinkFieldType::EDBLinkFieldType m_ConstraintFieldType
virtual vector< string > GetVals(const CObject &object)
virtual vector< CConstRef< CObject > > GetObjects(CBioseq_Handle bsh)
virtual void ClearVal(CObject &object)
virtual vector< CConstRef< CObject > > GetRelatedObjects(const CObject &object, CRef< CScope > scope)
virtual bool SetVal(CObject &object, const string &newValue, objects::edit::EExistingText existing_text)
CObject –.
Definition: ncbiobj.hpp:180
CSeq_entry_Handle –.
namespace ncbi::objects::
Definition: Seq_feat.hpp:58
CSeqdesc_CI –.
Definition: seqdesc_ci.hpp:65
const char * kFieldTypeSeqId
#define check(s)
Definition: describecol2.c:21
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
#define NULL
Definition: ncbistd.hpp:225
CBioseq_Handle GetBioseqHandle(const CSeq_id &id)
Get bioseq handle by seq-id.
Definition: scope.cpp:95
CScope & GetScope(void) const
Get scope this handle belongs to.
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:1439
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static bool EqualNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive equality of a substring with another string.
Definition: ncbistr.hpp:5347
const TStr & GetStr(void) const
Get the variant data.
bool IsSetData(void) const
the object itself Check if a value has been assigned to Data data member.
bool IsStr(void) const
Check if variant Str is selected.
Definition: Object_id_.hpp:291
bool IsSetType(void) const
type of object within class Check if a value has been assigned to Type data member.
bool IsStrs(void) const
Check if variant Strs is selected.
const TStrs & GetStrs(void) const
Get the variant data.
const TData & GetData(void) const
Get the Data member data.
bool IsStr(void) const
Check if variant Str is selected.
TData & SetData(void)
Assign a value to Data data member.
const TStr & GetStr(void) const
Get the variant data.
Definition: Object_id_.hpp:297
void SetLabel(TLabel &value)
Assign a value to Label data member.
const TData & GetData(void) const
Get the Data member data.
void ResetData(void)
Reset Data data member.
void SetData(TData &value)
Assign a value to Data data member.
const TType & GetType(void) const
Get the Type member data.
bool IsSetData(void) const
Check if a value has been assigned to Data data member.
vector< CStringUTF8 > TStrs
E_Choice Which(void) const
Which variant is currently selected.
@ e_not_set
No variant selected.
const TLocation & GetLocation(void) const
Get the Location member data.
Definition: Seq_feat_.hpp:1117
const TUser & GetUser(void) const
Get the variant data.
Definition: Seqdesc_.cpp:384
TUser & SetUser(void)
Select the variant.
Definition: Seqdesc_.cpp:390
bool IsUser(void) const
Check if variant User is selected.
Definition: Seqdesc_.hpp:1122
@ e_User
user defined object
Definition: Seqdesc_.hpp:124
bool DoesObjectMatchFieldConstraint(const CObject &object, const string &field_name, CRef< objects::edit::CStringConstraint > string_constraint, CRef< objects::CScope > scope)
Definition: fix_pub.hpp:45
EExistingText
@ eExistingText_add_qual
@ eExistingText_replace_old
bool AddValueToString(string &str, const string &value, EExistingText existing_text)
Add text to an existing string, using the "existing_text" directive to combine new text with existing...
Modified on Fri Sep 20 14:57:31 2024 by modify_doxy.py rev. 669887