-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbioXML2pubmedXML.php
230 lines (203 loc) · 5.97 KB
/
bioXML2pubmedXML.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
<?php
echo "starting bioXML2pubmedXML.php<br>\n";
$biofilename= "bio.xml";
$bioxml=simplexml_load_file($biofilename);
$fullnames = getFullNameArray($bioxml);
// these are the pubmed ids based on the name search
$fnpmids = fullnameQuery($fullnames);
$fnPubXMLstr = getPubXML($fnpmids);
// var_dump($fnPubXML);
$fnPubXML = new SimpleXMLElement($fnPubXMLstr);
$fnArticles = $fnPubXML->xpath('//PubmedArticle');
$fnArticleCount = count($fnArticles);
$gladstoneAffiliatedArticles = gladstoneFilter($fnArticles);
$ids = getUCSFids($bioxml);
$ids = array_unique($ids);
// foreach ($ids as $id ){
// echo $id."\n<br>";
// }
echo "ids ".gettype($ids)." count ".count($ids)." id[0]".$ids[0]."<br>";
$pubArr = queryProfiles($ids);
echo "pubarr ".gettype($pubArr)." count ".count($pubArr)."<br>";
$pubmedURLs = $pubArr['pubmedURLs'];
echo "pubmedURLs ".gettype($pubmedURLs)." count ".count($pubmedURLs)."<br>";
$pubmedURLs = array_unique($pubmedURLs);
$profilesPMIDs = pubmedUrlToPMID($pubmedURLs);
$pubXML = getPubXML($profilesPMIDs);
function gladstoneFilter($fnArticles){
$gladArticles = array();
foreach($fnArticles as $art){
$art = new SimpleXMLElement($art->asXML());
$affiliations = $art->xpath('//Affiliation');
$glad = 0;
foreach($affiliations as $aff){
// echo "$aff\n";
if (preg_match("/Gladstone/", $aff)){
$glad = 1;
}
}
if($glad == 1){
array_push($gladArticles, $aff);
}
}
// var_dump($gladArticles);
echo count($gladArticles)."\n";
echo count($fnArticles)."\n";
return $gladArticles;
}
function fullnameQuery($fullnames){
$pmids = array();
$apiQuery="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";
foreach($fullnames as $name){
$ename = explode(" ", $name);
$queryStr = "db=pubmed&term=".implode("+", $ename)."[Author+-+Full]&retmax=5000";
echo $queryStr."\n";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_POSTFIELDS => $queryStr
);
$ch = curl_init($apiQuery);
curl_setopt_array($ch, $options);
if( $content = curl_exec($ch) ){
echo "curl success<br>";
}else {
die("curl failure");
}
echo $content."\n";
$contentxml = new SimpleXMLElement($content);
echo gettype($contentxml);
$ids = $contentxml->xpath('//Id');
foreach ( $ids as $id ){
if($id != ""){
array_push($pmids, $id);
}
}
}
$pmids = array_unique($pmids);
// var_dump($pmids);
// readline();
$pmids = array_filter($pmids);
return $pmids;
}
function getFullNameArray( $bioxml ){
$fullnames = array();
$records = $bioxml->xpath('//RECORD');
// var_dump($records);
foreach ($records as $record){
$record = new SimpleXMLElement($record->asXML());
$fname = $record->xpath('//field_bio_name_given');
$lname = $record->xpath('//field_bio_name_family');
$fullname = $fname[0]." ".$lname[0];
array_push($fullnames, $fullname);
}
$fullnames = array_unique($fullnames);
// var_dump($fullnames);
return $fullnames;
}
function makeCommaSepIds($pmids){
echo count($pmids)."\n";
// readline();
$csids = "";
for ($i = 0 ; $i < count($pmids) ; $i++){
// echo "pmid: $pmids[$i]\t\t";
if(isset($pmids[$i])){
$csids .= $pmids[$i].",";
}
}
// foreach($pmids as $pmid){
// $csids .= $pmids[$i].",";
// }
// readline();
$csids = substr($csids, 0 , -1);
echo $csids."\n";
// readline();
return $csids;
}
function pubmedUrlToPMID($pubmedURLs){
$size = count($pubmedURLs);
$pat = "/(\d+)$/";
$pmids = array();
foreach( $pubmedURLs as $url){
preg_match($pat, $url, $matches);
array_push($pmids, $matches[1]);
}
return $pmids;
}
function getPubXML($pmids){
$XMLs = array();
$count = 0;
// $pmids = preg_grep($pat, $pubmedURLs);
$successfulPMIDs = array();
$i=0;
$csids = makeCommaSepIds($pmids);
// echo $csids."<br>";
// $apiQuery = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=".$csids."&retmode=xml";
$apiQuery = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi";
// echo $apiQuery."<br>";
//set curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POST => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_POSTFIELDS => "db=pubmed&id=$csids&retmode=xml"
);
$ch = curl_init($apiQuery);
curl_setopt_array($ch, $options);
if( $content = curl_exec($ch) ){
echo "curl success<br>";
}else {
die("curl failure");
}
return $content;
}
function getUCSFids($bioxml){
$out = $bioxml->xpath('//field_ucsfid_value');
return $out;
}
function queryProfiles( $ids ){
$out = array();
$pubmedURLs = array();
$pubWOpubmedIDs = array();
$num = 0;
$i = 0;
$successfulIDs = array();
while(count($ids) > 1 && $i < 50 ){
$size = count($ids);
$num = 0;
echo "Trying to query profiles for Ids: ".++$i."\n<br>";
foreach($ids as $id){
echo "Processing id: ".$id." number: ".++$num." out of ".$size."\n<br>";
if($id != 0){
$person_id = (int) ($id/10) + 2569307;
$apiQuery = "https://profiles.ucsf.edu/CustomAPI/v1/JSONProfile.aspx?source=Gladstone&Person=".$person_id."&publications=full";
if ( $handle = fopen($apiQuery, 'r')){
$jsonstr = stream_get_contents($handle);
array_push($successfulIDs, $id);
}
$json = json_decode( $jsonstr , $assoc = true);
$keys1 = array();
if (isset($json["Profiles"][0]['Publications'])){
$keys1 = array_keys( $json["Profiles"][0]['Publications']);
foreach( $keys1 as $key ){
if (isset($json["Profiles"][0]['Publications'][$key]['PublicationSource'][0]['PublicationSourceURL'] )){
$URL = $json["Profiles"][0]['Publications'][$key]['PublicationSource'][0]['PublicationSourceURL'];
echo "found URL: ".$URL."\n<br>";
array_push($pubmedURLs, $URL);
}
else {
array_push($pubWOpubmedIDs , $json["Profiles"][0]['Publications'][$key] );
}
}
}
}
}
$ids = array_diff($ids, $successfulIDs);
}
$out['pubmedURLs'] = $pubmedURLs;
$out['pubWOpubmedIDs'] = $pubWOpubmedIDs;
return $out;
}