-
Notifications
You must be signed in to change notification settings - Fork 2
/
PubmedSearchExternalModule.php
488 lines (439 loc) · 15.5 KB
/
PubmedSearchExternalModule.php
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
<?php
namespace Vanderbilt\PubmedSearchExternalModule;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use \REDCap as REDCap;
require_once("emLoggerTrait.php");
define('SLEEP_TIME', 1);
define('MAX_RETRIES', 5);
class PubmedSearchExternalModule extends AbstractExternalModule
{
public function getPids() {
$sql="SELECT DISTINCT(s.project_id) AS project_id FROM redcap_external_modules m, redcap_external_module_settings s INNER JOIN redcap_projects AS p ON p.project_id = s.project_id WHERE p.date_deleted IS NULL AND m.external_module_id = s.external_module_id AND s.value = 'true' AND m.directory_prefix = 'pubmed_search' AND s.`key` = 'enabled'";
$q = db_query($sql);
if ($error = db_error()) {
error_log("PubmedSearchExternalModule ERROR: $error");
}
$pids = array();
while ($row = db_fetch_assoc($q)) {
$pids[] = $row['project_id'];
}
return $pids;
}
public function pubmed() {
// check if cron is enabled in settings
if( ! $this->getProjectSetting('enable-cron')){
return;
}
$pids = $this->getPids();
error_log("PubmedSearchExternalModule::pubmed with ".json_encode($pids));
foreach ($pids as $pid) {
$this->pubmedRun($pid);
}
}
public function pubmedRun($pid, $specific_record=null) {
error_log("PubmedSearchExternalModule::pubmedRun with $pid");
# field names
$firstName = $this->getProjectSetting("first_name", $pid);
$lastName = $this->getProjectSetting("last_name", $pid);
$citations = $this->getProjectSetting("citations", $pid);
$helper = $this->getProjectSetting("helper", $pid);
$recordId = $this->getProjectSetting("record_id", $pid);
$defaultInstitution = $this->getProjectSetting("institution", $pid);
$institutionFields = $this->getProjectSetting("institution_fields", $pid);
$citationsCount = $this->getProjectSetting("count", $pid);
# get the first name, last name, citations, and helper
$fields = array();
array_push($fields, $firstName);
array_push($fields, $lastName);
array_push($fields, $citations);
array_push($fields, $helper);
array_push($fields, $recordId);
foreach ($institutionFields as $institutionField) {
if ($institutionField) {
array_push($fields, $institutionField);
}
}
if (is_null($specific_record)){
$json = \REDCap::getData($pid, "json", NULL, $fields);
}else{
$json = \REDCap::getData($pid, "json", [$specific_record['record_id']], $fields);
}
$data = json_decode($json, true);
# organize the data
$dataRows = array();
foreach ($data as $row) {
if (!isset($dataRows[$row[$recordId]])) {
$dataRows[$row[$recordId]] = array();
}
array_push($dataRows[$row[$recordId]], $row);
}
# read in the data into data structures
$names = array();
$citationsForm = array("form" => "", "instance" => "");
$helperForm = array("form" => "", "instance" => "");
foreach ($dataRows as $id => $rows) {
# these are default fields that might be blank to begin with
# record_id, last_name, and first_name must be filled in
$ary = array($citations => "", $helper => "[]");
$institutions[$id] = preg_split("/\s*[,\n]\s*/", $defaultInstitution);
foreach ($rows as $row) {
foreach ($fields as $field) {
if ($row[$field]) {
if (in_array($field, $institutionFields)) {
$myInstitutions = preg_split("/\s*[,\n]\s*/", $row[$field]);
foreach ($myInstitutions as $institution) {
array_push($institutions[$id], $institution);
}
} else {
$ary[$field] = $row[$field];
}
}
}
}
if (count($ary) > 2) {
array_push($names, $ary);
}
}
error_log("PubmedSearchExternalModule: ".count($names)." names");
# process the data
$upload = array();
foreach ($names as $values) {
if (is_null($specific_record)){
$row = self::getPubMed( $values[$firstName],
$values[$lastName],
$institutions[$values[$recordId]],
json_decode($values[$helper]),
$values[$citations],
$citations,
$helper);
}else{
$inst = $institutions[$values[$recordId]];
if (! in_array($specific_record['institution'], $inst)){
$inst[] = $specific_record['institution'];
}
$row = self::getPubMed(
$specific_record['first'],
$specific_record['last'],
$inst,
json_decode($values[$helper]),
$values[$citations],
$citations,
$helper, true);
}
if ($row && $row[$helper] && $row[$citations]) {
array_push($upload, array( $recordId => $values[$recordId],
$helper => $row[$helper],
$citations => $row[$citations],
$citationsCount => count(explode("\n", $row[$citations])),
));
}
}
error_log("PubmedSearchExternalModule upload: ".count($upload)." rows");
// if it's a brand new redcap record, recordid will be null, so will $names
if(!is_null($specific_record) && is_null($specific_record['record_id'])){
$row = self::getPubMed(
$specific_record['first'],
$specific_record['last'],
$specific_record['institution'],
"",
"",
$citations,
$helper,
true);
if ($row && $row[$helper] && $row[$citations]) {
array_push($upload, array( $recordId => $values[$recordId],
$helper => $row[$helper],
$citations => $row[$citations],
$citationsCount => count(explode("\n", $row[$citations])),
));
}
}
if (!empty($upload)) {
if (is_null($specific_record) && $this->getProjectSetting('enable-cron')){
$feedback = \REDCap::saveData($pid, "json", json_encode($upload));
error_log("PubmedSearchExternalModule upload: ".json_encode($feedback));
}
}
return json_encode($upload);
}
public static function getPubMed($firstName, $lastName, $institutions, $prevCitations, $citationsStr, $citationField, $citationIdField, $deltas=false) {
$cs = explode("\n", $citationsStr);
$citations = array();
foreach ($cs as $c) {
if ($c) {
array_push($citations, $c);
}
}
$upload = array();
$total = 0;
$totalNew = 0;
$lastNamesIntermediate = preg_split("/\s*[\s\-]\s*/", strtolower($lastName));
$lastNames = array(strtolower($lastName));
foreach($lastNamesIntermediate as $thisLastName) {
$thisLastName = preg_replace("/^\(/", "", $thisLastName);
$thisLastName = preg_replace("/\)$/", "", $thisLastName);
$thisLastName = strtolower($thisLastName);
if (!in_array($thisLastName, $lastNames)) {
$lastNames[] = strtolower($thisLastName);
}
}
if (preg_match("/\s\(/", strtolower($firstName))) {
# nickname in parentheses
$namesWithFormatting = preg_split("/\s\(/", strtolower($firstName));
$firstNames = array();
foreach ($namesWithFormatting as $formattedFirstName) {
$firstName = preg_replace("/\)$/", "", $formattedFirstName);
$firstName = preg_replace("/\s+/", "+", $firstName);
array_push($firstNames, $firstName);
}
} else {
# specified full name => search as group
$firstNames = array(preg_replace("/\s+/", "+", $firstName));
}
$pmids = array();
foreach ($lastNames as $lastName) {
foreach ($firstNames as $firstName) {
foreach ($institutions as $institution) {
$institution = preg_replace("/\s+/", "+", $institution);
$url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmax=100000&retmode=json&term=".$firstName."+".$lastName."+%5Bau%5D+AND+".strtolower($institution)."%5Bad%5D";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$output = curl_exec($ch);
curl_close($ch);
sleep(SLEEP_TIME);
$pmData = json_decode($output, true);
if ($pmData['esearchresult'] && $pmData['esearchresult']['idlist']) {
# if the errorlist is not blank, it might search for simplified
# it might search for simplified names and produce bad results
if (!isset($pmData['esearchresult']['errorlist'])
|| !$pmData['esearchresult']['errorlist']
|| !$pmData['esearchresult']['errorlist']['phrasesnotfound']
|| empty($pmData['esearchresult']['errorlist']['phrasesnotfound'])) {
foreach ($pmData['esearchresult']['idlist'] as $pmid) {
if (!in_array($pmid, $pmids)) {
$pmids[] = $pmid;
}
}
}
}
}
}
}
error_log("PubmedSearchExternalModule Got ".count($pmids)." pmids for ".json_encode($lastNames).", ".json_encode($firstNames)." at ".json_encode($institutions));
$pmidsUnique = array();
foreach ($pmids as $pmid) {
if (!in_array($pmid, $prevCitations)) {
$pmidsUnique[] = $pmid;
}
}
$total += count($pmids);
$totalNew += count($pmidsUnique);
$citations = array();
if (!empty($pmidsUnique)) {
$pullSize = 10;
for ($i = 0; $i < count($pmidsUnique); $i += $pullSize) {
$pmidsUniqueForPull = array();
for ($j = $i; $j < count($pmidsUnique) && $j < $i + $pullSize; $j++) {
array_push($pmidsUniqueForPull, $pmidsUnique[$j]);
}
$url = "https://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?format=json&ids=".implode(",", $pmidsUniqueForPull);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$output = curl_exec($ch);
curl_close($ch);
sleep(SLEEP_TIME);
$data = json_decode($output, true);
# indexed by PMID
$pmcids = array();
foreach ($data['records'] as $record) {
if ($record['pmid'] && $record['pmcid']) {
$pmcids[$record['pmid']] = $record['pmcid'];
}
}
$retry = 0;
do {
$url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=".implode(",", $pmidsUniqueForPull);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$output = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string(utf8_encode($output));
sleep(SLEEP_TIME);
$retry++;
} while (!$xml && ($retry < MAX_RETRIES));
if (!$xml) {
throw new \Exception("Error: After ".MAX_RETRIES." retries, cannot create object (".json_encode($output).") from ".$url);
}
foreach ($xml->PubmedArticle as $medlineCitation) {
$article = $medlineCitation->MedlineCitation->Article;
$authors = array();
if ($article->AuthorList->Author) {
foreach ($article->AuthorList->Author as $authorXML) {
$author = $authorXML->LastName." ".$authorXML->Initials;
# " " is if no LastName and no Initials in the line above
if ($author != " ") {
$authors[] = $author;
} else {
$authors[] = $authorXML->CollectiveName;
}
}
}
$title = preg_replace("/\.$/", "", $article->ArticleTitle);
$journal = preg_replace("/\.$/", "", $article->Journal->ISOAbbreviation);
$issue = $article->Journal->JournalIssue;
$date = $issue->PubDate->Year." ".$issue->PubDate->Month;
if ($issue->PubDate->Day) {
$date = $date." ".$issue->PubDate->Day;
}
$journalIssue = $issue->Volume."(".$issue->Issue."):".$article->Journal->ISSN;
$pmid = $medlineCitation->MedlineCitation->PMID;
$pubmed = "PubMed PMID: ".$pmid;
$pmc = "";
if (isset($pmcids["$pmid"])) {
$pmc = $pmcids["$pmid"].". ";
}
$citation = implode(",", $authors);
if ($citation) {
$citation .= ". ";
}
$citation .= $title.". ".$journal.". ".$date.";".$journalIssue.". ".$pmc.$pubmed.".";
$citations[] = $citation;
}
}
}
//
if (! $deltas){
$newCitationIds = array_merge($prevCitations, $pmidsUnique);
$uploadRow = array(
$citationIdField => self::json_encode_with_spaces($newCitationIds),
$citationField => implode("\n", $citations),
);
}else{
$uploadRow = array(
$citationIdField => json_encode($pmidsUnique),
$citationField => json_encode($citations),
);
}
return $uploadRow;
}
private static function json_encode_with_spaces($data) {
$str = json_encode($data);
$str = preg_replace("/,/", ", ", $str);
return $str;
}
// THESE FUNCTIONS BORROWED FROM LOOKUP ASSISTANT
public $errors = array();
public $settings = array();
private function validateJson($contents) {
// // Verify file exists
// if (file_exists($path)) {
// $contents = file_get_contents($path);
// Verify contents are json
$obj = json_decode($contents);
if (json_last_error() == JSON_ERROR_NONE) {
// All good
return $contents;
} else {
$this->emError("Error decoding JSON", json_last_error_msg());
return false;
// $this->errors[] = "Error decoding path $path: " . json_last_error_msg();
}
// } else {
// $this->errors[] = "Unable to locate file $path";
// }
// return false;
}
// Take the project settings and convert them into a more friendly settings object to pass to javascript
public function loadSettings($instrument) {
$set = $this->getProjectSettings();
$set['module-path'] = $this->getPostURL();
$set['record_id'] = $this->getRecordId();
$this->settings[] = $set;
}
function injectLookup($instrument)
{
$this->loadSettings($instrument);
if (!empty($this->errors)) {
$this->emDebug($this->errors);
return false;
}
// Skip out if there is nothing to do
if (empty($this->settings)) return false;
// DUMP CONTENTS TO JAVASCRIPT
// Append the select2 controls
$this->insertSelect2();
echo "<style type='text/css'>" . $this->dumpResource("css/lookup-assistant.css") . "</style>";
// // Insert our custom JS
echo "<script type='text/javascript'>" . $this->dumpResource("js/lookupAssistant.js") . "</script>";
// Insert our custom JS
echo "<script type='text/javascript'>lookupAssistant.settings = " . json_encode($this->settings) . "</script>";
echo "<script type='text/javascript'>lookupAssistant.jsDebug = " . json_encode($this->getProjectSetting('enable-js-debug')) . "</script>";
?>
<style>
.lookupIcon {
position: absolute;
}
.lookupIcon > span {
position: relative;
left: -20px;
cursor: pointer;
}
</style>
<?php
}
function redcap_data_entry_form_top($project_id, $record, $instrument, $event_id, $group_id)
{
if( ! $this->getProjectSetting('enable-cron')){
$this->injectLookup($instrument);
}
}
function redcap_survey_page_top($project_id, $record, $instrument, $event_id, $group_id)
{
if( ! $this->getProjectSetting('enable-cron')){
$this->injectLookup($instrument);
}
}
public function insertSelect2()
{
?>
<script type="text/javascript"><?php echo $this->dumpResource('js/select2.full.min.js'); ?></script>
<style><?php echo $this->dumpResource('css/select2.min.css'); ?></style>
<style><?php echo $this->dumpResource('css/select2-bootstrap.min.css'); ?></style>
<?php
}
public function dumpResource($name)
{
$file = $this->getModulePath() . $name;
if (file_exists($file)) {
$contents = file_get_contents($file);
return $contents;
} else {
$this->emError("Unable to find $file");
}
}
public function getPostURL(){
return $this->getUrl("ajax.php", true, false);
}
}