00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef NCL_NXSPUBLICBLOCKS_H
00020 #define NCL_NXSPUBLICBLOCKS_H
00021
00022 #include <vector>
00023 #include "ncl/nxsdefs.h"
00024 #include "ncl/nxsblock.h"
00025 #include "ncl/nxsassumptionsblock.h"
00026 #include "ncl/nxscharactersblock.h"
00027 #include "ncl/nxsdatablock.h"
00028 #include "ncl/nxsdistancesblock.h"
00029 #include "ncl/nxstaxablock.h"
00030 #include "ncl/nxstreesblock.h"
00031 #include "ncl/nxsunalignedblock.h"
00032 #include "ncl/nxsreader.h"
00033
00034 class NxsStoreTokensBlockReader
00035 : public NxsBlock
00036 {
00037 public:
00038
00039
00040
00041 NxsStoreTokensBlockReader(std::string blockName, bool storeTokenInfo)
00042 :storeAllTokenInfo(storeTokenInfo),
00043 tolerateEOFInBlock(false)
00044 {
00045 id = NxsString(blockName.c_str());
00046 }
00047 void Read(NxsToken &token);
00048 void Reset();
00049 void Report(std::ostream &out) NCL_COULD_BE_CONST
00050 {
00051 ReportConst(out);
00052 }
00053 void WriteAsNexus(std::ostream &out) const;
00054
00055
00056
00057 NxsStoreTokensBlockReader & operator=(const NxsStoreTokensBlockReader &other)
00058 {
00059 Reset();
00060 CopyBaseBlockContents(static_cast<const NxsBlock &>(other));
00061 commandsRead = other.commandsRead;
00062 justTokens = other.justTokens;
00063 storeAllTokenInfo = other.storeAllTokenInfo;
00064 tolerateEOFInBlock = other.tolerateEOFInBlock;
00065 return *this;
00066 }
00067
00068 NxsStoreTokensBlockReader * Clone() const
00069 {
00070 NxsStoreTokensBlockReader * b = new NxsStoreTokensBlockReader(id, storeAllTokenInfo);
00071 *b = *this;
00072 return b;
00073 }
00075 virtual bool CanReadBlockType(const NxsToken & token)
00076 {
00077 if (id.length() == 0)
00078 {
00079 id.assign(token.GetTokenReference().c_str());
00080 id.ToUpper();
00081 return true;
00082 }
00083 return token.Equals(id);
00084 }
00085 virtual bool TolerateEOFInBlock() const
00086 {
00087 return tolerateEOFInBlock;
00088 }
00089 void SetTolerateEOFInBlock(bool v)
00090 {
00091 tolerateEOFInBlock = v;
00092 }
00093 const std::list<ProcessedNxsCommand> & GetCommands() const
00094 {
00095 return commandsRead;
00096 }
00097 protected:
00098 void ReadCommand(NxsToken &token);
00099 void ReportConst(std::ostream &out) const;
00100
00101 typedef std::vector<std::string> VecString;
00102 typedef std::list<VecString> ListVecString;
00103
00104
00105 std::list<ProcessedNxsCommand> commandsRead;
00106 ListVecString justTokens;
00107 bool storeAllTokenInfo;
00108 bool tolerateEOFInBlock;
00109 };
00118 class NxsDefaultPublicBlockFactory
00119 : public NxsBlockFactory
00120 {
00121 public:
00128 NxsDefaultPublicBlockFactory(bool readUnknownBlocks, bool storeTokenInfo)
00129 :tokenizeUnknownBlocks(readUnknownBlocks),
00130 storeTokenInfoArg(storeTokenInfo)
00131 {}
00132 virtual NxsBlock * GetBlockReaderForID(const std::string & id, NxsReader *reader, NxsToken *token);
00133
00134 protected:
00135 NxsAssumptionsBlockFactory assumpBlockFact;
00136 NxsCharactersBlockFactory charBlockFact;
00137 NxsDataBlockFactory dataBlockFact;
00138 NxsDistancesBlockFactory distancesBlockFact;
00139 NxsTaxaBlockFactory taxaBlockFact;
00140 NxsTreesBlockFactory treesBlockFact;
00141 NxsUnalignedBlockFactory unalignedBlockFact;
00142
00143 bool tokenizeUnknownBlocks;
00144 bool storeTokenInfoArg;
00145 };
00146
00147
00153 class NxsCloneBlockFactory
00154 : public NxsBlockFactory
00155 {
00156 public:
00157 NxsCloneBlockFactory()
00158 :defPrototype(NULL)
00159 {}
00166 virtual NxsBlock * GetBlockReaderForID(const std::string & id,
00167 NxsReader *,
00168 NxsToken *)
00169 {
00170 std::string b(id.c_str());
00171 NxsString::to_upper(b);
00172 std::map<std::string , const NxsBlock *>::const_iterator pIt = prototypes.find(b);
00173 if (pIt == prototypes.end())
00174 return (defPrototype ? defPrototype->Clone() : NULL);
00175 return pIt->second->Clone();
00176 }
00177
00182 bool AddDefaultPrototype(const NxsBlock * exemplar)
00183 {
00184 bool replaced = defPrototype != NULL;
00185 defPrototype = exemplar;
00186 return replaced;
00187 }
00191 bool AddPrototype(const NxsBlock * exemplar,
00192 const char * blockName = NULL)
00193 {
00194 std::string b;
00195 if (blockName)
00196 b.assign(blockName);
00197 else
00198 {
00199 if (exemplar == NULL)
00200 return false;
00201 NxsString bId = exemplar->GetID();
00202 b.assign(bId.c_str());
00203 }
00204 NxsString::to_upper(b);
00205 bool replaced = prototypes.find(b) != prototypes.end();
00206 prototypes[b] = exemplar;
00207 return replaced;
00208 }
00209
00210 protected:
00211 std::map<std::string , const NxsBlock *> prototypes;
00212 const NxsBlock * defPrototype;
00213 };
00214
00215
00216
00217 typedef std::pair<std::string, std::string> NxsNameToNameTrans;
00218
00220 void writeAttributeValue(std::ostream & out, const std::string & v);
00221
00225 class NxsConversionOutputRecord
00226 {
00227 public:
00228
00229 NxsConversionOutputRecord()
00230 :addNumbersToDisambiguateNames(false),
00231 writeNameTranslationFile(true),
00232 translationFilename("NameTranslationFile"),
00233 numberTranslationFiles(true),
00234 verboseWritingOfNameTranslationFile(true)
00235 {}
00236
00237 void writeNameTranslation(std::vector<NxsNameToNameTrans>, const NxsTaxaBlockAPI * );
00238
00239 static std::string getUniqueFilenameWithLowestIndex(const char * prefix);
00240 static void writeTaxonNameTranslationFilepath(const char * fn, const std::vector<NxsNameToNameTrans> & nameTrans, const NxsTaxaBlockAPI *, bool verbose=false);
00241 static void writeTaxonNameTranslationStream(std::ostream & fn, const std::vector<NxsNameToNameTrans> & nameTrans, const NxsTaxaBlockAPI *);
00242
00243
00244
00245
00246
00247 bool addNumbersToDisambiguateNames;
00248 bool writeNameTranslationFile;
00249 std::string translationFilename;
00250 bool numberTranslationFiles;
00251 bool verboseWritingOfNameTranslationFile;
00252 std::map<const NxsTaxaBlockAPI *, std::string> taxaBlocksToConversionFiles;
00253 };
00254
00255
00281 class PublicNexusReader: public ExceptionRaisingNxsReader
00282 {
00283 public:
00284
00285 static BlockReaderList parseFileOrThrow(const char *filepath,
00286 NxsReader::WarningHandlingMode mode = NxsReader::WARNINGS_TO_STDERR,
00287 bool parsePrivateBlocks=true,
00288 bool storeTokenInfo=true);
00289
00293 enum NexusBlocksToRead
00294 {
00295 NEXUS_TAXA_BLOCK_BIT = 0x01,
00296 NEXUS_TREES_BLOCK_BIT = 0x02,
00297 NEXUS_CHARACTERS_BLOCK_BIT = 0x04,
00298 NEXUS_ASSUMPTIONS_BLOCK_BIT = 0x08,
00299 NEXUS_SETS_BLOCK_BIT = 0x10,
00300 NEXUS_UNALIGNED_BLOCK_BIT = 0x20,
00301 NEXUS_DISTANCES_BLOCK_BIT = 0x40,
00302 NEXUS_UNKNOWN_BLOCK_BIT = 0x80
00303 };
00304
00312 PublicNexusReader(const int blocksToRead = -1, NxsReader::WarningHandlingMode mode=NxsReader::WARNINGS_TO_STDERR);
00313 virtual ~PublicNexusReader();
00314
00315 virtual void Execute(NxsToken& token, bool notifyStartStop = true);
00316 std::string GetErrorMessage()
00317 {
00318 return errorMsg;
00319 }
00320
00329 NxsTaxaBlock * RegisterTaxa(const std::vector<std::string> & tl);
00330
00339 NxsAssumptionsBlock * GetAssumptionsBlockTemplate() {return assumptionsBlockTemplate;}
00348 NxsCharactersBlock * GetCharactersBlockTemplate() {return charactersBlockTemplate;}
00357 NxsDataBlock * GetDataBlockTemplate() {return dataBlockTemplate;}
00366 NxsDistancesBlock * GetDistancesBlockTemplate() {return distancesBlockTemplate;}
00375 NxsTaxaBlock * GetTaxaBlockTemplate() {return taxaBlockTemplate;}
00384 NxsTreesBlock * GetTreesBlockTemplate() {return treesBlockTemplate;}
00393 NxsUnalignedBlock * GetUnalignedBlockTemplate() {return unalignedBlockTemplate;}
00402 NxsStoreTokensBlockReader * GetUnknownBlockTemplate() const {return storerBlockTemplate;}
00403
00408 unsigned GetNumAssumptionsBlocks(const NxsTaxaBlock *taxa) const;
00413 unsigned GetNumAssumptionsBlocks(const NxsCharactersBlock *chars) const;
00418 unsigned GetNumAssumptionsBlocks(const NxsTreesBlock *trees) const;
00429 NxsAssumptionsBlock * GetAssumptionsBlock(const NxsTaxaBlock *taxa, unsigned index) const;
00430 NxsAssumptionsBlock * GetAssumptionsBlock(const NxsCharactersBlock *taxa, unsigned index) const;
00431 NxsAssumptionsBlock * GetAssumptionsBlock(const NxsTreesBlock *taxa, unsigned index) const;
00432
00435 unsigned GetNumUnknownBlocks() const;
00440 NxsStoreTokensBlockReader * GetUnknownBlock(unsigned index) const;
00441
00446 unsigned GetNumCharactersBlocks(const NxsTaxaBlock *taxa) const;
00457 NxsCharactersBlock * GetCharactersBlock(const NxsTaxaBlock *taxa, unsigned index) const;
00458
00463 unsigned GetNumDistancesBlocks(const NxsTaxaBlock *taxa) const;
00474 NxsDistancesBlock * GetDistancesBlock(const NxsTaxaBlock *taxa, unsigned index) const;
00475
00480 unsigned GetNumTaxaBlocks() const;
00484 NxsTaxaBlock * GetTaxaBlock(unsigned index) const;
00485
00490 unsigned GetNumTreesBlocks(const NxsTaxaBlock *taxa) const;
00501 NxsTreesBlock * GetTreesBlock(const NxsTaxaBlock *taxa, unsigned index) const;
00502
00507 unsigned GetNumUnalignedBlocks(const NxsTaxaBlock *taxa) const;
00518 NxsUnalignedBlock * GetUnalignedBlock(const NxsTaxaBlock *taxa, unsigned index) const;
00519
00520
00525 virtual void DeleteBlocksFromFactories()
00526 {
00527 NxsReader::DeleteBlocksFromFactories();
00528 ClearUsedBlockList();
00529 }
00536 virtual void ClearUsedBlockList();
00545 virtual void ClearContent()
00546 {
00547 assumptionsBlockVec.clear();
00548 charactersBlockVec.clear();
00549 dataBlockVec.clear();
00550 distancesBlockVec.clear();
00551 storerBlockVec.clear();
00552 taxaBlockVec.clear();
00553 treesBlockVec.clear();
00554 unalignedBlockVec.clear();
00555 ExceptionRaisingNxsReader::ClearContent();
00556 }
00557
00564 void AddReadAssumptionsBlock(NxsAssumptionsBlock * block)
00565 {
00566 assumptionsBlockVec.push_back(block);
00567 AddReadBlock("ASSUMPTIONS", block);
00568 }
00575 void AddReadCharactersBlock(NxsCharactersBlock * block)
00576 {
00577 charactersBlockVec.push_back(block);
00578 AddReadBlock("CHARACTERS", block);
00579 }
00586 void AddReadDataBlock(NxsDataBlock * block)
00587 {
00588 dataBlockVec.push_back(block);
00589 AddReadBlock("CHARACTERS", block);
00590 }
00597 void AddReadDistancesBlock(NxsDistancesBlock * block)
00598 {
00599 distancesBlockVec.push_back(block);
00600 AddReadBlock("DISTANCES", block);
00601 }
00608 void AddReadTaxaBlock(NxsTaxaBlock * block)
00609 {
00610 taxaBlockVec.push_back(block);
00611 AddReadBlock("TAXA", block);
00612 }
00619 void AddReadTreesBlock(NxsTreesBlock * block)
00620 {
00621 treesBlockVec.push_back(block);
00622 AddReadBlock("TREES", block);
00623 }
00630 void AddReadUnalignedBlock(NxsUnalignedBlock * block)
00631 {
00632 unalignedBlockVec.push_back(block);
00633 AddReadBlock("UNKNOWN", block);
00634 }
00641 void AddReadUnknownBlock(NxsStoreTokensBlockReader * block)
00642 {
00643 storerBlockVec.push_back(block);
00644 AddReadBlock(block->GetID(), block);
00645 }
00646
00652 NxsConversionOutputRecord conversionOutputRecord;
00653
00654 protected:
00655 void PostExecuteHook();
00656 virtual void AddFactory(NxsBlockFactory *);
00657 int bitsForBlocksToRead;
00658 NxsCloneBlockFactory cloneFactory;
00659
00660 NxsAssumptionsBlock * assumptionsBlockTemplate;
00661 NxsCharactersBlock * charactersBlockTemplate;
00662 NxsDataBlock * dataBlockTemplate;
00663 NxsDistancesBlock * distancesBlockTemplate;
00664 NxsStoreTokensBlockReader * storerBlockTemplate;
00665 NxsTaxaBlock * taxaBlockTemplate;
00666 NxsTreesBlock * treesBlockTemplate;
00667 NxsUnalignedBlock * unalignedBlockTemplate;
00668
00669 std::vector<NxsAssumptionsBlock *> assumptionsBlockVec;
00670 std::vector<NxsCharactersBlock *> charactersBlockVec;
00671 std::vector<NxsDataBlock *> dataBlockVec;
00672 std::vector<NxsDistancesBlock *> distancesBlockVec;
00673 std::vector<NxsStoreTokensBlockReader *> storerBlockVec;
00674 std::vector<NxsTaxaBlock *> taxaBlockVec;
00675 std::vector<NxsTreesBlock *> treesBlockVec;
00676 std::vector<NxsUnalignedBlock *> unalignedBlockVec;
00677
00678 std::string errorMsg;
00679 private:
00680 PublicNexusReader(const PublicNexusReader &);
00681 PublicNexusReader & operator=(const PublicNexusReader &);
00682
00683 };
00684
00685
00686 #endif
00687
00688