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

Go to the SVN repository for this file.

1 /* $Id: suc_data.cpp 47392 2023-03-06 19:00:01Z evgeniev $
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  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 
35 #include <iterator>
36 
39 
40 CSUCLine::CSUCLine (const string& line, const string& accession, const CObject* obj) : m_Line(line), m_Count(1)
41 {
42  m_RelatedObjects.emplace_back(accession, CConstRef<CObject>(obj));
43 }
44 
45 
46 void CSUCLine::Add(const string& accession, const CObject* obj)
47 {
48  m_Count++;
49  m_RelatedObjects.emplace_back(accession, CConstRef<CObject>(obj));
50 }
51 
52 void CSUCBlock::AddBlock(const string& block_text, const string& accession, const CObject* obj)
53 {
54  size_t n_quotes = 0;
55  size_t n_brackets_left = 0;
56  size_t n_brackets_right = 0;
57  bool can_split(true);
58 
59  size_t posE = -1;
60  do {
61  size_t pos = posE + 1;
62  posE = block_text.find('\n', pos);
63  string line = (posE == string::npos) ? block_text.substr(pos) : block_text.substr(pos, posE - pos);
64 
65  if (line.empty())
66  continue;
67 
68  for (const auto& c : line) {
69  if (c == '\"')
70  ++n_quotes;
71  else if (c == '(')
72  ++n_brackets_left;
73  else if (c == ')')
74  ++n_brackets_right;
75  }
76 
77  AddLine(line, accession, obj, can_split);
78  if (n_quotes % 2 == 0 && n_brackets_left == n_brackets_right)
79  {
80  m_label.clear();
81  can_split = true;
82  }
83  else
84  {
85  can_split = false;
86  }
87  } while (posE != string::npos);
88 }
89 
90 
91 void CSUCBlock::AddLine(const string& line, const string& accession, const CObject* obj, bool can_split)
92 {
93  static const char* spaces = " \t";
94  if (line.find_first_not_of(spaces) == string::npos)
95  return;
96 
97  {
98  auto lit = m_Lines.find(line);
99  if (lit == m_Lines.end())
100  {
101  CRef<CSUCLine> new_line(new CSUCLine(line, accession, obj));
102  m_Lines.emplace(new_line->GetLine(), new_line);
103  m_pos = -1;
104  }
105  else
106  {
107  lit->second->Add(accession, obj);
108  }
109  }
110 
111  if ( HasSecondLevel() )
112  {
113  if (can_split)
114  {
115  string whole = line;
116  whole.erase(0, whole.find_first_not_of(spaces));
117  whole.erase(whole.find_last_not_of(spaces) + 1);
118 
119  if (!whole.empty() && whole[0] == '/')
120  m_label = whole.substr(0, whole.find('='));
121  else
122  m_label = whole.substr(0, whole.find_first_of(spaces));
123  }
124 
125  if (!m_label.empty())
126  {
127  auto& map = m_SecondLevel[m_label];
128  auto lit = map.find(line);
129  if (lit == map.end())
130  {
131  CRef<CSUCLine> new_line(new CSUCLine(line, accession, obj));
132  map.emplace(new_line->GetLine(), make_pair(can_split, new_line));
133  }
134  else
135  {
136  lit->second.second->Add(accession, obj);
137  }
138  }
139  }
140 }
141 
142 
144 {
145  for (auto& it : m_Lines)
146  {
147  it.second->Print();
148  }
149 }
150 
152 {
153 
154  if (m_pos < 0)
155  {
156  m_pos_it = m_Lines.cbegin();
157  advance(m_pos_it, pos);
158  m_pos = static_cast<int>(pos);
159  }
160  else if (pos != m_pos)
161  {
162  int diff = static_cast<int>(pos);
163  diff -= m_pos;
164  advance (m_pos_it,diff);
165  m_pos = static_cast<int>(pos);
166  }
167 
168  return CConstRef<CSUCLine>(m_pos_it->second.GetPointer());
169 }
170 
171 void CSUCBlock::GetText(wxString &text) const
172 {
173  for (auto& line : m_Lines)
174  {
175  text << "\t";
176  text << line.second->GetCount();
177  text << "\t";
178  text << line.second->GetLine();
179  text << "\n";
180  }
181 }
182 
184 {
185  switch (block_type) {
187  return "LOCUS";
188  break;
190  return "DEFLINE";
191  break;
193  return "ACCESSION";
194  break;
196  return "VERSION";
197  break;
199  return "KEYWORDS";
200  break;
202  return "SOURCE";
203  break;
205  return "REFERENCE";
206  break;
208  return "COMMENT";
209  break;
212  return "FEATURE";
213  break;
215  return "SOURCEFEAT";
216  break;
218  return "BASECOUNT";
219  break;
221  return "ORIGIN";
222  break;
224  return "SEQUENCE";
225  break;
227  return "PROJECT";
228  break;
229  default:
230  return "";
231  break;
232  }
233 }
234 
236 {
238 }
239 
241 {
242  m_Blocks.clear();
256 
257  m_order.clear();
258  m_order.resize(15);
272 
273 }
274 
275 
276 void CSUCResults::AddText(string & block_text, CFlatFileConfig::FGenbankBlocks which_block, const string& accession, CConstRef<CObject> obj)
277 {
278  auto it = m_Blocks.find(which_block);
279  if ( it == m_Blocks.end())
280  {
281  CRef<CSUCBlock> new_block(new CSUCBlock(which_block));
282  new_block->AddBlock(block_text, accession, obj);
283  m_Blocks[which_block] = new_block;
284  m_order.push_back(which_block);
285  }
286  else
287  {
288  it->second->AddBlock(block_text, accession, obj);
289  }
290 }
291 
292 
294 {
295  size_t count = 0;
296  for (auto it = m_Blocks.begin(); it != m_Blocks.end(); ++it)
297  {
298  if (it->second->GetExpanded())
299  {
300  count += it->second->CountLines();
301  }
302  }
303  return count;
304 }
305 
306 void CSUCResults::GetText(wxString& text)
307 {
308  for (size_t i = 0; i < m_order.size(); i++)
309  {
310  objects::CFlatFileConfig::FGenbankBlocks which_block = m_order[i];
311  auto it = m_Blocks.find(which_block);
312  if (it != m_Blocks.end())
313  {
314  it->second->SetExpanded(true);
315  it->second->GetText(text);
316  }
317  }
318 }
319 
321 {
322  size_t block_pos = 0;
324  if (x_GetBlockPos(pos, block_type, block_pos))
325  {
326  auto it = m_Blocks.find(block_type);
327  if (it != m_Blocks.end())
328  return it->second->GetLine(block_pos);
329  }
330 
331  return CConstRef<CSUCLine>(NULL);
332 }
333 
335 {
336  size_t block_pos = 0;
338  if (x_GetBlockPos(pos, block_type, block_pos))
339  {
340  return block_type;
341  }
342 
344 }
345 
346 
347 void CSUCResults::ExpandBlock(bool val, objects::CFlatFileConfig::FGenbankBlocks which_block)
348 {
349  auto it = m_Blocks.find(which_block);
350  if (it != m_Blocks.end())
351  {
352  it->second->SetExpanded(val);
353  }
354 }
355 
356 
358 {
359  for (auto it = m_Blocks.begin(); it != m_Blocks.end(); ++it)
360  {
361  it->second->SetExpanded(val);
362  }
363 }
364 
365 
366 bool CSUCResults::GetExpanded(objects::CFlatFileConfig::FGenbankBlocks which_block) const
367 {
368  bool rval = false;
369  auto it = m_Blocks.find(which_block);
370  if (it != m_Blocks.end())
371  {
372  rval = it->second->GetExpanded();
373  }
374  return rval;
375 }
376 
377 
378 bool CSUCResults::x_GetBlockPos(size_t pos, objects::CFlatFileConfig::FGenbankBlocks& block_type, size_t& block_pos) const
379 {
380  size_t remainder = pos;
381 
382  for (size_t i = 0; i < m_order.size(); i++)
383  {
384  objects::CFlatFileConfig::FGenbankBlocks which_block = m_order[i];
385  auto it = m_Blocks.find(which_block);
386  if (it != m_Blocks.end() && it->second->GetExpanded())
387  {
388  size_t num_lines = it->second->CountLines();
389  if (remainder < num_lines)
390  {
391  block_type = which_block;
392  block_pos = remainder;
393  return true;
394  }
395  remainder -= num_lines;
396  }
397  }
398  return false;
399 }
400 
402 
CObject –.
Definition: ncbiobj.hpp:180
void AddBlock(const string &block_text, const string &accession, const CObject *obj)
Definition: suc_data.cpp:52
string m_label
Definition: suc_data.hpp:100
map< string, map< CTempString, pair< bool, CRef< CSUCLine > > > > m_SecondLevel
Definition: suc_data.hpp:96
void Print()
Definition: suc_data.cpp:143
int m_pos
Definition: suc_data.hpp:98
static const char * GetBlockLabel(objects::CFlatFileConfig::FGenbankBlocks block_type)
Definition: suc_data.cpp:183
bool HasSecondLevel(void)
Definition: suc_data.cpp:235
void AddLine(const string &line, const string &accession, const CObject *obj, bool can_split)
Definition: suc_data.cpp:91
map< CTempString, CRef< CSUCLine > >::const_iterator m_pos_it
Definition: suc_data.hpp:97
map< CTempString, CRef< CSUCLine > > m_Lines
Definition: suc_data.hpp:95
objects::CFlatFileConfig::FGenbankBlocks m_Block
Definition: suc_data.hpp:93
void GetText(wxString &text) const
Definition: suc_data.cpp:171
CConstRef< CSUCLine > GetLine(size_t pos) const
Definition: suc_data.cpp:151
CSUCLine(const string &line, const string &accession, const CObject *obj)
Definition: suc_data.cpp:40
const string & GetLine() const
Definition: suc_data.hpp:55
void Add(const string &accession, const CObject *obj)
Definition: suc_data.cpp:46
size_t m_Count
Definition: suc_data.hpp:64
vector< TRelatedObject > m_RelatedObjects
Definition: suc_data.hpp:65
CConstRef< CSUCLine > GetLine(size_t pos) const
Definition: suc_data.cpp:320
objects::CFlatFileConfig::FGenbankBlocks GetBlockType(size_t pos) const
Definition: suc_data.cpp:334
void GetText(wxString &text)
Definition: suc_data.cpp:306
unordered_map< unsigned int, CRef< CSUCBlock > > m_Blocks
Definition: suc_data.hpp:121
void AddText(string &block_text, objects::CFlatFileConfig::FGenbankBlocks which_block, const string &accession, CConstRef< CObject > obj)
Definition: suc_data.cpp:276
bool x_GetBlockPos(size_t pos, objects::CFlatFileConfig::FGenbankBlocks &block_type, size_t &block_pos) const
Definition: suc_data.cpp:378
size_t CountLines() const
Definition: suc_data.cpp:293
void ExpandBlock(bool val, objects::CFlatFileConfig::FGenbankBlocks which_block)
Definition: suc_data.cpp:347
vector< objects::CFlatFileConfig::FGenbankBlocks > m_order
Definition: suc_data.hpp:122
void ExpandAll(bool val)
Definition: suc_data.cpp:357
bool GetExpanded(objects::CFlatFileConfig::FGenbankBlocks which_block) const
Definition: suc_data.cpp:366
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is whole
int i
static void text(MDB_val *v)
Definition: mdb_dump.c:62
USING_SCOPE(objects)
Modified on Sat Apr 13 11:50:52 2024 by modify_doxy.py rev. 669887