00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef NCL_NXSCHARACTERSBLOCK_H
00021 #define NCL_NXSCHARACTERSBLOCK_H
00022
00023 #include <sstream>
00024 #include <cfloat>
00025 #include <climits>
00026
00027 #include "ncl/nxsdefs.h"
00028 #include "ncl/nxsdiscretedatum.h"
00029 #include "ncl/nxstaxablock.h"
00030
00031
00032 class NxsTaxaBlockAPI;
00033 class NxsAssumptionsBlockAPI;
00034 class NxsDiscreteDatatypeMapper;
00035
00036 void NxsWriteSetCommand(const char *cmd, const NxsUnsignedSetMap & usetmap, std::ostream &out, const char * nameOfDef = NULL);
00037 void NxsWritePartitionCommand(const char *cmd, const NxsPartitionsByName &partitions, std::ostream & out, const char * nameOfDef = NULL);
00038
00042 class NxsRealStepMatrix
00043 {
00044 public:
00045 typedef std::vector<double> DblVec;
00046 typedef std::vector<DblVec> DblMatrix;
00047
00048 NxsRealStepMatrix(const std::vector<std::string> &symbolsOrder, const DblMatrix & mat)
00049 :symbols(symbolsOrder),
00050 matrix(mat)
00051 {
00052 }
00053
00054 const std::vector<std::string> & GetSymbols() const
00055 {
00056 return symbols;
00057 }
00058
00059 const DblMatrix & GetMatrix() const
00060 {
00061 return matrix;
00062 }
00063 private:
00064 std::vector<std::string> symbols;
00065 DblMatrix matrix;
00066 };
00067
00071 class NxsIntStepMatrix
00072 {
00073 public:
00074 typedef std::vector<int> IntVec;
00075 typedef std::vector<IntVec> IntMatrix;
00076
00077 NxsIntStepMatrix(const std::vector<std::string> &symbolsOrder, const IntMatrix & mat)
00078 :symbols(symbolsOrder),
00079 matrix(mat)
00080 {
00081 }
00082 const std::vector<std::string> & GetSymbols() const
00083 {
00084 return symbols;
00085 }
00086 const IntMatrix & GetMatrix() const
00087 {
00088 return matrix;
00089 }
00090 private:
00091 std::vector<std::string> symbols;
00092 IntMatrix matrix;
00093 };
00094
00095
00096
00097 class NxsGeneticCodesManager
00098 {
00099 public:
00100 NxsGeneticCodesManager();
00101 void Reset() {}
00102 bool IsEmpty() const
00103 {
00104 return true;
00105 }
00106 void WriteGeneticCode(std::ostream & ) const
00107 {}
00108 bool IsValidCodeName(const std::string &cn) const;
00109 protected:
00110 std::set<std::string> standardCodeNames;
00111 std::set<std::string> userDefinedCodeNames;
00112
00113 };
00114
00115
00120 class NxsTransformationManager
00121 {
00122 public:
00123 typedef std::pair<int, std::set<unsigned> > IntWeightToIndexSet;
00124 typedef std::list<IntWeightToIndexSet> ListOfIntWeights;
00125
00126 typedef std::pair<double, std::set<unsigned> > DblWeightToIndexSet;
00127 typedef std::list<DblWeightToIndexSet> ListOfDblWeights;
00128
00129 typedef std::pair<std::string, std::set<unsigned> > TypeNameToIndexSet;
00130 typedef std::list<TypeNameToIndexSet> ListOfTypeNamesToSets;
00131
00132
00133 NxsTransformationManager()
00134 {
00135 Reset();
00136 }
00137
00139 static int GetWeightForIndex(const ListOfIntWeights & wtset,
00140 unsigned index);
00143 static double GetWeightForIndex(const ListOfDblWeights & wtset,
00144 unsigned index);
00148 static const NxsIntStepMatrix::IntMatrix GetOrderedType(unsigned nStates);
00150 static const NxsIntStepMatrix::IntMatrix GetUnorderedType(unsigned nStates);
00151
00153 const std::set<std::string> & GetTypeNames() const;
00155 const std::set<std::string> & GetUserTypeNames() const;
00157 const std::set<std::string> & GetStandardTypeNames() const;
00159 const std::string GetDefaultTypeName() const;
00161 std::set<std::string> GetWeightSetNames() const;
00166 bool IsDoubleWeightSet(const std::string &) const;
00167
00169 std::vector<double> GetDefaultDoubleWeights() const
00170 {
00171 return GetDoubleWeights(def_wtset);
00172 }
00173
00175 std::vector<int> GetDefaultIntWeights() const
00176 {
00177 return GetIntWeights(def_wtset);
00178 }
00179
00185 std::vector<double> GetDoubleWeights(const std::string &wtsetname) const;
00191 std::vector<int> GetIntWeights(const std::string &) const;
00192
00194 std::set<std::string> GetTypeSetNames() const;
00195
00197 const std::string & GetDefaultWeightSetName() const;
00199 const std::string & GetDefaultTypeSetName() const;
00200
00201 bool IsEmpty() const;
00202
00203 bool IsValidTypeName(const std::string & ) const;
00204 bool IsStandardType(const std::string & ) const;
00205 bool IsIntType(const std::string & ) const;
00206
00207 const NxsIntStepMatrix & GetIntType(const std::string & name) const;
00208
00209 const NxsRealStepMatrix & GetRealType(const std::string & name) const;
00210
00211
00212 void SetDefaultTypeName(const std::string &);
00213 bool AddIntType(const std::string &, const NxsIntStepMatrix &);
00214 bool AddRealType(const std::string &, const NxsRealStepMatrix &);
00215
00216 bool AddIntWeightSet(const std::string &, const ListOfIntWeights &, bool isDefault);
00217 bool AddRealWeightSet(const std::string &, const ListOfDblWeights &, bool isDefault);
00218
00219 bool AddTypeSet(const std::string &, const NxsPartition &, bool isDefault);
00220
00221 void Reset();
00222
00223 void WriteUserType(std::ostream &out) const;
00224 void WriteWtSet(std::ostream &out) const;
00225 void WriteTypeSet(std::ostream &out) const
00226 {
00227 NxsWritePartitionCommand("TypeSet", typeSets, out, def_typeset.c_str());
00228 }
00229
00230 private:
00231 std::set<std::string> standardTypeNames;
00232 std::set<std::string> userTypeNames;
00233 std::set<std::string> allTypeNames;
00234 std::map<std::string, NxsRealStepMatrix> dblUserTypes;
00235 std::map<std::string, NxsIntStepMatrix> intUserTypes;
00236 std::set<std::string> allWtSetNames;
00237 std::map<std::string, ListOfDblWeights> dblWtSets;
00238 std::map<std::string, ListOfIntWeights> intWtSets;
00239 NxsPartitionsByName typeSets;
00240 std::string def_wtset;
00241 std::string def_typeset;
00242 std::string def_type;
00243 };
00244
00245 inline const std::string NxsTransformationManager::GetDefaultTypeName() const
00246 {
00247 return def_type;
00248 }
00249 inline const std::string & NxsTransformationManager::GetDefaultWeightSetName() const
00250 {
00251 return def_wtset;
00252 }
00253 inline const std::string & NxsTransformationManager::GetDefaultTypeSetName() const
00254 {
00255 return def_typeset;
00256 }
00257 inline const std::set<std::string> & NxsTransformationManager::GetTypeNames() const
00258 {
00259 return allTypeNames;
00260 }
00261 inline const std::set<std::string> & NxsTransformationManager::GetUserTypeNames() const
00262 {
00263 return userTypeNames;
00264 }
00265 inline const std::set<std::string> & NxsTransformationManager::GetStandardTypeNames() const
00266 {
00267 return standardTypeNames;
00268 }
00269 inline bool NxsTransformationManager::IsDoubleWeightSet(const std::string &s) const
00270 {
00271 const std::vector<double> d = GetDoubleWeights(s);
00272 return !(d.empty());
00273 }
00274
00278 class NxsCharactersBlockAPI
00279 : public NxsBlock, public NxsLabelToIndicesMapper
00280 {
00281 public:
00282 virtual unsigned ApplyExset(NxsUnsignedSet &exset) = 0;
00283 virtual bool AddNewExSet(const std::string &label, const NxsUnsignedSet & inds) = 0;
00284 virtual bool IsRespectCase() const = 0;
00285 virtual unsigned GetNCharTotal() const = 0;
00286 virtual NxsTransformationManager & GetNxsTransformationManagerRef() = 0;
00287 virtual const NxsTransformationManager & GetNxsTransformationManagerRef() const = 0;
00288 virtual std::vector<const NxsDiscreteDatatypeMapper *> GetAllDatatypeMappers() const = 0;
00289 virtual bool AddNewCodonPosPartition(const std::string &label, const NxsPartition & inds, bool isDefault) = 0;
00290 virtual std::string GetDefaultCodonPosPartitionName() const = 0;
00291 virtual NxsPartition GetCodonPosPartition(const std::string &label) const = 0;
00292 enum GapModeEnum
00293 {
00294 GAP_MODE_MISSING = 0,
00295 GAP_MODE_NEWSTATE = 1
00296 };
00297 virtual GapModeEnum GetGapModeSetting() const = 0;
00298 virtual void SetGapModeSetting(GapModeEnum m) = 0;
00299
00300 };
00301
00302 #if defined(NCL_SMALL_STATE_CELL)
00303 typedef signed char NxsDiscreteStateCell;
00304 #else
00305 typedef int NxsDiscreteStateCell;
00306 #endif
00307 typedef std::vector<NxsDiscreteStateCell> NxsDiscreteStateRow;
00308 typedef std::vector<NxsDiscreteStateRow> NxsDiscreteStateMatrix;
00309
00310
00320 enum {
00321 NXS_INVALID_STATE_CODE = -3,
00322 NXS_GAP_STATE_CODE = -2,
00323 NXS_MISSING_CODE = -1
00324 };
00325
00326 class NxsCodonTriplet {
00327 public:
00328 unsigned char firstPos;
00329 unsigned char secondPos;
00330 unsigned char thirdPos;
00331
00332 NxsCodonTriplet(const char *triplet);
00334
00335
00336
00337
00338 typedef std::pair<int, int> MutDescription;
00339 MutDescription getSingleMut(const NxsCodonTriplet & other) const;
00340
00341 };
00342
00343 enum NxsGeneticCodesEnum {
00344 NXS_GCODE_NO_CODE = -1,
00345 NXS_GCODE_STANDARD = 0,
00346 NXS_GCODE_VERT_MITO = 1,
00347 NXS_GCODE_YEAST_MITO = 2,
00348 NXS_GCODE_MOLD_MITO = 3,
00349 NXS_GCODE_INVERT_MITO = 4,
00350 NXS_GCODE_CILIATE = 5,
00351 NXS_GCODE_ECHINO_MITO = 8,
00352 NXS_GCODE_EUPLOTID = 9,
00353 NXS_GCODE_PLANT_PLASTID = 10,
00354 NXS_GCODE_ALT_YEAST = 11,
00355 NXS_GCODE_ASCIDIAN_MITO = 12,
00356 NXS_GCODE_ALT_FLATWORM_MITO = 13,
00357 NXS_GCODE_BLEPHARISMA_MACRO = 14,
00358 NXS_GCODE_CHLOROPHYCEAN_MITO = 15,
00359 NXS_GCODE_TREMATODE_MITO = 20,
00360 NXS_GCODE_SCENEDESMUS_MITO = 21,
00361 NXS_GCODE_THRAUSTOCHYTRIUM_MITO = 22,
00362 NXS_GCODE_CODE_ENUM_SIZE = 23
00363 };
00364 NxsGeneticCodesEnum geneticCodeNameToEnum(std::string);
00365 std::string geneticCodeEnumToName(NxsGeneticCodesEnum);
00366 std::string getGeneticCodeAAOrder(NxsGeneticCodesEnum codeIndex);
00367 std::vector<std::string> getGeneticCodeNames();
00368
00369
00370
00371
00372
00373
00374
00375
00376 class CodonRecodingStruct
00377 {
00378 public:
00379 std::vector<int> compressedCodonIndToAllCodonsInd;
00380 std::vector<int> aaInd;
00381 std::vector<std::string> codonStrings;
00382 };
00383
00384 class NxsDiscreteDatatypeMapper;
00470 class NxsCharactersBlock
00471 : public NxsCharactersBlockAPI, public NxsTaxaBlockSurrogate
00472 {
00473 friend class NxsAssumptionsBlock;
00474
00475
00476 public:
00477 typedef std::map<std::string, std::vector<double> > ContinuousCharCell;
00478 typedef std::vector<ContinuousCharCell> ContinuousCharRow;
00479 typedef std::vector<ContinuousCharRow> ContinuousCharMatrix;
00480 typedef std::vector<std::string> VecString;
00481 typedef std::map<unsigned, std::string> IndexToLabelMap;
00482 typedef std::map<std::string, unsigned> LabelToIndexMap;
00483 typedef std::pair<NxsDiscreteDatatypeMapper, NxsUnsignedSet> DatatypeMapperAndIndexSet;
00484 typedef std::vector<DatatypeMapperAndIndexSet> VecDatatypeMapperAndIndexSet;
00485
00486
00487 enum DataTypesEnum
00488 {
00489 standard = 1,
00490 dna,
00491 rna,
00492 nucleotide,
00493 protein,
00494 codon,
00495 continuous,
00496 mixed
00497 };
00498 enum StatesFormatEnum
00499 {
00500 STATES_PRESENT = 1,
00501 STATE_COUNT,
00502 STATE_FREQUENCY,
00503 INDIVIDUALS
00504 };
00505
00508 NxsCharactersBlock(NxsTaxaBlockAPI *tb,
00509 NxsAssumptionsBlockAPI *ab);
00510 virtual ~NxsCharactersBlock() {}
00511
00512
00513
00518 void SetSupportMixedDatatype(bool v)
00519 {
00520 supportMixedDatatype = v;
00521 }
00527 bool AugmentedSymbolsToMixed();
00539 void SetConvertAugmentedToMixed(bool v)
00540 {
00541 convertAugmentedToMixed = v;
00542 }
00547 void SetAllowAugmentingOfSequenceSymbols(bool v)
00548 {
00549 allowAugmentingOfSequenceSymbols = v;
00550 }
00551
00556 bool GetAllowAugmentingOfSequenceSymbols() const
00557 {
00558 return allowAugmentingOfSequenceSymbols;
00559 }
00564 std::map<DataTypesEnum, NxsUnsignedSet> GetDatatypeMapForMixedType() const
00565 {
00566 return mixedTypeMapping;
00567 }
00568
00569
00595 DataTypesEnum GetDataType() const;
00598 bool TaxonIndHasData(const unsigned ind) const;
00600 unsigned GetNCharTotal() const ;
00608 DataTypesEnum GetOriginalDataType() const;
00613 unsigned GetNumActiveChar() NCL_COULD_BE_CONST ;
00615 const NxsUnsignedSet & GetExcludedIndexSet() const;
00619 bool IsActiveChar(unsigned j) const;
00624 unsigned ApplyExset(NxsUnsignedSet &exset);
00629 unsigned ApplyIncludeset(NxsUnsignedSet &inset);
00634 void ExcludeCharacter(unsigned i);
00639 void IncludeCharacter(unsigned i);
00640
00642 NxsString GetCharLabel(unsigned i) const;
00644 bool HasCharLabels() const;
00651 GapModeEnum GetGapModeSetting() const
00652 {
00653 return this->gapMode;
00654 }
00655
00656
00657 const char *GetSymbols() NCL_COULD_BE_CONST ;
00658 unsigned GetNumStates(unsigned i, unsigned j) NCL_COULD_BE_CONST ;
00659 char GetState(unsigned i, unsigned j, unsigned k = 0) const;
00661 const NxsTransformationManager & GetNxsTransformationManagerRef() const
00662 {
00663 return transfMgr;
00664 }
00666 bool IsGapState(unsigned taxInd, unsigned charInd) NCL_COULD_BE_CONST ;
00668 bool IsMissingState(unsigned i, unsigned j) NCL_COULD_BE_CONST ;
00670 bool IsPolymorphic(unsigned i, unsigned j) NCL_COULD_BE_CONST ;
00671
00672
00673
00682 const NxsDiscreteStateRow & GetDiscreteMatrixRow(unsigned taxonIndex) const;
00683
00684
00685
00686
00687
00688
00689
00690
00691 const NxsDiscreteDatatypeMapper * GetDatatypeMapperForChar(unsigned charIndex) const;
00698 std::vector<const NxsDiscreteDatatypeMapper *> GetAllDatatypeMappers() const;
00699
00700
00710 std::vector<double> GetContinuousValues(unsigned taxIndex, unsigned charIndex, const std::string key) NCL_COULD_BE_CONST;
00714 std::vector<std::string> GetItems() const;
00720 StatesFormatEnum GetStatesFormat() const;
00721
00722
00723
00724
00725 void FindConstantCharacters(NxsUnsignedSet &c) const;
00726 void FindGappedCharacters(NxsUnsignedSet &c) const;
00727 virtual const std::string & GetBlockName() const;
00734 void SetGapModeSetting(GapModeEnum m)
00735 {
00736 this->gapMode = m;
00737 }
00738 static const char * GetNameOfDatatype(DataTypesEnum);
00739 NxsDiscreteStateCell GetInternalRepresentation(unsigned i, unsigned j, unsigned k = 0) NCL_COULD_BE_CONST;
00746 virtual unsigned GetMaxObsNumStates(bool countMissingStates=true, bool onlyActiveChars=false) NCL_COULD_BE_CONST ;
00756 virtual unsigned GetNumObsStates(unsigned columnIndex, bool countMissingStates=true) NCL_COULD_BE_CONST {
00757 return (unsigned)GetObsStates(columnIndex, countMissingStates).size();
00758 }
00769 std::set<NxsDiscreteStateCell> GetObsStates(unsigned columnIndex, bool countMissingStates=true) const {
00770 if (countMissingStates)
00771 return GetMaximalStateSetOfColumn(columnIndex);
00772 return GetNamedStateSetOfColumn(columnIndex);
00773 }
00774
00775 double GetSimpleContinuousValue(unsigned i, unsigned j) NCL_COULD_BE_CONST ;
00776
00782 NxsString GetStateLabel(unsigned charIndex, unsigned charStateIndex) const
00783 {
00784 return GetStateLabelImpl(charIndex, charStateIndex);
00785 }
00792 bool WasRestrictionDataype() const;
00793
00794
00795
00796
00797 static std::map<char, NxsString> GetDefaultEquates(DataTypesEnum);
00798 static std::string GetDefaultSymbolsForType(DataTypesEnum dt);
00799 const NxsDiscreteDatatypeMapper & GetDatatypeMapperForCharRef(unsigned charIndex) const;
00801 const char * GetDatatypeName() const
00802 {
00803 return NxsCharactersBlock::GetNameOfDatatype(datatype);
00804 }
00805 char GetGapSymbol() const;
00806 void SetGapSymbol(char);
00807 char GetMatchcharSymbol() NCL_COULD_BE_CONST ;
00808 char GetMissingSymbol() const;
00809 unsigned GetMaxIndex() const;
00810 const NxsDiscreteStateMatrix & GetRawDiscreteMatrixRef() const
00811 {
00812 return discreteMatrix;
00813 }
00814
00815
00816
00817 virtual unsigned CharLabelToNumber(NxsString s) NCL_COULD_BE_CONST ;
00818 virtual unsigned CharLabelToNumber(const std::string & s) const;
00819 unsigned GetIndexSet(const std::string &label, NxsUnsignedSet * toFill) const
00820 {
00821 return NxsLabelToIndicesMapper::GetIndicesFromSets(label, toFill, charSets);
00822 }
00823 unsigned GetIndicesForLabel(const std::string &label, NxsUnsignedSet *inds) const;
00824 unsigned GetNCharTotal() ;
00825 unsigned GetNumIncludedChars() const ;
00826 unsigned GetNumEliminated() NCL_COULD_BE_CONST ;
00827 unsigned GetNChar() const;
00828 unsigned GetNumChar() const;
00829
00830 virtual unsigned GetObsNumStates(unsigned columnIndex, bool countMissingStates=true) NCL_COULD_BE_CONST {
00831 return (unsigned) GetObsStates(columnIndex, countMissingStates).size();
00832 }
00836 NxsString GetStateLabel(unsigned charIndex, unsigned charStateIndex)
00837
00838 {
00839 return GetStateLabelImpl(charIndex, charStateIndex);
00840 }
00841
00842
00843 NxsString GetTaxonLabel(unsigned i) const;
00844
00845
00846 NxsTransformationManager & GetNxsTransformationManagerRef()
00847 {
00848 return transfMgr;
00849 }
00850 bool IsActiveChar(unsigned j) ;
00851 bool IsEliminated(unsigned charIndex) NCL_COULD_BE_CONST ;
00852 bool IsExcluded(unsigned j) const;
00853 bool IsExcluded(unsigned j) ;
00854 bool IsMixedType() const;
00855
00856 virtual unsigned TaxonLabelToNumber(NxsString s) const;
00857 virtual bool AddNewCodonPosPartition(const std::string &label, const NxsPartition & inds, bool isDefault);
00858 bool AddNewIndexSet(const std::string &label, const NxsUnsignedSet & inds);
00859 bool AddNewExSet(const std::string &label, const NxsUnsignedSet & inds);
00860 bool AddNewPartition(const std::string &label, const NxsPartition & inds);
00861 void Consume(NxsCharactersBlock &other);
00869 std::set<NxsDiscreteStateCell> GetNamedStateSetOfColumn(const unsigned colIndex) const;
00877 std::set<NxsDiscreteStateCell> GetMaximalStateSetOfColumn(const unsigned colIndex) const;
00878
00879
00880
00881
00882
00883 virtual void Report(std::ostream &out) NCL_COULD_BE_CONST ;
00884 void ShowStateLabels(std::ostream &out, unsigned i, unsigned c, unsigned first_taxon) const;
00885 void WriteAsNexus(std::ostream &out) const;
00886 virtual void DebugShowMatrix(std::ostream &out, bool use_matchchar, const char *marginText = NULL) const;
00887 virtual void WriteLinkCommand(std::ostream &out) const;
00888 void WriteStatesForTaxonAsNexus(std::ostream &out, unsigned taxNum, unsigned begChar, unsigned endChar) const;
00889 void WriteCharLabelsCommand(std::ostream &out) const;
00890 void WriteCharStateLabelsCommand(std::ostream &out) const;
00891 void WriteEliminateCommand(std::ostream &out) const;
00892 void WriteFormatCommand(std::ostream &out) const;
00893 void WriteMatrixCommand(std::ostream &out) const;
00894
00895
00896
00897
00898 bool IsInterleave() NCL_COULD_BE_CONST ;
00899 bool IsLabels() NCL_COULD_BE_CONST ;
00900 bool IsRespectCase() const;
00901 bool IsTokens() NCL_COULD_BE_CONST ;
00902 bool IsTranspose() NCL_COULD_BE_CONST ;
00903
00904
00905
00906
00907
00908 unsigned GetNTaxWithData() const ;
00909 virtual VecBlockPtr GetImpliedBlocks();
00910 unsigned GetNumEquates() NCL_COULD_BE_CONST ;
00911 unsigned GetNumUserEquates() const;
00912 unsigned GetNumMatrixCols() NCL_COULD_BE_CONST ;
00913
00914 unsigned GetNumMatrixRows() NCL_COULD_BE_CONST ;
00915 virtual void Reset();
00916 void SetNexus(NxsReader *nxsptr);
00917 const ContinuousCharRow & GetContinuousMatrixRow(unsigned taxNum) const;
00918
00919
00920
00921 virtual void HandleLinkCommand(NxsToken & token);
00922
00923
00924
00925
00926
00927
00928 NxsCharactersBlock & operator=(const NxsCharactersBlock &other)
00929 {
00930 Reset();
00931 CopyBaseBlockContents(static_cast<const NxsBlock &>(other));
00932 CopyTaxaBlockSurrogateContents(other);
00933 CopyCharactersContents(other);
00934 return *this;
00935 }
00936
00937 virtual void CopyCharactersContents(const NxsCharactersBlock &other);
00938 virtual NxsCharactersBlock * Clone() const
00939 {
00940 NxsCharactersBlock * a = new NxsCharactersBlock(taxa, assumptionsBlock);
00941 *a = *this;
00942 return a;
00943 }
00944
00945
00946 void SetWriteInterleaveLen(int interleaveLen)
00947 {
00948 writeInterleaveLen = interleaveLen;
00949 }
00950
00951 std::string GetMatrixRowAsStr(const unsigned rowIndex) const;
00952 NxsDiscreteStateCell GetStateIndex(unsigned i, unsigned j, unsigned k) const;
00953
00954
00956 static void CodonPosPartitionToPosList(const NxsPartition &codonPos, std::list<int> * charIndices);
00957
00958
00959
00960
00961
00962
00963
00964
00965 static NxsCharactersBlock * NewCodonsCharactersBlock(
00966 const NxsCharactersBlock * charBlock,
00967 bool mapPartialAmbigToUnknown,
00968 bool gapsToUnknown,
00969 bool honorCharActive,
00970 const std::list<int> * charIndices = NULL,
00971 NxsCharactersBlock ** spareNucs = NULL
00972 );
00973 static NxsCharactersBlock * NewProteinCharactersBlock(
00974 const NxsCharactersBlock * codonBlock,
00975 bool mapPartialAmbigToUnknown,
00976 bool gapToUnknown,
00977 NxsGeneticCodesEnum codeIndex);
00978 static NxsCharactersBlock * NewProteinCharactersBlock(
00979 const NxsCharactersBlock * codonBlock,
00980 bool mapPartialAmbigToUnknown,
00981 bool gapToUnknown,
00982 const std::vector<NxsDiscreteStateCell> & aaIndices);
00984 virtual std::string GetDefaultCodonPosPartitionName() const {
00985 return defCodonPosPartitionName;
00986 }
00987 virtual NxsPartition GetCodonPosPartition(const std::string &label) const {
00988 NxsPartitionsByName::const_iterator pIt = codonPosPartitions.find(label);
00989 if (pIt == codonPosPartitions.end())
00990 return NxsPartition();
00991 return pIt->second;
00992 }
00993
00994 unsigned NumAmbigInTaxon(const unsigned taxInd, const NxsUnsignedSet * charIndices, const bool countOnlyCompletelyMissing, const bool treatGapsAsMissing) const;
00995 bool FirstTaxonStatesAreSubsetOfSecond(const unsigned firstTaxonInd, const unsigned secondTaxonInd, const NxsUnsignedSet * charIndices, const bool treatAmbigAsMissing, const bool treatGapAsMissing) const;
00996
00997
00998 std::pair<unsigned, unsigned> GetPairwiseDist(const unsigned firstTaxonInd, const unsigned secondTaxonInd, const NxsUnsignedSet * charIndices, const bool treatAmbigAsMissing, const bool treatGapAsMissing) const;
00999 CodonRecodingStruct RemoveStopCodons(NxsGeneticCodesEnum);
01000 bool SwapEquivalentTaxaBlock(NxsTaxaBlockAPI * tb)
01001 {
01002 return SurrogateSwapEquivalentTaxaBlock(tb);
01003 }
01004
01005
01006 protected:
01007
01008 void SetNChar(unsigned nc)
01009 {
01010 this->nChar = nc;
01011 }
01012
01013 void SetNTax(unsigned nt)
01014 {
01015 this->nTaxWithData = nt;
01016 }
01017
01018 NxsString GetStateLabelImpl(unsigned i, unsigned j) const;
01019 void WriteStatesForMatrixRow(std::ostream &out, unsigned taxon, unsigned first_taxon, unsigned begChar, unsigned endChar) const;
01020
01021 NxsDiscreteDatatypeMapper * GetMutableDatatypeMapperForChar(unsigned charIndex);
01022 bool IsInSymbols(char ch) NCL_COULD_BE_CONST ;
01023 void ShowStates(std::ostream &out, unsigned i, unsigned j) NCL_COULD_BE_CONST ;
01024
01025 void HandleCharlabels(NxsToken &token);
01026 void HandleCharstatelabels(NxsToken &token);
01027 void HandleDimensions(NxsToken &token, NxsString newtaxaLabel, NxsString ntaxLabel, NxsString ncharLabel);
01028 void HandleEliminate(NxsToken &token);
01029 virtual void HandleFormat(NxsToken &token);
01030 virtual void HandleMatrix(NxsToken &token);
01031 bool HandleNextContinuousState(NxsToken &token, unsigned taxNum, unsigned charNum, ContinuousCharRow & row, const NxsString & nameStr);
01032 bool HandleNextDiscreteState(NxsToken &token, unsigned taxNum, unsigned charNum, NxsDiscreteStateRow & row, NxsDiscreteDatatypeMapper &, const NxsDiscreteStateRow * firstTaxonRow, const NxsString & nameStr);
01033 bool HandleNextTokenState(NxsToken &token, unsigned taxNum, unsigned charNum, NxsDiscreteStateRow & row, NxsDiscreteDatatypeMapper &, const NxsDiscreteStateRow * firstTaxonRow, const NxsString & nameStr);
01034 void HandleStatelabels(NxsToken &token);
01035 virtual void HandleStdMatrix(NxsToken &token);
01036 virtual NxsDiscreteStateCell HandleTokenState(NxsToken &token, unsigned taxNum, unsigned charNum, NxsDiscreteDatatypeMapper &mapper, const NxsDiscreteStateRow * firstTaxonRow, const NxsString & nameStr);
01037 virtual void HandleTransposedMatrix(NxsToken &token);
01038 virtual void Read(NxsToken &token);
01039 void ResetSymbols();
01040
01041 void WriteStates(NxsDiscreteDatum &d, char *s, unsigned slen) NCL_COULD_BE_CONST ;
01042
01043
01044 NxsAssumptionsBlockAPI *assumptionsBlock;
01045
01046 unsigned nChar;
01047 unsigned nTaxWithData;
01048
01049 char matchchar;
01050 bool respectingCase;
01051 bool transposing;
01052 bool interleaving;
01053 mutable bool tokens;
01054 bool labels;
01055
01056 char missing;
01057 char gap;
01058 GapModeEnum gapMode;
01059 std::string symbols;
01060 std::map<char, NxsString> userEquates;
01061 std::map<char, NxsString> defaultEquates;
01062 VecDatatypeMapperAndIndexSet datatypeMapperVec;
01063 NxsDiscreteStateMatrix discreteMatrix;
01064 ContinuousCharMatrix continuousMatrix;
01065
01066 NxsUnsignedSet eliminated;
01067 NxsUnsignedSet excluded;
01068
01069 LabelToIndexMap ucCharLabelToIndex;
01070 IndexToLabelMap indToCharLabel;
01071 NxsStringVectorMap charStates;
01072 NxsStringVector globalStateLabels;
01073 VecString items;
01074
01075 NxsUnsignedSetMap charSets;
01076 NxsUnsignedSetMap exSets;
01077 NxsPartitionsByName charPartitions;
01078 NxsTransformationManager transfMgr;
01079 bool datatypeReadFromFormat;
01080 NxsPartitionsByName codonPosPartitions;
01081 std::string defCodonPosPartitionName;
01082 std::map<DataTypesEnum, NxsUnsignedSet> mixedTypeMapping;
01083 private:
01084 DataTypesEnum datatype;
01085 DataTypesEnum originalDatatype;
01086 StatesFormatEnum statesFormat;
01087 bool restrictionDataype;
01088 bool supportMixedDatatype;
01089 bool convertAugmentedToMixed;
01090 bool allowAugmentingOfSequenceSymbols;
01091 int writeInterleaveLen;
01092
01093 void CreateDatatypeMapperObjects(const NxsPartition & , const std::vector<DataTypesEnum> &);
01094 friend class PublicNexusReader;
01095 friend class MultiFormatReader;
01096 };
01097
01098 typedef NxsCharactersBlock CharactersBlock;
01099
01100
01101 class NxsCharactersBlockFactory
01102 :public NxsBlockFactory
01103 {
01104 public:
01105 virtual NxsCharactersBlock * GetBlockReaderForID(const std::string & id, NxsReader *reader, NxsToken *token);
01106 };
01107
01108 class NxsDiscreteStateSetInfo
01109 {
01110 public:
01111 NxsDiscreteStateSetInfo(const std::set<NxsDiscreteStateCell> & stateSet, bool polymorphic=false, char symbol='\0')
01112 :states(stateSet),
01113 nexusSymbol(symbol),
01114 isPolymorphic(polymorphic)
01115 {}
01116
01117
01118 std::set<NxsDiscreteStateCell> states;
01119 char nexusSymbol;
01120 bool isPolymorphic;
01121 };
01122
01126 class NxsDiscreteDatatypeMapper
01127 {
01128 public:
01129
01130
01131
01132
01133 static void GenerateNxsExceptionMatrixReading(const char *, unsigned taxInd, unsigned charInd, NxsToken &, const NxsString &nameStr);
01134 static void GenerateNxsExceptionMatrixReading(const std::string &s, unsigned taxInd, unsigned charInd, NxsToken & token, const NxsString &nameStr)
01135 {
01136 GenerateNxsExceptionMatrixReading(s.c_str(), taxInd, charInd, token, nameStr);
01137 }
01138
01139
01140 NxsDiscreteDatatypeMapper();
01141 NxsDiscreteDatatypeMapper(NxsCharactersBlock::DataTypesEnum datatypeE, bool hasGaps);
01142 NxsDiscreteDatatypeMapper(NxsCharactersBlock::DataTypesEnum datatype, const std::string & symbols,
01143 char missingChar, char gapChar, char matchChar,
01144 bool respectCase, const std::map<char, NxsString> & extraEquates);
01145
01147 unsigned GetNumStateCodes() const
01148 {
01149 return (unsigned)stateSetsVec.size();
01150 }
01151
01152 NxsCharactersBlock::DataTypesEnum GetDatatype() const
01153 {
01154 return datatype;
01155 }
01156 unsigned GetNumStates() const;
01157 unsigned GetNumStatesIncludingGap() const;
01158 std::string GetSymbols() const
01159 {
01160 return symbols;
01161 }
01162 std::string GetSymbolsWithGapChar() const
01163 {
01164 if (gapChar == '\0')
01165 return GetSymbols();
01166 std::string s;
01167 s = symbols;
01168 s.append(1, gapChar);
01169 return s;
01170 }
01171
01172 const std::set<NxsDiscreteStateCell> & GetStateSetForCode(NxsDiscreteStateCell stateCode) const;
01173 bool IsSemanticallyEquivalent(const NxsDiscreteDatatypeMapper &other) const;
01174 bool IsPolymorphic(NxsDiscreteStateCell stateCode) const;
01175 NxsDiscreteStateCell PositionInSymbols(const char currChar) const;
01180 NxsDiscreteStateCell GetStateCodeStored(char currChar) const
01181 {
01182 return cLookup[static_cast<int>(currChar)];
01183 }
01192 NxsDiscreteStateCell GetHighestStateCode() const
01193 {
01194 return ((NxsDiscreteStateCell) stateSetsVec.size()) + sclOffset - 1;
01195 }
01196
01197
01198
01199
01200
01201 NxsDiscreteDatatypeMapper(const NxsDiscreteDatatypeMapper& other)
01202 :datatype(other.datatype)
01203 {
01204 *this = other;
01205 }
01206 NxsDiscreteDatatypeMapper & operator=(const NxsDiscreteDatatypeMapper&);
01207
01208 char GetGapSymbol() const
01209 {
01210 return gapChar;
01211 }
01212
01213 void SetGapSymbol(char c)
01214 {
01215 gapChar = c;
01216 }
01217 char GetMissingSymbol() const
01218 {
01219 return missing;
01220 }
01221 std::map<char, NxsString> GetExtraEquates() const
01222 {
01223 return extraEquates;
01224 }
01225 unsigned GetNumStatesInStateCode(NxsDiscreteStateCell stateCode) const;
01226 NxsDiscreteStateCell GetOneStateForCode(NxsDiscreteStateCell stateCode, unsigned stateIndex) const;
01227 NxsDiscreteStateRow GetStateVectorForCode(NxsDiscreteStateCell stateCode) const;
01228 std::vector<std::vector<int> > GetPythonicStateVectors() const;
01229 NxsDiscreteStateCell PositionInSymbolsOrGaps(const char currChar) const
01230 {
01231 if (currChar == gapChar)
01232 return NXS_GAP_STATE_CODE;
01233 return PositionInSymbols(currChar);
01234 }
01235 std::string StateCodeToNexusString(NxsDiscreteStateCell, bool demandSymbols = true) const;
01236 NxsDiscreteStateCell StateCodeForNexusChar(const char currChar, NxsToken & token,
01237 unsigned taxInd, unsigned charInd,
01238 const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr) const;
01239 void WriteStartOfFormatCommand(std::ostream & out) const;
01240 void WriteStateCodeRowAsNexus(std::ostream & out, const std::vector<NxsDiscreteStateCell> &row) const;
01241 void WriteStateCodeRowAsNexus(std::ostream & out, std::vector<NxsDiscreteStateCell>::const_iterator & begIt, const std::vector<NxsDiscreteStateCell>::const_iterator & endIt) const;
01242 void WriteStateCodeAsNexusString(std::ostream & out, NxsDiscreteStateCell scode, bool demandSymbols = true) const;
01243 bool WasRestrictionDataype() const;
01244 void SetWasRestrictionDataype(bool v) {restrictionDataype = v;}
01245 NxsDiscreteStateCell EncodeNexusStateString(const std::string &stateAsNexus, NxsToken & token,
01246 const unsigned taxInd, const unsigned charInd,
01247 const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr);
01248 NxsDiscreteStateCell StateCodeForStateSet(const std::set<NxsDiscreteStateCell> &, const bool isPolymorphic,
01249 const bool addToLookup, const char symbol);
01250
01251 void DebugPrint(std::ostream &) const;
01252
01253 bool GetUserDefinedEquatesBeforeConversion() const
01254 {
01255 return userDefinedEquatesBeforeConversion;
01256 }
01257
01258 bool IsRespectCase() const
01259 {
01260 return respectCase;
01261 }
01262
01263 const std::set<NxsDiscreteStateCell> & GetStateIntersection(NxsDiscreteStateCell stateCode, NxsDiscreteStateCell otherStateCode) const
01264 {
01265 if (stateIntersectionMatrix.empty())
01266 BuildStateIntersectionMatrix();
01267 const NxsDiscreteStateCell sc = stateCode - NXS_GAP_STATE_CODE;
01268 const NxsDiscreteStateCell osc = otherStateCode - NXS_GAP_STATE_CODE;
01269 return stateIntersectionMatrix.at(sc).at(osc);
01270 }
01271
01272 bool FirstIsSubset(NxsDiscreteStateCell stateCode, NxsDiscreteStateCell otherStateCode, bool treatGapAsMissing) const
01273 {
01274 if (isStateSubsetMatrix.empty())
01275 BuildStateSubsetMatrix();
01276 const NxsDiscreteStateCell sc = stateCode - NXS_GAP_STATE_CODE;
01277 const NxsDiscreteStateCell osc = otherStateCode - NXS_GAP_STATE_CODE;
01278 if (treatGapAsMissing)
01279 return isStateSubsetMatrixGapsMissing.at(sc).at(osc);
01280 return isStateSubsetMatrix.at(sc).at(osc);
01281 }
01282
01283 NxsGeneticCodesEnum geneticCode;
01284
01286 void DebugWriteMapperFields(std::ostream & out) const;
01287 private:
01288 NxsDiscreteStateCell AddStateSet(const std::set<NxsDiscreteStateCell> & states, char nexusSymbol, bool symRespectCase, bool isPolymorphic);
01289 NxsDiscreteStateCell StateCodeForNexusMultiStateSet(const char nexusSymbol, const std::string & stateAsNexus, NxsToken & token,
01290 unsigned taxInd, unsigned charInd,
01291 const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr);
01292 NxsDiscreteStateCell StateCodeForNexusPossibleMultiStateSet(const char nexusSymbol, const std::string & stateAsNexus, NxsToken & token,
01293 unsigned taxInd, unsigned charInd,
01294 const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr);
01295
01296 void RefreshMappings(NxsToken *token);
01297 void ValidateStateIndex(NxsDiscreteStateCell state) const;
01298 void ValidateStateCode(NxsDiscreteStateCell state) const;
01299 void BuildStateSubsetMatrix() const;
01300 void BuildStateIntersectionMatrix() const;
01301 void DeleteStateIndices(const std::set<NxsDiscreteStateCell> & deletedInds);
01302
01303 NxsDiscreteStateCell * cLookup;
01304 NxsDiscreteStateSetInfo * stateCodeLookupPtr;
01305 std::string symbols;
01306 std::string lcsymbols;
01307 unsigned nStates;
01308 char matchChar;
01309 char gapChar;
01310 char missing;
01311 bool respectCase;
01312 std::map<char, NxsString> extraEquates;
01313 NxsCharactersBlock::DataTypesEnum datatype;
01314 std::vector<NxsDiscreteStateSetInfo> stateSetsVec;
01315 std::vector<NxsDiscreteStateCell> charToStateCodeLookup;
01316 int sclOffset;
01317 bool restrictionDataype;
01318 bool userDefinedEquatesBeforeConversion;
01319
01320 typedef std::vector< std::set<NxsDiscreteStateCell> > StateIntersectionRow;
01321 typedef std::vector< StateIntersectionRow > StateIntersectionMatrix;
01322 typedef std::vector< bool > IsStateSubsetRow;
01323 typedef std::vector< IsStateSubsetRow > IsStateSubsetMatrix;
01324 mutable StateIntersectionMatrix stateIntersectionMatrix;
01325 mutable IsStateSubsetMatrix isStateSubsetMatrix;
01326 mutable IsStateSubsetMatrix isStateSubsetMatrixGapsMissing;
01327
01328 friend class NxsCharactersBlock;
01329 };
01330
01331 inline unsigned NxsDiscreteDatatypeMapper::GetNumStatesIncludingGap() const
01332 {
01333 return nStates + (gapChar == '\0' ? 0 : 1);
01334 }
01335
01336 inline unsigned NxsDiscreteDatatypeMapper::GetNumStates() const
01337 {
01338 return nStates;
01339 }
01340
01346 inline std::vector<NxsDiscreteStateCell> NxsDiscreteDatatypeMapper::GetStateVectorForCode(NxsDiscreteStateCell c) const
01347 {
01348 const std::set<NxsDiscreteStateCell> & ss = GetStateSetForCode(c);
01349 return std::vector<NxsDiscreteStateCell>(ss.begin(), ss.end());
01350 }
01351
01356 inline const std::set<NxsDiscreteStateCell> & NxsDiscreteDatatypeMapper::GetStateSetForCode(NxsDiscreteStateCell c) const
01357 {
01358 NCL_ASSERT(stateCodeLookupPtr);
01359 ValidateStateCode(c);
01360 return stateCodeLookupPtr[c].states;
01361 }
01362
01369 inline NxsDiscreteStateCell NxsDiscreteDatatypeMapper::GetOneStateForCode(NxsDiscreteStateCell stateCode, unsigned stateIndex) const
01370 {
01371 const std::set<NxsDiscreteStateCell> & s = GetStateSetForCode(stateCode);
01372 unsigned i = 0;
01373 for (std::set<NxsDiscreteStateCell>::const_iterator sIt = s.begin(); sIt != s.end(); ++sIt, ++i)
01374 {
01375 if (i == stateIndex)
01376 return *sIt;
01377 }
01378 NCL_ASSERT(false);
01379 throw NxsException("State index out of range in NxsDiscreteDatatypeMapper::GetOneStateForCode");
01380 }
01381
01393 inline std::string NxsDiscreteDatatypeMapper::StateCodeToNexusString(NxsDiscreteStateCell scode, bool demandSymbols) const
01394 {
01395 std::ostringstream o;
01396 WriteStateCodeAsNexusString(o, scode, demandSymbols);
01397 return o.str();
01398 }
01399
01400
01406 inline NxsDiscreteStateCell NxsDiscreteDatatypeMapper::EncodeNexusStateString(
01407 const std::string &stateAsNexus,
01408 NxsToken & token,
01409 const unsigned taxInd,
01410 const unsigned charInd,
01411 const NxsDiscreteStateRow * firstTaxonRow, const NxsString &nameStr)
01412 {
01413 const unsigned tlen = (unsigned) stateAsNexus.length();
01414 if (tlen == 0)
01415 GenerateNxsExceptionMatrixReading("Unexpected empty token encountered", taxInd, charInd, token, nameStr);
01416 if (tlen == 1)
01417 return StateCodeForNexusChar(stateAsNexus[0], token, taxInd, charInd, firstTaxonRow, nameStr);
01418 return StateCodeForNexusMultiStateSet('\0', stateAsNexus, token, taxInd, charInd, firstTaxonRow, nameStr);
01419 }
01420
01425 inline bool NxsDiscreteDatatypeMapper::WasRestrictionDataype() const
01426 {
01427 return restrictionDataype;
01428 }
01433 inline bool NxsCharactersBlock::WasRestrictionDataype() const
01434 {
01435 return restrictionDataype;
01436 }
01437
01438 inline void NxsCharactersBlock::SetNexus(NxsReader *nxsptr)
01439 {
01440 NxsBlock::SetNexus(nxsptr);
01441 NxsTaxaBlockSurrogate::SetNexusReader(nxsptr);
01442 }
01443
01444 inline bool NxsCharactersBlock::IsMixedType() const
01445 {
01446 return (datatypeMapperVec.size() > 1);
01447 }
01448
01452 inline std::vector<std::string> NxsCharactersBlock::GetItems() const
01453 {
01454 return items;
01455 }
01456
01464 inline std::vector<double> NxsCharactersBlock::GetContinuousValues(
01465 unsigned i,
01466 unsigned j,
01467 const std::string key) NCL_COULD_BE_CONST
01468 {
01469 const ContinuousCharCell & cell = continuousMatrix.at(i).at(j);
01470 ContinuousCharCell::const_iterator cIt = cell.find(key);
01471 if (cIt == cell.end())
01472 return std::vector<double>();
01473 return cIt->second;
01474 }
01475
01482 inline double NxsCharactersBlock::GetSimpleContinuousValue(
01483 unsigned i,
01484 unsigned j) NCL_COULD_BE_CONST
01485 {
01486 const std::vector<double> av = GetContinuousValues(i, j, std::string("AVERAGE"));
01487 if (av.empty())
01488 return DBL_MAX;
01489 return av.at(0);
01490 }
01491
01492
01497 inline NxsString NxsCharactersBlock::GetCharLabel(
01498 unsigned i) const
01499 {
01500 std::map<unsigned, std::string>::const_iterator tlIt = indToCharLabel.find(i);
01501 if (tlIt == indToCharLabel.end())
01502 return NxsString(" ");
01503 return NxsString(tlIt->second.c_str());
01504 }
01505
01509 inline bool NxsCharactersBlock::HasCharLabels() const
01510 {
01511 return !indToCharLabel.empty();
01512 }
01516 inline char NxsCharactersBlock::GetGapSymbol() const
01517 {
01518 return gap;
01519 }
01520
01525 inline void NxsCharactersBlock::SetGapSymbol(char g)
01526 {
01527 gap = g;
01528 if (datatypeMapperVec.size() == 1)
01529 datatypeMapperVec[0].first.SetGapSymbol(g);
01530 }
01531
01532
01539 inline NxsCharactersBlock::DataTypesEnum NxsCharactersBlock::GetDataType() const
01540 {
01541 if (datatypeMapperVec.empty())
01542 return datatype;
01543 if (datatypeMapperVec.size() > 1)
01544 return mixed;
01545 return datatypeMapperVec[0].first.GetDatatype();
01546 }
01547
01548 inline NxsCharactersBlock::DataTypesEnum NxsCharactersBlock::GetOriginalDataType() const
01549 {
01550 return originalDatatype;
01551 }
01552
01556 inline char NxsCharactersBlock::GetMatchcharSymbol() NCL_COULD_BE_CONST
01557 {
01558 return matchchar;
01559 }
01560
01579 inline NxsDiscreteStateCell NxsCharactersBlock::GetInternalRepresentation(
01580 unsigned i,
01581 unsigned j,
01582 unsigned k) NCL_COULD_BE_CONST
01583 {
01584 if (IsGapState(i, j))
01585 return -3;
01586 else if (IsMissingState(i, j))
01587 return -2;
01588 else
01589 return GetStateIndex(i, j, k);
01590 }
01591
01595 inline char NxsCharactersBlock::GetMissingSymbol() const
01596 {
01597 return missing;
01598 }
01599
01605 inline unsigned NxsCharactersBlock::GetNumChar() const
01606 {
01607 return nChar;
01608 }
01609
01615 inline unsigned NxsCharactersBlock::GetNChar() const
01616 {
01617 return nChar;
01618 }
01619
01623 inline unsigned NxsCharactersBlock::GetNumIncludedChars() const
01624 {
01625 return nChar - excluded.size();
01626 }
01627
01628
01629 inline unsigned NxsCharactersBlock::GetNCharTotal()
01630 {
01631 return nChar;
01632 }
01633
01634 inline unsigned NxsCharactersBlock::GetNCharTotal() const
01635 {
01636 return nChar;
01637 }
01638
01639 inline unsigned NxsCharactersBlock::GetNTaxWithData() const
01640 {
01641 return nTaxWithData;
01642 }
01643
01647 inline unsigned NxsCharactersBlock::GetNumEliminated() NCL_COULD_BE_CONST
01648 {
01649 return (unsigned)eliminated.size();
01650 }
01651
01655 inline unsigned NxsCharactersBlock::GetNumUserEquates() const
01656 {
01657 return (unsigned)(userEquates.size());
01658 }
01659
01663 inline unsigned NxsCharactersBlock::GetNumEquates() NCL_COULD_BE_CONST
01664 {
01665 return (unsigned)(userEquates.size() + defaultEquates.size());
01666 }
01667
01672 inline unsigned NxsCharactersBlock::GetNumMatrixCols() NCL_COULD_BE_CONST
01673 {
01674 return nChar;
01675 }
01676
01681 inline unsigned NxsCharactersBlock::GetNumMatrixRows() NCL_COULD_BE_CONST
01682 {
01683 return GetNTaxTotal();
01684 }
01685
01686 inline NxsDiscreteStateCell NxsCharactersBlock::GetStateIndex(
01687 unsigned taxInd,
01688 unsigned charInd,
01689 unsigned k) const
01690 {
01691 const NxsDiscreteDatatypeMapper * currMapper = GetDatatypeMapperForChar(charInd);
01692 NCL_ASSERT(currMapper);
01693 const NxsDiscreteStateRow & row = GetDiscreteMatrixRow(taxInd);
01694 NCL_ASSERT(row.size() > charInd);
01695 return currMapper->GetOneStateForCode(row[charInd], k);
01696 }
01712 inline char NxsCharactersBlock::GetState(
01713 unsigned i,
01714 unsigned j,
01715 unsigned k) const
01716 {
01717 NCL_ASSERT(!symbols.empty());
01718 const NxsDiscreteStateCell p = GetStateIndex(i, j, k);
01719 if (p < 0)
01720 {
01721 NCL_ASSERT(p == NXS_GAP_STATE_CODE);
01722 return gap;
01723 }
01724 NCL_ASSERT(p < (int)symbols.length());
01725 return symbols[(int)p];
01726 }
01727
01730 inline const char *NxsCharactersBlock::GetSymbols() NCL_COULD_BE_CONST
01731 {
01732 return symbols.c_str();
01733 }
01734
01738 inline NxsString NxsCharactersBlock::GetTaxonLabel(
01739 unsigned i) const
01740 {
01741 NxsString s = taxa->GetTaxonLabel(i);
01742 return s;
01743 }
01744
01745 inline bool NxsCharactersBlock::TaxonIndHasData(
01746 unsigned taxInd) const
01747 {
01748 if (datatype == continuous)
01749 return (taxInd < continuousMatrix.size() && !continuousMatrix[taxInd].empty());
01750 return (taxInd < discreteMatrix.size() && !discreteMatrix[taxInd].empty());
01751 }
01752
01753
01754 inline const NxsUnsignedSet & NxsCharactersBlock::GetExcludedIndexSet() const
01755 {
01756 return excluded;
01757 }
01758
01759 inline bool NxsCharactersBlock::IsActiveChar(
01760 unsigned j) const
01761 {
01762 return (j < nChar && excluded.count(j) == 0);
01763 }
01764
01765
01770 inline bool NxsCharactersBlock::IsExcluded(
01771 unsigned j) const
01772 {
01773 return !IsActiveChar(j);
01774 }
01775
01776 inline bool NxsCharactersBlock::IsActiveChar(
01777 unsigned j)
01778 {
01779 return (j < nChar && excluded.count(j) == 0);
01780 }
01781
01782
01787 inline bool NxsCharactersBlock::IsExcluded(
01788 unsigned j)
01789 {
01790 return !IsActiveChar(j);
01791 }
01792
01793
01797 inline bool NxsCharactersBlock::IsInterleave() NCL_COULD_BE_CONST
01798 {
01799 return interleaving;
01800 }
01801
01805 inline bool NxsCharactersBlock::IsLabels() NCL_COULD_BE_CONST
01806 {
01807 return labels;
01808 }
01809
01813 inline bool NxsCharactersBlock::IsRespectCase() const
01814 {
01815 return respectingCase;
01816 }
01817
01821 inline bool NxsCharactersBlock::IsTokens() NCL_COULD_BE_CONST
01822 {
01823 return tokens;
01824 }
01825
01829 inline bool NxsCharactersBlock::IsTranspose() NCL_COULD_BE_CONST
01830 {
01831 return transposing;
01832 }
01833
01838 inline unsigned NxsCharactersBlock::TaxonLabelToNumber(
01839 NxsString s) const
01840 {
01841 try
01842 {
01843 return 1 + taxa->FindTaxon(s);
01844 }
01845 catch(NxsTaxaBlock::NxsX_NoSuchTaxon)
01846 {
01847 }
01848
01849 return 0;
01850 }
01851
01852
01853 inline VecBlockPtr NxsCharactersBlock::GetImpliedBlocks()
01854 {
01855 return GetCreatedTaxaBlocks();
01856 }
01857 inline const std::string & NxsCharactersBlock::GetBlockName() const
01858 {
01859 return id;
01860 }
01861 inline void NxsCharactersBlock::HandleLinkCommand(NxsToken & token)
01862 {
01863 HandleLinkTaxaCommand(token);
01864 }
01865 inline void NxsCharactersBlock::WriteLinkCommand(std::ostream &out) const
01866 {
01867 WriteLinkTaxaCommand(out);
01868 }
01869
01870
01871 inline NxsCharactersBlock::StatesFormatEnum NxsCharactersBlock::GetStatesFormat() const
01872 {
01873 return statesFormat;
01874 }
01875
01876 inline unsigned NxsCharactersBlock::CharLabelToNumber(NxsString s) NCL_COULD_BE_CONST
01877 {
01878 const NxsCharactersBlock *b = (const NxsCharactersBlock *)(this);
01879 return b->CharLabelToNumber(s);
01880 }
01881
01885 inline bool NxsCharactersBlock::IsEliminated(
01886 unsigned charIndex) NCL_COULD_BE_CONST
01887 {
01888 return eliminated.count(charIndex) > 0 ;
01889 }
01890
01891
01892
01893
01894
01895
01896
01897
01898 inline const NxsDiscreteDatatypeMapper * NxsCharactersBlock::GetDatatypeMapperForChar(unsigned charIndex) const
01899 {
01900 NxsCharactersBlock *mt = const_cast<NxsCharactersBlock *>(this);
01901 return mt->GetMutableDatatypeMapperForChar(charIndex);
01902 }
01903
01904 inline const NxsDiscreteDatatypeMapper & NxsCharactersBlock::GetDatatypeMapperForCharRef(unsigned charIndex) const
01905 {
01906 const NxsDiscreteDatatypeMapper * dm = this->GetDatatypeMapperForChar(charIndex);
01907 NCL_ASSERT(dm);
01908 return *dm;
01909 }
01910
01911 inline const NxsDiscreteStateRow & NxsCharactersBlock::GetDiscreteMatrixRow(unsigned int taxIndex) const
01912 {
01913 return discreteMatrix.at(taxIndex);
01914 }
01915
01916 inline const NxsCharactersBlock::ContinuousCharRow & NxsCharactersBlock::GetContinuousMatrixRow(unsigned taxIndex) const
01917 {
01918 return continuousMatrix.at(taxIndex);
01919 }
01920
01928 inline NxsDiscreteDatatypeMapper * NxsCharactersBlock::GetMutableDatatypeMapperForChar(unsigned int charIndex)
01929 {
01930 if (datatypeMapperVec.size() == 1)
01931 return &(datatypeMapperVec[0].first);
01932 for (VecDatatypeMapperAndIndexSet::iterator dmvIt = datatypeMapperVec.begin(); dmvIt != datatypeMapperVec.end(); ++dmvIt)
01933 {
01934 const NxsUnsignedSet & currCS = dmvIt->second;
01935 if (currCS.count(charIndex) > 0)
01936 return &(dmvIt->first);
01937 }
01938 return NULL;
01939 }
01940
01941 inline std::vector<const NxsDiscreteDatatypeMapper *> NxsCharactersBlock::GetAllDatatypeMappers() const
01942 {
01943 std::vector<const NxsDiscreteDatatypeMapper *> v;
01944 for (VecDatatypeMapperAndIndexSet::const_iterator dmvIt = datatypeMapperVec.begin(); dmvIt != datatypeMapperVec.end(); ++dmvIt)
01945 v.push_back(&(dmvIt->first));
01946 return v;
01947 }
01948
01949 inline void NxsDiscreteDatatypeMapper::WriteStateCodeRowAsNexus(std::ostream & out, const NxsDiscreteStateRow &row) const
01950 {
01951 std::vector<NxsDiscreteStateCell>::const_iterator b = row.begin();
01952 const std::vector<NxsDiscreteStateCell>::const_iterator e = row.end();
01953 WriteStateCodeRowAsNexus(out, b, e);
01954 }
01955
01956 inline void NxsDiscreteDatatypeMapper::WriteStateCodeRowAsNexus(std::ostream & out, NxsDiscreteStateRow::const_iterator & begIt, const NxsDiscreteStateRow::const_iterator & endIt) const
01957 {
01958 for (; begIt != endIt; ++begIt)
01959 WriteStateCodeAsNexusString(out, *begIt, true);
01960 }
01961
01962
01963 #endif