1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
//
// $Id$
//
//
// Original author: Darren Kessner <darren@proteowizard.org>
//
// Copyright 2006 Louis Warschaw Prostate Cancer Center
// Cedars Sinai Medical Center, Los Angeles, California 90048
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#define PWIZ_SOURCE
#include "Chemistry.hpp"
#include "ChemistryData.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include "pwiz/utility/misc/Singleton.hpp"
namespace pwiz {
namespace chemistry {
PWIZ_API_DECL bool MassAbundance::operator==(const MassAbundance& that) const
{
return this->mass==that.mass && this->abundance==that.abundance;
}
PWIZ_API_DECL bool MassAbundance::operator!=(const MassAbundance& that) const
{
return !operator==(that);
}
PWIZ_API_DECL ostream& operator<<(ostream& os, const MassAbundance& ma)
{
os << "<" << ma.mass << ", " << ma.abundance << ">";
return os;
}
PWIZ_API_DECL ostream& operator<<(ostream& os, const MassDistribution& md)
{
copy(md.begin(), md.end(), ostream_iterator<MassAbundance>(os, "\n"));
return os;
}
namespace Element {
PWIZ_API_DECL std::ostream& operator<<(std::ostream& os, Type type)
{
os << Info::record(type).symbol;
return os;
}
class RecordData : public boost::singleton<RecordData>
{
public:
RecordData(boost::restricted)
{
// iterate through the ChemistryData array and put it in our own data structure
detail::Element* it = detail::elements();
detail::Element* end = detail::elements() + detail::elementsSize();
data_.resize(detail::elementsSize());
for (; it!=end; ++it)
{
Info::Record record;
record.type = it->type;
record.symbol = it->symbol;
record.atomicNumber = it->atomicNumber;
record.atomicWeight = it->atomicWeight;
record.monoisotope.abundance = 0;
for (detail::Isotope* p=it->isotopes; p<it->isotopes+it->isotopesSize; ++p)
{
record.isotopes.push_back(MassAbundance(p->mass, p->abundance));
if (p->abundance > record.monoisotope.abundance)
record.monoisotope = record.isotopes.back();
}
data_[it->type] = record;
}
}
inline const Info::Record& record(Type type) const
{
if (data_.size() <= size_t(type))
throw runtime_error("[chemistry::Element::Info::Impl::record()] Record not found.");
return data_[type];
}
private:
vector<Info::Record> data_;
};
// implementation of element symbol->type (text->enum) mapping
namespace {
struct Text2EnumMap : public map<string, Element::Type>,
public boost::singleton<Text2EnumMap>
{
Text2EnumMap(boost::restricted)
{
for (detail::Element* it = detail::elements();
it != detail::elements() + detail::elementsSize();
++it)
{
insert(make_pair(it->symbol, it->type));
if (it->synonym)
insert(make_pair(it->synonym, it->type));
}
}
};
Element::Type text2enum(const string& text)
{
Text2EnumMap::lease text2EnumMap;
Text2EnumMap::const_iterator itr = text2EnumMap->find(text);
if (itr == text2EnumMap->end())
throw runtime_error(("[chemistry::text2enum()] Error translating symbol " + text).c_str());
return itr->second;
}
} // namespace
PWIZ_API_DECL const Info::Record& Info::record(Type type) {return RecordData::instance->record(type);}
PWIZ_API_DECL const Info::Record& Info::record(const string& symbol) {return record(text2enum(symbol));}
PWIZ_API_DECL ostream& operator<<(ostream& os, const Info::Record& r)
{
cout << r.symbol << " " << r.atomicNumber << " " << r.atomicWeight << " " << r.monoisotope << " ";
copy(r.isotopes.begin(), r.isotopes.end(), ostream_iterator<MassAbundance>(cout, " "));
return os;
}
} // namespace Element
// Formula implementation
class Formula::Impl
{
public:
Impl(const string& formula);
inline void calculateMasses()
{
if (dirty)
{
dirty = false;
monoMass = avgMass = 0;
for (size_t i=0; i <= size_t(Element::_15N); ++i)
{
int count = CHONSP_data[i];
if (count != 0)
{
const Element::Info::Record& r = Element::Info::record(Element::Type(i));
if (!r.isotopes.empty())
monoMass += r.monoisotope.mass * count;
avgMass += r.atomicWeight * count;
}
}
vector<Data::iterator> zeroElements;
for (Data::iterator it=data.begin(); it!=data.end(); ++it)
{
if (it->second == 0)
zeroElements.push_back(it);
else
{
const Element::Info::Record& r = Element::Info::record(it->first);
if (!r.isotopes.empty())
monoMass += r.monoisotope.mass * it->second;
avgMass += r.atomicWeight * it->second;
}
}
for (size_t i=0; i < zeroElements.size(); ++i)
data.erase(zeroElements[i]);
}
}
typedef map<Element::Type, int> Data;
Data data;
vector<int> CHONSP_data;
double monoMass;
double avgMass;
bool dirty; // true if masses need updating
};
Formula::Impl::Impl(const string& formula)
: monoMass(0), avgMass(0), dirty(false)
{
CHONSP_data.resize(size_t(Element::_15N)+1, 0);
if (formula.empty())
return;
// parse the formula string
// this implementation is correct, but should be done with a
// regular expression library if performance becomes an issue
const string& whitespace_ = " \t\n\r";
const string& digits_ = "-0123456789";
const string& symbolLeads_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
const string& lowers_ = "abcdefghijklmnopqrstuvwxyz";
string::size_type index = 0;
while (index < formula.size() && index != string::npos)
{
string::size_type indexTypeBegin = formula.find_first_of(symbolLeads_, index);
if (indexTypeBegin == string::npos)
throw runtime_error("[Formula::Impl::Impl()] Invalid formula: " + formula);
string::size_type indexTypeEnd = indexTypeBegin;
if (formula[indexTypeBegin] == '_')
{
indexTypeEnd = formula.find_first_of(symbolLeads_, indexTypeBegin+1); // Skip the "2" in _2H
}
// Distinguish "H" and "He" or "Uuu" and "Uub"
indexTypeEnd++;
while (indexTypeEnd < formula.size() && lowers_.find_first_of(formula[indexTypeEnd]) != string::npos)
{
indexTypeEnd++;
}
string symbol = formula.substr(indexTypeBegin, indexTypeEnd - indexTypeBegin);
string::size_type indexNextTypeBegin = (indexTypeEnd == formula.size()) ? string::npos :
formula.find_first_of(symbolLeads_, indexTypeEnd);
string::size_type indexCountBegin = (indexTypeEnd == formula.size()) ? string::npos :
formula.find_first_of(digits_, indexTypeEnd);
string::size_type indexCountEnd;
int count;
if (indexCountBegin == string::npos ||
(indexNextTypeBegin != string::npos && indexNextTypeBegin < indexCountBegin))
{
count = 1;
indexCountEnd = indexTypeEnd; // Start next symbol read from here
}
else
{
indexCountEnd = formula.find_first_not_of(digits_, indexCountBegin);
if (indexCountEnd == string::npos)
{
indexCountEnd = formula.size();
}
try
{
count = lexical_cast<int>(formula.substr(indexCountBegin, indexCountEnd-indexCountBegin));
}
catch(bad_lexical_cast&)
{
throw runtime_error("[Formula::Impl::Impl()] Invalid count in formula: " + formula);
}
}
Element::Type type = Element::text2enum(symbol);
if (type > Element::_15N)
data[type] = count;
else
CHONSP_data[size_t(type)] = count;
index = formula.find_first_not_of(whitespace_, indexCountEnd);
const Element::Info::Record& r = Element::Info::record(type);
if (!r.isotopes.empty())
monoMass += r.monoisotope.mass * count;
avgMass += r.atomicWeight * count;
}
}
PWIZ_API_DECL Formula::Formula(const string& formula)
: impl_(new Impl(formula))
{}
PWIZ_API_DECL Formula::Formula(const char* formula)
: impl_(new Impl(formula))
{}
PWIZ_API_DECL Formula::Formula(const Formula& formula)
: impl_(new Impl(*formula.impl_))
{}
PWIZ_API_DECL const Formula& Formula::operator=(const Formula& formula)
{
*impl_ = *formula.impl_;
return *this;
}
PWIZ_API_DECL Formula::~Formula()
{} // auto destruction of impl_
PWIZ_API_DECL double Formula::monoisotopicMass() const
{
impl_->calculateMasses();
return impl_->monoMass;
}
PWIZ_API_DECL double Formula::molecularWeight() const
{
impl_->calculateMasses();
return impl_->avgMass;
}
PWIZ_API_DECL string Formula::formula() const
{
// collect a term for each element
vector<string> terms;
for (size_t i=0; i <= size_t(Element::_15N); ++i)
{
int count = impl_->CHONSP_data[i];
ostringstream term;
if (count != 0)
term << Element::Type(i) << count;
terms.push_back(term.str());
}
for (Impl::Data::const_iterator it=impl_->data.begin(); it!=impl_->data.end(); ++it)
{
ostringstream term;
if (it->second != 0)
term << it->first << it->second;
terms.push_back(term.str());
}
// sort alphabetically and return the concatenation
sort(terms.begin(), terms.end());
return accumulate(terms.begin(), terms.end(), string());
}
PWIZ_API_DECL int Formula::operator[](Element::Type e) const
{
if (e > Element::_15N)
return impl_->data[e];
else
return impl_->CHONSP_data[e];
}
PWIZ_API_DECL int& Formula::operator[](Element::Type e)
{
impl_->dirty = true; // worst-case
if (e > Element::_15N)
return impl_->data[e];
else
return impl_->CHONSP_data[e];
}
PWIZ_API_DECL map<Element::Type, int> Formula::data() const
{
map<Element::Type, int> dataCopy(impl_->data);
for (size_t i=0; i <= size_t(Element::_15N); ++i)
{
int count = impl_->CHONSP_data[i];
if (count != 0)
dataCopy[Element::Type(i)] = count;
}
return dataCopy;
}
PWIZ_API_DECL Formula& Formula::operator+=(const Formula& that)
{
impl_->CHONSP_data[0] += that.impl_->CHONSP_data[0];
impl_->CHONSP_data[1] += that.impl_->CHONSP_data[1];
impl_->CHONSP_data[2] += that.impl_->CHONSP_data[2];
impl_->CHONSP_data[3] += that.impl_->CHONSP_data[3];
impl_->CHONSP_data[4] += that.impl_->CHONSP_data[4];
impl_->CHONSP_data[5] += that.impl_->CHONSP_data[5];
impl_->CHONSP_data[6] += that.impl_->CHONSP_data[6];
impl_->CHONSP_data[7] += that.impl_->CHONSP_data[7];
impl_->CHONSP_data[8] += that.impl_->CHONSP_data[8];
impl_->CHONSP_data[9] += that.impl_->CHONSP_data[9];
for (Map::const_iterator it=that.impl_->data.begin(); it!=that.impl_->data.end(); ++it)
impl_->data[it->first] += it->second;
impl_->dirty = true;
return *this;
}
PWIZ_API_DECL Formula& Formula::operator-=(const Formula& that)
{
impl_->CHONSP_data[0] -= that.impl_->CHONSP_data[0];
impl_->CHONSP_data[1] -= that.impl_->CHONSP_data[1];
impl_->CHONSP_data[2] -= that.impl_->CHONSP_data[2];
impl_->CHONSP_data[3] -= that.impl_->CHONSP_data[3];
impl_->CHONSP_data[4] -= that.impl_->CHONSP_data[4];
impl_->CHONSP_data[5] -= that.impl_->CHONSP_data[5];
impl_->CHONSP_data[6] -= that.impl_->CHONSP_data[6];
impl_->CHONSP_data[7] -= that.impl_->CHONSP_data[7];
impl_->CHONSP_data[8] -= that.impl_->CHONSP_data[8];
impl_->CHONSP_data[9] -= that.impl_->CHONSP_data[9];
for (Map::const_iterator it=that.impl_->data.begin(); it!=that.impl_->data.end(); ++it)
impl_->data[it->first] -= it->second;
impl_->dirty = true;
return *this;
}
PWIZ_API_DECL Formula& Formula::operator*=(int scalar)
{
impl_->CHONSP_data[0] *= scalar;
impl_->CHONSP_data[1] *= scalar;
impl_->CHONSP_data[2] *= scalar;
impl_->CHONSP_data[3] *= scalar;
impl_->CHONSP_data[4] *= scalar;
impl_->CHONSP_data[5] *= scalar;
impl_->CHONSP_data[6] *= scalar;
impl_->CHONSP_data[7] *= scalar;
impl_->CHONSP_data[8] *= scalar;
impl_->CHONSP_data[9] *= scalar;
for (Map::iterator it=impl_->data.begin(); it!=impl_->data.end(); ++it)
it->second *= scalar;
impl_->dirty = true;
return *this;
}
PWIZ_API_DECL bool Formula::operator==(const Formula& that) const
{
if (impl_->CHONSP_data[0] != that.impl_->CHONSP_data[0] ||
impl_->CHONSP_data[1] != that.impl_->CHONSP_data[1] ||
impl_->CHONSP_data[2] != that.impl_->CHONSP_data[2] ||
impl_->CHONSP_data[3] != that.impl_->CHONSP_data[3] ||
impl_->CHONSP_data[4] != that.impl_->CHONSP_data[4] ||
impl_->CHONSP_data[5] != that.impl_->CHONSP_data[5] ||
impl_->CHONSP_data[6] != that.impl_->CHONSP_data[6] ||
impl_->CHONSP_data[7] != that.impl_->CHONSP_data[7] ||
impl_->CHONSP_data[8] != that.impl_->CHONSP_data[8] ||
impl_->CHONSP_data[9] != that.impl_->CHONSP_data[9])
return false;
impl_->calculateMasses(); // will remove zero-count elements from data map
that.impl_->calculateMasses();
if (impl_->data.size() != that.impl_->data.size())
return false;
Map::const_iterator itr, thatItr;
for (itr = impl_->data.begin(), thatItr = that.impl_->data.begin();
itr != impl_->data.end();
++itr, ++thatItr)
// equal maps are pairwise equal for both elements of the pair
if (itr->first != thatItr->first || itr->second != thatItr->second)
return false;
return true;
}
PWIZ_API_DECL bool Formula::operator!=(const Formula& that) const
{
return !(*this == that);
}
PWIZ_API_DECL Formula operator+(const Formula& a, const Formula& b)
{
Formula result(a);
result += b;
return result;
}
PWIZ_API_DECL Formula operator-(const Formula& a, const Formula& b)
{
Formula result(a);
result -= b;
return result;
}
PWIZ_API_DECL Formula operator*(const Formula& a, int scalar)
{
Formula result(a);
result *= scalar;
return result;
}
PWIZ_API_DECL Formula operator*(int scalar, const Formula& a)
{
Formula result(a);
result *= scalar;
return result;
}
PWIZ_API_DECL ostream& operator<<(ostream& os, const Formula& formula)
{
os << formula.formula();
return os;
}
} // namespace chemistry
} // namespace pwiz
|