/
CoauthorNetworkCrawler.php
282 lines (222 loc) · 10.9 KB
/
CoauthorNetworkCrawler.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
<?php
require_once("vendor/autoload.php");
require_once("URLParser.php");
require_once("addMOProperty.php");
use Everyman\Neo4j\Cypher\Query;
//Only search coauthor information from pubmed and IEEE datasource
class CoauthorNetworkCrawler{
private $pubmed_search_url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&RetMax=20";
private $pubmed_fetch_url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=";
private $ieee_url = "http://ieeexplore.ieee.org/gateway/ipsSearch.jsp";
private $firstName;
private $lastName;
private $affiliation;
public function __construct($name, $affiliation){
$parser = new HumanNameParser_Parser($name);
$this->firstName = $parser->getFirst();
$this->lastName = $parser->getLast();
$this->affiliation = $affiliation;
}
public function crawlIEEE(){
$url = $this->ieee_url . "?au=" . urlencode($this->firstName." ".$this->lastName);
$IEEE_parser = new URLParser($url);
$IEEE_item_array = $IEEE_parser -> XMLToArray();
$retVal = array();
if(array_key_exists('document', $IEEE_item_array) && !empty($IEEE_item_array['document'])){
$IEEE_item_array['document'] = isset($IEEE_item_array['document'][0]) ? $IEEE_item_array['document'] : array($IEEE_item_array['document']);
foreach($IEEE_item_array['document'] as $ind => $attr_array){
$title = $attr_array['title'];
$pdf_url = $attr_array['pdf'];
//$affiliations = (isset($attr_array['affiliations'])) ? $attr_array['affiliations'] : "";
$authorListString = $attr_array['authors'];
$authorListArray = explode("; ", $authorListString);
$peopleList = array();
foreach($authorListArray as $au){
$parser = new HumanNameParser_Parser($au);
$peopleList[] = array("firstName" => $parser->getFirst(), "lastName" => $parser->getLast(), "affiliation" => "");
}
$retVal[] = array("title" => $title, "url" => $pdf_url, "people" => $peopleList);
}
}
return $retVal;
}
public function crawlPubmed(){
$search_url = $this->pubmed_search_url . "&term=".urlencode($this->firstName." ".$this->lastName."[Author]"." AND ".$this->affiliation."[Affiliation]");
$search_url_parser = new URLParser($search_url);
$pubmedSearch_item_array = $search_url_parser->XMLToArray();
$returnVal = array();
if(array_key_exists('Count', $pubmedSearch_item_array) && $pubmedSearch_item_array['Count'] > 0){
if($pubmedSearch_item_array['Count'] == 1 || $pubmedSearch_item_array['RetMax'] == 1){
$pubmedSearch_item_array['IdList']['Id'] = array($pubmedSearch_item_array['IdList']['Id']);
}
$pid_url = "";
foreach($pubmedSearch_item_array['IdList']['Id'] as $ind => $pubid){
$pid_url .= $pubid . ",";
}
$pid_url = rtrim($pid_url,",");
$fetch_url = $this->pubmed_fetch_url . $pid_url;
$fetch_url_parser = new URLParser($fetch_url);
$pubmedFetchResultArray = $fetch_url_parser -> XMLToArray();
if(!array_key_exists('PubmedArticle', $pubmedFetchResultArray)){
trigger_error("PubmedArticle not set in pubmedFetchResultArray", E_USER_ERROR);
return null;
}
$pubmedFetchResultArray['PubmedArticle'] = isset($pubmedFetchResultArray['PubmedArticle'][0])
? $pubmedFetchResultArray['PubmedArticle']
: array($pubmedFetchResultArray['PubmedArticle']);
foreach ($pubmedFetchResultArray['PubmedArticle'] as $record){
if(!array_key_exists('MedlineCitation', $record)){
trigger_error("MedlineCitation not set in one pubmed record", E_USER_ERROR);
continue;
}
$coauthorList = array();
$title = $record['MedlineCitation']['Article']['ArticleTitle'];
$url = 'http://www.ncbi.nlm.nih.gov/pubmed/'.$record['MedlineCitation']['PMID'];
if(array_key_exists('AuthorList', $record['MedlineCitation']['Article'])
&& array_key_exists('Author', $record['MedlineCitation']['Article']['AuthorList'])
&& !empty($record['MedlineCitation']['Article']['AuthorList'])){
$authorList = $record['MedlineCitation']['Article']['AuthorList']['Author'];
}
else{
trigger_error("authorlist/author not set or author is empty", E_USER_ERROR);
continue;
}
if(!isset($authorList[0])){
continue;
}
if(count($authorList) > 20){
continue;
}
foreach($authorList as $a){
//converting pubmed name to neo4j compatible format
$parser = new HumanNameParser_Parser($a['LastName']. ", ". $a['ForeName']);
$first = $parser->getFirst();
$last = $parser->getLast();
if(array_key_exists('AffiliationInfo', $a)){
if(array_key_exists('Affiliation', $a['AffiliationInfo'])){
$affi = $a['AffiliationInfo']['Affiliation'];
}
else if(array_key_exists(0, $a['AffiliationInfo']) && array_key_exists('Affiliation', $a['AffiliationInfo'][0])){
$affi = $a['AffiliationInfo'][0]['Affiliation'];
}
}
else{
$affi = "";
}
$coauthorList[] = array("firstName" => $first, "lastName" => $last, "affiliation" => $affi);
}
$returnVal[] = array("title"=>$title, "url" => $url, "people"=>$coauthorList);
}
}
return $returnVal;
}
public function insertDB($host, $port, $username, $auth, $custom_data = null){
require('vendor/autoload.php');
$client = new Everyman\Neo4j\Client($host, $port);
$client->getTransport()
->setAuth($username, $auth);
if(is_null($custom_data)){
$values = $this->crawlPubmed();
}
else{
$values = $custom_data;
}
//$values = array_merge($this->crawlPubmed(), $this->crawlIEEE());
//print_r($values);
//return;
if(!is_array($values) || !isset($values[0])){
print_r($values);
die("something is wrong with the values");
}
$transaction = $client->beginTransaction();
$neo4jQueryArray = array();
//$queryForCentralPerson = "merge (u:Person {name: \"".$this->lastName.", ".$this->firstName."\"}) set u.affiliation = \"".$this->affiliation."\"";
//$query = new Query($client, $queryForCentralPerson);
//$result = $transaction->addStatements($query);
foreach($values as $one){
//print_r($one) has the format:
//[17] => Array
//(
// [title] => RNA-protein distance patterns in ribosomes reveal the mechanism of translational attenuation.
// [url] => http://www.ncbi.nlm.nih.gov/pubmed/25326828
// [people] => Array
// (
// [0] => Array
// (
// [firstName] => DongMei
// [lastName] => Yu
// [affiliation] => Department of Biological Engineering, University of Missouri, Columbia, MO, 65211, USA.
// )
// [1] => Array
// (
// [firstName] => Chao
// [lastName] => Zhang
// [affiliation] =>
// )
// )
//)
(array_key_exists('title', $one) && !empty($one['title'])) ? $articleTitle = $one['title'] : die("why the title has not been set?");
if(!array_key_exists('url', $one)){
$articleURL = "";
}
else{
$articleURL = $one['url'];
}
(array_key_exists('people', $one) && is_array($one['people']) && isset($one['people'][0])) ? $articleAuthorList = $one['people'] : die("People list might be wrong");
$queryToCreatePublication = "merge (paper:Publication {title: \"". addslashes($articleTitle)."\"}) set paper.url = \"".$articleURL."\"";
$neo4jQueryArray[] = new Query($client, $queryToCreatePublication);
//$result = $transaction->addStatements($query);
//$queryToCreateWroteRelation = "match (u:Person {name: \"". $this->lastName.", ".$this->firstName."\"}), (p:Publication {title: \"".addslashes($theTitle)."\"}) create unique (u)-[r:Wrote]->(p)";
//echo $queryToCreateWroteRelation."\n";
//$query = new Query($client, $queryToCreateWroteRelation);
//$result = $transaction->addStatements($query);
foreach($articleAuthorList as $p){
if(!is_array($p) || !array_key_exists('firstName', $p) || !array_key_exists('lastName', $p) || !array_key_exists('affiliation', $p)){
die("Ariticle Author List format Error");
}
if(empty($p['firstName']) || empty($p['lastName'])){
continue;
}
if(empty($p['affiliation'])){
$queryToCreatePerson = "merge (author:Person {name: \"".$p['lastName'].", ".$p['firstName']."\"})";
}
else{
$queryToCreatePerson = "merge (author:Person {name: \"".$p['lastName'].", ".$p['firstName']."\"}) set author.affiliation = \"".addslashes($p['affiliation'])."\"";
}
$neo4jQueryArray[] = new Query($client, $queryToCreatePerson);
$queryToCreateWroteRelation = "match (u:Person {name: \"". $p['lastName'].", ".$p['firstName']."\"}), (p:Publication {title: \"".addslashes($articleTitle)."\"}) create unique (u)-[r:Wrote]->(p)";
$neo4jQueryArray[] = new Query($client, $queryToCreateWroteRelation);
$queryToAdjustNumOfPublication = "match (u:Person {name: \"". $p['lastName'].", ".$p['firstName'] ."\"}) - [:Wrote] - (p) with u, count(distinct p) as num set u.publicationNum = num";
$neo4jQueryArray[] = new Query($client, $queryToAdjustNumOfPublication);
//addMOProperty($p['lastName'].", ".$p['firstName'], $client);
//should be after transition->commit()
}
for($i=0; $i<sizeof($articleAuthorList); $i++){
for($j=$i+1; $j<sizeof($articleAuthorList); $j++){
$queryToCreateCoauthoredRelation = "match (p:Publication), (u1:Person {name: \"".$articleAuthorList[$i]['lastName'].", ".$articleAuthorList[$i]['firstName']."\"}), (u2:Person {name: \"".$articleAuthorList[$j]['lastName'].", ".$articleAuthorList[$j]['firstName']."\"})
where (u1)-[:Wrote]->(p) AND (u2)-[:Wrote]->(p) with count(p) as n
match (u1:Person {name: \"".$articleAuthorList[$i]['lastName'].", ".$articleAuthorList[$i]['firstName']."\"}), (u2:Person {name: \"".$articleAuthorList[$j]['lastName'].", ".$articleAuthorList[$j]['firstName']."\"})
create unique (u1)-[r:COAUTHORED]-(u2) set r.numOfPapers = n";
//echo $queryToCreateCoauthoredRelation."\n";
$neo4jQueryArray[] = new Query($client, $queryToCreateCoauthoredRelation);
//$result = $query->getResultSet();
//$result = $transaction->addStatements($query);
}
}
}
$transaction -> addStatements($neo4jQueryArray, true);
foreach($values as $one){
(array_key_exists('people', $one) && is_array($one['people']) && isset($one['people'][0])) ? $articleAuthorList = $one['people'] : die("People list might be wrong");
foreach($articleAuthorList as $p) {
if(!is_array($p) || !array_key_exists('firstName', $p) || !array_key_exists('lastName', $p) || !array_key_exists('affiliation', $p)){
die("Ariticle Author List format Error");
}
if(empty($p['firstName']) || empty($p['lastName'])){
continue;
}
addMOProperty($p['lastName'].", ".$p['firstName'], $client);
}
}
}
}
?>