~ubuntu-branches/ubuntu/raring/libbpp-phyl/raring

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Io/Newick.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-yvx78svv6w7xxgph
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
72
                TreeTemplate<Node> * 
 
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 (! in.eof())
 
83
  {
 
84
                getline(in, temp, '\n');  // Copy current line in temporary string
 
85
    string::size_type index = temp.find(";");
 
86
                if(index != string::npos)
 
87
    {
 
88
                        description += temp.substr(0, index + 1);
 
89
                        break;
 
90
                }
 
91
    else description += temp;
 
92
        }
 
93
        if (allowComments_) description = TextTools::removeSubstrings(description, '[', ']');
 
94
        return TreeTemplateTools::parenthesisToTree(description, useBootstrap_, bootstrapPropertyName_);
 
95
}
 
96
 
 
97
/******************************************************************************/
 
98
 
 
99
void Newick::write_(const Tree& tree, ostream& out) const throw (Exception)
 
100
{
 
101
        // Checking the existence of specified file, and possibility to open it in write mode
 
102
        if (! out) { throw IOException ("Newick::writeTree: failed to write to stream"); }
 
103
  if(useBootstrap_)
 
104
  {
 
105
    out << TreeTools::treeToParenthesis(tree, writeId_);
 
106
  }
 
107
  else
 
108
  {
 
109
    out << TreeTools::treeToParenthesis(tree, false, bootstrapPropertyName_);
 
110
  }
 
111
}
 
112
 
 
113
/******************************************************************************/
 
114
 
 
115
template<class N>
 
116
void Newick::write_(const TreeTemplate<N>& tree, ostream& out) const throw (Exception)
 
117
{
 
118
        // Checking the existence of specified file, and possibility to open it in write mode
 
119
        if (! out) { throw IOException ("Newick::writeTree: failed to write to stream"); }
 
120
  if(useBootstrap_)
 
121
  {
 
122
    out << TreeTemplateTools::treeToParenthesis(tree, writeId_);
 
123
  }
 
124
  else
 
125
  {
 
126
    out << TreeTemplateTools::treeToParenthesis(tree, false, bootstrapPropertyName_);
 
127
  }
 
128
}
 
129
 
 
130
/******************************************************************************/
 
131
 
 
132
void Newick::read(istream& in, vector<Tree*>& trees) const throw (Exception)
 
133
{
 
134
        // Checking the existence of specified file
 
135
        if (! in) { throw IOException ("Newick::read: failed to read from stream"); }
 
136
        
 
137
        // Main loop : for all file lines
 
138
        string temp, description;// Initialization
 
139
  string::size_type index;
 
140
  while (!in.eof())
 
141
  {
 
142
          //We concatenate all line in file till we reach the ending semi colon:
 
143
          while (!in.eof())
 
144
    {
 
145
                  getline(in, temp, '\n');  // Copy current line in temporary string
 
146
      index = temp.find(";");
 
147
                  if (index != string::npos)
 
148
      {
 
149
                          description += temp.substr(0, index + 1);
 
150
              if (allowComments_) description = TextTools::removeSubstrings(description, '[', ']');
 
151
              trees.push_back(TreeTemplateTools::parenthesisToTree(description, useBootstrap_, bootstrapPropertyName_));
 
152
        description = temp.substr(index + 1);
 
153
                  }
 
154
      else description += temp;
 
155
          }
 
156
  }
 
157
}
 
158
 
 
159
/******************************************************************************/
 
160
 
 
161
void Newick::write_(const vector<Tree*>& trees, ostream& out) const throw (Exception)
 
162
{
 
163
        // Checking the existence of specified file, and possibility to open it in write mode
 
164
        if (! out) { throw IOException ("Newick::write: failed to write to stream"); }
 
165
  for(unsigned int i = 0; i < trees.size(); i++)
 
166
  {
 
167
    if(useBootstrap_)
 
168
    {
 
169
      out << TreeTools::treeToParenthesis(*trees[i], writeId_);
 
170
    }
 
171
    else
 
172
    {
 
173
      out << TreeTools::treeToParenthesis(*trees[i], false, bootstrapPropertyName_);
 
174
    }
 
175
  }
 
176
}
 
177
 
 
178
/******************************************************************************/
 
179
 
 
180
template<class N>
 
181
void Newick::write_(const vector<TreeTemplate<N>*>& trees, ostream& out) const throw (Exception)
 
182
{
 
183
        // Checking the existence of specified file, and possibility to open it in write mode
 
184
        if (! out) { throw IOException ("Newick::write: failed to write to stream"); }
 
185
  for(unsigned int i = 0; i < trees.size(); i++)
 
186
  {
 
187
    if(useBootstrap_)
 
188
    {
 
189
      out << TreeTemplateTools::treeToParenthesis(*trees[i], writeId_);
 
190
    }
 
191
    else
 
192
    {
 
193
      out << TreeTemplateTools::treeToParenthesis(*trees[i], false, bootstrapPropertyName_);
 
194
    }
 
195
  }
 
196
}
 
197
 
 
198
/******************************************************************************/
 
199