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

Go to the SVN repository for this file.

1 /* $Id: cmd_factory.cpp 45144 2020-06-08 16:24:37Z grichenk $
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: Roman Katargin
27 *
28 * File Description:
29 */
30 
31 #include <ncbi_pch.hpp>
33 
37 
44 #include <objects/pub/Pub.hpp>
45 #include <objects/pub/Pub_set.hpp>
47 
48 #include <objmgr/feat_ci.hpp>
49 
52 
53 static void s_GetCitIds(const CPub& pub, int& pmid, int& muid);
54 static void s_GetCitIds(const CCit_art& gen, int& pmid, int& muid);
55 static void s_GetCitIds(const CMedline_entry& mle, int& pmid, int& muid);
56 static void s_GetCitIds(const CCit_gen& art, int& pmid, int& muid);
57 
58 static bool s_CitMatches(const CPub& pub, int pmid, int muid, const string& uniqueStr);
59 
60 static bool s_IsOnlySerial(const CPub& pub)
61 {
62  if (!pub.IsGen()) {
63  return false;
64  }
65 
66  const CCit_gen& gen = pub.GetGen();
67 
68  if ( !gen.IsSetCit() )
69  {
70  if (!gen.IsSetJournal() && !gen.IsSetDate() &&
71  gen.IsSetSerial_number() && gen.GetSerial_number() > 0) {
72  return true;
73  }
74  }
75 
76  return false;
77 }
78 
81 {
82  int pmid = 0, muid = 0;
83  string uniqueStr;
84 
85  ITERATE (CPub_equiv::Tdata, it, pubs.Get()) {
86  const CPub& pub = **it;
87  s_GetCitIds(pub, pmid, muid);
88  }
89 
90  if (pmid == 0 && muid == 0) {
91  ITERATE (CPub_equiv::Tdata, it, pubs.Get()) {
92  const CPub& pub = **it;
93  if (pub.IsMuid() || pub.IsPmid() || pub.IsPat_id() || pub.IsEquiv()) {
94  continue;
95  }
96  if (!s_IsOnlySerial(pub)) {
98  }
99  }
100  }
101 
102  CRef<CCmdComposite> composite;
103 
104  for (CFeat_CI mf(seh); mf; ++mf) {
105  if (!mf->IsSetCit())
106  continue;
107 
108  CRef<CSeq_feat> new_feat(new CSeq_feat());
109  new_feat->Assign(mf->GetOriginalFeature());
110  if (!new_feat->CanGetCit())
111  continue;
112 
113  CPub_set& pubs = new_feat->SetCit();
114  if (!pubs.IsPub())
115  continue;
116 
117  CPub_set::TPub& cont = pubs.SetPub();
118 
119  bool modified = false;
120 
121  for (CPub_set::TPub::iterator it = cont.begin(); it != cont.end(); ) {
122  if (s_CitMatches(**it, pmid, muid, uniqueStr)) {
123  modified = true;
124  it = pubs.SetPub().erase(it);
125  }
126  else
127  ++it;
128  }
129 
130  if (modified) {
131  if (!composite)
132  composite.Reset(new CCmdComposite("Modify Feature(s)"));
133 
134  CIRef<IEditCommand> chgFeat(new CCmdChangeSeq_feat(*mf, *new_feat));
135  composite->AddCommand(*chgFeat);
136  }
137  }
138 
139  return CIRef<IEditCommand>(composite.GetPointerOrNull());
140 }
141 
142 static void s_GetCitIds(const CPub& pub, int& pmid, int& muid)
143 {
144  switch (pub.Which()) {
145  case CPub::e_Gen:
146  s_GetCitIds(pub.GetGen(), pmid, muid);
147  break;
148 
149  case CPub::e_Medline:
150  s_GetCitIds(pub.GetMedline(), pmid, muid);
151  break;
152 
153  case CPub::e_Muid:
154  if (muid == 0) {
155  muid = ENTREZ_ID_TO(int, pub.GetMuid());
156  }
157  break;
158 
159  case CPub::e_Pmid:
160  if (pmid == 0) {
161  pmid = ENTREZ_ID_TO(int, pub.GetPmid().Get());
162  }
163  break;
164 
165  case CPub::e_Article:
166  s_GetCitIds(pub.GetArticle(), pmid, muid);
167  break;
168 
169  default:
170  break;
171  }
172 }
173 
174 static void s_GetCitIds(const CCit_gen& gen, int& pmid, int& muid)
175 {
176  if (gen.CanGetMuid() && muid == 0) {
177  muid = ENTREZ_ID_TO(int, gen.GetMuid());
178  }
179  if (gen.CanGetPmid() && pmid == 0) {
180  pmid = ENTREZ_ID_TO(int, gen.GetPmid().Get());
181  }
182 }
183 
184 static void s_GetCitIds(const CMedline_entry& mle, int& pmid, int& muid)
185 {
186  if (mle.CanGetUid() && muid == 0) {
187  muid = ENTREZ_ID_TO(int, mle.GetUid());
188  }
189 
190  if (mle.CanGetPmid() && pmid == 0) {
191  pmid = ENTREZ_ID_TO(int, mle.GetPmid().Get());
192  }
193 
194  if (mle.CanGetCit()) {
195  s_GetCitIds(mle.GetCit(), pmid, muid);
196  }
197 }
198 
199 static void s_GetCitIds(const CCit_art& art, int& pmid, int& muid)
200 {
201  if (!art.CanGetIds()) {
202  return;
203  }
204 
205  ITERATE (CArticleIdSet::Tdata, it, art.GetIds().Get()) {
206  switch ((*it)->Which()) {
208  if (pmid == 0) {
209  pmid = ENTREZ_ID_TO(int, (*it)->GetPubmed().Get());
210  }
211  break;
213  if (muid == 0) {
214  muid = ENTREZ_ID_TO(int, (*it)->GetMedline().Get());
215  }
216  break;
217  default:
218  break;
219  }
220  }
221 }
222 
223 static bool s_CitMatches(const CPub& pub, int pmid, int muid, const string& uniqueStr)
224 {
225  switch (pub.Which()) {
226  case CPub::e_Muid:
227  return ENTREZ_ID_TO(int, pub.GetMuid()) == muid;
228  case CPub::e_Pmid:
229  return ENTREZ_ID_TO(int, pub.GetPmid().Get()) == pmid;
230  case CPub::e_Equiv:
231  ITERATE (CPub::TEquiv::Tdata, it, pub.GetEquiv().Get()) {
232  if ( s_CitMatches(**it, pmid, muid, uniqueStr) ) {
233  return true;
234  }
235  }
236  break;
237  default:
238  // compare based on unique string
239  {{
240  // you can only compare on unique string if the reference
241  // does not have a pmid or muid (example accession: L40362.1)
242  if( muid == 0 && pmid == 0 ) {
243  string pub_unique;
245 
246  size_t len = pub_unique.length();
247  if (len > 0 && pub_unique[len - 1] == '>') {
248  --len;
249  }
250  len = min(len , uniqueStr.length());
251  pub_unique.resize(len);
252  if (!NStr::IsBlank(uniqueStr) && !NStr::IsBlank(pub_unique)) {
253  if (NStr::StartsWith(uniqueStr, pub_unique, NStr::eNocase)) {
254  return true;
255  }
256  }
257  }
258  }}
259  break;
260  }
261  return false;
262 }
263 
264 static bool s_EquivGenes(const CGene_ref& gene1, const CGene_ref& gene2)
265 {
266  if (gene2.CanGetLocus()) {
267  if (!gene1.CanGetLocus())
268  return false;
269  if (gene1.GetLocus() != gene2.GetLocus())
270  return false;
271  }
272 
273  if (gene2.CanGetLocus_tag()) {
274  if (!gene1.CanGetLocus_tag())
275  return false;
276  if (gene1.GetLocus_tag() != gene2.GetLocus_tag())
277  return false;
278  }
279 
280  if (gene2.CanGetAllele()) {
281  if (!gene1.CanGetAllele())
282  return false;
283  if (gene1.GetAllele() != gene2.GetAllele())
284  return false;
285  }
286 
287  return true;
288 }
289 
292 {
293  CRef<CCmdComposite> composite;
294 
295  for (CFeat_CI mf(seh); mf; ++mf) {
296  if (mf->GetData().IsGene() || !mf->IsSetXref())
297  continue;
298 
299  ITERATE(CSeq_feat::TXref, it, mf->GetXref()) {
300  if ((**it).CanGetData()) {
301  const CSeqFeatData& data = (**it).GetData();
302  if (data.IsGene() && s_EquivGenes(gene, data.GetGene())) {
303  if (!composite)
304  composite.Reset(new CCmdComposite("Delete Feature(s)"));
305 
306  CIRef<IEditCommand> delFeat(new CCmdDelSeq_feat(*mf));
307  composite->AddCommand(*delFeat);
308  }
309  }
310  }
311  }
312 
313  return CIRef<IEditCommand>(composite.GetPointerOrNull());
314 }
315 
User-defined methods of the data storage class.
User-defined methods of the data storage class.
void AddCommand(IEditCommand &command)
static CIRef< IEditCommand > RemoveCitations(objects::CSeq_entry_Handle &seh, const objects::CPub_equiv &pubs)
Definition: cmd_factory.cpp:80
static CIRef< IEditCommand > DeleteGeneXRefs(objects::CSeq_entry_Handle &seh, const objects::CGene_ref &gene)
CFeat_CI –.
Definition: feat_ci.hpp:64
Definition: Pub.hpp:56
@ eContent
Definition: Pub.hpp:66
bool GetLabel(string *label, ELabelType type=eContent, TLabelFlags flags=0, ELabelVersion version=eLabel_DefaultVersion) const
Concatenate a label for this pub to label.
Definition: Pub.cpp:76
CSeq_entry_Handle –.
namespace ncbi::objects::
Definition: Seq_feat.hpp:58
@ eLabel_V1
Traditional GetLabel semantics, modeled on the C Toolkit's PubLabelUnique.
@ fLabel_Unique
Append a unique tag [V1].
USING_SCOPE(objects)
static void s_GetCitIds(const CPub &pub, int &pmid, int &muid)
static bool s_EquivGenes(const CGene_ref &gene1, const CGene_ref &gene2)
static bool s_CitMatches(const CPub &pub, int pmid, int muid, const string &uniqueStr)
static bool s_IsOnlySerial(const CPub &pub)
Definition: cmd_factory.cpp:60
char data[12]
Definition: iconv.c:80
#define ENTREZ_ID_TO(T, entrez_id)
Definition: ncbimisc.hpp:1097
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
const TPrim & Get(void) const
Definition: serialbase.hpp:347
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:986
#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 IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5412
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
bool CanGetIds(void) const
Check if it is safe to call GetIds method.
Definition: Cit_art_.hpp:591
const Tdata & Get(void) const
Get the member data.
list< CRef< CArticleId > > Tdata
const TIds & GetIds(void) const
Get the Ids member data.
Definition: Cit_art_.hpp:597
@ e_Pubmed
see types below
Definition: ArticleId_.hpp:100
bool CanGetLocus(void) const
Check if it is safe to call GetLocus method.
Definition: Gene_ref_.hpp:499
bool CanGetLocus_tag(void) const
Check if it is safe to call GetLocus_tag method.
Definition: Gene_ref_.hpp:787
bool CanGetAllele(void) const
Check if it is safe to call GetAllele method.
Definition: Gene_ref_.hpp:546
const TLocus_tag & GetLocus_tag(void) const
Get the Locus_tag member data.
Definition: Gene_ref_.hpp:793
const TLocus & GetLocus(void) const
Get the Locus member data.
Definition: Gene_ref_.hpp:505
const TAllele & GetAllele(void) const
Get the Allele member data.
Definition: Gene_ref_.hpp:552
bool CanGetPmid(void) const
Check if it is safe to call GetPmid method.
bool CanGetCit(void) const
Check if it is safe to call GetCit method.
bool CanGetUid(void) const
Check if it is safe to call GetUid method.
const TCit & GetCit(void) const
Get the Cit member data.
TUid GetUid(void) const
Get the Uid member data.
const TPmid & GetPmid(void) const
Get the Pmid member data.
bool IsPmid(void) const
Check if variant Pmid is selected.
Definition: Pub_.hpp:677
const TMedline & GetMedline(void) const
Get the variant data.
Definition: Pub_.cpp:211
list< CRef< CPub > > Tdata
Definition: Pub_equiv_.hpp:90
const TPmid & GetPmid(void) const
Get the variant data.
Definition: Pub_.hpp:683
const TArticle & GetArticle(void) const
Get the variant data.
Definition: Pub_.cpp:233
bool IsPat_id(void) const
Check if variant Pat_id is selected.
Definition: Pub_.hpp:659
const Tdata & Get(void) const
Get the member data.
Definition: Pub_equiv_.hpp:165
const TEquiv & GetEquiv(void) const
Get the variant data.
Definition: Pub_.cpp:387
E_Choice Which(void) const
Which variant is currently selected.
Definition: Pub_.hpp:555
bool IsEquiv(void) const
Check if variant Equiv is selected.
Definition: Pub_.hpp:671
list< CRef< CPub > > TPub
Definition: Pub_set_.hpp:159
bool IsPub(void) const
Check if variant Pub is selected.
Definition: Pub_set_.hpp:380
TPub & SetPub(void)
Select the variant.
Definition: Pub_set_.hpp:393
const TGen & GetGen(void) const
Get the variant data.
Definition: Pub_.cpp:167
TMuid GetMuid(void) const
Get the variant data.
Definition: Pub_.hpp:608
bool IsMuid(void) const
Check if variant Muid is selected.
Definition: Pub_.hpp:602
bool IsGen(void) const
Check if variant Gen is selected.
Definition: Pub_.hpp:584
@ e_Pmid
PubMedId.
Definition: Pub_.hpp:114
@ e_Article
Definition: Pub_.hpp:106
@ e_Medline
Definition: Pub_.hpp:104
@ e_Gen
general or generic unparsed
Definition: Pub_.hpp:102
@ e_Muid
medline uid
Definition: Pub_.hpp:105
@ e_Equiv
to cite a variety of ways
Definition: Pub_.hpp:113
bool CanGetCit(void) const
Check if it is safe to call GetCit method.
Definition: Seq_feat_.hpp:1234
void SetCit(TCit &value)
Assign a value to Cit data member.
Definition: Seq_feat_.cpp:170
vector< CRef< CSeqFeatXref > > TXref
Definition: Seq_feat_.hpp:122
int len
T min(T x_, T y_)
User-defined methods of the data storage class.
Modified on Tue Apr 16 20:13:06 2024 by modify_doxy.py rev. 669887