libsequence  1.9.5
PolySites.cc
1 /*
2 
3 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
4 
5 Remove the brackets to email me.
6 
7 This file is part of libsequence.
8 
9 libsequence is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 libsequence is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 long with libsequence. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #include <Sequence/PolySites.hpp>
25 #include <Sequence/Fasta.hpp>
26 #include <sstream>
27 #include <iterator>
28 #include <algorithm>
29 #include <iostream>
30 
118 namespace Sequence
119 {
120  PolySites::PolySites (void) : PolyTable()
121  {}
122 
123 
124  // PolySites::PolySites (const std::vector < double > &List, const std::vector <std::string > &stringList):
125  // PolyTable(List,stringList)
126  // /*!
127  // Use this constructor if you already have a list of positions and characters
128  // \param List a list of doubles representing positions of polymorphic positions
129  // \param stringList a vector of strings representing the polymorphic characters
130  // */
131  // {
132  // }
133 
134  PolySites::PolySites ( std::vector < double > List, std::vector < std::string > stringList) :
135  PolyTable(std::move(List),std::move(stringList))
136  {
137  }
138 
139  PolySites::PolySites (PolyTable::const_site_iterator beg,
140  PolyTable::const_site_iterator end):
141  PolyTable(beg,end)
142  {
143  }
144 
145  PolySites::PolySites( PolySites && pt ) : PolyTable(std::move(pt))
146  {
147  }
148 
149  PolySites::PolySites(const PolySites & rhs) : PolyTable(rhs)
150  {
151  }
152 
153  PolySites & PolySites::operator=(PolySites && pt)
154  {
155  PolyTable::operator=(std::move(pt));
156  return *this;
157  }
158 
159  PolySites & PolySites::operator=(const PolySites & pt)
160  {
161  PolyTable::operator=(pt);
162  return *this;
163  }
164 
165  std::istream & PolySites::read(std::istream &s)
166  {
167  std::vector<double> _pos;
168  std::vector<std::string> _ind;
169  std::string temp;
170  //get positions--which is the first line of input
171  std::getline(s,temp);
172  std::istringstream i(temp);
173  std::copy(std::istream_iterator<double>(i),std::istream_iterator<double>(),
174  std::back_inserter(_pos));
175 
176  while (std::getline(s,temp))
177  {
178  std::string temp2;
179  std::istringstream i(temp);
180  std::copy(std::istream_iterator<char>(i),std::istream_iterator<char>(),
181  std::back_inserter(temp2));
182  _ind.push_back(temp2);
183  }
184  if (! assign( std::move(_pos), std::move(_ind) ) )
185  {
186  throw std::runtime_error("PolySites::read() -- format error, unable to assign data");
187  }
188  return s;
189  }
190 
191  std::ostream & PolySites::print(std::ostream &stream) const
200  {
201  for(unsigned i = 0 ; i < this->numsites() ; ++i)
202  {
203  if(i==0)
204  stream << this->position(i);
205  else
206  stream << '\t' << this->position(i);
207  }
208  stream << '\n';
209 
210  for(unsigned i = 0 ; i < this->size() ; ++i)
211  {
212  for(unsigned j = 0 ; j < this->numsites() ; ++j)
213  {
214  if(j==0)
215  stream << (*this)[i][j];
216  else
217  stream << '\t'<<(*this)[i][j];
218  }
219  if(i < this->size()-1)
220  stream << '\n';
221  }
222  return (stream);
223  }
224 }
The base class for polymorphism tables.
Polymorphism tables for sequence data.
Definition: PolySites.hpp:33
STL namespace.
std::ostream & print(std::ostream &stream) const
output a tab-delimited array of positions and character states
Definition: PolySites.cc:191
The namespace in which this library resides.
Sequence::PolySites, generates polymorphism tables from data.
Declaration of Sequence::Fasta streams.