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

Go to the SVN repository for this file.

1 /* $Id: unit_test_source_edit.cpp 93572 2021-04-30 13:48:31Z stakhovv $
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 * Author: Andrea Asztalos, based on code from Colleen Bollin
27 *
28 * File Description:
29 * Unit tests for the source editing functions
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include <corelib/ncbi_system.hpp>
36 #include <corelib/ncbiapp.hpp>
38 
39 // This macro should be defined before inclusion of test_boost.hpp in all
40 // "*.cpp" files inside executable except one. It is like function main() for
41 // non-Boost.Test executables is defined only in one *.cpp file - other files
42 // should not include it. If NCBI_BOOST_NO_AUTO_TEST_MAIN will not be defined
43 // then test_boost.hpp will define such "main()" function for tests.
44 //
45 // Usually if your unit tests contain only one *.cpp file you should not
46 // care about this macro at all.
47 //
48 //#define NCBI_BOOST_NO_AUTO_TEST_MAIN
49 
50 
51 // This header must be included before all Boost.Test headers if there are any
52 #include <corelib/test_boost.hpp>
60 
62 
63 #include <common/test_assert.h> /* This header must go last */
64 
65 
68 
69 extern const char* sc_TestEntry_FixOrgnames;
70 
72 {
73  if ( !CNcbiApplication::Instance()->GetConfig().HasEntry("NCBI", "Data") ) {
74  }
75 }
76 
77 static bool s_debugMode = false;
78 
80 {
81  // Here we make descriptions of command line parameters that we are
82  // going to use.
83 
84  arg_desc->AddFlag( "debug_mode",
85  "Debugging mode writes errors seen for each test" );
86 }
87 
89 {
90  // initialization function body
91 
92  const CArgs& args = CNcbiApplication::Instance()->GetArgs();
93  if (args["debug_mode"]) {
94  s_debugMode = true;
95  }
96 }
97 
98 
99 BOOST_AUTO_TEST_CASE(Test_RemoveOldName)
100 {
101  CBioSource src;
102  src.SetOrg().SetTaxname("Norovirus");
103 
104  unit_test_util::SetOrgMod(src, COrgMod::eSubtype_old_name, "Norwalk-like viruses");
107  edit::RemoveOldName(src);
108  FOR_EACH_ORGMOD_ON_BIOSOURCE ( orgmod, src ) {
109  if ((*orgmod)->IsSetSubtype())
110  BOOST_CHECK_EQUAL((*orgmod)->GetSubtype() != COrgMod::eSubtype_old_name, true);
111  }
112 }
113 
114 BOOST_AUTO_TEST_CASE(Test_RemoveTaxId)
115 {
116  CBioSource src;
117  src.SetOrg().SetTaxname("Norovirus");
118 
119  unit_test_util::SetDbxref (src, "newDB", "3456");
120  unit_test_util::SetDbxref (src, "taxon", "4569");
121  edit::RemoveTaxId(src);
122  FOR_EACH_DBXREF_ON_ORGREF ( db, src.GetOrg() ) {
123  if ((*db)->IsSetDb())
124  BOOST_CHECK_EQUAL(NStr::Equal((*db)->GetDb(), "taxon", NStr::eNocase), false);
125  }
126 }
127 
128 
129 
130 BOOST_AUTO_TEST_CASE(Test_CleanupForTaxnameChange)
131 {
132  CBioSource src;
133  src.SetOrg().SetTaxname("Norovirus");
134 
135  // set the tax id
136  unit_test_util::SetDbxref (src, "newDB", "3456");
137  unit_test_util::SetDbxref (src, "taxon", "4569");
138  // set the old name
140  unit_test_util::SetOrgMod(src, COrgMod::eSubtype_old_name, "Norwalk-like viruses");
142  // set common name
143  src.SetOrg().SetCommon("common name for Norovirus");
144  // set synonym
145  list<string> synonyms;
146  synonyms.push_back("syn1");
147  synonyms.push_back("syn2");
148  src.SetOrg().SetSyn() = synonyms;
149 
150 
152  FOR_EACH_DBXREF_ON_ORGREF ( db, src.GetOrg() ) {
153  if ((*db)->IsSetDb())
154  BOOST_CHECK_EQUAL(NStr::Equal((*db)->GetDb(), "taxon", NStr::eNocase), false);
155  }
156 
157  FOR_EACH_ORGMOD_ON_BIOSOURCE ( orgmod, src ) {
158  if ((*orgmod)->IsSetSubtype())
159  BOOST_CHECK_EQUAL((*orgmod)->GetSubtype() != COrgMod::eSubtype_old_name, true);
160  }
161 
162  BOOST_CHECK_EQUAL(src.GetOrg().IsSetSyn(), false);
163 }
164 
165 
166 bool s_HasOrgMod(const COrg_ref& org, COrgMod::ESubtype subtype, const string& val)
167 {
168  if (!org.IsSetOrgname() || !org.GetOrgname().IsSetMod()) {
169  return false;
170  }
171  ITERATE(COrgName::TMod, it, org.GetOrgname().GetMod()) {
172  if ((*it)->GetSubtype() == subtype && NStr::Equal((*it)->GetSubname(), val)) {
173  return true;
174  }
175  }
176  return false;
177 }
178 
179 
180 BOOST_AUTO_TEST_CASE(Test_SQD_2100)
181 {
182  CRef<CBioSource> src1(new CBioSource());
183  CRef<CBioSource> src2(new CBioSource());
184 
185  CRef<CBioSource> common = edit::MakeCommonBioSource(*src1, *src2);
186  // fail because no org-refs set
187  BOOST_CHECK(!common);
188 
189  // TaxIds are same, drop non-common attributes
190  src1->SetOrg().SetTaxname("Pneumocystis carinii");
191  CRef<CDbtag> tag(new CDbtag());
192  tag->SetDb("taxon");
193  tag->SetTag().SetId(142052);
194  src1->SetOrg().SetDb().push_back(tag);
195  CRef<COrgMod> mod(new COrgMod());
197  mod->SetSubname("rattus-tertii");
198  src1->SetOrg().SetOrgname().SetMod().push_back(mod);
199 
200  src2->Assign(*src1);
201 
204 
205  common = edit::MakeCommonBioSource(*src1, *src2);
206  BOOST_CHECK(common);
207  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxname(), "Pneumocystis carinii");
208  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxId(), TAX_ID_CONST(4754));
209  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_forma_specialis, "rattus-tertii"), true);
210  BOOST_CHECK(!common->IsSetGenome());
211 
212  // examples from CXX-9372
213 
214  //tax IDs are the same, but need to look up before merging to get taxnames to match
215  src1->ResetOrg();
216  src1->SetOrg().SetTaxname("Eremothecium gossypii ATCC 10895");
217  src1->SetOrg().SetTaxId(TAX_ID_CONST(284811));
218  src1->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_strain, "ATCC 10895")));
219  src1->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_culture_collection, "ATCC:10895")));
220 
221  src2->ResetOrg();
222  src2->SetOrg().SetTaxname("Ashbya gossypii ATCC 10895");
223  src2->SetOrg().SetTaxId(TAX_ID_CONST(284811));
224  src2->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_strain, "ATCC 10895")));
225  src2->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_culture_collection, "ATCC:10895")));
226  common = edit::MakeCommonBioSource(*src1, *src2);
227  BOOST_CHECK(common);
228  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxname(), "Eremothecium gossypii ATCC 10895");
229  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxId(), TAX_ID_CONST(284811));
230  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_strain, "ATCC 10895"), true);
231  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_culture_collection, "ATCC:10895"), true);
232 
233  //2 sub-sub strains with common strain above them
234  src1->ResetOrg();
235  src1->SetOrg().SetTaxId(TAX_ID_CONST(588858));
236  src1->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_strain, "14028S")));
237  src1->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_serovar, "Typhimurium")));
238  src2->ResetOrg();
239  src2->SetOrg().SetTaxId(TAX_ID_CONST(1159899));
240  src2->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_strain, "LT2-4")));
241  src2->SetOrg().SetOrgname().SetMod().push_back(CRef<COrgMod>(new COrgMod(COrgMod::eSubtype_serovar, "Typhimurium")));
242  common = edit::MakeCommonBioSource(*src1, *src2);
243  BOOST_CHECK(common);
244  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxname(), "Salmonella enterica subsp. enterica serovar Typhimurium str. LT2");
245  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxId(), TAX_ID_CONST(99287));
246  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_serovar, "Typhimurium"), true);
247  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_strain, "LT2"), true);
248 
249  // 2 strains that their common parent is several levels up, but still under same species
250  src1->ResetOrg();
251  src1->SetOrg().SetTaxId(TAX_ID_CONST(588858));
252  src2->ResetOrg();
253  src2->SetOrg().SetTaxId(TAX_ID_CONST(1410937));
254  common = edit::MakeCommonBioSource(*src1, *src2);
255  BOOST_CHECK(common);
256  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxname(), "Salmonella enterica subsp. enterica serovar Typhimurium");
257  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxId(), TAX_ID_CONST(90371));
258  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_serovar, "Typhimurium"), true);
259 
260  // one taxid is parent of the other
261  src1->ResetOrg();
262  src1->SetOrg().SetTaxId(TAX_ID_CONST(588858));
263  src2->ResetOrg();
264  src2->SetOrg().SetTaxId(TAX_ID_CONST(59201));
265  common = edit::MakeCommonBioSource(*src1, *src2);
266  BOOST_CHECK(common);
267  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxname(), "Salmonella enterica subsp. enterica");
268  BOOST_CHECK_EQUAL(common->GetOrg().GetTaxId(), TAX_ID_CONST(59201));
269  BOOST_CHECK_EQUAL(s_HasOrgMod(common->GetOrg(), COrgMod::eSubtype_serovar, "Typhimurium"), false);
270 
271  // strains of different species
272  // result should be failure, because 2 different species cannot be merged
273  src1->ResetOrg();
274  src1->SetOrg().SetTaxId(TAX_ID_CONST(1416749));
275  src2->ResetOrg();
276  src2->SetOrg().SetTaxId(TAX_ID_CONST(644337));
277  common = edit::MakeCommonBioSource(*src1, *src2);
278  BOOST_CHECK(!common);
279 
280  //
281  // Tax IDs not species level same
282 
283  src2->SetOrg().SetDb().front()->SetTag().SetId(1069680);
284  src2->SetOrg().SetTaxname("Pneumocystis murina B123");
285  src2->SetOrg().SetOrgname().ResetMod();
286  CRef<COrgMod> strain(new COrgMod());
288  strain->SetSubname("B123");
289  src2->SetOrg().SetOrgname().SetMod().push_back(strain);
290  CRef<COrgMod> subsp(new COrgMod());
292  subsp->SetSubname("B123");
293  src2->SetOrg().SetOrgname().SetMod().push_back(subsp);
294  common = edit::MakeCommonBioSource(*src1, *src2);
295  BOOST_CHECK(!common);
296 
297 }
298 
CArgs –.
Definition: ncbiargs.hpp:379
Definition: Dbtag.hpp:53
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:244
@OrgMod.hpp User-defined methods of the data storage class.
Definition: OrgMod.hpp:54
TTaxId GetTaxId() const
Definition: Org_ref.cpp:72
#define TAX_ID_CONST(id)
Definition: ncbimisc.hpp:1112
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:285
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
void CleanupForTaxnameChange(CObjectInfo oi)
Definition: macro_util.cpp:526
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Test for equality of a substring with another string.
Definition: ncbistr.hpp:5383
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
const TOrg & GetOrg(void) const
Get the Org member data.
Definition: BioSource_.hpp:509
void SetGenome(TGenome value)
Assign a value to Genome data member.
Definition: BioSource_.hpp:428
bool IsSetGenome(void) const
Check if a value has been assigned to Genome data member.
Definition: BioSource_.hpp:397
void ResetOrg(void)
Reset Org data member.
Definition: BioSource_.cpp:99
void SetOrg(TOrg &value)
Assign a value to Org data member.
Definition: BioSource_.cpp:108
const TMod & GetMod(void) const
Get the Mod member data.
Definition: OrgName_.hpp:839
void SetSubtype(TSubtype value)
Assign a value to Subtype data member.
Definition: OrgMod_.hpp:316
const TTaxname & GetTaxname(void) const
Get the Taxname member data.
Definition: Org_ref_.hpp:372
bool IsSetMod(void) const
Check if a value has been assigned to Mod data member.
Definition: OrgName_.hpp:827
list< CRef< COrgMod > > TMod
Definition: OrgName_.hpp:332
bool IsSetOrgname(void) const
Check if a value has been assigned to Orgname data member.
Definition: Org_ref_.hpp:529
bool IsSetSyn(void) const
synonyms for taxname or common Check if a value has been assigned to Syn data member.
Definition: Org_ref_.hpp:504
void SetSubname(const TSubname &value)
Assign a value to Subname data member.
Definition: OrgMod_.hpp:356
const TOrgname & GetOrgname(void) const
Get the Orgname member data.
Definition: Org_ref_.hpp:541
@ eSubtype_sub_species
Definition: OrgMod_.hpp:105
@ eSubtype_strain
Definition: OrgMod_.hpp:85
@ eSubtype_old_name
Definition: OrgMod_.hpp:124
@ eSubtype_acronym
Definition: OrgMod_.hpp:102
@ eSubtype_serovar
Definition: OrgMod_.hpp:92
@ eSubtype_culture_collection
Definition: OrgMod_.hpp:118
@ eSubtype_forma_specialis
Definition: OrgMod_.hpp:109
const char * tag
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Int mod(Int i, Int j)
Definition: njn_integer.hpp:67
The Object manager core.
Utility macros and typedefs for exploring NCBI objects from seqfeat.asn.
#define FOR_EACH_DBXREF_ON_ORGREF(Itr, Var)
FOR_EACH_DBXREF_ON_ORGREF EDIT_EACH_DBXREF_ON_ORGREF.
#define FOR_EACH_ORGMOD_ON_BIOSOURCE(Itr, Var)
FOR_EACH_ORGMOD_ON_BIOSOURCE EDIT_EACH_ORGMOD_ON_BIOSOURCE.
CRef< CBioSource > MakeCommonBioSource(const objects::CBioSource &src1, const objects::CBioSource &src2)
bool RemoveOldName(objects::CBioSource &src)
bool RemoveTaxId(objects::CBioSource &src)
Utility stuff for more convenient using of Boost.Test library.
USING_SCOPE(objects)
static bool s_debugMode
NCBITEST_INIT_TREE()
const char * sc_TestEntry_FixOrgnames
NCBITEST_INIT_CMDLINE(arg_desc)
bool s_HasOrgMod(const COrg_ref &org, COrgMod::ESubtype subtype, const string &val)
BOOST_AUTO_TEST_CASE(Test_RemoveOldName)
NCBITEST_AUTO_INIT()
void SetOrgMod(objects::CBioSource &src, objects::COrgMod::TSubtype subtype, string val)
void SetDbxref(objects::CBioSource &src, string db, objects::CObject_id::TId id)
Modified on Thu Nov 30 04:52:11 2023 by modify_doxy.py rev. 669887