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

Go to the SVN repository for this file.

1 /* $Id: gbproject_tool.cpp 46973 2022-03-24 13:47:42Z 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: Mike DiCuccio
27  *
28  * File Description:
29  * Application for manipulating GBench projects.
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbifile.hpp>
34 #include <corelib/ncbiapp.hpp>
35 #include <corelib/ncbienv.hpp>
36 #include <corelib/ncbiargs.hpp>
37 #include <serial/objistr.hpp>
38 #include <serial/objostr.hpp>
39 #include <serial/serial.hpp>
51 #include <gui/objutils/label.hpp>
52 #include <objects/general/Date.hpp>
53 #include <objmgr/scope.hpp>
56 
57 #include "fileloader.hpp"
58 #include "projectfilewriter.hpp"
59 
62 
64 unique_ptr<CProjectFileWriter> Writer;
65 
67 {
68 private:
69  void Init();
70  int Run();
71 
72  void x_CreateNewProject(const CArgs& args, ESerialDataFormat fmt);
73 
74  void x_AddFolder(const CArgs& args, ESerialDataFormat fmt);
75 
76  void x_ListFolders(const CArgs& args);
77 
78  void x_AddItem(const CArgs& args, ESerialDataFormat fmt);
79 
80  void x_AddItem(CGBProjectHandle& project, const CArgs& args, const string& in_folder);
81 
82  void x_LoadGeneralData(CProjectItem& item, ESerialDataFormat fmt, const string& filename, const string& track_name);
83 
84  void x_LoadGffData(CProjectItem& item, CNcbiIstream& istr);
85 
86  void x_Collapse(const CArgs& args, ESerialDataFormat fmt);
87 
88  void x_Expand(const CArgs& args);
89 
90  void x_RemoveItem(const CArgs& args, ESerialDataFormat fmt);
91 
92  void x_RemoveFolder(const CArgs& args, ESerialDataFormat fmt);
93 
94  CRef<CScope> m_Scope{ nullptr };
95 };
96 
97 
99 {
102 
103  //SetDiagPostFlag(eDPF_All);
104  //SetDiagPostLevel(eDiag_Info);
105 
106  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions());
107 
108  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
109  "Application for manipulating GBench projects");
110 
111  arg_desc->AddOptionalKey("i", "InputFile",
112  "Input project file",
114 
115  arg_desc->AddOptionalKey("o", "OutputFile",
116  "Output project file",
118 
119  arg_desc->AddFlag("noid", "disable access to genbank or id and id2");
120 
121  arg_desc->AddFlag("list", "List the contents of a project file");
122  arg_desc->SetDependency("list", CArgDescriptions::eRequires, "i");
123 
124  arg_desc->AddFlag("create", "Create a new project file");
125  arg_desc->SetDependency("create", CArgDescriptions::eRequires, "o");
126 
127  arg_desc->AddOptionalKey("title", "Title",
128  "Title for new project", CArgDescriptions::eString);
129 
130 
131  arg_desc->AddOptionalKey("expand", "expand",
132  "Create a subdirectory tree from a project file", CArgDescriptions::eInputFile);
133 
134  arg_desc->SetDependency("expand", CArgDescriptions::eRequires, "i");
135 
136  arg_desc->AddDefaultKey("expand-fmt", "expand_format",
137  "Format to expand project file parts out in", CArgDescriptions::eString, "asnb");
138 
139  arg_desc->SetConstraint("expand-fmt",
140  &((*new CArgAllow_Strings(NStr::eNocase)), "asnt", "asnb", "xml", "gtf"));
141 
142  arg_desc->AddOptionalKey("collapse", "collapse",
143  "Create a project file from a directory tree",
145  arg_desc->SetDependency("collapse", CArgDescriptions::eRequires, "o");
146 
147  {
148  arg_desc->AddOptionalKey("add", "ItemToAdd",
149  "Add an item to a project file", CArgDescriptions::eInputFile);
150 
151  arg_desc->AddOptionalKey("fmt", "ItemFormat",
152  "Format of input item", CArgDescriptions::eString);
153 
154  arg_desc->SetConstraint("fmt", &(*new CArgAllow_Strings, "gff", "asn", "asnb"));
155 
156  arg_desc->AddOptionalKey("track", "TrackName",
157  "Name of the track in GSV", CArgDescriptions::eString);
158 
159  arg_desc->SetDependency("add", CArgDescriptions::eRequires, "fmt");
160  arg_desc->SetDependency("add", CArgDescriptions::eRequires, "i");
161  arg_desc->SetDependency("add", CArgDescriptions::eRequires, "o");
162  arg_desc->SetDependency("track", CArgDescriptions::eRequires, "add");
163 
164  arg_desc->AddOptionalKey("proj-folder", "FolderName",
165  "Add item into the named folder", CArgDescriptions::eString);
166 
167  }
168 
169 
170  arg_desc->AddOptionalKey("add-folder", "FolderToAdd",
171  "Create a folder in the existing project",
173  arg_desc->SetDependency("add-folder", CArgDescriptions::eRequires, "i");
174  arg_desc->SetDependency("add-folder", CArgDescriptions::eRequires, "o");
175 
176  arg_desc->AddOptionalKey("rm", "RemoveItem",
177  "Remove an item from an existing project",
179  arg_desc->SetDependency("rm", CArgDescriptions::eRequires, "i");
180  arg_desc->SetDependency("rm", CArgDescriptions::eRequires, "o");
181 
182  arg_desc->AddOptionalKey("rmdir", "RemoveFolder",
183  "Remove a folder from an existing project",
185  arg_desc->SetDependency("rmdir", CArgDescriptions::eRequires, "i");
186  arg_desc->SetDependency("rmdir", CArgDescriptions::eRequires, "o");
187 
188  SetupArgDescriptions(arg_desc.release());
189 }
190 
191 static void s_ScopeProjectFolder(const CProjectFolder& folder)
192 {
193  if (folder.IsSetFolders()) {
194  ITERATE (CProjectFolder::TFolders, iter, folder.GetFolders()) {
195  s_ScopeProjectFolder(**iter);
196  }
197  }
198 
199  if (folder.IsSetItems()) {
200  ITERATE (CProjectFolder::TItems, iter, folder.GetItems()) {
201  Writer->AddToScope(CConstRef<CSerialObject>(((*iter)->GetObject())));
202  }
203  }
204 }
205 
206 static void s_ExpandProjectFolder(const CProjectFolder& folder,
207  CDir& parentDir,
208  EFormat format,
209  int level = 0)
210 {
211  const string kSpacer(level * 2, ' ');
212 
213  string currentName = CDirEntry::ConcatPathEx(parentDir.GetPath(),
214  folder.GetInfo().GetTitle());
215  CDir currentDir(currentName);
216  if(level == 0) // get rid of the /Data/ dir thats always on the top when expanding
217  currentDir = parentDir;
218  currentDir.CreatePath();
219  cout << kSpacer << "[folder] " << folder.GetInfo().GetTitle()
220  << " (created: "
221  << folder.GetInfo().GetCreate_date().AsCTime().AsString("M/D/Y h:m:s")
222  << ")" << " " << currentName << endl;
223 
224  if (folder.IsSetFolders()) {
225  ITERATE (CProjectFolder::TFolders, iter, folder.GetFolders()) {
226  s_ExpandProjectFolder(**iter, currentDir, format, level + 1);
227  }
228  }
229 
230  if (folder.IsSetItems()) {
231  ITERATE (CProjectFolder::TItems, iter, folder.GetItems()) {
232  string itemTitle;
233  CLabel::GetLabel(**iter, &itemTitle);
234  string itemFilename = CDirEntry::ConcatPathEx(currentDir.GetPath(), itemTitle);
235  cout << kSpacer << " " << "[item:"
236  << (*iter)->GetId() << "] " << itemTitle << " " << itemFilename << endl;
237 
238  Writer->WriteFile(itemFilename,
239  CConstRef<CSerialObject>(((*iter)->GetObject())),
240  format);
241  //unique_ptr<CObjectOStream> ObjS(CObjectOStream::Open(format, itemFilename));
242  //*ObjS << *((*iter)->GetObject());
243  }
244  }
245 }
246 
248 {
249  time_t mod_a, mod_b;
250  A->GetTimeT(&mod_a);
251  B->GetTimeT(&mod_b);
252  return (mod_a < mod_b);
253 }
254 
255 static string s_GetAlignTitle(const CSeq_align& align, CScope& scope)
256 {
257  CSeq_id_Handle query_idh, subject_idh;
258  query_idh = scope.GetAccVer(CSeq_id_Handle::GetHandle(align.GetSeq_id(0)));
259  subject_idh = scope.GetAccVer(CSeq_id_Handle::GetHandle(align.GetSeq_id(1)));
260 
261  string query_label, subject_label;
262  if (query_idh.GetSeqIdOrNull().IsNull()) {
263  query_label = align.GetSeq_id(0).GetSeqIdString(true);
264  }
265  else {
266  query_label = query_idh.GetSeqId()->GetSeqIdString(true);
267  }
268 
269  if (subject_idh.GetSeqIdOrNull().IsNull()) {
270  subject_label = align.GetSeq_id(1).GetSeqIdString(true);
271  }
272  else {
273  subject_label = subject_idh.GetSeqId()->GetSeqIdString(true);
274  }
275  return query_label + " x " + subject_label;
276 }
277 
278 static void s_CollapseProjectFolder(CScope& scope, CGBProjectHandle& project,
279  CProjectFolder& folder, CDir& dir)
280 {
281  // when package = true, the seq-aligns will be wrapped by seq-annots.
282  auto children = dir.GetEntries(kEmptyStr, CDir::fIgnoreRecursive);
283 
284  vector<CDirEntry*> children_vec;
285  for (const auto& child : children) {
286  CDirEntry* new_dir = new CDirEntry(*child);
287  children_vec.push_back(new_dir);
288  }
289 
290  sort(children_vec.begin(), children_vec.end(), s_Compare_DirEntry_ModDate);
291 
292  using TItemMap = multimap<string, CRef<CProjectItem>>;
293  TItemMap items;
294 
295  string CurrLabel;
296  NON_CONST_ITERATE(vector<CDirEntry*>, childIter, children_vec) {
297 
298  // Save the file name early, in case it is actually a symbolic link.
299  CurrLabel = (*childIter)->GetName();
300 
301  // Then if its a link, dereference it
302  if( (*childIter)->IsLink() ) {
303  (*childIter)->DereferenceLink();
304  }
305 
306  if( (*childIter)->IsDir() ) {
307  CRef<CProjectFolder> childFolder(new CProjectFolder);
308  childFolder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
309  childFolder->SetInfo().SetTitle(CurrLabel);
310  cout << "Dir " << CurrLabel << endl;
311 
312  folder.SetFolders().push_back(childFolder);
313  CDir childDir(**childIter);
314  s_CollapseProjectFolder(scope, project, *childFolder, childDir);
315  }
316  else if( (*childIter)->IsFile() ) {
317  cout << "File " << CurrLabel << endl;
319  CFileLoader::LoadFile( (*childIter)->GetPath(), Objects);
320 
321  if(Objects.empty())
322  continue;
323 
324  ITERATE(CFileLoader::TSerialObjectList, ObjIter, Objects) {
325 
326  CRef<CSerialObject> Object = *ObjIter;
327  string label = CurrLabel;
328 
329  if(Object->GetThisTypeInfo() != NULL &&
330  Object->GetThisTypeInfo()->GetName() == "Seq-annot") {
331  CRef<CSeq_annot> Annot(static_cast<CSeq_annot*>(Object.GetPointer()));
332  if (Annot->CanGetDesc()) {
334  CConstRef<CAnnotdesc> title;
335  ITERATE(CSeq_annot::TDesc::Tdata, desciter, Annot->GetDesc().Get()) {
336  if((*desciter)->IsName()) {
337  name = *desciter;
338  } else if ((*desciter)->IsTitle()) {
339  title = *desciter;
340  }
341  }
342  if (title) {
343  label = title->GetTitle();
344  } else if (name) {
345  label = name->GetName();
346  }
347  }
348  else{
349  Annot->SetNameDesc(CurrLabel);
350  Annot->SetTitleDesc(CurrLabel);
351  }
352  }
353  else if(Object->GetThisTypeInfo() != NULL &&
354  Object->GetThisTypeInfo()->GetName() == "Seq-align") {
355  CRef<CSeq_align> Align(static_cast<CSeq_align*>(Object.GetPointer()));
356  label = s_GetAlignTitle(*Align, scope);
357  }
358  else if(Object->GetThisTypeInfo() != NULL &&
359  Object->GetThisTypeInfo()->GetName() == "Project-item") {
360  CRef<CProjectItem> Item(static_cast<CProjectItem*>(Object.GetPointer()));
361  items.insert(TItemMap::value_type(Item->GetLabel(), Item));
362  continue;
363  }
364 
365  CRef<CProjectItem> childItem(new CProjectItem);
366  CTime CreateTime;
367  (*childIter)->GetTime(&CreateTime);
368  childItem->SetCreateDate(CreateTime);
369 
370  if (Object->GetThisTypeInfo() == CProjectItem::GetTypeInfo()) {
371  childItem.Reset(dynamic_cast<CProjectItem*>(Object.GetPointer()));
372  } else {
373  childItem->SetLabel(label);
374  childItem->SetObject(*Object);
375  cout << " Title: " << label << endl;
376  }
377  items.insert(TItemMap::value_type(label, childItem));
378  }
379  }
380 
381  delete *childIter;
382  *childIter = nullptr;
383  }
384 
385  NON_CONST_ITERATE(TItemMap, it, items) {
386  project.AddItem(*it->second, folder);
387  }
388 }
389 
390 
391 CRef<CProjectFolder> s_GetOrCreateFolder(CGBProjectHandle& project,
392  CProjectFolder& parent_folder,
393  const string& folder_name,
394  bool top = true)
395 {
396  if (parent_folder.IsSetFolders()) {
397  NON_CONST_ITERATE(CProjectFolder::TFolders, iter, parent_folder.SetFolders()) {
398  if( (*iter)->GetInfo().GetTitle() == folder_name) {
399  return (*iter); // iter = folder.SetFolders().erase(iter);
400  }
401  else
402  s_GetOrCreateFolder(project, **iter, folder_name, false);
403  }
404  }
405 
406  if(top) {
408  folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
409  folder->SetInfo().SetTitle(folder_name);
410  parent_folder.SetFolders().push_back(folder);
411  return folder;
412  }
413  return CRef<CProjectFolder>();
414 }
415 
416 
418 {
419  const CArgs& args = GetArgs();
420 
421  bool EnableID = true;
422  if(args["noid"].HasValue() && args["noid"].AsBoolean())
423  EnableID = false;
424 
425 
427  m_Scope.Reset(new CScope(*object_manager));
429 
430  if(EnableID) {
433  }
434 
435  Writer.reset(new CProjectFileWriter(m_Scope));
436 
437 
439 
440  if (args["create"]) {
441  x_CreateNewProject(args, saveformat);
442  }
443  else if (args["add-folder"].HasValue()) {
444  x_AddFolder(args, saveformat);
445  }
446  else if (args["list"]) {
447  x_ListFolders(args);
448  }
449  else if (args["expand"].HasValue()) {
450  x_Expand(args);
451  }
452  else if (args["collapse"].HasValue()) {
453  x_Collapse(args, saveformat);
454  }
455  else if (args["add"].HasValue()) {
456  x_AddItem(args, saveformat);
457  }
458  else if (args["rm"].HasValue()) {
459  x_RemoveItem(args, saveformat);
460  }
461  else if (args["rmdir"].HasValue()) {
462  x_RemoveFolder(args, saveformat);
463  }
464  else {
465  NCBI_THROW(CException, eUnknown, "command not yet implemented");
466  }
467 
468  return 0;
469 }
470 
472 {
473  // create a new, 'empty' project
474  CGBProject_ver2 projObj;
475  CGBProjectHandle project(projObj);
476 
477  CTime create_time(CTime::eCurrent);
478 
479  /// set the project's basic descriptors
480  CProjectDescr& descr = project.SetProject().SetDescr();
481  descr.SetCreate_date().SetToTime(create_time);
482  if (args["title"].HasValue()) {
483  descr.SetTitle(args["title"].AsString());
484  }
485  else {
486  descr.SetTitle("Default");
487  }
488 
489  /// set the data descriptors
490  CProjectFolder& data = project.SetProject().SetData();
491  data.SetInfo().SetTitle("Data");
492  data.SetInfo().SetCreate_date().SetToTime(create_time);
493 
494  /// serialize
495  CNcbiOstream& ostr = args["o"].AsOutputFile();
496  project.Save(ostr, fmt);
497 }
498 
500 {
501  // add a folder to an existing project
502  CGBProjectHandle project;
503  {{
504  CNcbiIstream& istr = args["i"].AsInputFile();
505  if (!project.Load(istr, 0)) {
506  NCBI_THROW(CException, eUnknown, "failed to read project");
507  }
508  }}
509 
510  string folder_name = args["add-folder"].AsString();
511 
513  folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
514  folder->SetInfo().SetTitle(folder_name);
515 
516  CProjectFolder& root = project.SetProject().SetData();
517  root.SetFolders().push_back(folder);
518 
519  /// serialize
520  CNcbiOstream& ostr = args["o"].AsOutputFile();
521  project.Save(ostr, fmt);
522 }
523 
524 static void s_ListProjectFolder(const CProjectFolder& folder, CNcbiOstream& ostr, int level = 0)
525 {
526  const string kSpacer(level * 2, ' ');
527 
528  ostr << kSpacer << "[folder] " << folder.GetInfo().GetTitle()
529  << " (created: "
530  << folder.GetInfo().GetCreate_date().AsCTime().AsString("M/D/Y h:m:s")
531  << ")" << endl;
532 
533  if (folder.IsSetFolders()) {
534  ITERATE(CProjectFolder::TFolders, iter, folder.GetFolders()) {
535  s_ListProjectFolder(**iter, ostr, level + 1);
536  }
537  }
538 
539  if (folder.IsSetItems()) {
540  ITERATE(CProjectFolder::TItems, iter, folder.GetItems()) {
541  string s;
542  CLabel::GetLabel(**iter, &s);
543  ostr << kSpacer << " " << "[item:"
544  << (*iter)->GetId() << "] " << s << endl;
545  }
546  }
547 }
548 
550 {
551  // lists the folders of an existing project
552  CGBProjectHandle project;
553  {
554  CNcbiIstream& istr = args["i"].AsInputFile();
555  if (!project.Load(istr, 0)) {
556  NCBI_THROW(CException, eUnknown, "failed to read project");
557  }
558  }
559 
560  const CProjectFolder& folder = project.GetProject().GetData();
561  CNcbiOstream& ostr = (args["o"]) ? args["o"].AsOutputFile() : NcbiCout;
562  s_ListProjectFolder(folder, ostr);
563 }
564 
566 {
567  // add an item to the project
568  CGBProjectHandle project;
569  {
570  CNcbiIstream& istr = args["i"].AsInputFile();
571  if (!project.Load(istr, 0)) {
572  NCBI_THROW(CException, eUnknown, "failed to read project");
573  }
574  }
575 
576  string folder;
577  if (args["proj-folder"].HasValue()) {
578  folder = args["proj-folder"].AsString();
579  }
580  x_AddItem(project, args, folder);
581 
582  /// serialize
583  CNcbiOstream& ostr = args["o"].AsOutputFile();
584  project.Save(ostr, fmt);
585 }
586 
587 void CGBProjectTool::x_AddItem(CGBProjectHandle& project, const CArgs& args, const string& in_folder)
588 {
590  const string& fmt_str = args["fmt"].AsString();
591  CNcbiIstream& istr = args["add"].AsInputFile();
592  const string track_name = (args["track"]) ? args["track"].AsString() : kEmptyStr;
593 
594  if (fmt_str == "gff") {
595  x_LoadGffData(*item, istr);
596  }
597  else if (fmt_str == "asn") {
598  x_LoadGeneralData(*item, eSerial_AsnText, args["add"].AsString(), track_name);
599  }
600  else if (fmt_str == "asnb") {
601  x_LoadGeneralData(*item, eSerial_AsnBinary, args["add"].AsString(), track_name);
602  }
603  else {
604  NCBI_THROW(CException, eUnknown, "The file format is not supported!");
605  }
606 
607  // add the new item into the project (under a folder if given)
608  if (!in_folder.empty()) {
609  CProjectFolder& root = project.SetProject().SetData();
610 
611  // check if there is a folder named 'in_folder'
612  bool folder_found = false;
613  if (root.IsSetFolders()) {
615  if ((*iter)->GetInfo().GetTitle() == in_folder) {
616  project.AddItem(*item, **iter);
617  folder_found = true;
618  break;
619  }
620  }
621  }
622 
623  // otherwise add a new folder
624  if (!folder_found) {
626  folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
627  folder->SetInfo().SetTitle(in_folder);
628  root.SetFolders().push_back(folder);
629  project.AddItem(*item, *folder);
630  }
631  }
632  else {
633  project.AddItem(*item, project.SetData());
634  }
635 }
636 
638 {
640  CReaderBase::TAnnots annots;
641 
642  CGff3Reader reader(flags);
643  reader.ReadSeqAnnots(annots, istr);
644  if (!annots.empty()) {
645  item.SetObject(*annots.front());
646  }
647 }
648 
650  const string& filename, const string& track_name)
651 {
652  CNcbiIfstream instr(filename.c_str(), IOS_BASE::in | IOS_BASE::binary);
653  if (!instr.is_open() || !instr.good()) {
654  return;
655  }
656  unique_ptr<CObjectIStream> os(CObjectIStream::Open(fmt, instr));
657  string obj_type = os->ReadFileHeader();
658  os->SetStreamPos(0);
659 
660  if (obj_type == "Seq-annot") {
661  CRef<CSeq_annot> obj(new CSeq_annot);
662  *os >> *obj;
663  if (!track_name.empty()) {
664  obj->SetNameDesc(track_name);
665  }
666  item.SetObject(*obj);
667  }
668  else if (obj_type == "Seq-loc") {
669  CRef<CSeq_loc> obj(new CSeq_loc);
670  *os >> *obj;
671  item.SetObject(*obj);
672  }
673  else if (obj_type == "Seq-id") {
674  CRef<CSeq_id> obj(new CSeq_id);
675  *os >> *obj;
676  item.SetObject(*obj);
677  }
678  else if (obj_type == "Seq-align") {
679  // combine all Seq-aligns into a single Seq-annot
680 
682  CFileLoader::LoadFile(filename, objects);
683 
685  for (auto& obj_it : objects) {
686  auto type_info = obj_it->GetThisTypeInfo();
687  if (!type_info) {
688  NCBI_THROW(CException, eUnknown, "Data contains non-serializable object");
689  }
690  if (type_info->GetName() != "Seq-align") {
691  NCBI_THROW(CException, eUnknown, "Data should only contain Seq-align objects");
692  }
693 
694  CRef<CSeq_align> align(dynamic_cast<CSeq_align*>(obj_it.GetNCPointer()));
695  result->SetData().SetAlign().push_back(align);
696  }
697 
698  if (result->IsSetData() && !result->GetData().GetAlign().empty()) {
699  CFile file(filename);
700  string name = file.GetName();
701  if (!track_name.empty()) {
702  result->SetNameDesc(track_name);
703  }
704  else {
705  result->SetNameDesc(name);
706  }
707  result->SetTitleDesc(name);
708  item.SetObject(*result);
709  }
710  }
711  else {
712  NCBI_THROW(CException, eUnknown, "The data type is not supported!");
713  }
714 }
715 
716 
718 {
719  // Reads in all files under the given directory and writes them into a
720  // combined Gbench project file to the -o file
721  CGBProjectHandle project(*new CGBProject_ver2);
722 
723  if (args["i"].HasValue()) {
724  CNcbiIstream& istr = args["i"].AsInputFile();
725  if (!project.Load(istr, 0)) {
726  // do nothing
727  }
728  }
729 
730  CDir parent(args["collapse"].AsString());
731  CRef<CProjectFolder> folder = CRef<CProjectFolder>(&project.SetProject().SetData());
732  if (args["proj-folder"].HasValue()) {
733  string ProjFolder = args["proj-folder"].AsString();
734  folder = s_GetOrCreateFolder(project, *folder, ProjFolder);
735  }
736 
737  if (!args["i"].HasValue())
738  project.SetDescr().SetTitle(parent.GetBase());
739 
740  if (args["title"].HasValue())
741  project.SetDescr().SetTitle(args["title"].AsString());
742 
743  project.SetDescr().SetCreateDate(CTime(CTime::eCurrent));
744  project.SetDescr().SetModifiedDate(CTime(CTime::eCurrent));
745  s_CollapseProjectFolder(*m_Scope, project, *folder, parent);
746 
747  CNcbiOstream& ostr = args["o"].AsOutputFile();
748  project.Save(ostr, fmt);
749 
750 }
751 
753 {
754  // Expands (saves the folders to directories and items to files)
755  // of an existing project
756  CGBProjectHandle project;
757  {
758  CNcbiIstream& istr = args["i"].AsInputFile();
759  if (!project.Load(istr, 0)) {
760  NCBI_THROW(CException, eUnknown, "failed to read project");
761  }
762  }
763 
764  EFormat format = CFormatGuess::eBinaryASN;//eSerial_AsnBinary;
765  string formatstr = args["expand-fmt"].AsString();
766  if (formatstr == "asnb")
768  else if (formatstr == "asnt")
770  else if (formatstr == "xml")
772  //else if(formatstr == "json")
773  // format = eSerial_Json;
774  else if (formatstr == "gtf")
776 
777  const CProjectFolder& folder = project.GetProject().GetData();
778  CDir parent(args["expand"].AsString());
779  s_ScopeProjectFolder(folder);
780  s_ExpandProjectFolder(folder, parent, format);
781 }
782 
783 static void s_RemoveItem(CGBProjectHandle& project,
784  CProjectFolder& folder,
785  const string& label)
786 {
787  if (folder.IsSetFolders()) {
789  s_RemoveItem(project, **iter, label);
790  }
791  }
792 
793  if (folder.IsSetItems()) {
795  string s;
796  CLabel::GetLabel(**iter, &s);
797 
798  if (s == label) {
800  "FIXME: CGBProjectHandle::RemoveItem() not supported");
801  //project.RemoveItem(**iter);
802  return;
803  //iter = folder.SetItems().erase(iter);
804  }
805  }
806  }
807 }
808 
810 {
811  // remove an item from the project
812  CGBProjectHandle project;
813  {
814  CNcbiIstream& istr = args["i"].AsInputFile();
815  if (!project.Load(istr, 0)) {
816  NCBI_THROW(CException, eUnknown, "failed to read project");
817  }
818  }
819 
820  CProjectFolder& folder = project.SetProject().SetData();
821  string label = args["rm"].AsString();
822  s_RemoveItem(project, folder, label);
823 
824  /// serialize
825  CNcbiOstream& ostr = args["o"].AsOutputFile();
826  project.Save(ostr, fmt);
827 }
828 
829 static void s_RemoveFolder(CGBProjectHandle& project,
830  CProjectFolder& folder,
831  const string& label)
832 {
833  if (folder.IsSetFolders()) {
835  if ((*iter)->GetInfo().GetTitle() == label)
836  iter = folder.SetFolders().erase(iter);
837  else
838  s_RemoveFolder(project, **iter, label);
839  }
840  }
841 }
842 
844 {
845  // remove a folder from the project
846  CGBProjectHandle project;
847  {
848  CNcbiIstream& istr = args["i"].AsInputFile();
849  if (!project.Load(istr, 0)) {
850  NCBI_THROW(CException, eUnknown, "failed to read project");
851  }
852  }
853 
854  CProjectFolder& folder = project.SetProject().SetData();
855  string label = args["rmdir"].AsString();
856  s_RemoveFolder(project, folder, label);
857 
858  /// serialize
859  CNcbiOstream& ostr = args["o"].AsOutputFile();
860  project.Save(ostr, fmt);
861 }
862 
863 
864 
867 
868 
869 int NcbiSys_main(int argc, ncbi::TXChar* argv[])
870 {
871  return CGBProjectTool().AppMain(argc, argv);
872 }
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
void NCBI_SeqTable_RegisterModuleClasses(void)
void NCBI_Sequence_RegisterModuleClasses(void)
User-defined methods of the data storage class.
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1641
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
CTime AsCTime(CTime::ETimeZone tz=CTime::eLocal) const
Definition: Date.cpp:70
CDirEntry –.
Definition: ncbifile.hpp:262
CDir –.
Definition: ncbifile.hpp:1695
list< CRef< CSerialObject > > TSerialObjectList
Definition: fileloader.hpp:46
static void LoadFile(const string &Filename, TSerialObjectList &Objects, EFormat Format=CFormatGuess::eUnknown)
Definition: fileloader.cpp:99
CFile –.
Definition: ncbifile.hpp:1604
EFormat
The formats are checked in the same order as declared here.
@ eBinaryASN
Binary ASN.1.
@ eGtf
New GTF, CGtfReader.
@ eTextASN
Text ASN.1.
static string GetLoaderNameFromArgs(CReader *reader=0)
Definition: gbloader.cpp:377
static TRegisterLoaderInfo RegisterInObjectManager(CObjectManager &om, CReader *reader=0, CObjectManager::EIsDefault is_default=CObjectManager::eDefault, CObjectManager::TPriority priority=CObjectManager::kPriority_NotSet)
Definition: gbloader.cpp:366
void x_LoadGffData(CProjectItem &item, CNcbiIstream &istr)
int Run()
Run the application.
void x_Collapse(const CArgs &args, ESerialDataFormat fmt)
CRef< CScope > m_Scope
void x_Expand(const CArgs &args)
void x_AddItem(const CArgs &args, ESerialDataFormat fmt)
void x_LoadGeneralData(CProjectItem &item, ESerialDataFormat fmt, const string &filename, const string &track_name)
void x_RemoveItem(const CArgs &args, ESerialDataFormat fmt)
void x_CreateNewProject(const CArgs &args, ESerialDataFormat fmt)
void x_AddFolder(const CArgs &args, ESerialDataFormat fmt)
void x_ListFolders(const CArgs &args)
void x_RemoveFolder(const CArgs &args, ESerialDataFormat fmt)
void Init()
Initialize the application.
void ReadSeqAnnots(TAnnotList &, CNcbiIstream &, ILineErrorListener *=nullptr) override
Read all objects from given insput stream, returning them as a vector of Seq-annots.
void SetCreateDate(const CTime &time)
SetCreateDate() will add a descriptor for creation date, and insure that only one such descriptor exi...
Definition: ProjectItem.cpp:73
void SetObject(CSerialObject &object)
wrapper for setting the object pointed to by this item
long TReaderFlags
Definition: reader_base.hpp:84
TAnnotList TAnnots
Definition: reader_base.hpp:91
CScope –.
Definition: scope.hpp:92
const CSeq_id & GetSeq_id(TDim row) const
Get seq-id (the first one if segments have different ids).
Definition: Seq_align.cpp:317
void SetNameDesc(const string &name)
Definition: Seq_annot.cpp:66
void SetTitleDesc(const string &title)
Definition: Seq_annot.cpp:96
CTime –.
Definition: ncbitime.hpp:296
static uch flags
char data[12]
Definition: iconv.c:80
CRef< CProjectFolder > s_GetOrCreateFolder(CGBProjectHandle &project, CProjectFolder &parent_folder, const string &folder_name, bool top=true)
CFormatGuess::EFormat EFormat
USING_SCOPE(objects)
static string s_GetAlignTitle(const CSeq_align &align, CScope &scope)
static void s_ExpandProjectFolder(const CProjectFolder &folder, CDir &parentDir, EFormat format, int level=0)
static void s_ScopeProjectFolder(const CProjectFolder &folder)
int NcbiSys_main(int argc, ncbi::TXChar *argv[])
static void s_RemoveItem(CGBProjectHandle &project, CProjectFolder &folder, const string &label)
bool s_Compare_DirEntry_ModDate(CDirEntry *A, CDirEntry *B)
static void s_ListProjectFolder(const CProjectFolder &folder, CNcbiOstream &ostr, int level=0)
unique_ptr< CProjectFileWriter > Writer
static void s_CollapseProjectFolder(CScope &scope, CGBProjectHandle &project, CProjectFolder &folder, CDir &dir)
static void s_RemoveFolder(CGBProjectHandle &project, CProjectFolder &folder, const string &label)
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:819
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1195
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
Definition: ncbimisc.hpp:822
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
@ eRequires
One argument requires another.
Definition: ncbiargs.hpp:956
@ eInputFile
Name of file (must exist and be readable)
Definition: ncbiargs.hpp:595
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
@ eOutputFile
Name of file (must be writable)
Definition: ncbiargs.hpp:596
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
TEntries GetEntries(const string &mask=kEmptyStr, TGetEntriesFlags flags=0) const
Get directory entries based on the specified "mask".
Definition: ncbifile.cpp:3846
string GetBase(void) const
Get the base entry name without extension.
Definition: ncbifile.hpp:3924
bool CreatePath(TCreateFlags flags=fCreate_Default) const
Create the directory path recursively possibly more than one at a time.
Definition: ncbifile.cpp:4106
bool GetTimeT(time_t *modification, time_t *last_access=0, time_t *creation=0) const
Get time stamp(s) of a directory entry (time_t version).
Definition: ncbifile.cpp:2017
static string ConcatPathEx(const string &first, const string &second)
Concatenate two parts of the path for any OS.
Definition: ncbifile.cpp:791
const string & GetPath(void) const
Get entry path.
Definition: ncbifile.hpp:3910
@ fIgnoreRecursive
Suppress "self recursive" elements (the directories "." and "..").
Definition: ncbifile.hpp:1755
static void GetLabel(const CObject &obj, string *label, ELabelType type=eDefault)
Definition: label.cpp:140
@ eUnknown
Definition: app_popup.hpp:72
virtual const CTypeInfo * GetThisTypeInfo(void) const =0
ESerialDataFormat
Data file format.
Definition: serialdef.hpp:71
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
string GetSeqIdString(bool with_version=false) const
Return seqid string with optional version for text seqid type.
Definition: Seq_id.cpp:2145
CConstRef< CSeq_id > GetSeqId(void) const
CConstRef< CSeq_id > GetSeqIdOrNull(void) const
static CSeq_id_Handle GetHandle(const CSeq_id &id)
Normal way of getting a handle, works for any seq-id.
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
void AddDataLoader(const string &loader_name, TPriority pri=kPriority_Default)
Add data loader by name.
Definition: scope.cpp:510
CSeq_id_Handle GetAccVer(const CSeq_id_Handle &idh, TGetFlags flags=0)
Get accession.version Seq-id Returns null CSeq_id_Handle if the sequence is not found or if it doesn'...
Definition: scope.cpp:413
static CRef< CObjectManager > GetInstance(void)
Return the existing object manager or create one.
void ResetDataAndHistory(void)
Clear all information in the scope except added data loaders.
Definition: scope.cpp:331
bool IsNull(void) const THROWS_NONE
Check if pointer is null – same effect as Empty().
Definition: ncbiobj.hpp:1401
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
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NcbiCout
Definition: ncbistre.hpp:543
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
#define kEmptyStr
Definition: ncbistr.hpp:123
char TXChar
Definition: ncbistr.hpp:172
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
@ eCurrent
Use current time. See also CCurrentTime.
Definition: ncbitime.hpp:300
const string & GetName(void) const
Get name of this type.
Definition: typeinfo.cpp:249
static const char label[]
const TCreate_date & GetCreate_date(void) const
Get the Create_date member data.
void SetTitle(const TTitle &value)
Assign a value to Title data member.
list< CRef< CProjectItem > > TItems
void SetCreate_date(TCreate_date &value)
Assign a value to Create_date data member.
const TInfo & GetInfo(void) const
Get the Info member data.
void SetLabel(const TLabel &value)
Assign a value to Label data member.
TFolders & SetFolders(void)
Assign a value to Folders data member.
bool IsSetFolders(void) const
Check if a value has been assigned to Folders data member.
bool IsSetItems(void) const
Check if a value has been assigned to Items data member.
const TLabel & GetLabel(void) const
Get the Label member data.
const TFolders & GetFolders(void) const
Get the Folders member data.
const TTitle & GetTitle(void) const
Get the Title member data.
list< CRef< CProjectFolder > > TFolders
void SetInfo(TInfo &value)
Assign a value to Info data member.
const TItems & GetItems(void) const
Get the Items member data.
TItems & SetItems(void)
Assign a value to Items data member.
const Tdata & Get(void) const
Get the member data.
const TDesc & GetDesc(void) const
Get the Desc member data.
Definition: Seq_annot_.hpp:852
bool CanGetDesc(void) const
Check if it is safe to call GetDesc method.
Definition: Seq_annot_.hpp:846
list< CRef< CAnnotdesc > > Tdata
FILE * file
constexpr auto sort(_Init &&init)
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
Magic spell ;-) needed for some weird compilers... very empiric.
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static Format format
Definition: njn_ioutil.cpp:53
std::istream & in(std::istream &in_, double &x_)
else result
Definition: token2.c:20
Modified on Thu Apr 25 08:20:36 2024 by modify_doxy.py rev. 669887