/usr/include/ncl/nxstaxablock.h is in libncl-dev 2.1.18+dfsg-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | // Copyright (C) 1999-2003 Paul O. Lewis
//
// This file is part of NCL (Nexus Class Library) version 2.0.
//
// NCL is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// NCL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with NCL; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#ifndef NCL_NXSTAXABLOCK_H
#define NCL_NXSTAXABLOCK_H
#include "ncl/nxsdefs.h"
#include "ncl/nxsblock.h"
/*! This abstract class describes the interface that every block that wants to serve
as a reader of NEXUS TAXA blocks should fulfill.
A parsed taxa block in NEXUS is essentially a list of unique taxon labels.
When compared the label comparison is not case-sensitive.
The taxon can be referred to in NEXUS files by its number (numbering starting at 1), or its label.
*/
class NxsTaxaBlockAPI
: public NxsBlock, public NxsLabelToIndicesMapper
{
public:
/*! Adds taxon label 's' to end of list of taxon labels and increments dimNTax by 1.
\returns the (0-based) index of taxon label just added.
*/
virtual unsigned AddTaxonLabel(const std::string & s) = 0;
/*! Changes the label for taxon `i` to `s`
The index `i` should be 0-based.
\throws NxsNCLAPIException if `i` is out of range.
\throws DuplicatedLabelNxsException if the label is already in the block
\throws NxsException if the label is not a legal taxon name (eg. it is a punctation character).
*/
virtual void ChangeTaxonLabel(unsigned i, NxsString s) = 0; /*v2.1to2.2 4 */
/*! \returns the 0-based index of taxon named 's' in taxonLabels list.
\throws NxsX_NoSuchTaxon exception if taxon named 's' cannot be found.
\warning {This function does NOT implement the interpret-label-as-a-number functionality}
*/
virtual unsigned FindTaxon(const NxsString & label) const = 0; /*v2.1to2.2 4 */
/*! \returns true if the label `label` already a taxon label (not case sensitive)
*/
virtual bool IsAlreadyDefined(const std::string & label) = 0;
/*! \returns the length of the longest label.
\note {The label length, does not include any extra characters (such as quotes) that may be needed to write the file
out to NEXUS format}
*/
virtual unsigned GetMaxTaxonLabelLength() = 0;
/*! \returns the number of taxon labels (should be the same as GetNTax after a valid parse).
*/
virtual unsigned GetNumTaxonLabels() const = 0;
/*! \returns the number of taxa
*/
virtual unsigned GetNTax() const = 0;
/*! \returns the number of taxa
The prescence of this function and GetNTax is a historical artifact. They have the same behavior.
*/
virtual unsigned GetNTaxTotal() const = 0;
/*! \returns the label for taxon with (0-based) index of `i`
\throws NxsNCLAPIException if `i` is out of range.
\warning{Can return a 1-based number if the taxon lacks a name (in NEXUS parsing "1" refers to the first taxon).}
*/
virtual NxsString GetTaxonLabel(unsigned i) const = 0; /*v2.1to2.2 4 */
/*! \returns a vector of all of the taxon labels */
virtual std::vector<std::string> GetAllLabels() const;
/*! \returns a 1-based number of the taxon with label of `label` (not case-sensitive).
This is a low-level function not intended for widespread use (it is faster way to
query the label list because it does not throw exceptions or do the numeric interpretation
of labels).
\warning{does NOT apply the numeric interpretation of the label.}
\warning{ 1-based numbering}
*/
virtual unsigned TaxLabelToNumber(const std::string &label) const = 0;
/*! hook called during NxsTaxaBlock::Read() when the TaxLabels command is encountered */
virtual void HandleTaxLabels(NxsToken &token) = 0;
/*! writes the taxon labes as NEXUS to `out` */
virtual void WriteTaxLabelsCommand(std::ostream &out) const = 0;
/*! Sets the number of taxa to be included in the block
\warning{This can cause the cropping of labels.}
*/
virtual void SetNtax(unsigned n) = 0;
/*! \returns the number of taxa that have not been inactivated by calling InactivateTaxa
*/
virtual unsigned GetNumActiveTaxa() const = 0;
/*! \returns true if the taxon (denoted by a 0-based index) is in range and not inactivated
\note{Works, but is not currently used by the library added for planned delete/restor functionality}
*/
virtual bool IsActiveTaxon(unsigned i) const = 0;
/*! flags a set of taxa as inactive. Takes a set of 0-based indices.
\note{Works, but is not currently used by the library added for planned delete/restor functionality}
*/
virtual unsigned InactivateTaxa(const std::set<unsigned> &s) = 0;
/*! flags a set of taxa as active. Takes a set of 0-based indices.
\note{Works, but is not currently used by the library added for planned delete/restor functionality}
*/
virtual unsigned ActivateTaxa(const std::set<unsigned> &) = 0;
/*! flags a taxon as active. Takes a 0-based index.
\note{Works, but is not currently used by the library added for planned delete/restor functionality}
*/
virtual unsigned InactivateTaxon(unsigned ) = 0;
/*! flags a taxon as active. Takes a 0-based index.
\note{Works, but is not currently used by the library added for planned delete/restor functionality}
*/
virtual unsigned ActivateTaxon(unsigned ) = 0;
};
/*! The default implementation of the NxsTaxaBlockAPI that is used to parse TAXA blocks into a list of
unique (case-insensitive) labels.
*/
class NxsTaxaBlock
: public NxsTaxaBlockAPI
{
friend class NxsDataBlock;
friend class NxsAllelesBlock;
friend class NxsCharactersBlock;
friend class NxsDistancesBlock;
public:
NxsTaxaBlock();
virtual ~NxsTaxaBlock();
virtual unsigned AddTaxonLabel(const std::string & s);
void ChangeTaxonLabel(unsigned i, NxsString s); /*v2.1to2.2 4 */
unsigned TaxLabelToNumber(const std::string &label) const;
unsigned FindTaxon(const NxsString & label) const; /*v2.1to2.2 4 */
bool IsAlreadyDefined(const std::string &label);
unsigned GetIndexSet(const std::string &label, NxsUnsignedSet * toFill) const
{
return NxsLabelToIndicesMapper::GetIndicesFromSets(label, toFill, taxSets);
}
unsigned GetMaxTaxonLabelLength();
unsigned GetNTax() const;
unsigned GetNTaxTotal() const;
unsigned GetNumTaxonLabels() const;
NxsString GetTaxonLabel(unsigned i) const; /*v2.1to2.2 4 */
void HandleTaxLabels(NxsToken &token);
bool NeedsQuotes(unsigned i);
virtual void Report(std::ostream &out) NCL_COULD_BE_CONST ; /*v2.1to2.2 1 */
virtual void Reset();
void SetNtax(unsigned n);
void WriteAsNexus(std::ostream &out) const;
virtual unsigned GetNumActiveTaxa() const;
virtual bool IsActiveTaxon(unsigned i) const;
virtual unsigned InactivateTaxa(const std::set<unsigned> &);
virtual unsigned ActivateTaxa(const std::set<unsigned> &);
virtual unsigned InactivateTaxon(unsigned );
virtual unsigned ActivateTaxon(unsigned );
class NxsX_NoSuchTaxon {}; /* thrown if FindTaxon cannot locate a supplied taxon label in the taxLabels vector */
void WriteTaxLabelsCommand(std::ostream &out) const;
unsigned GetMaxIndex() const;
unsigned GetNumLabelsCurrentlyStored() const;
unsigned GetIndicesForLabel(const std::string &label, NxsUnsignedSet *inds) const;
bool AddNewIndexSet(const std::string &label, const NxsUnsignedSet & inds);
bool AddNewPartition(const std::string &label, const NxsPartition & inds);
/*----------------------------------------------------------------------
| AppendNewLabel should not be called in most client code. It is only
| added because some blocks create their own taxa block on-the-fly.
*/
virtual unsigned AppendNewLabel(std::string &label)
{
while (dimNTax <= taxLabels.size())
dimNTax++;
return AddTaxonLabel(label);
}
NxsTaxaBlock &operator=(const NxsTaxaBlock &other)
{
Reset();
CopyBaseBlockContents(static_cast<const NxsBlock &>(other));
CopyTaxaContents(other);
return *this;
}
void CopyTaxaContents(const NxsTaxaBlock &other)
{
taxLabels = other.taxLabels;
labelToIndex = other.labelToIndex;
dimNTax = other.dimNTax;
taxSets = taxSets;
taxPartitions = other.taxPartitions;
inactiveTaxa = other.inactiveTaxa;
}
NxsTaxaBlock * Clone() const
{
NxsTaxaBlock *taxa = new NxsTaxaBlock();
*taxa = *this;
return taxa;
}
protected:
NxsStringVector taxLabels; /* storage for list of taxon labels */
std::map<std::string, unsigned> labelToIndex;
unsigned dimNTax;
NxsUnsignedSetMap taxSets;
NxsPartitionsByName taxPartitions;
std::set<unsigned> inactiveTaxa;
virtual void Read(NxsToken &token);
void CheckCapitalizedTaxonLabel(const std::string &s) const;
unsigned CapitalizedTaxLabelToNumber(const std::string & s) const;
void RemoveTaxonLabel(unsigned taxInd);
};
/*! This class is the base class for blocks that can (in a pinch) serve as
a TAXA block reader(NxsCharactersBlock, NxsTreesBlock, NxsUnalignedBlock, and NxsDistancesBlock)
Client code rarely needs to call the special functionality of this class.
In broad terms, this class delegates calls to the taxa block if there is an
external taxa block.
If no external taxa block has been associated with the NxsTaxaBlockSurrogate, then
will create one (which will later be returned to the NxsReader as an
implied block)
*/
class NxsTaxaBlockSurrogate
{
public:
void SetCreateImpliedBlock(bool v)
{
createImpliedBlock = v;
}
/*! \returns an integer that will be a facet of NxsBlockLinkStatus which reflects how the taxa block was determined.*/
int GetTaxaLinkStatus() const
{
return taxaLinkStatus;
}
void SetTaxaLinkStatus(NxsBlock::NxsBlockLinkStatus s);
NxsTaxaBlockAPI * GetTaxaBlockPtr(int *status) const;
NxsTaxaBlockAPI * GetTaxaBlockPtr() {return GetTaxaBlockPtr(0L);}
virtual const std::string & GetBlockName() const = 0;
virtual unsigned GetNTax() const;
virtual unsigned GetNTaxTotal() const;
virtual unsigned GetNumActiveTaxa() const;
virtual bool IsActiveTaxon(unsigned i) const;
virtual unsigned InactivateTaxa(const std::set<unsigned> &);
virtual unsigned ActivateTaxa(const std::set<unsigned> &);
virtual unsigned InactivateTaxon(unsigned );
virtual unsigned ActivateTaxon(unsigned );
NxsTaxaBlockSurrogate &operator=(const NxsTaxaBlockSurrogate &other)
{
ResetSurrogate();
CopyTaxaBlockSurrogateContents(other);
return *this;
}
/*
| Aliases the same taxa block as `other`, but `other` retains ownership!!
*/
virtual void CopyTaxaBlockSurrogateContents(const NxsTaxaBlockSurrogate &other)
{
ResetSurrogate();
taxa = other.taxa;
taxaLinkStatus = other.taxaLinkStatus;
newtaxa = other.newtaxa;
ownsTaxaBlock = false;
passedRefOfOwnedBlock = other.passedRefOfOwnedBlock;
createImpliedBlock = other.createImpliedBlock;
nxsReader = other.nxsReader;
}
protected:
NxsTaxaBlockSurrogate(NxsTaxaBlockAPI *tb, NxsReader * reader)
:taxa(tb),
newtaxa(false),
ownsTaxaBlock(false),
passedRefOfOwnedBlock(false),
createImpliedBlock(false),
nxsReader(reader)
{
taxaLinkStatus = (tb == NULL ? NxsBlock::BLOCK_LINK_UNINITIALIZED : NxsBlock::BLOCK_LINK_TO_ONLY_CHOICE);
}
virtual ~NxsTaxaBlockSurrogate()
{
ResetSurrogate();
}
void SetTaxaBlockPtr(NxsTaxaBlockAPI *c, NxsBlock::NxsBlockLinkStatus s);
VecBlockPtr GetCreatedTaxaBlocks();
void FlagTaxaBlockAsUsed()
{
taxaLinkStatus |= NxsBlock::BLOCK_LINK_USED;
}
void AssureTaxaBlock(bool allocBlock, NxsToken &, const char *cmd);
void ResetSurrogate();
void SetNexusReader(NxsReader *nxsptr)
{
nxsReader = nxsptr;
}
virtual void HandleTaxLabels(NxsToken & token);
virtual void HandleLinkTaxaCommand(NxsToken & );
virtual void WriteLinkTaxaCommand(std::ostream &out) const;
bool SurrogateSwapEquivalentTaxaBlock(NxsTaxaBlockAPI * tb);
NxsTaxaBlockAPI *taxa; /* pointer to the TAXA block in which taxon labels are stored */
int taxaLinkStatus;
bool newtaxa;
bool ownsTaxaBlock;
bool passedRefOfOwnedBlock;
bool createImpliedBlock; /*if true and NEWTAXA is read in the DIMENSIONS command, then a new TaxaBlock will be allocated (instead of resetting the TAXA block). false by default.*/
NxsReader *nxsReader;
};
// The following typedef maintains compatibility with existing code.
// The TaxaBlock class name is deprecated; please use NxsTaxaBlock instead.
//
typedef NxsTaxaBlock TaxaBlock;
class NxsTaxaBlockFactory
:public NxsBlockFactory
{
public:
virtual NxsTaxaBlock * GetBlockReaderForID(const std::string & NCL_BLOCKTYPE_ATTR_NAME, NxsReader *reader, NxsToken *token);
};
inline unsigned NxsTaxaBlock::GetNTax() const
{
return dimNTax;
}
inline unsigned NxsTaxaBlock::GetNTaxTotal() const
{
return dimNTax;
}
/*! \returns a 1-based number of the taxon with label of `r` OR returns 0 to indicate that the label was not found.
Not for public usage
\warning{`r` must be capitalized for this function to work.}
\warning{does NOT apply the numeric interpretation of the label.}
\warning{ 1-based numbering}
*/
inline unsigned NxsTaxaBlock::CapitalizedTaxLabelToNumber(const std::string &r) const
{
std::map<std::string, unsigned>::const_iterator rIt = labelToIndex.find(r);
if (rIt == labelToIndex.end())
return 0;
return rIt->second + 1;
}
/*!
Returns true if taxon `i' is active. If taxon `i' has been deleted, returns false. Assumes `i' is in the range
[0..`ntax').
*/
inline bool NxsTaxaBlockSurrogate::IsActiveTaxon(
unsigned taxInd) const /* the taxon in question, in the range [0..`ntax') */
{
if (!taxa)
throw NxsNCLAPIException("Calling IsActiveTaxon on uninitialized block");
return taxa->IsActiveTaxon(taxInd);
}
inline unsigned NxsTaxaBlockSurrogate::GetNumActiveTaxa() const
{
if (!taxa)
throw NxsNCLAPIException("Calling GetNumActiveTaxa on uninitialized block");
return taxa->GetNumActiveTaxa();
}
inline unsigned NxsTaxaBlockSurrogate::InactivateTaxa(const std::set<unsigned> &s)
{
if (!taxa)
throw NxsNCLAPIException("Calling InactivateTaxa on uninitialized block");
return taxa->InactivateTaxa(s);
}
inline unsigned NxsTaxaBlockSurrogate::ActivateTaxa(const std::set<unsigned> &s)
{
if (!taxa)
throw NxsNCLAPIException("Calling ActivateTaxa on uninitialized block");
return taxa->ActivateTaxa(s);
}
inline unsigned NxsTaxaBlockSurrogate::InactivateTaxon(unsigned i)
{
if (!taxa)
throw NxsNCLAPIException("Calling InactivateTaxon on uninitialized block");
return taxa->InactivateTaxon(i);
}
inline unsigned NxsTaxaBlockSurrogate::ActivateTaxon(unsigned i)
{
if (!taxa)
throw NxsNCLAPIException("Calling ActivateTaxon on uninitialized block");
return taxa->ActivateTaxon(i);
}
inline unsigned NxsTaxaBlockSurrogate::GetNTax() const
{
if (!taxa)
throw NxsNCLAPIException("Calling GetNTax on uninitialized block");
return taxa->GetNTax();
}
inline unsigned NxsTaxaBlockSurrogate::GetNTaxTotal() const
{
if (!taxa)
throw NxsNCLAPIException("Calling GetNTaxTotal on uninitialized block");
return taxa->GetNTaxTotal();
}
inline unsigned NxsTaxaBlock::GetNumActiveTaxa() const
{
return GetNTax() - (unsigned)inactiveTaxa.size();
}
inline bool NxsTaxaBlock::IsActiveTaxon(unsigned i) const
{
return i < GetNTax() && (inactiveTaxa.count(i) == 0);
}
inline unsigned NxsTaxaBlock::InactivateTaxa(const std::set<unsigned> &s)
{
for (std::set<unsigned>::const_iterator sIt = s.begin(); sIt != s.end(); ++sIt)
InactivateTaxon(*sIt);
return GetNumActiveTaxa();
}
inline unsigned NxsTaxaBlock::ActivateTaxa(const std::set<unsigned> &s)
{
for (std::set<unsigned>::const_iterator sIt = s.begin(); sIt != s.end(); ++sIt)
ActivateTaxon(*sIt);
return GetNumActiveTaxa();
}
inline unsigned NxsTaxaBlock::InactivateTaxon(unsigned i)
{
if (i > GetNTax())
throw NxsNCLAPIException("Taxon index out of range in InactivateTaxon");
inactiveTaxa.insert(i);
return GetNumActiveTaxa();
}
inline unsigned NxsTaxaBlock::ActivateTaxon(unsigned i)
{
if (i > GetNTax())
throw NxsNCLAPIException("Taxon index out of range in InactivateTaxon");
inactiveTaxa.erase(i);
return GetNumActiveTaxa();
}
#endif
|