-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTorsadePredictMethods.cpp
More file actions
245 lines (215 loc) · 8.87 KB
/
Copy pathTorsadePredictMethods.cpp
File metadata and controls
245 lines (215 loc) · 8.87 KB
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
/*
Copyright (c) 2005-2025, University of Oxford.
All rights reserved.
University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.
This file is part of Chaste.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Oxford nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TorsadePredictMethods.hpp"
#include "CardiovascRes2011DataStructure.hpp"
#include "FileFinder.hpp"
#include "SetupModel.hpp"
std::string TorsadePredictMethods::PrintArguments()
{
std::string message = "TorsadePredict::Please provide these inputs:\n";
message += PrintCommonArguments();
return message;
}
TorsadePredictMethods::TorsadePredictMethods()
: ApPredictMethods()
{
mProgramName = "Torsade PreDiCT";
mOutputFolder = "TorsadePredict_output";
}
void TorsadePredictMethods::Run()
{
// Make and clean the above directories.
mpFileHandler.reset(new OutputFileHandler(mOutputFolder));
SetupModel setup(this->mHertz, 5u); // Hardcoded to Grandi model.
mpModel = setup.GetModel();
CommonRunMethod();
assert(mComplete);
// After calling the normal methods
// Additional steps for Torsade runs included here.
MakeTorsadePredictions();
// This is in a separate method to the above so we can test the above on its own...
WriteTorsadeResultsToFile();
}
void TorsadePredictMethods::MakeTorsadePredictions()
{
if (mApd90s.size() == 0)
{
EXCEPTION("APDs do not appear to have been recorded.");
}
mTorsadePredictions.reserve(mApd90s.size());
// Work out the Measure
std::vector<double> largest_percent_change;
largest_percent_change.push_back(0.0); // First is always control
for (unsigned i = 1; i < mApd90s.size(); i++)
{
double percent = 100 * (mApd90s[i] - mApd90s[0]) / mApd90s[0];
largest_percent_change.push_back(percent);
}
// Second pass to check for largest +ve effect at low dose...
// This makes the measure "Largest Effect Dose" rather than just "dose".
for (unsigned i = 1; i < mApd90s.size(); i++)
{
// Check over lower doses
for (unsigned j = 0; j < i; j++)
{
// If the lower dose made the AP longer than control (and longer than this dose) then replace this APD with that one.
if (largest_percent_change[j] > 0 && largest_percent_change[j] > largest_percent_change[i])
{
largest_percent_change[i] = largest_percent_change[j];
}
}
}
// Perform the LDA
LinearDiscriminantAnalysis lda = LoadLdaFromDrugData();
for (unsigned i = 0; i < largest_percent_change.size(); i++)
{
vector<double> test_point(1);
test_point(0) = largest_percent_change[i];
if (std::isnan(test_point(0)))
{
// Dire prognostications shall befall thee. (an error occurred in APD calculation. Call it dangerous)
mTorsadePredictions.push_back(2);
}
else
{
mTorsadePredictions.push_back(lda.ClassifyThisPoint(test_point) + 2u); // We add two because our redfern categories start at 2 not 0.
}
}
}
void TorsadePredictMethods::WriteTorsadeResultsToFile()
{
assert(mpFileHandler);
// Open an output file for the Torsade results
out_stream torsade_results_file = mpFileHandler->OpenOutputFile("tdp_results.html");
*torsade_results_file << "<html>\n<head><title>Torsade preDiCT Results</title></head>\n";
*torsade_results_file << "<STYLE TYPE=\"text/css\">\n<!--\nTD{font-size: 12px;}\n--->\n</STYLE>\n";
*torsade_results_file << "<body>\n";
*torsade_results_file << "<table width=\"60%\" style=\"background-color:white\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">\n";
*torsade_results_file << "<tr><td>Concentration (uM)</td><td>APD90 (ms)</td><td>Risk Category Prediction</td></tr>\n"; // Header line
std::vector<std::string> colours;
colours.push_back("Red");
colours.push_back("Orange");
colours.push_back("Limegreen");
colours.push_back("Limegreen");
for (unsigned i = 0; i < mTorsadePredictions.size(); i++)
{
if (!mSuppressOutput)
std::cout << "Conc = " << mConcs[i] << "uM, APD90 = " << mApd90s[i] << "ms, risk prediction = " << mTorsadePredictions[i] << "\n"; // << std::flush;
*torsade_results_file << "<tr style=\"background-color:" << colours[mTorsadePredictions[i] - 2] << "\"><td>" << mConcs[i] << "</td><td>" << mApd90s[i] << "</td><td>" << mTorsadePredictions[i] << "</td></tr>\n";
}
*torsade_results_file << "</table>\n</body>\n</html>\n";
torsade_results_file->close();
}
LinearDiscriminantAnalysis TorsadePredictMethods::LoadLdaFromDrugData()
{
// We have to look for the drug data in a couple of places because the executable won't have the Chaste source.
const std::string drug_data_file = "paper_drug_data.dat";
boost::shared_ptr<FileFinder> p_file_finder = boost::shared_ptr<FileFinder>(new FileFinder("projects/ApPredict/test/data/" + drug_data_file, RelativeTo::ChasteSourceRoot));
if (!p_file_finder->Exists())
{
p_file_finder->SetPath(drug_data_file, RelativeTo::CWD);
if (!p_file_finder->Exists())
{
EXCEPTION("The file \"" << drug_data_file << "\" should be in the current working directory and is missing.");
}
}
CardiovascRes2011DataStructure drug_data(*p_file_finder);
matrix<double> cat1and2(1, 1);
matrix<double> cat3(1, 1);
matrix<double> cat4(1, 1);
matrix<double> cat5(1, 1);
cat1and2(0, 0) = cat3(0, 0) = cat4(0, 0) = cat5(0, 0) = DBL_MAX;
// Generate structures for training data...
for (unsigned i = 0; i < drug_data.GetNumDrugs(); i++)
{
double grandi_measure;
try
{
grandi_measure = drug_data.GetGrandiMeasure(i);
}
catch (Exception& e)
{
assert(e.GetShortMessage() == "No data available on Grandi measure for this drug.");
continue;
}
if (drug_data.GetDrugName(i) == "Ranolazine")
{ // We aren't currently using Ranolazine for the training.
continue;
}
unsigned redfern = drug_data.GetRedfernCategory(i);
if (redfern == 1 || redfern == 2)
{
if (cat1and2(0, 0) != DBL_MAX)
{
cat1and2.resize(cat1and2.size1() + 1, 1);
}
cat1and2(cat1and2.size1() - 1, 0) = grandi_measure;
}
else if (redfern == 3)
{
if (cat3(0, 0) != DBL_MAX)
{
cat3.resize(cat3.size1() + 1, 1);
}
cat3(cat3.size1() - 1, 0) = grandi_measure;
}
else if (redfern == 4)
{
if (cat4(0, 0) != DBL_MAX)
{
cat4.resize(cat4.size1() + 1, 1);
}
cat4(cat4.size1() - 1, 0) = grandi_measure;
}
else
{
assert(redfern == 5);
if (cat5(0, 0) != DBL_MAX)
{
cat5.resize(cat5.size1() + 1, 1);
}
cat5(cat5.size1() - 1, 0) = grandi_measure;
}
}
std::vector<matrix<double> > training;
training.push_back(cat1and2);
training.push_back(cat3);
training.push_back(cat4);
training.push_back(cat5);
LinearDiscriminantAnalysis lda(training);
return lda;
}
std::vector<unsigned> TorsadePredictMethods::GetTorsadePredictions(void)
{
if (!mComplete)
{
EXCEPTION("Simulation has not been run - check arguments.");
}
return mTorsadePredictions;
}