00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef NCL_NXSDISTANCESBLOCK_H
00021 #define NCL_NXSDISTANCESBLOCK_H
00022
00023 #include "ncl/nxsdefs.h"
00024
00025 #include "ncl/nxstaxablock.h"
00026 #include "ncl/nxsdistancedatum.h"
00027
00066 class NxsDistancesBlock
00067 : public NxsBlock, public NxsTaxaBlockSurrogate
00068 {
00069 public:
00070 NxsDistancesBlock(NxsTaxaBlockAPI *t);
00071 virtual ~NxsDistancesBlock();
00072
00073 double GetDistance(unsigned i, unsigned j) const;
00074 char GetMissingSymbol() NCL_COULD_BE_CONST ;
00075 unsigned GetNchar() NCL_COULD_BE_CONST ;
00076 unsigned GetTriangle() NCL_COULD_BE_CONST ;
00077 bool IsRectangular() NCL_COULD_BE_CONST ;
00078 bool IsBoth() NCL_COULD_BE_CONST ;
00079 bool IsDiagonal() NCL_COULD_BE_CONST ;
00080 bool IsInterleave() NCL_COULD_BE_CONST ;
00081 bool IsLabels() NCL_COULD_BE_CONST ;
00082 bool IsLowerTriangular() NCL_COULD_BE_CONST ;
00083 bool IsMissing(unsigned i, unsigned j) const;
00084 bool IsUpperTriangular() NCL_COULD_BE_CONST ;
00085 virtual void Report(std::ostream &out) NCL_COULD_BE_CONST ;
00086 virtual void Reset();
00087 void SetDistance(unsigned i, unsigned j, double d);
00088 void SetMissing(unsigned i, unsigned j);
00089 void SetNchar(unsigned i);
00090 void SetNexus(NxsReader *nxsptr)
00091 {
00092 NxsBlock::SetNexus(nxsptr);
00093 NxsTaxaBlockSurrogate::SetNexusReader(nxsptr);
00094 }
00096 virtual const std::string & GetBlockName() const
00097 {
00098 return id;
00099 }
00100
00101 enum NxsDistancesBlockEnum
00102 {
00103 upper = 1,
00104 lower = 2,
00105 both = 3
00106 };
00107
00108 virtual VecBlockPtr GetImpliedBlocks()
00109 {
00110 return GetCreatedTaxaBlocks();
00111 }
00112
00113
00114 virtual void HandleLinkCommand(NxsToken & token)
00115 {
00116 HandleLinkTaxaCommand(token);
00117 }
00118 virtual void WriteLinkCommand(std::ostream &out) const
00119 {
00120 WriteLinkTaxaCommand(out);
00121 }
00122 void WriteAsNexus(std::ostream &out) const;
00123
00124
00125 NxsDistancesBlock &operator=(const NxsDistancesBlock &other)
00126 {
00127 Reset();
00128 CopyBaseBlockContents(static_cast<const NxsBlock &>(other));
00129 CopyTaxaBlockSurrogateContents(other);
00130 CopyDistancesContents(other);
00131 return *this;
00132 }
00133
00134 void CopyDistancesContents(const NxsDistancesBlock &other);
00135 NxsDistancesBlock * Clone() const
00136 {
00137 NxsDistancesBlock *d = new NxsDistancesBlock(taxa);
00138 *d = *this;
00139 return d;
00140 }
00141 bool SwapEquivalentTaxaBlock(NxsTaxaBlockAPI * tb)
00142 {
00143 return SurrogateSwapEquivalentTaxaBlock(tb);
00144 }
00145
00146 protected:
00147 void WriteFormatCommand(std::ostream &out) const;
00148 void WriteMatrixCommand(std::ostream &out) const;
00149
00150 void HandleDimensionsCommand(NxsToken &token);
00151 void HandleFormatCommand(NxsToken &token);
00152 void HandleMatrixCommand(NxsToken &token);
00153 bool HandleNextPass(NxsToken &token, unsigned &offset, std::vector<unsigned> & fileMatrixCmdOrderToTaxInd, std::set<unsigned> & taxIndsRead);
00154 virtual void Read(NxsToken &token);
00155
00156 private:
00157 NxsDistanceDatum & GetCell(unsigned i, unsigned j)
00158 {
00159 return matrix.at(i).at(j);
00160 }
00161 const NxsDistanceDatum & GetCell(unsigned i, unsigned j) const
00162 {
00163 return matrix.at(i).at(j);
00164 }
00165 typedef std::vector<NxsDistanceDatum> NxsDistanceDatumRow;
00166 typedef std::vector<NxsDistanceDatumRow> NxsDistanceDatumMatrix;
00167
00168 unsigned expectedNtax;
00169 unsigned nchar;
00170
00171 bool diagonal;
00172 bool interleave;
00173 bool labels;
00174
00175 int triangle;
00176
00177 char missing;
00178
00179 NxsDistanceDatumMatrix matrix;
00180 friend class PublicNexusReader;
00181 };
00182
00183 typedef NxsDistancesBlock DistancesBlock;
00184
00185 class NxsDistancesBlockFactory
00186 :public NxsBlockFactory
00187 {
00188 public:
00189 virtual NxsDistancesBlock * GetBlockReaderForID(const std::string & id, NxsReader *reader, NxsToken *token);
00190 };
00191
00192 inline bool NxsDistancesBlock::IsBoth() NCL_COULD_BE_CONST
00193 {
00194 return this->IsRectangular();
00195 }
00196
00197 #endif
00198