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

Go to the SVN repository for this file.

00001 /*  $Id: gbproject_tool.cpp 28756 2013-08-23 17:44:43Z wuliangs $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Mike DiCuccio
00027  *
00028  * File Description:
00029  *     Application for manipulating GBench projects.
00030  */
00031 
00032 #include <ncbi_pch.hpp>
00033 #include <corelib/ncbifile.hpp>
00034 #include <corelib/ncbiapp.hpp>
00035 #include <corelib/ncbienv.hpp>
00036 #include <corelib/ncbiargs.hpp>
00037 #include <serial/objistr.hpp>
00038 #include <serial/objostr.hpp>
00039 #include <serial/serial.hpp>
00040 #include <objtools/readers/gff_reader.hpp>
00041 #include <objects/seq/Annotdesc.hpp>
00042 #include <objects/seq/Annot_descr.hpp>
00043 #include <objects/seq/NCBI_Sequence_module.hpp>
00044 #include <objects/seqtable/NCBI_SeqTable_module.hpp>
00045 #include <gui/objects/GBProjectHandle.hpp>
00046 #include <objects/gbproj/GBProject_ver2.hpp>
00047 #include <objects/gbproj/ProjectDescr.hpp>
00048 #include <objects/gbproj/ProjectFolder.hpp>
00049 #include <objects/gbproj/FolderInfo.hpp>
00050 #include <objects/gbproj/ProjectItem.hpp>
00051 #include <gui/objutils/label.hpp>
00052 #include <objects/general/Date.hpp>
00053 #include <objmgr/scope.hpp>
00054 #include <objtools/simple/simple_om.hpp>
00055 #include <objtools/data_loaders/genbank/gbloader.hpp>
00056 
00057 #include "fileloader.hpp"
00058 #include "projectfilewriter.hpp"
00059 
00060 BEGIN_NCBI_SCOPE
00061 USING_SCOPE(objects);
00062 
00063 typedef CFormatGuess::EFormat EFormat;
00064 auto_ptr<CProjectFileWriter> Writer;
00065 
00066 class CGBProjectTool : public CNcbiApplication
00067 {
00068 private:
00069     void Init();
00070     int Run();
00071 
00072     void x_LoadGeneralData(CProjectItem& item,
00073                            ESerialDataFormat fmt,
00074                            CNcbiIstream& istr);
00075 
00076     void x_LoadGffData(CProjectItem& item,
00077                        CNcbiIstream& istr);
00078 
00079 
00080     void x_AddItem(CGBProjectHandle& project,
00081                    CNcbiIstream& istr,
00082                    const string& fmt_str,
00083                    const string& in_folder);
00084 };
00085 
00086 
00087 void CGBProjectTool::Init()
00088 {
00089     NCBI_Sequence_RegisterModuleClasses();
00090     NCBI_SeqTable_RegisterModuleClasses();
00091 
00092     auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions());
00093     arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
00094                               "Application for manipulating GBench projects");
00095 
00096     arg_desc->AddDefaultKey("i", "InputFile",
00097                             "Input project file",
00098                             CArgDescriptions::eInputFile,
00099                             "-");
00100 
00101     arg_desc->AddDefaultKey("o", "OutputFile",
00102                             "Output project file",
00103                             CArgDescriptions::eOutputFile,
00104                             "-");
00105 
00106     arg_desc->AddFlag("noid", "disable access to genbank or id and id2");
00107     arg_desc->AddFlag("list", "List the contents of a project file");
00108     arg_desc->AddFlag("create", "Create a new project file");
00109 
00110     arg_desc->AddOptionalKey("expand", "expand",
00111                              "Create a subdirectory tree from a project file",
00112                              CArgDescriptions::eInputFile);
00113     arg_desc->AddDefaultKey("expand-fmt", "expand_format",
00114                             "Format to expand project file parts out in",
00115                             CArgDescriptions::eString, "asnb");
00116     arg_desc->SetConstraint("expand-fmt", &((*new CArgAllow_Strings(NStr::eNocase)), "asnt", "asnb", "xml", "gtf"));
00117 
00118 
00119     arg_desc->AddOptionalKey("collapse", "collapse",
00120                              "Create a project file from a directory tree",
00121                              CArgDescriptions::eInputFile);
00122 
00123 
00124     arg_desc->AddOptionalKey("title", "Title",
00125                              "Title for new project",
00126                              CArgDescriptions::eString);
00127 
00128     arg_desc->AddOptionalKey("add", "ItemToAdd",
00129                              "Add an item to a project file",
00130                              CArgDescriptions::eInputFile);
00131     arg_desc->AddOptionalKey("fmt", "ItemFormat",
00132                              "Format of input item",
00133                              CArgDescriptions::eString);
00134     arg_desc->SetConstraint("fmt",
00135                             &(*new CArgAllow_Strings,
00136                               "gff", "asn", "asnb", "agp"));
00137     arg_desc->AddOptionalKey("proj-folder", "FolderName",
00138                              "Add item into the named folder",
00139                              CArgDescriptions::eString);
00140     arg_desc->SetDependency("add", CArgDescriptions::eRequires, "fmt");
00141 
00142     arg_desc->AddOptionalKey("add-folder", "FolderToAdd",
00143                              "Create a folder in the existing project",
00144                              CArgDescriptions::eString);
00145 
00146     arg_desc->AddOptionalKey("rm", "RemoveItem",
00147                              "Remove an item from an existing project",
00148                              CArgDescriptions::eString);
00149     arg_desc->AddOptionalKey("rmdir", "RemoveFolder",
00150                              "Remove a folder from an existing project",
00151                              CArgDescriptions::eString);
00152 
00153     SetupArgDescriptions(arg_desc.release());
00154 }
00155 
00156 
00157 static void s_ListProjectFolder(const CProjectFolder& folder,
00158                                 CNcbiOstream& ostr,
00159                                 int level = 0)
00160 {
00161     const string kSpacer(level * 2, ' ');
00162 
00163     ostr << kSpacer << "[folder] " << folder.GetInfo().GetTitle()
00164         << " (created: "
00165         << folder.GetInfo().GetCreate_date().AsCTime().AsString("M/D/Y h:m:s")
00166         << ")" << endl;
00167 
00168     if (folder.IsSetFolders()) {
00169         ITERATE (CProjectFolder::TFolders, iter, folder.GetFolders()) {
00170             s_ListProjectFolder(**iter, ostr, level + 1);
00171         }
00172     }
00173 
00174     if (folder.IsSetItems()) {
00175         ITERATE (CProjectFolder::TItems, iter, folder.GetItems()) {
00176             string s;
00177             CLabel::GetLabel(**iter, &s);
00178             ostr << kSpacer << "  " << "[item:"
00179                 << (*iter)->GetId() << "] " << s << endl;
00180         }
00181     }
00182 }
00183 
00184 
00185 
00186 static void s_ScopeProjectFolder(CRef<CScope> Scope,
00187                                  const CProjectFolder& folder)
00188 {
00189     if (folder.IsSetFolders()) {
00190         ITERATE (CProjectFolder::TFolders, iter, folder.GetFolders()) {
00191             s_ScopeProjectFolder(Scope, **iter);
00192         }
00193     }
00194 
00195     if (folder.IsSetItems()) {
00196         ITERATE (CProjectFolder::TItems, iter, folder.GetItems()) {
00197             Writer->AddToScope(CConstRef<CSerialObject>(((*iter)->GetObject())));
00198         }
00199     }
00200 }
00201 
00202 
00203 static void s_ExpandProjectFolder(CRef<CScope> scope,
00204                                   const CProjectFolder& folder,
00205                                   CDir& parentDir,
00206                                   EFormat format,
00207                                   int level = 0)
00208 {
00209     const string kSpacer(level * 2, ' ');
00210 
00211     string currentName = CDirEntry::ConcatPathEx(parentDir.GetPath(),
00212                                                  folder.GetInfo().GetTitle());
00213     CDir   currentDir(currentName);
00214     if(level == 0)  // get rid of the /Data/ dir thats always on the top when expanding
00215         currentDir = parentDir;
00216     currentDir.CreatePath();
00217     cout << kSpacer << "[folder] " << folder.GetInfo().GetTitle()
00218         << " (created: "
00219         << folder.GetInfo().GetCreate_date().AsCTime().AsString("M/D/Y h:m:s")
00220         << ")" << "    " << currentName << endl;
00221 
00222     if (folder.IsSetFolders()) {
00223         ITERATE (CProjectFolder::TFolders, iter, folder.GetFolders()) {
00224             s_ExpandProjectFolder(scope, **iter, currentDir, format, level + 1);
00225         }
00226     }
00227 
00228     if (folder.IsSetItems()) {
00229         ITERATE (CProjectFolder::TItems, iter, folder.GetItems()) {
00230             string itemTitle;
00231             CLabel::GetLabel(**iter, &itemTitle);
00232             string itemFilename = CDirEntry::ConcatPathEx(currentDir.GetPath(), itemTitle);
00233             cout << kSpacer << "  " << "[item:"
00234                 << (*iter)->GetId() << "] " << itemTitle << "  " << itemFilename << endl;
00235 
00236             Writer->WriteFile(itemFilename,
00237                              CConstRef<CSerialObject>(((*iter)->GetObject())),
00238                              format);
00239             //auto_ptr<CObjectOStream> ObjS(CObjectOStream::Open(format, itemFilename));
00240             //*ObjS << *((*iter)->GetObject());
00241         }
00242     }
00243 }
00244 
00245 bool s_Compare_DirEntry_ModDate(CDirEntry* A, CDirEntry* B)
00246 {
00247     time_t mod_a, mod_b;
00248     A->GetTimeT(&mod_a);
00249     B->GetTimeT(&mod_b);
00250     return (mod_a < mod_b);
00251 }
00252 
00253 static void s_CollapseProjectFolder(CRef<CScope> scope,
00254                                     CGBProjectHandle& project,
00255                                     CProjectFolder& folder,
00256                                     CDir& dir)
00257 {
00258     list< AutoPtr< CDirEntry > > children =
00259         dir.GetEntries(kEmptyStr, CDir::fIgnoreRecursive);
00260 
00261     vector< CDirEntry* > children_vec;
00262     ITERATE(list<AutoPtr<CDirEntry> >, childIter, children) {
00263         CDirEntry* New = new CDirEntry(**childIter);
00264         children_vec.push_back(New);
00265     }
00266     sort(children_vec.begin(), children_vec.end(), s_Compare_DirEntry_ModDate);
00267 
00268     typedef multimap<string, CRef<CProjectItem> > TItemMap;
00269     TItemMap items;
00270 
00271     string CurrLabel;
00272     NON_CONST_ITERATE(vector<CDirEntry*>, childIter, children_vec) {
00273 
00274         // Save the file name early, in case its actually a symbolic link.
00275         CurrLabel = (*childIter)->GetName();
00276 
00277         // Then if its a link, dereference it
00278         if( (*childIter)->IsLink() ) {
00279             (*childIter)->DereferenceLink();
00280         }
00281 
00282         if( (*childIter)->IsDir() ) {
00283             CRef<CProjectFolder> childFolder(new CProjectFolder);
00284             childFolder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
00285             childFolder->SetInfo().SetTitle(CurrLabel);
00286             cout << "Dir   " << CurrLabel << endl;
00287             folder.SetFolders().push_back(childFolder);
00288             CDir childDir(**childIter);
00289             s_CollapseProjectFolder(scope, project, *childFolder, childDir);
00290         }
00291         else if( (*childIter)->IsFile() ) {
00292 
00293             cout << "File  " << CurrLabel << endl;
00294             CFileLoader::TSerialObjectList Objects;
00295             CFileLoader::LoadFile( (*childIter)->GetPath(), Objects);
00296 
00297             if(Objects.empty())
00298                 continue;
00299 
00300             ITERATE(CFileLoader::TSerialObjectList, ObjIter, Objects) {
00301 
00302                 CRef<CSerialObject> Object = *ObjIter;
00303                 string label = CurrLabel;
00304 
00305                 if(Object->GetThisTypeInfo() != NULL &&
00306                    Object->GetThisTypeInfo()->GetName() == "Seq-annot") {
00307                     CRef<CSeq_annot> Annot(static_cast<CSeq_annot*>(Object.GetPointer()));
00308                     if (Annot->CanGetDesc()) {
00309                         CConstRef<CAnnotdesc> name;
00310                         CConstRef<CAnnotdesc> title;
00311                         ITERATE(CSeq_annot::TDesc::Tdata, desciter, Annot->GetDesc().Get()) {
00312                             if((*desciter)->IsName()) {
00313                                 name = *desciter;
00314                             } else if ((*desciter)->IsTitle()) {
00315                                 title = *desciter;
00316                             }
00317                         }
00318                         if (title) {
00319                             label = title->GetTitle();
00320                         } else if (name) {
00321                             label = name->GetName();
00322                         }
00323                     }
00324                     else{
00325                         Annot->SetNameDesc(CurrLabel);
00326                         Annot->SetTitleDesc(CurrLabel);
00327                     }
00328                 } else if(Object->GetThisTypeInfo() != NULL &&
00329                           Object->GetThisTypeInfo()->GetName() == "Seq-align") {
00330                     CRef<CSeq_align> Align(static_cast<CSeq_align*>(Object.GetPointer()));
00331                     string AlignTitle;
00332                     CSeq_id_Handle Id1, Id2;
00333                     string Id1Str, Id2Str;
00334                     Id1 = scope->GetAccVer(CSeq_id_Handle::GetHandle(Align->GetSeq_id(0)));
00335                     Id2 = scope->GetAccVer(CSeq_id_Handle::GetHandle(Align->GetSeq_id(1)));
00336                     if(Id1.GetSeqIdOrNull().IsNull())
00337                         Id1Str = Align->GetSeq_id(0).GetSeqIdString(true);
00338                     else
00339                         Id1Str = Id1.GetSeqId()->GetSeqIdString(true);
00340                     if(Id2.GetSeqIdOrNull().IsNull())
00341                         Id2Str = Align->GetSeq_id(1).GetSeqIdString(true);
00342                     else
00343                         Id2Str = Id2.GetSeqId()->GetSeqIdString(true);
00344                     AlignTitle = Id1Str + " x " + Id2Str;
00345                     label = AlignTitle;
00346                 } else if(Object->GetThisTypeInfo() != NULL &&
00347                           Object->GetThisTypeInfo()->GetName() == "Project-item") {
00348                     CRef<CProjectItem> Item(static_cast<CProjectItem*>(Object.GetPointer()));
00349                     items.insert(TItemMap::value_type(Item->GetLabel(), Item));
00350                     continue;
00351                 }
00352 
00353                 CRef<CProjectItem> childItem(new CProjectItem);
00354                 CTime CreateTime;
00355                 (*childIter)->GetTime(&CreateTime);
00356                 childItem->SetCreateDate(CreateTime);
00357 
00358                 if (Object->GetThisTypeInfo() == CProjectItem::GetTypeInfo()) {
00359                     childItem.Reset(dynamic_cast<CProjectItem*>(Object.GetPointer()));
00360                 } else {
00361                     childItem->SetLabel(label);
00362                     childItem->SetObject(*Object);
00363                     cout << "    Title: " << label << endl;
00364                 }
00365                 items.insert(TItemMap::value_type(label, childItem));
00366             }
00367         }
00368     
00369         delete *childIter;
00370         *childIter = NULL;
00371     }
00372 
00373     NON_CONST_ITERATE(TItemMap, it, items) {
00374         project.AddItem(*it->second, folder);
00375     }
00376 
00377 }
00378 
00379 
00380 static void s_RemoveItem(CGBProjectHandle& project,
00381                          CProjectFolder& folder,
00382                          const string& label)
00383 {
00384     if (folder.IsSetFolders()) {
00385         NON_CONST_ITERATE(CProjectFolder::TFolders, iter, folder.SetFolders()) {
00386             s_RemoveItem(project, **iter, label);
00387         }
00388     }
00389 
00390     if (folder.IsSetItems()) {
00391         NON_CONST_ITERATE(CProjectFolder::TItems, iter, folder.SetItems()) {
00392             string s;
00393             CLabel::GetLabel(**iter, &s);
00394 
00395             if(s == label) {
00396                 NCBI_THROW(CException, eUnknown,
00397                            "FIXME: CGBProjectHandle::RemoveItem() not supported");
00398                 //project.RemoveItem(**iter);
00399                 return;
00400                 //iter = folder.SetItems().erase(iter);
00401             }
00402         }
00403     }
00404 }
00405 
00406 
00407 static void s_RemoveFolder(CGBProjectHandle& project,
00408                            CProjectFolder& folder,
00409                            const string& label)
00410 {
00411     if (folder.IsSetFolders()) {
00412         NON_CONST_ITERATE(CProjectFolder::TFolders, iter, folder.SetFolders()) {
00413             if( (*iter)->GetInfo().GetTitle() == label)
00414                 iter = folder.SetFolders().erase(iter);
00415             else
00416                 s_RemoveFolder(project, **iter, label);
00417         }
00418     }
00419 }
00420 
00421 
00422 CRef<CProjectFolder> s_GetOrCreateFolder(CGBProjectHandle& project,
00423                                     CProjectFolder& parent_folder,
00424                                     const string& folder_name,
00425                                     bool top = true)
00426 {
00427     if (parent_folder.IsSetFolders()) {
00428         NON_CONST_ITERATE(CProjectFolder::TFolders, iter, parent_folder.SetFolders()) {
00429             if( (*iter)->GetInfo().GetTitle() == folder_name) {
00430                 return (*iter); //    iter = folder.SetFolders().erase(iter);
00431             }
00432             else
00433                 s_GetOrCreateFolder(project, **iter, folder_name, false);
00434         }
00435     }
00436 
00437     if(top) {
00438         CRef<CProjectFolder> folder(new CProjectFolder);
00439         folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
00440         folder->SetInfo().SetTitle(folder_name);
00441         parent_folder.SetFolders().push_back(folder);
00442         return folder;
00443     }
00444     return CRef<CProjectFolder>();
00445 }
00446 
00447 
00448 void CGBProjectTool::x_LoadGeneralData(CProjectItem& item,
00449                                      ESerialDataFormat fmt,
00450                                      CNcbiIstream& istr)
00451 {
00452     auto_ptr<CObjectIStream> os(CObjectIStream::Open(fmt, istr));
00453     string obj_type = os->ReadFileHeader();
00454     os->SetStreamPos(0);
00455 
00456     if (obj_type == "Seq-annot") {
00457         CRef<CSeq_annot> obj(new CSeq_annot);
00458         *os >> *obj;
00459         item.SetObject(*obj);
00460     } else if (obj_type == "Seq-loc") {
00461         CRef<CSeq_loc> obj(new CSeq_loc);
00462         *os >> *obj;
00463         item.SetObject(*obj);
00464     } else if (obj_type == "Seq-id") {
00465         CRef<CSeq_id> obj(new CSeq_id);
00466         *os >> *obj;
00467         item.SetObject(*obj);
00468     } else {
00469         NCBI_THROW(CException, eUnknown, "The data type is not supported!");
00470     }
00471 }
00472 
00473 
00474 void CGBProjectTool::x_LoadGffData(CProjectItem& item,
00475                                    CNcbiIstream& istr)
00476 {
00477     // always treat it as a seq-entry
00478     CGFFReader reader;
00479     CRef<CSeq_entry> obj = reader.Read(istr);
00480     item.SetObject(*obj);
00481 }
00482 
00483 
00484 void CGBProjectTool::x_AddItem(CGBProjectHandle& project,
00485                                CNcbiIstream& istr,
00486                                const string& fmt_str,
00487                                const string& in_folder)
00488 {
00489     CRef<CProjectItem> item(new CProjectItem);
00490 
00491     if (fmt_str == "gff") {
00492         x_LoadGffData(*item, istr);
00493     } else {
00494         if (fmt_str == "asn") {
00495             x_LoadGeneralData(*item, eSerial_AsnText, istr);
00496         } else if (fmt_str == "asnb") {
00497             x_LoadGeneralData(*item, eSerial_AsnBinary, istr);
00498         } else {
00499             NCBI_THROW(CException, eUnknown, "The file format is not currently supported!");
00500         }
00501     }
00502 
00503     // add the new item into the project (under a folder if given)
00504     if (!in_folder.empty()) {
00505         CProjectFolder& root = project.SetProject().SetData();
00506 
00507         // check if there is a folder named 'in_folder'
00508         bool folder_found = false;
00509         if (root.IsSetFolders()) {
00510             NON_CONST_ITERATE (CProjectFolder::TFolders, iter, root.SetFolders()) {
00511                 if ((*iter)->GetInfo().GetTitle() == in_folder) {
00512                     project.AddItem(*item, **iter);
00513                     folder_found = true;
00514                     break;
00515                 }
00516             }
00517         }
00518 
00519         // otherwise add a new folder
00520         if (!folder_found) {
00521             CRef<CProjectFolder> folder(new CProjectFolder);
00522             folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
00523             folder->SetInfo().SetTitle(in_folder);
00524             root.SetFolders().push_back(folder);
00525             project.AddItem(*item, *folder);
00526         }
00527     } else {
00528         project.AddItem(*item, project.SetData());
00529     }
00530 }
00531 
00532 
00533 int CGBProjectTool::Run()
00534 {
00535     CArgs args = GetArgs();
00536 
00537     bool EnableID = true;
00538     if(args["noid"].HasValue() && args["noid"].AsBoolean())
00539         EnableID = false;
00540 
00541 
00542     CRef<CObjectManager> object_manager = CObjectManager::GetInstance();
00543     CRef<CScope> scope(new CScope(*object_manager));                                                  
00544     scope->ResetDataAndHistory();  
00545 
00546     if(EnableID) {
00547         CGBDataLoader::RegisterInObjectManager(*object_manager, NULL, CObjectManager::eDefault);  
00548         scope->AddDataLoader(CGBDataLoader::GetLoaderNameFromArgs());                         
00549     }
00550 
00551     Writer.reset(new CProjectFileWriter(scope));
00552     
00553 
00554     ESerialDataFormat saveformat = eSerial_AsnBinary;
00555 
00556     if (args["create"]) {
00557 
00558         //
00559         // create a new project
00560         //
00561         CGBProject_ver2 projObj;
00562         CGBProjectHandle project(projObj);
00563 
00564         CTime create_time(CTime::eCurrent);
00565 
00566         /// set the project's basic descriptors
00567         CProjectDescr& descr = project.SetProject().SetDescr();
00568         descr.SetCreate_date().SetToTime(create_time);
00569         if (args["title"].HasValue()) {
00570             descr.SetTitle(args["title"].AsString());
00571         } else {
00572             descr.SetTitle("Default");
00573         }
00574 
00575         /// set the data descriptors
00576         CProjectFolder& data = project.SetProject().SetData();
00577         data.SetInfo().SetTitle("Data");
00578         data.SetInfo().SetCreate_date().SetToTime(create_time);
00579 
00580         /// serialize
00581         CNcbiOstream& ostr = args["o"].AsOutputFile();
00582         project.Save(ostr, saveformat);
00583 
00584     } else if (args["add-folder"].HasValue()) {
00585         ///
00586         /// add a folder to an existing project
00587         ///
00588         CGBProjectHandle project;
00589         {{
00590             CNcbiIstream& istr = args["i"].AsInputFile();
00591             if ( !project.Load(istr,0) ) {
00592                 NCBI_THROW(CException, eUnknown, "failed to read project");
00593             }
00594         }}
00595 
00596         string folder_name = args["add-folder"].AsString();
00597 
00598         CRef<CProjectFolder> folder(new CProjectFolder);
00599         folder->SetInfo().SetCreateDate(CTime(CTime::eCurrent));
00600         folder->SetInfo().SetTitle(folder_name);
00601 
00602         CProjectFolder& root = project.SetProject().SetData();
00603         root.SetFolders().push_back(folder);
00604 
00605         /// serialize
00606         CNcbiOstream& ostr = args["o"].AsOutputFile();
00607         project.Save(ostr, saveformat);
00608 
00609     } else if (args["list"]) {
00610         /// lists the folders of an existing project
00611         CGBProjectHandle project;
00612         {{
00613             CNcbiIstream& istr = args["i"].AsInputFile();
00614             if ( !project.Load(istr,0) ) {
00615                 NCBI_THROW(CException, eUnknown, "failed to read project");
00616             }
00617         }}
00618 
00619         const CProjectFolder& folder = project.GetProject().GetData();
00620         CNcbiOstream& ostr = args["o"].AsOutputFile();
00621         s_ListProjectFolder(folder, ostr);
00622     } else if (args["expand"].HasValue()) {
00623         /// Expands (saves the folders to directories and items to files)
00624         //   of an existing project
00625         CGBProjectHandle project;
00626         {{
00627             CNcbiIstream& istr = args["i"].AsInputFile();
00628             if ( !project.Load(istr,0) ) {
00629                 NCBI_THROW(CException, eUnknown, "failed to read project");
00630             }
00631         }}
00632 
00633         EFormat format = CFormatGuess::eBinaryASN;//eSerial_AsnBinary;
00634         string formatstr = args["expand-fmt"].AsString();
00635         if(formatstr == "asnb")
00636             format = CFormatGuess::eBinaryASN;
00637         else if(formatstr == "asnt")
00638             format = CFormatGuess::eTextASN;
00639         else if(formatstr == "xml")
00640             format = CFormatGuess::eXml;
00641         //else if(formatstr == "json")
00642         //    format = eSerial_Json;
00643         else if(formatstr == "gtf")
00644             format = CFormatGuess::eGtf;
00645 
00646         const CProjectFolder& folder = project.GetProject().GetData();
00647         CDir parent(args["expand"].AsString());
00648         s_ScopeProjectFolder(scope, folder);
00649         s_ExpandProjectFolder(scope, folder, parent, format);
00650     } else if (args["collapse"].HasValue()) {
00651         /// Expands (saves the folders to directories and items to files)
00652         //   of an existing project
00653         CGBProjectHandle project(*new CGBProject_ver2);
00654         {{
00655             CNcbiIstream& istr = args["i"].AsInputFile();
00656             if ( !project.Load(istr,0) ) {
00657             //cerr << "Failed to load, making new proj" << endl;
00658             //    NCBI_THROW(CException, eUnknown, "failed to read project");
00659             }
00660         }}
00661 
00662 
00663         CDir parent(args["collapse"].AsString());
00664         CRef<CProjectFolder> folder = CRef<CProjectFolder>(&project.SetProject().SetData());
00665         if(args["proj-folder"].HasValue()) {
00666             string ProjFolder = args["proj-folder"].AsString();
00667             folder = s_GetOrCreateFolder(project, *folder, ProjFolder);
00668         }
00669 
00670         if(args["i"].AsString() == "-")
00671             project.SetDescr().SetTitle(parent.GetBase());
00672 
00673         if(args["title"].HasValue())
00674             project.SetDescr().SetTitle(args["title"].AsString());
00675 
00676         project.SetDescr().SetCreateDate(CTime(CTime::eCurrent));
00677         project.SetDescr().SetModifiedDate(CTime(CTime::eCurrent));
00678         s_CollapseProjectFolder(scope, project, *folder, parent);
00679         CNcbiOstream& ostr = args["o"].AsOutputFile();
00680         project.Save(ostr, saveformat);
00681     } else if (args["add"].HasValue()) {
00682         ///
00683         /// add an item to the project
00684         ///
00685         CGBProjectHandle project;
00686         {{
00687             CNcbiIstream& istr = args["i"].AsInputFile();
00688             if ( !project.Load(istr,0) ) {
00689                 NCBI_THROW(CException, eUnknown, "failed to read project");
00690             }
00691         }}
00692 
00693         string folder;
00694         if (args["proj-folder"].HasValue()) {
00695             folder = args["proj-folder"].AsString();
00696         }
00697         x_AddItem(project, args["add"].AsInputFile(),
00698                   args["fmt"].AsString(), folder);
00699         /// serialize
00700         CNcbiOstream& ostr = args["o"].AsOutputFile();
00701         project.Save(ostr, saveformat);
00702 
00703     } else if (args["rm"].HasValue()) {
00704 
00705         // remove an item from the project
00706         CGBProjectHandle project;
00707         {{
00708             CNcbiIstream& istr = args["i"].AsInputFile();
00709             if ( !project.Load(istr,0) ) {
00710                 NCBI_THROW(CException, eUnknown, "failed to read project");
00711             }
00712         }}
00713 
00714         CProjectFolder& folder = project.SetProject().SetData();
00715         string label = args["rm"].AsString();
00716         s_RemoveItem(project, folder, label);
00717 
00718         /// serialize
00719         CNcbiOstream& ostr = args["o"].AsOutputFile();
00720         project.Save(ostr, saveformat);
00721 
00722     } else if (args["rmdir"].HasValue()) {
00723 
00724         // remove an item from the project
00725         CGBProjectHandle project;
00726         {{
00727             CNcbiIstream& istr = args["i"].AsInputFile();
00728             if ( !project.Load(istr,0) ) {
00729                 NCBI_THROW(CException, eUnknown, "failed to read project");
00730             }
00731         }}
00732 
00733         CProjectFolder& folder = project.SetProject().SetData();
00734         string label = args["rmdir"].AsString();
00735         s_RemoveFolder(project, folder, label);
00736 
00737         /// serialize
00738         CNcbiOstream& ostr = args["o"].AsOutputFile();
00739         project.Save(ostr, saveformat);
00740 
00741     } else {
00742         NCBI_THROW(CException, eUnknown, "command not yet implemented");
00743     }
00744 
00745     return 0;
00746 }
00747 
00748 
00749 END_NCBI_SCOPE
00750 USING_SCOPE(ncbi);
00751 
00752 
00753 int main(int argc, const char* argv[])
00754 {
00755     return CGBProjectTool().AppMain(argc, argv, 0, eDS_Default, 0);
00756 }
Modified on Sun Mar 29 12:25:28 2015 by modify_doxy.py rev. 426318