ProteoWizard
AminoAcidTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2006 Louis Warschaw Prostate Cancer Center
8// Cedars Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
25#include "AminoAcid.hpp"
26#include <cstring>
28#include "boost/thread/thread.hpp"
29#include "boost/thread/barrier.hpp"
30
31
32using namespace pwiz::util;
33using namespace pwiz::proteome;
34using namespace pwiz::chemistry;
35using namespace pwiz::chemistry::Element;
36using namespace pwiz::proteome::AminoAcid;
37
38
39ostream* os_ = 0;
40
41
46
47
48void printRecord(ostream* os, const AminoAcid::Info::Record& record)
49{
50 if (!os) return;
51
52 *os << record.symbol << ": "
53 << setw(14) << record.name << " "
54 << setw(11) << record.formula << " "
55 << setprecision(3)
56 << setw(5) << record.abundance << " "
57 << fixed << setprecision(6)
58 << setw(10) << record.formula.monoisotopicMass() << " "
59 << setw(10) << record.formula.molecularWeight() << " "
60 << setw(10) << record.residueFormula.monoisotopicMass() << " "
61 << setw(10) << record.residueFormula.molecularWeight() << endl;
62}
63
64
66{
67 double monoMass;
68 double avgMass;
69 char symbol;
70};
71
72// masses copied from http://www.unimod.org/xml/unimod.xml
74{
75 { 71.0371140, 71.07790, 'A' }, // Alanine
76 { 156.101111, 156.1857, 'R' }, // Arginine
77 { 114.042927, 114.1026, 'N' }, // Asparagine
78 { 115.026943, 115.0874, 'D' }, // Aspartic acid
79 { 103.009185, 103.1429, 'C' }, // Cysteine
80 { 129.042593, 129.1140, 'E' }, // Glutamic acid
81 { 128.058578, 128.1292, 'Q' }, // Glutamine
82 { 57.0214640, 57.05130, 'G' }, // Glycine
83 { 137.058912, 137.1393, 'H' }, // Histidine
84 { 113.084064, 113.1576, 'I' }, // Isoleucine
85 { 113.084064, 113.1576, 'L' }, // Leucine
86 { 128.094963, 128.1723, 'K' }, // Lysine
87 { 131.040485, 131.1961, 'M' }, // Methionine
88 { 147.068414, 147.1739, 'F' }, // Phenylalanine
89 { 97.0527640, 97.11520, 'P' }, // Proline
90 { 87.0320280, 87.07730, 'S' }, // Serine
91 { 150.953636, 150.0379, 'U' }, // Selenocysteine
92 { 101.047679, 101.1039, 'T' }, // Threonine
93 { 186.079313, 186.2099, 'W' }, // Tryptophan
94 { 163.063329, 163.1733, 'Y' }, // Tyrosine
95 { 99.0684140, 99.13110, 'V' }, // Valine
96 { 114.042927, 114.1026, 'B' }, // AspX
97 { 128.058578, 128.1292, 'Z' }, // GlutX
98 { 114.091900, 114.1674, 'X' } // Unknown (Averagine)
99};
100
101
102void test()
103{
104 // get a copy of all the records
105
106 vector<AminoAcid::Info::Record> records;
107
108 for (char symbol='A'; symbol<='Z'; symbol++)
109 {
110 try
111 {
112 const AminoAcid::Info::Record& record = AminoAcid::Info::record(symbol);
113 records.push_back(record);
114 }
115 catch (exception&)
116 {}
117 }
118
119 for (vector<AminoAcid::Info::Record>::iterator it=records.begin(); it!=records.end(); ++it)
120 printRecord(os_, *it);
121
122 unit_assert(AminoAcid::Info::record(Alanine).residueFormula[C] == 3);
123 unit_assert(AminoAcid::Info::record(Alanine).residueFormula[H] == 5);
124 unit_assert(AminoAcid::Info::record(Alanine).residueFormula[N] == 1);
125 unit_assert(AminoAcid::Info::record(Alanine).residueFormula[O] == 1);
126 unit_assert(AminoAcid::Info::record(Alanine).residueFormula[S] == 0);
127
133
135
136 // test single amino acids
137 for (int i=0; i < 22; ++i) // skip X for now
138 {
141 unit_assert_equal(residueFormula.monoisotopicMass(), aa.monoMass, 0.00001);
142 unit_assert_equal(residueFormula.molecularWeight(), aa.avgMass, 0.0001);
143 //set<char> mmNames = mm2n.getNames(aa.monoMass, EPSILON);
144 //set<char> amNames = am2n.getNames(aa.avgMass, EPSILON);
145 //unit_assert(mmNames.count(aa.symbol) > 0);
146 //unit_assert(amNames.count(aa.symbol) > 0);
147 }
148
149
150 // compute some averages
151
152 double averageMonoisotopicMass = 0;
153 double averageC = 0;
154 double averageH = 0;
155 double averageN = 0;
156 double averageO = 0;
157 double averageS = 0;
158
159 for (vector<AminoAcid::Info::Record>::iterator it=records.begin(); it!=records.end(); ++it)
160 {
161 const AminoAcid::Info::Record& record = *it;
162
163 Formula residueFormula = record.residueFormula;
164 averageMonoisotopicMass += residueFormula.monoisotopicMass() * record.abundance;
165 averageC += residueFormula[C] * record.abundance;
166 averageH += residueFormula[H] * record.abundance;
167 averageN += residueFormula[N] * record.abundance;
168 averageO += residueFormula[O] * record.abundance;
169 averageS += residueFormula[S] * record.abundance;
170 }
171
172 if (os_) *os_ << setprecision(8) << endl;
173 if (os_) *os_ << "average residue C: " << averageC << endl;
174 if (os_) *os_ << "average residue H: " << averageH << endl;
175 if (os_) *os_ << "average residue N: " << averageN << endl;
176 if (os_) *os_ << "average residue O: " << averageO << endl;
177 if (os_) *os_ << "average residue S: " << averageS << endl;
178 if (os_) *os_ << endl;
179
180 if (os_) *os_ << "average monoisotopic mass: " << averageMonoisotopicMass << endl;
181 double averageResidueMass = averageMonoisotopicMass;
182 if (os_) *os_ << "average residue mass: " << averageResidueMass << endl << endl;
183
184 // sort by monoisotopic mass and print again
185 sort(records.begin(), records.end(), hasLowerMass);
186 for (vector<AminoAcid::Info::Record>::iterator it=records.begin(); it!=records.end(); ++it)
187 printRecord(os_, *it);
188}
189
190
191void testThreadSafetyWorker(boost::barrier* testBarrier)
192{
193 testBarrier->wait(); // wait until all threads have started
194
195 try
196 {
197 test();
198 }
199 catch (exception& e)
200 {
201 cerr << "Exception in worker thread: " << e.what() << endl;
202 }
203 catch (...)
204 {
205 cerr << "Unhandled exception in worker thread." << endl;
206 }
207}
208
209void testThreadSafety(const int& testThreadCount)
210{
211 boost::barrier testBarrier(testThreadCount);
212 boost::thread_group testThreadGroup;
213 for (int i=0; i < testThreadCount; ++i)
214 testThreadGroup.add_thread(new boost::thread(&testThreadSafetyWorker, &testBarrier));
215 testThreadGroup.join_all();
216}
217
218
219int main(int argc, char* argv[])
220{
221 TEST_PROLOG(argc, argv)
222
223 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
224 if (os_) *os_ << "AminoAcidTest\n";
225
226 try
227 {
228 //testThreadSafety(1); // does not test thread-safety of singleton initialization
233 }
234 catch (exception& e)
235 {
236 TEST_FAILED(e.what())
237 }
238 catch (...)
239 {
240 TEST_FAILED("Caught unknown exception.")
241 }
242
244}
245
246
Selenocysteine
Definition AminoAcid.hpp:65
Alanine
Definition AminoAcid.hpp:45
int main(int argc, char *argv[])
void testThreadSafetyWorker(boost::barrier *testBarrier)
bool hasLowerMass(const AminoAcid::Info::Record &a, const AminoAcid::Info::Record &b)
TestAminoAcid testAminoAcids[]
void printRecord(ostream *os, const AminoAcid::Info::Record &record)
ostream * os_
void test()
void testThreadSafety()
Se
Definition Chemistry.hpp:84
N
Definition Chemistry.hpp:80
C
Definition Chemistry.hpp:80
S
Definition Chemistry.hpp:80
O
Definition Chemistry.hpp:80
H
Definition Chemistry.hpp:80
class to represent a chemical formula
double monoisotopicMass() const
double molecularWeight() const
scope for declarations related to elements
Definition Chemistry.hpp:74
PWIZ_API_DECL const Record & record(Type type)
returns the amino acid's Record by type
scope for types related to amino acids
Definition AminoAcid.hpp:39
struct for holding data for a single amino acid
Definition AminoAcid.hpp:79
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_equal(x, y, epsilon)
Definition unit.hpp:99
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175