A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxon.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 #include <algorithm>
34 #include <stdexcept>
35 
36 namespace genesis {
37 namespace taxonomy {
38 
39 // ================================================================================================
40 // Constructors and Rule of Five
41 // ================================================================================================
42 
44  : Taxonomy()
45  , name_()
46  , rank_()
47  , id_()
48  , parent_( nullptr )
49  , data_( nullptr )
50 {}
51 
52 Taxon::Taxon( std::string const& name, std::string const& rank, std::string const& id )
53  : Taxonomy()
54  , name_( name )
55  , rank_( rank )
56  , id_( id )
57  , parent_( nullptr )
58  , data_( nullptr )
59 {}
60 
61 Taxon::Taxon( Taxon const& other )
62  : Taxonomy( other )
63  , name_( other.name_ )
64  , rank_( other.rank_ )
65  , id_( other.id_ )
66  , parent_( nullptr )
67 {
68  if( other.has_data() ) {
69  reset_data( other.data_->clone() );
70  }
71  reset_parent_pointers_( this );
72 }
73 
74 Taxon::Taxon( Taxon&& other )
75  : Taxonomy( std::move( other ))
76  , name_( std::move( other.name_ ))
77  , rank_( std::move( other.rank_ ))
78  , id_( std::move( other.id_ ))
79  , parent_( other.parent_ )
80  , data_( std::move( other.data_ ))
81 {
82  reset_parent_pointers_( this );
83 }
84 
85 Taxon& Taxon::operator= ( Taxon const& other )
86 {
87  Taxonomy::operator=( static_cast< Taxonomy const& >( other ));
88  name_ = other.name_;
89  rank_ = other.rank_;
90  id_ = other.id_;
91  parent_ = nullptr;
92  if( other.has_data() ) {
93  reset_data( other.data_->clone() );
94  }
95  reset_parent_pointers_( this );
96  return *this;
97 }
98 
100 {
101  Taxonomy::operator=( static_cast< Taxonomy&& >( std::move( other )));
102  name_ = std::move( other.name_ );
103  rank_ = std::move( other.rank_ );
104  id_ = std::move( other.id_ );
105  parent_ = other.parent_;
106  data_ = std::move( other.data_ );
107  reset_parent_pointers_( this );
108  return *this;
109 }
110 
111 void swap( Taxon& lhs, Taxon& rhs )
112 {
113  using std::swap;
114  swap( static_cast< Taxonomy& >( lhs ), static_cast< Taxonomy& >( rhs ) );
115 
116  swap( lhs.name_, rhs.name_ );
117  swap( lhs.rank_, rhs.rank_ );
118  swap( lhs.id_, rhs.id_ );
119  swap( lhs.parent_, rhs.parent_ );
120  swap( lhs.data_, rhs.data_ );
121 }
122 
123 // ================================================================================================
124 // Properties
125 // ================================================================================================
126 
127 // -----------------------------------------------------
128 // Name
129 // -----------------------------------------------------
130 
131 std::string const& Taxon::name() const
132 {
133  return name_;
134 }
135 
136 void Taxon::name( std::string const& value )
137 {
138  name_ = value;
139 }
140 
141 // -----------------------------------------------------
142 // Rank
143 // -----------------------------------------------------
144 
145 std::string const& Taxon::rank() const
146 {
147  return rank_;
148 }
149 
150 void Taxon::rank( std::string const& value )
151 {
152  rank_ = value;
153 }
154 
155 // -----------------------------------------------------
156 // ID
157 // -----------------------------------------------------
158 
159 std::string const& Taxon::id() const
160 {
161  return id_;
162 }
163 
164 void Taxon::id( std::string const& value )
165 {
166  id_ = value;
167 }
168 
169 // -----------------------------------------------------
170 // Parent
171 // -----------------------------------------------------
172 
173 Taxon const* Taxon::parent () const
174 {
175  return parent_;
176 }
177 
179 {
180  return parent_;
181 }
182 
183 // ================================================================================================
184 // Data
185 // ================================================================================================
186 
187 bool Taxon::has_data() const
188 {
189  return data_.get() != nullptr;
190 }
191 
193 {
194  return data_.get();
195 }
196 
198 {
199  return data_.get();
200 }
201 
202 Taxon& Taxon::reset_data( std::unique_ptr< BaseTaxonData > data )
203 {
204  data_ = std::move( data );
205  return *this;
206 }
207 
208 // ================================================================================================
209 // Protected Implementation Details
210 // ================================================================================================
211 
213 {
214  auto& c = Taxonomy::add_child_( child, merge_duplicates );
215  // c.parent_ = this;
216 
217  // We added to the container. This might have caused relocation of the contant.
218  // Need to update parent pointers!
219  reset_parent_pointers_( this );
220  return c;
221 }
222 
223 } // namespace taxonomy
224 } // namespace genesis
Taxon()
Default constructor. Does nothing. Yields an empty Taxon.
Definition: taxon.cpp:43
Taxonomy & operator=(Taxonomy const &)
Copy assigment operator.
Definition: taxonomy.cpp:57
virtual Taxon & add_child_(Taxon const &child, bool merge_duplicates)
Virtual implementation function for adding a child Taxon.
Definition: taxonomy.cpp:267
std::string const & name() const
Return the name of this taxon.
Definition: taxon.cpp:131
Taxon & operator=(Taxon const &)
Copy assignment operator.
Definition: taxon.cpp:85
bool has_data() const
Return true if the Taxon has a data object assigned to it.
Definition: taxon.cpp:187
Taxon & add_child_(Taxon const &child, bool merge_duplicates) override
Virtual implementation function for adding a child taxon.
Definition: taxon.cpp:212
void reset_parent_pointers_(Taxon *parent)
Internal helper function that resets the parent pointer of all stored Taxa.
Definition: taxonomy.cpp:293
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Definition: taxon.hpp:76
void swap(Taxon &lhs, Taxon &rhs)
Definition: taxon.cpp:111
Taxon & reset_data(std::unique_ptr< BaseTaxonData > data)
Reset the data pointer of this Taxon.
Definition: taxon.cpp:202
BaseTaxonData * data_ptr()
Return a pointer to the data.
Definition: taxon.cpp:192
std::string const & id() const
Set the ID of this taxon.
Definition: taxon.cpp:159
Base class for storing data on Taxa of a Taxonomy.
Definition: taxon_data.hpp:56
Taxon const * parent() const
Return a pointer to the parent of this taxon, or a nullptr if this is the top level taxon...
Definition: taxon.cpp:173
void merge_duplicates(Sample &smp)
Look for Pqueries with the same name and merge them.
std::string const & rank() const
Return the rank of this taxon.
Definition: taxon.cpp:145