45 #include <Bpp/Text/TextTools.h> 46 #include "../Likelihood/PairedSiteLikelihoods.h" 61 istringstream iss (line);
62 size_t nmodels, nsites;
68 vector<vector<double> > loglikelihoods;
69 loglikelihoods.reserve(nmodels);
74 streampos pos (is.tellg());
76 if (line.find(
"\t") != string::npos)
78 else if (line.find(
" ") != string::npos)
85 while (getline(is, line))
87 size_t delim_pos (line.find(delim));
88 if (delim_pos == string::npos)
91 msg <<
"IOTreepuzzlePairedSiteLikelihoods::read: Couldn't find delimiter. The beggining of the line was : " 92 << endl << line.substr(0, 100);
93 throw Exception(msg.str());
97 names.push_back( TextTools::removeSurroundingWhiteSpaces(line.substr(0, delim_pos)) );
100 loglikelihoods.push_back(vector<double>());
101 loglikelihoods.back().reserve(nsites);
103 istringstream liks_stream ( line.substr(delim_pos) );
105 while (liks_stream >> currllik)
106 loglikelihoods.back().push_back(currllik);
109 if (loglikelihoods.back().size() != nsites)
112 oss <<
"IOTreepuzzlePairedSiteLikelihoods::read: Model '" << names.back()
113 <<
"' does not have the correct number of sites. (" 114 << loglikelihoods.back().size() <<
", expected: " << nsites <<
")";
115 throw Exception(oss.str());
120 if (loglikelihoods.size() != nmodels)
121 throw Exception(
"IOTreepuzzlePairedSiteLikelihoods::read: Wrong number of models.");
133 ifstream iF (path.c_str());
144 throw Exception(
"Writing an empty PairedSiteLikelihoods object to file.");
157 for (vector<double>::const_iterator sitelik = psl.
getLikelihoods().at(i).begin();
164 os << *sitelik <<
" ";
170 else if (delim ==
" ")
174 vector<size_t> name_sizes;
175 for (vector<string>::const_iterator name = psl.
getModelNames().begin();
179 name_sizes.push_back(name->size());
181 size_t names_field_size = *max_element(name_sizes.begin(), name_sizes.end()) + 2;
188 while (name.size() != names_field_size)
193 for (vector<double>::const_iterator sitelik = psl.
getLikelihoods().at(i).begin();
200 os << *sitelik <<
" ";
209 msg <<
"IOTreepuzzlePairedSiteLikelihoods::write: Unknown field delimiter " 210 <<
"\"" << delim <<
"\".";
211 os << msg.str() << endl;
212 throw Exception(msg.str());
222 ofstream oF (path.c_str());
223 IOTreepuzzlePairedSiteLikelihoods::write(psl, oF, delim);
230 vector<double> IOPhymlPairedSiteLikelihoods::read(std::istream& is)
throw (Exception)
232 vector<double> loglikelihoods;
237 string expected (
"Note : P(D|M) is the probability of site D given the model M (i.e., the site likelihood)");
238 if (str.find(expected) == string::npos)
241 msg <<
"IOPhymlPairedSiteLikelihoods::read: The first line was expected to be :" 243 <<
"and was :" << endl
245 throw Exception(msg.str());
249 for (
int i = 0; i < 6; ++i)
254 while (getline(is, str))
257 istringstream ss (str);
261 loglikelihoods.push_back(log(lik));
264 return loglikelihoods;
270 vector<double> IOPhymlPairedSiteLikelihoods::read(
const std::string& path)
throw (Exception)
272 ifstream iF (path.c_str());
273 vector<double> loglikelihoods(IOPhymlPairedSiteLikelihoods::read(iF));
274 return loglikelihoods;
const std::vector< std::string > & getModelNames() const
A container for paired-site likelihoods (likelihoods over the same sites for different models...
std::size_t getNumberOfSites() const
const std::vector< std::vector< double > > & getLikelihoods() const
size_t getNumberOfModels() const
Get the number of models in the container.