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

Go to the SVN repository for this file.

1 /* $Id: remove_structuredcomment_keyword.cpp 47479 2023-05-02 13:24:02Z ucko $
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: Igor Filippov
27  */
28 
29 
30 #include <ncbi_pch.hpp>
31 #include <objmgr/scope.hpp>
38 
41 
42 class CValidError_desc;
43 
45 {
46  m_PrefixToKeyword["##MIGS-Data-START##"] = "GSC:MIGS:2.1";
47  m_PrefixToKeyword["##MIMS-Data-START##"] = "GSC:MIMS:2.1";
48  m_PrefixToKeyword["##MIENS-Data-START##"] = "GSC:MIENS:2.1";
49  m_PrefixToKeyword["##MIGS:3.0-Data-START##"] = "GSC:MIxS;MIGS:3.0";
50  m_PrefixToKeyword["##MIMS:3.0-Data-START##"] = "GSC:MIxS;MIMS:3.0";
51  m_PrefixToKeyword["##MIMARKS:3.0-Data-START##"] = "GSC:MIxS;MIMARKS:3.0";
53  m_Keywords.insert(f->second);
54 }
55 
57 {
58  ApplyToCSeq_entry_user (tse, *(tse.GetCompleteSeq_entry()), composite);
59  ApplyToCSeq_entry_genbank(tse, *(tse.GetCompleteSeq_entry()), composite);
60  // doesn't really apply to seq-feat in this case it seems
61 }
62 
63 // get keyword for prefix
65 {
67  if (f != m_PrefixToKeyword.end())
68  return f->second;
69  else
70  return "";
71 }
72 
74 {
75  bool ret = false;
76  if (tse)
77  {
78  CRef<CCmdComposite> composite(new CCmdComposite(title));
79  apply_to_seq_and_feat(tse,composite);
80  cmdProcessor->Execute(composite.GetPointer());
81  ret = true;
82  }
83  return ret;
84 }
85 
86 
87 
89 {
90  auto objmgr = CObjectManager::GetInstance();
91  validator::CValidator validator(*objmgr);
92 
94  if ((*it)->IsUser()) {
95  const CUser_object& user = (*it)->GetUser();
96 
97  if (validator.IsValidStructuredComment(**it)) {
98  if (CComment_rule::IsStructuredComment(user) && user.IsSetData()) {
99  const CUser_field& prefix = user.GetField("StructuredCommentPrefix");
100  if (prefix.IsSetData() && prefix.GetData().IsStr()) {
101  const string& pfx = prefix.GetData().GetStr();
102  string keyword = PrefixToKeyword(pfx);
103  if (!keyword.empty())
104  m_ValidKeywords.insert(keyword);
105  }
106  }
107  }
108  }
109  }
110 
111  if (se.IsSet()) {
113  ApplyToCSeq_entry_user (tse, **it, composite);
114  }
115  }
116 }
117 
118 // remove keyword - it's actually in seqdesc->genbank->keywords and not in the structured comment itself
120 {
122  {
123  if ((*it)->IsGenbank() && (*it)->GetGenbank().CanGetKeywords())
124  {
125  CRef<CSerialObject> edited_object;
126  edited_object.Reset((CSerialObject*)CGB_block::GetTypeInfo()->Create());
127  edited_object->Assign((*it)->GetGenbank());
128  CGB_block& edited = dynamic_cast<CGB_block&>(*edited_object);
129  CGB_block::TKeywords &words = edited.SetKeywords();
130  set<string> to_delete;
131  for (CGB_block::TKeywords::iterator w = words.begin(); w != words.end(); ++w)
133  to_delete.insert(*w);
134  auto size1 = words.size();
135  for (set<string>::iterator w = to_delete.begin(); w != to_delete.end(); ++w)
136  words.remove(*w);
137  auto size2 = words.size();
138  if (size1 != size2)
139  {
140  if (size2 > 0)
141  {
143  CObject* actual = (CObject*) &((*it)->GetGenbank());
144  cmd->Add(actual, CConstRef<CObject>(edited_object));
145  composite->AddCommand(*cmd);
146  }
147  else // remove the whole seq-desc if empty
148  {
150  CIRef<IEditCommand> cmdDelDesc(new CCmdDelDesc(seh, **it));
151  composite->AddCommand(*cmdDelDesc);
152  }
153  }
154  }
155  }
156 
157  if (se.IsSet()) {
159  ApplyToCSeq_entry_genbank (tse, **it, composite);
160  }
161  }
162 
163 }
164 
165 
167 {
168  for (set<string>::const_iterator keyword = m_ValidKeywords.begin(); keyword != m_ValidKeywords.end(); keyword++)
169  {
170  bool modified = false;
171  add_to_existing_genbank(tse, se, composite, *keyword, modified);
172  if (!modified)
173  {
174  // create new GB_block
175  CRef<CSeqdesc> descr(new CSeqdesc());
176  descr->Select(CSeqdesc::e_Genbank);
177  descr->SetGenbank().ResetKeywords();
178  descr->SetGenbank().SetKeywords().push_back(*keyword);
180  CIRef<IEditCommand> cmdAddDesc(new CCmdCreateDesc(seh, *descr));
181  composite->AddCommand(*cmdAddDesc);
182  }
183  }
184 }
185 
186 void CAddStructuredCommentKeyword::add_to_existing_genbank (CSeq_entry_Handle tse, const CSeq_entry& se, CCmdComposite* composite, const string keyword, bool &modified)
187 {
189  {
190  if ((*it)->IsGenbank() && (*it)->GetGenbank().CanGetKeywords())
191  {
192  CRef<CSerialObject> edited_object;
193  edited_object.Reset((CSerialObject*)CGB_block::GetTypeInfo()->Create());
194  edited_object->Assign((*it)->GetGenbank());
195  CGB_block& edited = dynamic_cast<CGB_block&>(*edited_object);
196  CGB_block::TKeywords &words = edited.SetKeywords();
197  bool found = false;
198  for (CGB_block::TKeywords::iterator w = words.begin(); w != words.end(); ++w)
199  if (*w == keyword) found = true;
200  if (!found)
201  {
202  words.push_back(keyword);
204  CObject* actual = (CObject*) &((*it)->GetGenbank());
205  cmd->Add(actual, CConstRef<CObject>(edited_object));
206  composite->AddCommand(*cmd);
207  modified = true;
208  }
209  if (found) modified = true;
210  }
211  }
212 
213  if (se.IsSet()) {
215  add_to_existing_genbank (tse, **it, composite, keyword, modified);
216  }
217  }
218 
219 }
220 
221 
void add_to_existing_genbank(objects::CSeq_entry_Handle tse, const CSeq_entry &se, CCmdComposite *composite, string keyword, bool &modified)
virtual void ApplyToCSeq_entry_genbank(objects::CSeq_entry_Handle tse, const CSeq_entry &se, CCmdComposite *composite)
virtual void ApplyToCSeq_entry_user(CSeq_entry_Handle tse, const CSeq_entry &se, CCmdComposite *composite)
virtual void ApplyToCSeq_entry_genbank(objects::CSeq_entry_Handle tse, const CSeq_entry &se, CCmdComposite *composite)=0
bool apply(CSeq_entry_Handle tse, ICommandProccessor *cmdProcessor, string title)
CChangeUnindexedObjectCommand< objects::CGB_block > CChangeGBblockCommand
void apply_to_seq_and_feat(CSeq_entry_Handle tse, CCmdComposite *composite)
void AddCommand(IEditCommand &command)
static bool IsStructuredComment(const CUser_object &user)
CObject –.
Definition: ncbiobj.hpp:180
virtual void ApplyToCSeq_entry_genbank(objects::CSeq_entry_Handle tse, const CSeq_entry &se, CCmdComposite *composite)
CSeq_entry_Handle –.
Definition: Seq_entry.hpp:56
Base class for all serializable objects.
Definition: serialbase.hpp:150
const CUser_field & GetField(const string &str, const string &delim=".", NStr::ECase use_case=NStr::eCase) const
Access a named field in this user object.
Definition: User_object.cpp:71
Undo/Redo interface for editing operations.
virtual void Execute(IEditCommand *command, wxWindow *window=0)=0
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
iterator_bool insert(const value_type &val)
Definition: set.hpp:149
const_iterator begin() const
Definition: set.hpp:135
const_iterator find(const key_type &key) const
Definition: set.hpp:137
const_iterator end() const
Definition: set.hpp:136
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
CSeq_entry_Handle GetSeq_entryHandle(CDataLoader *loader, const TBlobId &blob_id, EMissing action=eMissing_Default)
Get Seq-entry handle by its blob-id, with possible loading.
Definition: scope.cpp:113
CConstRef< CSeq_entry > GetCompleteSeq_entry(void) const
Complete and get const reference to the seq-entry.
CScope & GetScope(void) const
Get scope this handle belongs to.
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
TKeywords & SetKeywords(void)
Assign a value to Keywords data member.
Definition: GB_block_.hpp:532
void ResetKeywords(void)
Reset Keywords data member.
Definition: GB_block_.cpp:63
list< string > TKeywords
Definition: GB_block_.hpp:93
bool IsSetData(void) const
the object itself Check if a value has been assigned to Data data member.
const TSet & GetSet(void) const
Get the variant data.
Definition: Seq_entry_.cpp:124
bool IsSet(void) const
Check if variant Set is selected.
Definition: Seq_entry_.hpp:263
TGenbank & SetGenbank(void)
Select the variant.
Definition: Seqdesc_.cpp:340
void Select(E_Choice index, EResetVariant reset=eDoResetVariant)
Select the requested variant if needed.
@ e_Genbank
GenBank specific info.
Definition: Seqdesc_.hpp:121
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
static const char * prefix[]
Definition: pcregrep.c:405
USING_SCOPE(objects)
#define FOR_EACH_SEQENTRY_ON_SEQSET(Itr, Var)
FOR_EACH_SEQENTRY_ON_SEQSET EDIT_EACH_SEQENTRY_ON_SEQSET.
#define FOR_EACH_SEQDESC_ON_SEQENTRY(Itr, Var)
FOR_EACH_SEQDESC_ON_SEQENTRY EDIT_EACH_SEQDESC_ON_SEQENTRY.
Modified on Fri May 24 14:51:59 2024 by modify_doxy.py rev. 669887