bpp-seq3  3.0.0
PhylipDistanceMatrixFormat.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Io/FileTools.h>
7 #include <Bpp/Text/TextTools.h>
8 
10 
11 // From SeqLib:
12 #include <Bpp/Seq/DistanceMatrix.h>
13 
14 using namespace bpp;
15 
16 // From the STL:
17 #include <iomanip>
18 
19 using namespace std;
20 
21 unique_ptr<DistanceMatrix> PhylipDistanceMatrixFormat::readDistanceMatrix(istream& in) const
22 {
23  string s = FileTools::getNextLine(in);
24  // the size of the matrix:
25  unsigned int n = TextTools::fromString<unsigned int>(s);
26  auto dist = make_unique<DistanceMatrix>(n);
27  unsigned int rowNumber = 0;
28  unsigned int colNumber = 0;
29  s = FileTools::getNextLine(in);
30  while (in)
31  {
32  if (colNumber == 0)
33  { // New row
34  if (extended_)
35  {
36  size_t pos = s.find(" ");
37  if (pos == string::npos)
38  throw Exception("PhylipDistanceMatrixFormat::read. Bad format, probably not 'extended' Phylip.");
39  dist->setName(rowNumber, s.substr(0, pos));
40  s = s.substr(pos + 2);
41  }
42  else
43  {
44  dist->setName(rowNumber, s.substr(0, 10));
45  s = s.substr(11);
46  }
47  }
48  StringTokenizer st(s, "\t ");
49  for ( ; colNumber < n && st.hasMoreToken(); colNumber++)
50  {
51  double d = TextTools::fromString<double>(st.nextToken());
52  (*dist)(rowNumber, colNumber) = d;
53  }
54  if (colNumber == n)
55  {
56  colNumber = 0;
57  rowNumber++;
58  }
59  s = FileTools::getNextLine(in);
60  }
61  return dist;
62 }
63 
65 {
66  size_t n = dist.size();
67  out << " " << n << endl;
68  size_t offset = 10;
69  if (extended_)
70  {
71  offset = 0;
72  for (size_t i = 0; i < n; ++i)
73  {
74  size_t s = dist.getName(i).size();
75  if (s > offset)
76  offset = s;
77  }
78  }
79  for (unsigned int i = 0; i < n; i++)
80  {
81  out << TextTools::resizeRight(dist.getName(i), offset, ' ');
82  if (extended_)
83  {
84  out << " ";
85  }
86  else
87  {
88  out << " ";
89  }
90  for (unsigned int j = 0; j < n; j++)
91  {
92  if (j > 0)
93  out << " ";
94  out << setprecision(8) << dist(i, j);
95  }
96  out << endl;
97  }
98 }
A Matrix class to store phylogenetic distances.
const std::string & getName(std::size_t i) const
std::size_t size() const
static std::string getNextLine(std::istream &in)
void writeDistanceMatrix(const DistanceMatrix &dist, const std::string &path, bool overwrite=true) const
Write a distance matrix to a file.
std::unique_ptr< DistanceMatrix > readDistanceMatrix(const std::string &path) const
Read a distance matrix from a file.
const std::string & nextToken()
bool hasMoreToken() const
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
This alphabet is used to deal NumericAlphabet.