-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTactioTest.java
494 lines (435 loc) · 19.3 KB
/
TactioTest.java
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
//copyright Selima Cheriff, Rohit Garudadri, Isabel Hadziomerovic, Alex Gass.
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class TactioTest {
public static String token = "";
public static void main(String[] args) throws JSONException, IOException {
TactioTest b = new TactioTest();
token = b.getToken();
System.out.println(token);
List<String> patientIds = b.getPatients();
double i = 0.0;
FileWriter infectionWriter = new FileWriter("infection.csv");
FileWriter obesityWriter = new FileWriter("obesity.csv");
FileWriter diabetesWriter = new FileWriter("diabetehypertension.csv");
FileWriter dyslipWriter = new FileWriter("dyslipidemia.csv");
infectionWriter.append("patient id");
infectionWriter.append("\n");
obesityWriter.append("patient id");
obesityWriter.append("\n");
diabetesWriter.append("patient id");
diabetesWriter.append("\n");
dyslipWriter.append("patient id");
dyslipWriter.append("\n");
for (String patientId: patientIds) {
i++;
if(i>=432){
List<JSONObject> obs = b.getObservations(patientId);
/*a)here the observations for one patient have been gathered
* we are now going to scrape that observation for information that will lead to
* the classification
* b) create patient
* c) classify patient
* d) add patient id to appropriate array list
* */
ArrayList<Double> bmi = b.helperGetValue(obs, "snomed", "60621009");
ArrayList<Double> wbcc = b.helperGetValue(obs, "loinc", "26464-8");
ArrayList<Double> glucoseLevels = b.helperGetValue(obs, "snomed", "434912009");
ArrayList<Double> glucosePostMeal = b.helperGetValue(obs, "snomed", "302788006");
ArrayList<Double> AIC = b.helperGetValue(obs, "snomed", "733829007");
ArrayList<Double> pulse = b.helperGetValue(obs, "snomed", "78564009");
ArrayList<Double> hdl = b.helperGetValue(obs, "snomed", "102737005");
ArrayList<Double> ldl = b.helperGetValue(obs, "snomed", "102739008");
ArrayList<Double> triglyceride = b.helperGetValue(obs, "loinc", "70218-3");
Patient p = new Patient(patientId, bmi,wbcc,glucoseLevels, glucosePostMeal,AIC,pulse,hdl,ldl,triglyceride);
boolean infected = false;
boolean obese = false;
boolean diabetic = false;
boolean dyslipidemiac = false;
infected = checkInfected(p);
obese = checkObese(p);
diabetic = checkDiabetic(p);
dyslipidemiac = checkDyslipidemia(p);
if (infected) {
infectionWriter.append(p.ID);
infectionWriter.append("\n");
}
if (obese) {
obesityWriter.append(p.ID);
obesityWriter.append("\n");
}
if (diabetic) {
diabetesWriter.append(p.ID);
diabetesWriter.append("\n");
}
if (dyslipidemiac) {
dyslipWriter.append(p.ID);
dyslipWriter.append("\n");
}
System.out.println("At patient "+i);
System.out.println("Getting observations " + i/patientIds.size()*100.0 + "% complete ... ");
}
}
infectionWriter.flush();
infectionWriter.close();
obesityWriter.flush();
obesityWriter.close();
diabetesWriter.flush();
diabetesWriter.close();
dyslipWriter.flush();
dyslipWriter.close();
}
public static boolean checkObese(Patient p) {
ArrayList<Double> bmi = p.bmi;
double avgBMI = calcAvg(bmi);
if (avgBMI < 30) {
return false;
}
return true;
}
public static boolean checkInfected(Patient p) {
//this field could be obtained from the patient
ArrayList<Double> WBCcount = p.whitebloodCellCounts;
double average = calcAvg(WBCcount);
double stDev = calcStDev(WBCcount);
//checking criteria for infection
if (WBCcount.size() < 2) {
return false;
}
if (average < 5500) {
return false;
}
if (stDev < 1000) {
return false;
}
//if nothing returned false, the patient is infected
return true;
}
public static boolean checkDiabetic(Patient p) {
//these fields could preferrably just be obtained from the patient, eg. glucose = p.glucose;
ArrayList<Double> glucose = p.glucoseLevels;
ArrayList<Double> glucosePM = p.glucosePostMeal;
ArrayList<Double> A1C = p.AIC;
ArrayList<Double> pulserate = p.pulse;
//checking criteria for diabetics
double avgGlucose = calcAvg(glucose);
if (avgGlucose < 5.5) {
return false;
}
double glucoseStDev = calcStDev(glucose);
if (glucoseStDev > 0.6) {
return false;
}
double avgGlucosePM = calcAvg(glucosePM);
if (avgGlucosePM < 6.9) {
return false;
}
double glucosePMStDev = calcStDev(glucosePM);
if (glucosePMStDev > 0.9) {
return false;
}
double avgA1C = calcAvg(A1C);
if (avgA1C < 6.0) {
return false;
}
double A1CStDev = calcStDev(A1C);
if (A1CStDev > 0.4) {
return false;
}
double avgPulserate= calcAvg(pulserate);
if (avgPulserate < 70.0) {
return false;
}
double pulserateStDev = calcStDev(pulserate);
if (pulserateStDev > 10.0) {
return false;
}
//if nothing returned false then patient is a diabetic
return true;
}
public static boolean checkDyslipidemia(Patient p) {
//fields obtained from the patient
ArrayList<Double> hdl = p.hdl;
ArrayList<Double> ldl = p.ldl;
ArrayList<Double> triglyceride = p.triglyceride;
//checking criteria for dyslipidemia
double avgHdl = calcAvg(hdl);
if (avgHdl < 1.6) {
return false;
}
double hdlStDev = calcStDev(hdl);
if (hdlStDev > 0.15) {
return false;
}
double avgLdl = calcAvg(ldl);
if (avgLdl > 3.1) {
return false;
}
double ldlStDev = calcStDev(ldl);
if (ldlStDev > 0.3) {
return false;
}
double avgTriglyc = calcAvg(triglyceride);
if (avgTriglyc < 1.13) {
return false;
}
double triglycStDev = calcStDev(triglyceride);
if (triglycStDev > 0.1) {
return false;
}
//if nothing returned false, then patient is a dyslipidemiac
return true;
}
public static double calcStDev(ArrayList<Double> values) {
//calculate average of the given values
double average = calcAvg(values);
//calculate the square differences for the values
ArrayList<Double> sqrDiffs = new ArrayList<Double>();
for (double v : values) {
double diff = v - average;
diff = Math.pow(diff, 2);
sqrDiffs.add(diff);
}
//calculate the average square difference
double averageSqrDiff = calcAvg(sqrDiffs);
double stDev = Math.sqrt(averageSqrDiff);
return stDev;
}
public static double calcAvg(ArrayList<Double> values) {
double avg = 0;
for (Double d : values) {
avg += d;
}
avg /= values.size();
return avg;
}
public ArrayList<Double> helperGetValue(List<JSONObject> obs, String standard, String code) throws JSONException{
ArrayList<Double> values= new ArrayList<Double>();
for(int i = 0; i<obs.size(); i++) {
JSONObject resources = obs.get(i).getJSONObject("resource");
JSONArray jsonArray= resources.getJSONObject("code").getJSONArray("coding");
for(int j = 0; j<jsonArray.length(); j++) {
JSONObject jsonObject = jsonArray.getJSONObject(j);
if (jsonObject.getString("system").contains(standard)) {
if (jsonObject.getString("code").contains(code)) {
JSONObject tmp = resources.getJSONObject("valueQuantity");
values.add(Double.parseDouble(tmp.getString("value")));
}
}
}
}
return values;
}
/* public ArrayList<Double> helperGetValueComponent(List<JSONObject> obs, String standard, String code) throws JSONException{
ArrayList<Double> values= new ArrayList<Double>();
for(int i = 0; i<obs.size(); i++) {
JSONObject component = obs.get(i).getJSONObject("resource").optJSONObject("component");
if(component!=null) {
JSONArray jsonArray= component.getJSONObject("code").getJSONArray("coding");
for(int j = 0; j<jsonArray.length(); j++) {
JSONObject jsonObject = jsonArray.getJSONObject(j);
if (jsonObject.getString("system").contains(standard)) {
if (jsonObject.getString("code").contains(code)) {
JSONObject tmp = component.getJSONObject("valueQuantity");
values.add(Double.parseDouble(tmp.getString("value")));
}
}
}
}
}
return values;
}*/
public List<String> getPatients() throws ClientProtocolException, IOException, JSONException {
int pageIndex = 1;
boolean hasNextPage = true;
List<String> pats = new ArrayList<String>();
while (hasNextPage) {
String response = getPatientPage(pageIndex);
pageIndex += 1;
JSONObject respObj = new JSONObject(response);
JSONArray entries = respObj.optJSONArray("entry");
if(entries!=null) {
for(int i = 0; i<entries.length(); i++) {
JSONObject entry = entries.getJSONObject(i);
JSONObject res = entry.getJSONObject("resource");
String id = res.getString("id");
pats.add(id);
}
}
JSONArray links = respObj.getJSONArray("link");
hasNextPage = false;
if(links!=null) {
for(int i = 0; i<links.length(); i++) {
JSONObject link = links.getJSONObject(i);
if (link.getString("relation").equals("next")) {
hasNextPage = true;
}
}
}
}
return pats;
}
public String getPatientPage(int pageNumber) throws ClientProtocolException, IOException {
String url = "https://sandbox86.tactiorpm7000.com/tactio-clinician-api/1.1.4/Patient" +
"?page=" + Integer.toString(pageNumber);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ this.token);
HttpResponse response = client.execute(request);
return readResponse(response);
}
public String readResponse(HttpResponse resp) throws UnsupportedOperationException, IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
line = rd.readLine();
while (line != null) {
result.append(line);
line = rd.readLine();
}
return result.toString();
}
//given a patient id, fetch all the observations
public List<JSONObject> getObservations(String patientId) throws ClientProtocolException, IOException, JSONException {
int pageIndex = 1;
boolean hasNextPage = true;
List<JSONObject> obs = new ArrayList<JSONObject>();
while (hasNextPage) {
String response = getObservationPage(patientId, pageIndex);
pageIndex += 1;
JSONObject respObj = new JSONObject(response);
JSONArray entries = respObj.optJSONArray("entry");
if(entries!=null) {
for(int i = 0; i<entries.length(); i++) {
JSONObject entry = entries.getJSONObject(i);
obs.add(entry);
}
}
JSONArray links = respObj.optJSONArray("link");
if (links != null) {
hasNextPage = false;
for (int i = 0; i < links.length(); i++) {
JSONObject link = links.getJSONObject(i);
if (link.getString("relation").equals("next")) {
hasNextPage = true;
}
}
}
}
return obs;
}
public String getObservationPage(String patientId, int pageNumber) throws ClientProtocolException, IOException {
String url = "https://sandbox86.tactiorpm7000.com/tactio-clinician-api/1.1.4/Observation" +
"?subject=" +patientId +
"&page=" + Integer.toString(pageNumber);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ this.token);
HttpResponse response = client.execute(request);
return readResponse(response);
}
public void getObservation() {
try {
String url = "https://sandbox86.tactiorpm7000.com/tactio-clinician-api/1.1.4/Observation";
JSONObject obj = new JSONObject();
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ this.token);
HttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
int counter = 1;
boolean idk = true;
while (idk) {
idk = false;
counter++;
request = new HttpGet("https://sandbox86.tactiorpm7000.com/tactio-clinician-api/1.1.4/Observation?page="+Integer.toString(counter));
result = new StringBuffer();
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ this.token);
response = client.execute(request);
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
line = rd.readLine();
while (line != null) {
if(line.contains("next")) {
idk = true;
}
if(line.contains("Patient")) {
String requiredString = line.substring(line.indexOf("Patient") + 9, line.indexOf("identifier")-3);
request = new HttpGet("https://sandbox86.tactiorpm7000.com/tactio-clinician-api/1.1.4/Observation?subject="+requiredString);
result = new StringBuffer();
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer "+ this.token);
response = client.execute(request);
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
line = "";
line = rd.readLine();
while (line != null) {
result.append(line);
line = rd.readLine();
}
}
result.append(line);
line = rd.readLine();
}
}
} catch (IOException e) {
System.out.println("Haha error bruv");
}
}
public String getToken() throws ClientProtocolException, JSONException{
JSONObject obj = new JSONObject();
obj.put("client_id", "083e9a44a763473fbeb62fbf90b74551");
obj.put("grant_type", "password");
obj.put("client_secret", "ba09798f0921456e8b4e5e4588ea536d");
obj.put("username", "tactioClinician");
obj.put("password", "tactio");
try {
HttpClient hc = HttpClientBuilder.create().build();
String url = "https://sandbox86.tactiorpm7000.com/token.php";
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
StringEntity entity = new StringEntity(obj.toString(),ContentType.APPLICATION_JSON);
request.setEntity(entity);
// add request header
//request.addHeader("User-Agent", );
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject responseResult = new JSONObject(result.toString());
return responseResult.getString("access_token");
}catch( IOException e){
System.out.println("Haha error bruv");
}
return null;
}
}