forked from UofS-Pulse-Binfo/tripal_bibtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripal_bibtex.module
198 lines (170 loc) · 6.45 KB
/
tripal_bibtex.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
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
<?php
module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
/**
* @ file
* Provides the main functionality for this module.
*/
/**
* Import publications from a bibtex formatted file.
*/
function tripal_bibtex_import_bibtex($filename, $options = []) {
print 'Filename: ' . $filename . "\n";
$in = fopen($filename, 'r');
if ($in) {
$record = [];
while (($line = fgets($in)) !== FALSE) {
$line = trim($line);
// Check if we are starting a new record.
if (preg_match('/@(\w+)\s*{/', $line, $matches)) {
$ptype = ($matches[1] == 'article' or $matches[1] == 'Article') ? 'Journal Article' : $matches[1];
if (sizeof($record) > 1) {
tripal_bibtext_import_publication($record);
}
// Start the new record.
$record = [
'Publication Type' => [ucwords($ptype)],
];
}
// Otherwise check which type of line we are on.
// Bibtex format consists of tag = {value}
// This is made slightly more complicated by the fact that value
// can span multiple lines.
// First; Simple Case: All on one line
// elseif (preg_match('/^(\w+) = ["{](.*)["}]/', $line, $matches)) {
elseif (preg_match('/\s*(.*?)\s*=\s*["{](.*)["}]/', $line, $matches)) {
$current_tag = ucwords($matches[1]);
$record[$current_tag] = $matches[2];
$current_tag = NULL;
$record_ended = TRUE;
}
// Numeric values don't need to be in quotes or brackets:
elseif (preg_match('/\s*(.*?)\s*=\s*(\d+\.*\d*)/', $line, $matches)) {
$current_tag = ucwords($matches[1]);
$record[$current_tag] = $matches[2];
$current_tag = NULL;
$record_ended = TRUE;
}
// Next up: the tag is starting.
elseif (preg_match('/\s*(.*?)\s*=\s*["{](.*)/', $line, $matches)) {
$current_tag = ucwords($matches[1]);
$record[$current_tag] = $matches[2];
$record_ended = FALSE;
}
// The tag is ending.
elseif (preg_match('/(.*)}/', $line, $matches) AND $current_tag) {
$record[$current_tag] .= $matches[1];
$current_tag = NULL;
$record_ended = TRUE;
}
// We are in the middle of a multi-line tag.
elseif ($record_ended == FALSE AND $current_tag) {
$record[$current_tag] .= $line;
}
// if we hit the ending bracket of the stanza do nothing.
elseif (preg_match('/^\s*}\s*$/', $line) or preg_match('/^\s*$/', $line)) {
// do nothing.
}
else {
print("worst case: '$line'\n");
}
}
}
// Last record.
if (sizeof($record) > 1) {
tripal_bibtext_import_publication($record);
}
// Finally, sync publications & contacts.
$pub_bundle = tripal_bibtex_get_chado_table_bundle('pub');
$contact_bundle = tripal_bibtex_get_chado_table_bundle('contact');
if ($pub_bundle) {
chado_publish_records(['bundle_name' => $pub_bundle]);
}
if ($contact_bundle) {
chado_publish_records(['bundle_name' => $contact_bundle]);
}
// else {
// print "ERROR: Unable to open file\n";
// }
}
/**
* Clean-up a bibtext pub array and insert it into chado using the tripal api.
*/
function tripal_bibtext_import_publication($record) {
// Clean-up Record to match expectations.
// Key Expectations:
// Publication Type: an array of publication types. a publication can have more than one type
// Authors: a string containing all of the authors of a publication
// Journal Name: a string containing the journal name
// Journal Abbreviation: a string containing the journal name abbreviation
// Series Name: a string containing the series (e.g. conference proceedings) name
// Series Abbreviation: a string containing the series name abbreviation
// Volume: the serives volume number
// Issue: the series issue number
// Pages: the page numbers for the publication
// Publication Date: A date in the
if (isset($record['Journal'])) {
$record['Journal Name'] = $record['Journal'];
}
unset($record['Journal']);
if (isset($record['Author'])) {
$record['Authors'] = $record['Author'];
}
unset($record['Author']);
if (preg_match('/(.*)\.$/', $record['Title'], $matches)) {
$record['Title'] = $matches[1];
}
if (isset($record['Note'])) {
$record['Notes'] = $record['Note'];
unset($record['Note']);
}
// Process the Authors.
// The main goal is to process the bibtex format into the array that tripal_pub_add_authors() requires.
// Additionally, the bibtex author list is ugly so we would like to re-format that based
// on the format of each author name.
$first_is_inital = [];
$record['Author List'] = explode(' and ', $record['Authors']);
foreach ($record['Author List'] as $k => $author) {
$author = explode(', ', $author);
$record['Author List'][$k] = [
'Surname' => $author[0],
'Given Name' => $author[1],
];
// Check if the first name is condensed into initals.
if (preg_match('/^[A-Z]+$/', $author[1])) {
$first_is_inital[] = $k;
}
}
// Now if all the authors are first initial formatted then remove comma's between name
// name parts and replace ands with comma's
// ie: "Sanderson, LA and Bett, KE and Caron, C" becomes "Sanderson LA, Bett KE, Caron C"
if (sizeof($first_is_inital) == sizeof($record['Author List'])) {
$record['Authors'] = str_replace(',', '', $record['Authors']);
$record['Authors'] = str_replace(' and ', ', ', $record['Authors']);
}
// Otherwise, just replace ands with semi-colons.
else {
$record['Authors'] = str_replace(' and ', '; ', $record['Authors']);
}
// Add Citation.
$record['Citation'] = chado_pub_create_citation($record);
// Finally, the Tripal pub check for duplicate record doesn't seem to be working for
// me. Thus check manually here.
// @TODO: Investigate why Tripal pub's check is not working for me.
// I'm just going to check title & year.
$pub_id = chado_query('SELECT pub_id FROM {pub} WHERE title=:title AND pyear=:year',
[':title' => $record['Title'], ':year' => $record['Year']])->fetchField();
if (!$pub_id) {
$action = NULL;
$pub_id = tripal_pub_add_publication($record, $action, TRUE);
}
}
function tripal_bibtex_get_chado_table_bundle($table_name) {
$query = db_select('public.chado_bundle', 'cb');
$query->fields('cb', ['bundle_id']);
$query->condition('data_table', $table_name);
$id = $query->execute()->fetchField();
if (!$id) {
return FALSE;
}
return 'bio_data_' . $id;
}