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

Go to the SVN repository for this file.

1 /* $Id: convert_comment.cpp 42186 2019-01-09 19:34:50Z 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: Igor Filippov
27  */
28 
29 #include <ncbi_pch.hpp>
30 #include <objmgr/scope.hpp>
35 
37 using namespace objects;
38 
39 
40 static void s_split(const string &s, string delim, vector<string> &elems)
41 {
42  size_t start = 0;
43  while (start < s.length())
44  {
45  size_t finish = s.find(delim,start);
46  if (finish == string::npos) finish = s.length();
47  string item = s.substr(start,finish-start);
49  if (!item.empty()) elems.push_back(item);
50  start = finish+delim.length();
51  }
52 }
53 
54 string CConvertComment::ApplyToComment (string comment, CCmdComposite* composite,CSeq_entry_Handle seh)
55 {
56  string result = comment;
57 
58  if (comment.find("-START##") != string::npos && comment.find("-END##") != string::npos && comment.find(m_DelimiterNameValue) != string::npos)
59  {
60  string prefix,suffix;
61  size_t e = comment.find("-START##");
62  size_t b = comment.rfind("##",e);
63  if (b == string::npos) b = 0;
64  e += strlen("-START##");
65  prefix = comment.substr(b,e-b);
66  comment.erase(0,e);
67 
68  e = comment.find("-END##");
69  b = comment.rfind("##",e);
70  if (b == string::npos) b = 0;
71  e += strlen("-END##");
72  suffix = comment.substr(b,e-b);
73  comment.erase(b,string::npos);
74 
75  vector<string> lines;
76  string name;
77  vector< pair<string,string> > fields;
78  s_split(comment,m_DelimiterFields,lines);
79  for (unsigned int i=0; i<lines.size(); ++i)
80  {
81  NStr::ReplaceInPlace (lines[i], "\r", "");
82  vector<string> field;
83  s_split(lines[i],m_DelimiterNameValue,field);
84  if (field.size() == 2)
85  fields.push_back(pair<string,string>(field[0],field[1]));
86  else if (field.size() == 1)
87  {
88  if (!name.empty())
89  {
90  fields.push_back(pair<string,string>(name,field[0]));
91  name.clear();
92  }
93  else
94  name = field[0];
95  }
96  }
97  if (!fields.empty())
98  {
99  CRef<CSeqdesc> descr(new CSeqdesc());
100  descr->Select(CSeqdesc::e_User);
101  CUser_object& user = descr->SetUser();
102  user.SetType().SetStr("StructuredComment");
103  if (!prefix.empty())
104  user.AddField("StructuredCommentPrefix",prefix);
105  for (unsigned int i=0; i<fields.size(); ++i)
106  user.AddField(fields[i].first,fields[i].second);
107  if (!suffix.empty())
108  user.AddField("StructuredCommentSuffix",suffix);
109 
110  CIRef<IEditCommand> cmdAddDesc(new CCmdCreateDesc(seh, *descr));
111  composite->AddCommand(*cmdAddDesc);
112 
113  e = result.find("-START##");
114  b = result.rfind("##",e);
115  if (b == string::npos) b = 0;
116  e = result.find("-END##");
117  e += strlen("-END##");
118  result.erase(b,e-b);
119  }
120  }
121 
122  return result;
123 }
124 
125 void CConvertComment::ApplyToCSeq_entry (objects::CSeq_entry_Handle tse, const CSeq_entry& se, CCmdComposite* composite)
126 {
127 
129  {
130  if ((*it)->IsComment())
131  {
133  string modified = ApplyToComment((*it)->GetComment(),composite,seh);
134  if (modified != (*it)->GetComment())
135  {
136  if (modified.empty())
137  {
138  CIRef<IEditCommand> cmdDelDesc(new CCmdDelDesc(seh, **it));
139  composite->AddCommand(*cmdDelDesc);
140  }
141  else
142  {
143  CRef<CSerialObject> edited_object;
144  edited_object.Reset((CSerialObject*)CSeqdesc::GetTypeInfo()->Create());
145  edited_object->Assign(**it);
146  CSeqdesc& edited_seqdesc = dynamic_cast<CSeqdesc&>(*edited_object);
147  edited_seqdesc.SetComment(modified);
148  CRef<CCmdChangeSeqdesc> cmd(new CCmdChangeSeqdesc(seh, **it, edited_seqdesc));
149  composite->AddCommand(*cmd);
150  }
151  }
152  }
153  }
154 
155  if (se.IsSet())
156  {
158  {
159  ApplyToCSeq_entry (tse, **it, composite);
160  }
161  }
162 }
163 
164 
165 bool CConvertComment::apply(objects::CSeq_entry_Handle tse, ICommandProccessor* cmdProcessor, string title, string delim_name_value, string delim_fields)
166 {
167  if (tse)
168  {
169  m_DelimiterNameValue = delim_name_value;
170  m_DelimiterFields = delim_fields;
171  CRef<CCmdComposite> composite(new CCmdComposite(title));
172  ApplyToCSeq_entry (tse, *(tse.GetCompleteSeq_entry()), composite);
173  cmdProcessor->Execute(composite.GetPointer());
174  return true;
175  }
176  else
177  return false;
178 }
179 
180 
181 
182 
void AddCommand(IEditCommand &command)
bool apply(CSeq_entry_Handle tse, ICommandProccessor *cmdProcessor, string title, string delim_name_value, string delim_fields)
string ApplyToComment(string comment, CCmdComposite *composite, CSeq_entry_Handle seh)
void ApplyToCSeq_entry(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
CUser_object & AddField(const string &label, const string &value, EParseField parse=eParse_String)
add a data field to the user object that holds a given value
Undo/Redo interface for editing operations.
virtual void Execute(IEditCommand *command, wxWindow *window=0)=0
static void s_split(const string &s, string delim, vector< string > &elems)
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.
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
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
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3197
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3401
void SetType(TType &value)
Assign a value to Type 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
TComment & SetComment(void)
Select the variant.
Definition: Seqdesc_.hpp:1065
void Select(E_Choice index, EResetVariant reset=eDoResetVariant)
Select the requested variant if needed.
TUser & SetUser(void)
Select the variant.
Definition: Seqdesc_.cpp:390
@ e_User
user defined object
Definition: Seqdesc_.hpp:124
int i
static const char * suffix[]
Definition: pcregrep.c:408
static const char * prefix[]
Definition: pcregrep.c:405
#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.
else result
Definition: token2.c:20
Modified on Fri Dec 01 04:50:06 2023 by modify_doxy.py rev. 669887