libsequence  1.9.5
SimData.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/SimData.hpp>
25 #include <cstdio>
26 #include <cstring>
27 #include <iostream>
28 #include <stdexcept>
29 
30 namespace Sequence
31 {
32  // SimData::SimData (const size_t & nsam, const size_t & nsnps):
33  // PolyTable(nsam,nsnps)
34  // /*!
35  // The constructor needs to know the sample size simulated.
36  // This is easily obtainted using Sequence::SimParams. An example
37  // of this is found in the example file tajd.cc
38  // */
39  // {
40  // }
41 
42  SimData::SimData() : PolyTable()
43  {
44  }
45 
46  SimData::SimData( SimData && pt) : PolyTable(std::move(pt))
47  {
48  }
49 
50  SimData::SimData(const SimData & rhs) : PolyTable(rhs)
51  {
52  }
53 
54 
55  // SimData::SimData(double *pos, const char **sample, const unsigned & nsam, const unsigned & S):
56  // PolyTable(pos,pos+S,sample,nsam)
57  // {
58  // }
59 
60 
61  // SimData::SimData(const std::vector<double> & pos,
62  // const std::vector<std::string> & data) : PolyTable(pos.begin(),
63  // pos.end(),
64  // data.begin(),
65  // data.end())
66  // {
67  // }
68 
69  SimData::SimData( std::vector<double> pos,
70  std::vector<std::string> data) : PolyTable(std::move(pos),
71  std::move(data))
72  {
73  }
74 
75  SimData::SimData(const SimData::const_site_iterator & beg,
76  const SimData::const_site_iterator & end) : PolyTable(beg,end)
77  {
78  }
79 
80  SimData & SimData::operator=(SimData && pt)
81  {
82  PolyTable::operator=(std::move(pt));
83  return *this;
84  }
85 
86  SimData & SimData::operator=(const SimData & pt)
87  {
88  PolyTable::operator=(pt);
89  return *this;
90  }
91 
92  std::istream & SimData::read (std::istream & stream)
93 
94 
99  {
100  char ch;
101  while(! stream.eof() )
102  {
103  stream >> ch;
104  if( ch == ':' ) break;
105  }
106  std::string temp;
107  unsigned S;
108  stream >> S >> temp;
109  std::vector<double> pos(S);
110  for( unsigned i = 0 ; i < S ; ++i )
111  {
112  stream >> pos[i];
113  }
114  stream >> std::ws;
115  //Read in the haplotypes until the next // and the stream is still ok
116  std::vector<std::string> haps;
117  while( !stream.eof() && char(stream.peek()) != '/' ) {
118  temp.resize(S);
119  stream.read( &temp[0], std::streamsize(S*sizeof(char)) );
120  stream >> std::ws;
121  haps.emplace_back(std::move(temp));
122  }
123  this->assign( std::move(pos),
124  std::move(haps) );
125  return stream;
126  }
127 
128  std::ostream & SimData::print(std::ostream &o) const
132  {
133  o << "//\n";
134  o << "segsites: " << (*this).numsites() << '\n';
135  if (this->numsites()>0)
136  {
137  o << "positions:";
138  for(unsigned i = 0 ; i < (*this).numsites() ; ++i)
139  {
140  o <<" "<< (*this).position(i);
141  }
142  }
143  o<<'\n';
144  for(unsigned i = 0 ; i < (*this).size() ; ++i)
145  {
146  if (i < (*this).size() - 1)
147  o << (*this)[i] << '\n';
148  else
149  o << (*this)[i];
150  }
151  return o;
152  }
153 
154  int SimData::fromfile( FILE * openedfile )
163  {
164  char ch;
165  int rv; //return value from fscanf
166  while(1)
167  {
168  rv = fscanf(openedfile,"%c",&ch);
169  if (rv == EOF) return rv;
170  if (ch == ':')
171  break;
172  }
173  unsigned ss;
174  rv = fscanf(openedfile,"%u",&ss);
175  if (rv == EOF) return rv;
176 
177  std::vector<double> _positions;
178  std::vector<std::string> _data;
179 
180  if (ss > 0)
181  {
182  _positions.resize(ss);
183  while(1)
184  {
185  rv=fscanf(openedfile,"%c",&ch);
186  if (rv == EOF) return rv;
187  if (ch == ':')
188  break;
189  }
190  for (unsigned i = 0; i < ss; ++i)
191  {
192  rv=fscanf(openedfile,"%lf",&_positions[i]);
193  if (rv == EOF) return rv;
194  }
195  char *seq = new char[ss+2];
196  while(1)
197  {
198  rv=fscanf(openedfile,"%s",seq);
199  if (rv == EOF)
200  {
201  //this is a special case:
202  //EOF while reading data
203  //means data have been read,
204  //so we don't return EOF
205  rv=1;
206  break;
207  }
208  else if ( strcmp(seq,"//") == 0)
209  {
210  break;
211  }
212  else
213  {
214  _data.push_back( std::string(seq) );
215  }
216  }
217  delete [] seq;
218  }
219  else if (ss == 0)
220  {
221  _positions.resize(0);
222  _data.resize(0);
223  }
224  //assign data into base class
225  this->assign(std::move(_positions),
226  std::move(_data));
227  return rv;
228  }
229 }
STL namespace.
The namespace in which this library resides.
Declaration of Sequence::SimData, a class representing polymorphism data from coalescent simulations ...
const_iterator end() const