bpp-phyl  2.2.0
Newick.cpp
Go to the documentation of this file.
1 //
2 // File: Newick.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Oct 23 15:35:03 2003
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for phylogenetic data analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "Newick.h"
41 #include "../Tree.h"
42 #include "../TreeTemplate.h"
43 #include "../TreeTemplateTools.h"
44 
45 #include <Bpp/Text/TextTools.h>
46 
47 using namespace bpp;
48 
49 // From the STL:
50 #include <iostream>
51 #include <fstream>
52 
53 using namespace std;
54 
55 /******************************************************************************/
56 
57 const string Newick::getFormatName() const { return "Newick"; }
58 
59 /******************************************************************************/
60 
61 const string Newick::getFormatDescription() const
62 {
63  return string("New hampshire parenthesis format. ") +
64  "See http://evolution.genetics.washington.edu/phylip/newicktree.html for more info.";
65 }
66 
67 /******************************************************************************/
68 
69 #if defined(NO_VIRTUAL_COV)
70  Tree*
71 #else
73 #endif
74 Newick::read(istream& in) const throw (Exception)
75 {
76  // Checking the existence of specified file
77  if (! in) { throw IOException ("Newick::read: failed to read from stream"); }
78 
79  //We concatenate all line in file till we reach the ending semi colon:
80  string temp, description;// Initialization
81  // Main loop : for all file lines
82  while (getline(in, temp, '\n'))
83  {
84  string::size_type index = temp.find(";");
85  if(index != string::npos)
86  {
87  description += temp.substr(0, index + 1);
88  break;
89  }
90  else description += temp;
91  }
92 
93  if (allowComments_) description = TextTools::removeSubstrings(description, '[', ']');
94  if (TextTools::isEmpty(description))
95  throw IOException("Newick::read: no tree was found!");
96  return TreeTemplateTools::parenthesisToTree(description, useBootstrap_, bootstrapPropertyName_);
97 }
98 
99 /******************************************************************************/
100 
101 void Newick::write_(const Tree& tree, ostream& out) const throw (Exception)
102 {
103  // Checking the existence of specified file, and possibility to open it in write mode
104  if (! out) { throw IOException ("Newick::writeTree: failed to write to stream"); }
105  if(useBootstrap_)
106  {
107  out << TreeTools::treeToParenthesis(tree, writeId_);
108  }
109  else
110  {
111  out << TreeTools::treeToParenthesis(tree, false, bootstrapPropertyName_);
112  }
113 }
114 
115 /******************************************************************************/
116 
117 template<class N>
118 void Newick::write_(const TreeTemplate<N>& tree, ostream& out) const throw (Exception)
119 {
120  // Checking the existence of specified file, and possibility to open it in write mode
121  if (! out) { throw IOException ("Newick::writeTree: failed to write to stream"); }
122  if(useBootstrap_)
123  {
124  out << TreeTemplateTools::treeToParenthesis(tree, writeId_);
125  }
126  else
127  {
128  out << TreeTemplateTools::treeToParenthesis(tree, false, bootstrapPropertyName_);
129  }
130 }
131 
132 /******************************************************************************/
133 
134 void Newick::read(istream& in, vector<Tree*>& trees) const throw (Exception)
135 {
136  // Checking the existence of specified file
137  if (! in) { throw IOException ("Newick::read: failed to read from stream"); }
138 
139  // Main loop : for all file lines
140  string temp, description;// Initialization
141  string::size_type index;
142  //We concatenate all line in file till we reach the ending semi colon:
143  while (getline(in, temp, '\n'))
144  {
145  index = temp.find(";");
146  if (index != string::npos)
147  {
148  description += temp.substr(0, index + 1);
149  if (allowComments_) description = TextTools::removeSubstrings(description, '[', ']');
150  trees.push_back(TreeTemplateTools::parenthesisToTree(description, useBootstrap_, bootstrapPropertyName_));
151  description = temp.substr(index + 1);
152  }
153  else description += temp;
154  }
155  //In case the file is empty, the method will not add any neww tree to the vector.
156 }
157 
158 /******************************************************************************/
159 
160 void Newick::write_(const vector<Tree*>& trees, ostream& out) const throw (Exception)
161 {
162  // Checking the existence of specified file, and possibility to open it in write mode
163  if (! out) { throw IOException ("Newick::write: failed to write to stream"); }
164  for(unsigned int i = 0; i < trees.size(); i++)
165  {
166  if(useBootstrap_)
167  {
168  out << TreeTools::treeToParenthesis(*trees[i], writeId_);
169  }
170  else
171  {
172  out << TreeTools::treeToParenthesis(*trees[i], false, bootstrapPropertyName_);
173  }
174  }
175 }
176 
177 /******************************************************************************/
178 
179 template<class N>
180 void Newick::write_(const vector<TreeTemplate<N>*>& trees, ostream& out) const throw (Exception)
181 {
182  // Checking the existence of specified file, and possibility to open it in write mode
183  if (! out) { throw IOException ("Newick::write: failed to write to stream"); }
184  for(unsigned int i = 0; i < trees.size(); i++)
185  {
186  if(useBootstrap_)
187  {
188  out << TreeTemplateTools::treeToParenthesis(*trees[i], writeId_);
189  }
190  else
191  {
192  out << TreeTemplateTools::treeToParenthesis(*trees[i], false, bootstrapPropertyName_);
193  }
194  }
195 }
196 
197 /******************************************************************************/
198 
const std::string getFormatName() const
Definition: Newick.cpp:57
static std::string treeToParenthesis(const Tree &tree, bool writeId=false)
Get the parenthesis description of a tree.
Definition: TreeTools.cpp:330
STL namespace.
The phylogenetic tree class.
Interface for phylogenetic tree objects.
Definition: Tree.h:148
const std::string getFormatDescription() const
Definition: Newick.cpp:61
TreeTemplate< Node > * read(const std::string &path) const
Read a tree from a file.
Definition: Newick.h:140
static std::string treeToParenthesis(const TreeTemplate< Node > &tree, bool writeId=false)
Get the parenthesis description of a tree.
void write_(const Tree &tree, std::ostream &out) const
Definition: Newick.cpp:101
static TreeTemplate< Node > * parenthesisToTree(const std::string &description, bool bootstrap=true, const std::string &propertyName=TreeTools::BOOTSTRAP, bool withId=false)
Parse a string in the parenthesis format and convert it to a tree.