-
Notifications
You must be signed in to change notification settings - Fork 3
/
tripal_eutils.module
119 lines (103 loc) · 4.14 KB
/
tripal_eutils.module
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
<?php
/**
* @file
* Tripal Eutils module file.
*/
require_once 'includes/EUtilsFactoryInterface.inc';
require_once 'includes/resources/EUtilsResource.inc';
require_once 'includes/resources/EUtilsRequest.inc';
require_once 'includes/resources/EFetch.inc';
require_once 'includes/resources/ESearch.inc';
require_once 'includes/resources/ESummary.inc';
require_once 'includes/resources/EUtils.inc';
require_once 'includes/resources/EFTP.inc';
// Connects to NCBI to fetch the current biosample property list hosted.
require_once 'includes/resources/BiosamplePropertyLookup.inc';
// Parsers. Convert XML into keyed arrays.
require_once 'includes/xml_parsers/EUtilsXMLParserFactory.inc';
require_once 'includes/xml_parsers/EUtilsParserInterface.inc';
require_once 'includes/xml_parsers/EUtilsBioSampleParser.inc';
require_once 'includes/xml_parsers/EUtilsBioProjectParser.inc';
require_once 'includes/xml_parsers/EUtilsAssemblyParser.inc';
require_once 'includes/xml_parsers/EUtilsPubmedParser.inc';
// Repositories. Handling DB operations.
require_once 'includes/repositories/EUtilsRepositoryFactory.inc';
require_once 'includes/repositories/EUtilsRepository.inc';
require_once 'includes/repositories/EUtilsBioSampleRepository.inc';
require_once 'includes/repositories/EUtilsBioProjectRepository.inc';
require_once 'includes/repositories/EUtilsAssemblyRepository.inc';
require_once 'includes/repositories/EUtilsPubmedRepository.inc';
// Repository helpers. Assist with looking up terms.
require_once 'includes/TagMapper.inc';
// Formatters.
require_once 'includes/formatters/EUtilsFormatter.inc';
require_once 'includes/formatters/EUtilsFormatterFactory.inc';
require_once 'includes/formatters/EUtilsBioSampleFormatter.inc';
require_once 'includes/formatters/EUtilsBioProjectFormatter.inc';
require_once 'includes/formatters/EUtilsAssemblyFormatter.inc';
/**
* Implements hook_menu().
*/
function tripal_eutils_menu() {
// Admin can configure which emails get sent out, more stuff?
// Set site-wide admin settings.
$items['admin/tripal/tripal_eutils'] = [
'title' => 'Tripal EUtils settings',
'page callback' => 'drupal_get_form',
'file' => 'includes/tripal_eutils_admin_settings.form.inc',
'access arguments' => ['access tripal_eutils admin'],
'page arguments' => ['tripal_eutils_admin_settings_form'],
];
return $items;
}
/**
* Implements hook_permission().
*/
function tripal_eutils_permission() {
return [
'access tripal_eutils admin' => [
'title' => t('Submit NCBI Accessions directly via admin form'),
],
];
}
/**
* Runs the EUtils class to create the records for an NCBI entry.
*
* @param string $db
* The database name (eg, biosample, assembly or bioproject).
* @param string $accession
* The numeric accession, or accessions separated by delimiter of comma, semicolon, or space.
* @param bool $create_linked_records
* Whether to create linked records or not.
*/
function tripal_eutils_create_records(string $db, string $accession, bool $create_linked_records, $job = NULL) {
$accs = preg_split('/[,; ]+/', trim($accession));
foreach ($accs as $acc) {
$attempts = 3;
$success = false;
while ((!$success) and ($attempts)) {
try {
$eutils = new EUtils($create_linked_records, $job);
$eutils->get($db, $acc);
$success = true;
}
catch (Exception $exception) {
$message = $exception->getMessage();
// Distinguish between download error and SQL error, e.g. from reloading same assembly twice.
// Download error: "ERROR Could not make request: Status: 400"
// SQL error: "SQLSTATE[25P02]: In failed sql transaction: 7 ERROR: current transaction is aborted, commands ignored until end of transaction block"
if (preg_match('/SQL/', $message)) {
$attempts = 1;
}
if ($attempts > 1) {
tripal_report_error('tripal_eutils', TRIPAL_WARNING, 'Download error, retrying '.$message, [], ['print' => TRUE, 'job' => $job]);
sleep(1);
}
else {
tripal_report_error('tripal_eutils', TRIPAL_ERROR, $message, [], ['print' => TRUE, 'job' => $job]);
}
}
$attempts--;
}
}
}