tripal_pub.PMID.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. <?php
  2. /**
  3. * @file
  4. * Tripal Pub PubMed Interface
  5. */
  6. /**
  7. * @stephen
  8. *
  9. * @param $form
  10. * @param $form_state
  11. * @param $num_criteria
  12. *
  13. * @ingroup tripal_pub
  14. */
  15. function tripal_pub_remote_alter_form_PMID($form, $form_state, $num_criteria = 1) {
  16. // PubMed doesn't have an 'Abstract' field, so we need to convert the criteria
  17. // from 'Abstract' to 'Title/Abstract'
  18. for($i = 1; $i <= $num_criteria; $i++) {
  19. $form['themed_element']['criteria'][$i]["scope-$i"]['#options']['abstract'] = 'Abstract/Title';
  20. }
  21. return $form;
  22. }
  23. /**
  24. * Validate the pub med form
  25. *
  26. * @param $form
  27. * @param $form_state
  28. *
  29. * @return
  30. * The form (drupal form api)
  31. *
  32. * @ingroup tripal_pub
  33. */
  34. function tripal_pub_remote_validate_form_PMID($form, $form_state) {
  35. $num_criteria = $form_state['values']['num_criteria'];
  36. for ($i = 1; $i <= $num_criteria; $i++) {
  37. $search_terms = trim($form_state['values']["search_terms-$i"]);
  38. $scope = $form_state['values']["scope-$i"];
  39. if ($scope == 'id' and !preg_match('/^PMID:\d+$/', $search_terms)) {
  40. form_set_error("search_terms-$i", "The PubMed accession must be a numeric value, prefixed with 'PMID:' (e.g. PMID:23024789).");
  41. }
  42. }
  43. return $form;
  44. }
  45. /**
  46. * @stephen
  47. *
  48. * @param $search_array
  49. * @param $num_to_retrieve
  50. * @param $page
  51. *
  52. * @ingroup tripal_pub
  53. */
  54. function tripal_pub_remote_search_PMID($search_array, $num_to_retrieve, $page) {
  55. // convert the terms list provicded by the caller into a string with words
  56. // separated by a '+' symbol.
  57. $num_criteria = $search_array['num_criteria'];
  58. $days = $search_array['days'];
  59. $search_str = '';
  60. for ($i = 1; $i <= $num_criteria; $i++) {
  61. $search_terms = trim($search_array['criteria'][$i]['search_terms']);
  62. $scope = $search_array['criteria'][$i]['scope'];
  63. $is_phrase = $search_array['criteria'][$i]['is_phrase'];
  64. $op = $search_array['criteria'][$i]['operation'];
  65. if ($op) {
  66. $search_str .= "$op ";
  67. }
  68. // if this is phrase make sure the search terms are surrounded by quotes
  69. if ($is_phrase) {
  70. $search_str .= "(\"$search_terms\" |SCOPE|)";
  71. }
  72. // if this is not a phase then we want to separate each 'OR or 'AND' into a unique criteria
  73. else {
  74. $search_str .= "(";
  75. if (preg_match('/and/i', $search_terms)) {
  76. $elements = preg_split('/\s+and+\s/i', $search_terms);
  77. foreach ($elements as $element) {
  78. $search_str .= "($element |SCOPE|) AND ";
  79. }
  80. $search_str = substr($search_str, 0, -5); // remove trailing 'AND '
  81. }
  82. elseif (preg_match('/or/i', $search_terms)) {
  83. $elements = preg_split('/\s+or+\s/i', $search_terms);
  84. foreach ($elements as $element) {
  85. $search_str .= "($element |SCOPE|) OR ";
  86. }
  87. $search_str = substr($search_str, 0, -4); // remove trailing 'OR '
  88. }
  89. else {
  90. $search_str .= "($search_terms |SCOPE|)";
  91. }
  92. $search_str .= ')';
  93. }
  94. if ($scope == 'title') {
  95. $search_str = preg_replace('/\|SCOPE\|/', '[Title]', $search_str);
  96. }
  97. elseif ($scope == 'author') {
  98. $search_str = preg_replace('/\|SCOPE\|/', '[Author]', $search_str);
  99. }
  100. elseif ($scope == 'abstract') {
  101. $search_str = preg_replace('/\|SCOPE\|/', '[Title/Abstract]', $search_str);
  102. }
  103. elseif ($scope == 'journal') {
  104. $search_str = preg_replace('/\|SCOPE\|/', '[Journal]', $search_str);
  105. }
  106. elseif ($scope == 'id') {
  107. $search_str = preg_replace('/PMID:([^\s]*)/', '$1', $search_str);
  108. $search_str = preg_replace('/\|SCOPE\|/', '[Uid]', $search_str);
  109. }
  110. else {
  111. $search_str = preg_replace('/\|SCOPE\|/', '', $search_str);
  112. }
  113. }
  114. if ($days) {
  115. // get the date of the day suggested
  116. $past_timestamp = REQUEST_TIME - ($days * 86400);
  117. $past_date = getdate($past_timestamp);
  118. $search_str .= " AND (\"" . sprintf("%04d/%02d/%02d", $past_date['year'], $past_date['mon'], $past_date['mday']) . "\"[Date - Create] : \"3000\"[Date - Create]))";
  119. }
  120. // now initialize the query
  121. $results = tripal_pub_PMID_search_init($search_str, $num_to_retrieve);
  122. $total_records = $results['Count'];
  123. $query_key = $results['QueryKey'];
  124. $web_env = $results['WebEnv'];
  125. // initialize the pager
  126. $start = $page * $num_to_retrieve;
  127. // if we have no records then return an empty array
  128. if ($total_records == 0) {
  129. return array(
  130. 'total_records' => $total_records,
  131. 'search_str' => $search_str,
  132. 'pubs' => array(),
  133. );
  134. }
  135. // now get the list of PMIDs from the initialized search
  136. $pmids_txt = tripal_pub_PMID_fetch($query_key, $web_env, 'uilist', 'text', $start, $num_to_retrieve);
  137. // iterate through each PMID and get the publication record. This requires a new search and new fetch
  138. $pmids = explode("\n", trim($pmids_txt));
  139. $pubs = array();
  140. foreach ($pmids as $pmid) {
  141. // now retrieve the individual record
  142. $pub_xml = tripal_pub_PMID_fetch($query_key, $web_env, 'null', 'xml', 0, 1, array('id' => $pmid));
  143. $pub = tripal_pub_PMID_parse_pubxml($pub_xml);
  144. $pubs[] = $pub;
  145. }
  146. return array(
  147. 'total_records' => $total_records,
  148. 'search_str' => $search_str,
  149. 'pubs' => $pubs,
  150. );
  151. }
  152. /**
  153. * @stephen
  154. *
  155. * @param $search_str
  156. * @param $retmax
  157. *
  158. * @ingroup tripal_pub
  159. */
  160. function tripal_pub_PMID_search_init($search_str, $retmax){
  161. // do a search for a single result so that we can establish a history, and get
  162. // the number of records. Once we have the number of records we can retrieve
  163. // those requested in the range.
  164. $query_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" .
  165. "db=Pubmed" .
  166. "&retmax=$retmax" .
  167. "&usehistory=y".
  168. "&term=" . urlencode($search_str);
  169. //dpm($query_url);
  170. $rfh = fopen($query_url, "r");
  171. if (!$rfh) {
  172. drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
  173. tripal_report_error('tpub_pubmed', TRIPAL_ERROR, "Could not perform Pubmed query. Cannot connect to Entrez.",
  174. array());
  175. return 0;
  176. }
  177. // retrieve the XML results
  178. $query_xml = '';
  179. while (!feof($rfh)) {
  180. $query_xml .= fread($rfh, 255);
  181. }
  182. fclose($rfh);
  183. //dpm("<pre>$query_xml</pre>");
  184. $xml = new XMLReader();
  185. $xml->xml($query_xml);
  186. // iterate though the child nodes of the <eSearchResult> tag and get the count, history and query_id
  187. $result = array();
  188. while ($xml->read()) {
  189. $element = $xml->name;
  190. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'WebEnv') {
  191. // we've read as much as we need. If we go too much further our counts
  192. // will get messed up by other 'Count' elements. so we're done.
  193. break;
  194. }
  195. if ($xml->nodeType == XMLReader::ELEMENT) {
  196. switch ($element) {
  197. case 'Count':
  198. $xml->read();
  199. $result['Count'] = $xml->value;
  200. break;
  201. case 'WebEnv':
  202. $xml->read();
  203. $result['WebEnv'] = $xml->value;
  204. break;
  205. case 'QueryKey':
  206. $xml->read();
  207. $result['QueryKey'] = $xml->value;
  208. break;
  209. }
  210. }
  211. }
  212. return $result;
  213. }
  214. /**
  215. * @stephen
  216. *
  217. * @param $query_key
  218. * @param $web_env
  219. * @param $rettype
  220. * @param $retmod
  221. * @param $start
  222. * @param $limit
  223. * @param $args
  224. *
  225. * @ingroup tripal_pub
  226. */
  227. function tripal_pub_PMID_fetch($query_key, $web_env, $rettype = 'null',
  228. $retmod = 'null', $start = 0, $limit = 10, $args = array()){
  229. // repeat the search performed previously (using WebEnv & QueryKey) to retrieve
  230. // the PMID's within the range specied. The PMIDs will be returned as a text list
  231. $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?".
  232. "rettype=$rettype" .
  233. "&retmode=$retmod" .
  234. "&retstart=$start" .
  235. "&retmax=$limit" .
  236. "&db=Pubmed" .
  237. "&query_key=$query_key".
  238. "&WebEnv=$web_env";
  239. foreach ($args as $key => $value) {
  240. if(is_array($value)) {
  241. $fetch_url .= "&$key=";
  242. foreach ($value as $item) {
  243. $fetch_url .= "$item,";
  244. }
  245. $fetch_url = substr($fetch_url, 0, -1); // remove trailing comma
  246. }
  247. else {
  248. $fetch_url .= "&$key=$value";
  249. }
  250. }
  251. //dpm($fetch_url);
  252. $rfh = fopen($fetch_url, "r");
  253. if (!$rfh) {
  254. drupal_set_message('ERROR: Could not perform PubMed query.', 'error');
  255. tripal_report_error('tpub_pubmed', TRIPAL_ERROR, "Could not perform PubMed query: %fetch_url.",
  256. array('%fetch_url' => $fetch_url));
  257. return '';
  258. }
  259. $results = '';
  260. if($rfh) {
  261. while (!feof($rfh)) {
  262. $results .= fread($rfh, 255);
  263. }
  264. fclose($rfh);
  265. }
  266. return $results;
  267. }
  268. /*
  269. * This function parses the XML containing details of a publication and
  270. * converts it into an associative array of where keys are Tripal Pub
  271. * ontology terms and the values are extracted from the XML. The
  272. * XML should contain only a single publication record.
  273. *
  274. * Information about the valid elements in the PubMed XML can be found here:
  275. * http://www.nlm.nih.gov/bsd/licensee/elements_descriptions.html
  276. *
  277. * Information about PubMed's citation format can be found here
  278. * http://www.nlm.nih.gov/bsd/policy/cit_format.html
  279. *
  280. * @param $pub_xml
  281. * An XML string describing a single publication
  282. *
  283. * @return
  284. * An array describing the publication
  285. *
  286. * @ingroup tripal_pub
  287. */
  288. function tripal_pub_PMID_parse_pubxml($pub_xml) {
  289. $pub = array();
  290. if (!$pub_xml) {
  291. return $pub;
  292. }
  293. // read the XML and iterate through it.
  294. $xml = new XMLReader();
  295. $xml->xml(trim($pub_xml));
  296. while ($xml->read()) {
  297. $element = $xml->name;
  298. if ($xml->nodeType == XMLReader::ELEMENT) {
  299. switch ($element) {
  300. case 'ERROR':
  301. $xml->read(); // get the value for this element
  302. tripal_report_error('tpub_pubmed', TRIPAL_ERROR, "Error: %err", array('%err' => $xml->value));
  303. break;
  304. case 'PMID':
  305. // thre are multiple places where a PMID is present in the XML and
  306. // since this code does not descend into every branch of the XML tree
  307. // we will encounter many of them here. Therefore, we only want the
  308. // PMID that we first encounter. If we already have the PMID we will
  309. // just skip it. Examples of other PMIDs are in the articles that
  310. // cite this one.
  311. $xml->read(); // get the value for this element
  312. if(!array_key_exists('Publication Dbxref', $pub)) {
  313. $pub['Publication Dbxref'] = 'PMID:' . $xml->value;
  314. }
  315. break;
  316. case 'Article':
  317. $pub_model = $xml->getAttribute('PubModel');
  318. $pub['Publication Model'] = $pub_model;
  319. tripal_pub_PMID_parse_article($xml, $pub);
  320. break;
  321. case 'MedlineJournalInfo':
  322. tripal_pub_PMID_parse_medline_journal_info($xml, $pub);
  323. break;
  324. case 'ChemicalList':
  325. // TODO: handle this
  326. break;
  327. case 'SupplMeshList':
  328. // TODO: meant for protocol list
  329. break;
  330. case 'CitationSubset':
  331. // TODO: not sure this is needed.
  332. break;
  333. case 'CommentsCorrections':
  334. // TODO: handle this
  335. break;
  336. case 'GeneSymbolList':
  337. // TODO: handle this
  338. break;
  339. case 'MeshHeadingList':
  340. // TODO: Medical subject headings
  341. break;
  342. case 'NumberOfReferences':
  343. // TODO: not sure we should keep this as it changes frequently.
  344. break;
  345. case 'PersonalNameSubjectList':
  346. // TODO: for works about an individual or with biographical note/obituary.
  347. break;
  348. case 'OtherID':
  349. // TODO: ID's from another NLM partner.
  350. break;
  351. case 'OtherAbstract':
  352. // TODO: when the journal does not contain an abstract for the publication.
  353. break;
  354. case 'KeywordList':
  355. // TODO: handle this
  356. break;
  357. case 'InvestigatorList':
  358. // TODO: personal names of individuals who are not authors (can be used with collection)
  359. break;
  360. case 'GeneralNote':
  361. // TODO: handle this
  362. break;
  363. case 'DeleteCitation':
  364. // TODO: need to know how to handle this
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. }
  371. $pub['Citation'] = tripal_pub_create_citation($pub);
  372. $pub['raw'] = $pub_xml;
  373. return $pub;
  374. }
  375. /**
  376. * @stephen
  377. *
  378. * @param $xml
  379. * @param $pub
  380. *
  381. * @ingroup tripal_pub
  382. */
  383. function tripal_pub_PMID_parse_medline_journal_info($xml, &$pub) {
  384. while ($xml->read()) {
  385. // get this element name
  386. $element = $xml->name;
  387. // if we're at the </Article> element then we're done with the article...
  388. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'MedlineJournalInfo') {
  389. return;
  390. }
  391. if ($xml->nodeType == XMLReader::ELEMENT) {
  392. switch ($element) {
  393. case 'Country':
  394. // the place of publication of the journal
  395. $xml->read();
  396. $pub['Journal Country'] = $xml->value;
  397. break;
  398. case 'MedlineTA':
  399. // TODO: not sure how this is different from ISOAbbreviation
  400. break;
  401. case 'NlmUniqueID':
  402. // TODO: the journal's unique ID in medline
  403. break;
  404. case 'ISSNLinking':
  405. // TODO: not sure how this is different from ISSN
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. /**
  414. * @stephen
  415. *
  416. * @param $xml
  417. * @param $pub
  418. *
  419. * @ingroup tripal_pub
  420. */
  421. function tripal_pub_PMID_parse_article($xml, &$pub) {
  422. while ($xml->read()) {
  423. // get this element name
  424. $element = $xml->name;
  425. // if we're at the </Article> element then we're done with the article...
  426. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Article') {
  427. return;
  428. }
  429. if ($xml->nodeType == XMLReader::ELEMENT) {
  430. switch ($element) {
  431. case 'Journal':
  432. tripal_pub_PMID_parse_journal($xml, $pub);
  433. break;
  434. case 'ArticleTitle':
  435. $xml->read();
  436. // remoave any trailing period from the title
  437. $pub['Title'] = trim(preg_replace('/\.$/', '', $xml->value));
  438. break;
  439. case 'Abstract':
  440. tripal_pub_PMID_parse_abstract($xml, $pub);
  441. break;
  442. case 'Pagination':
  443. tripal_pub_PMID_parse_pagination($xml, $pub);
  444. break;
  445. case 'ELocationID':
  446. $type = $xml->getAttribute('EIdType');
  447. $valid = $xml->getAttribute('ValidYN');
  448. $xml->read();
  449. $elocation = $xml->value;
  450. if ($type == 'doi' and $valid == 'Y') {
  451. $pub['DOI'] = $elocation;
  452. }
  453. if ($type == 'pii' and $valid == 'Y') {
  454. $pub['PII'] = $elocation;
  455. }
  456. $pub['Elocation'] = $elocation;
  457. break;
  458. case 'Affiliation':
  459. // the affiliation tag at this level is meant solely for the first author
  460. $xml->read();
  461. $pub['Author List'][0]['Affiliation'] = $xml->value;
  462. break;
  463. case 'AuthorList':
  464. $complete = $xml->getAttribute('CompleteYN');
  465. tripal_pub_PMID_parse_authorlist($xml, $pub);
  466. break;
  467. case 'InvestigatorList':
  468. // TODO: perhaps handle this one day. The investigator list is to list the names of people who
  469. // are members of a collective or corporate group that is an author in the paper.
  470. break;
  471. case 'Language':
  472. $xml->read();
  473. $lang_abbr = $xml->value;
  474. // there may be multiple languages so we store these in an array
  475. $pub['Language'][] = tripal_pub_remote_search_get_language($lang_abbr);
  476. $pub['Language Abbr'][] = $lang_abbr;
  477. break;
  478. case 'DataBankList':
  479. // TODO: handle this case
  480. break;
  481. case 'GrantList':
  482. // TODO: handle this case
  483. break;
  484. case 'PublicationTypeList':
  485. tripal_pub_PMID_parse_publication_type($xml, $pub);
  486. break;
  487. case 'VernacularTitle':
  488. $xml->read();
  489. $pub['Vernacular Title'][] = $xml->value;
  490. break;
  491. case 'ArticleDate':
  492. // TODO: figure out what to do with this element. We already have the
  493. // published date in the <PubDate> field, but this date should be in numeric
  494. // form and may have more information.
  495. break;
  496. default:
  497. break;
  498. }
  499. }
  500. }
  501. }
  502. /**
  503. * @stephen
  504. *
  505. * A full list of publication types can be found here:
  506. * http://www.nlm.nih.gov/mesh/pubtypes.html.
  507. *
  508. * The Tripal Pub ontology doesn't yet have terms for all of the
  509. * publication types so we store the value in the 'publication_type' term.
  510. *
  511. * @param $xml
  512. * @param $pub
  513. *
  514. * @ingroup tripal_pub
  515. */
  516. function tripal_pub_PMID_parse_publication_type($xml, &$pub) {
  517. while ($xml->read()) {
  518. $element = $xml->name;
  519. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'PublicationTypeList') {
  520. // we've reached the </PublicationTypeList> element so we're done.
  521. return;
  522. }
  523. if ($xml->nodeType == XMLReader::ELEMENT) {
  524. switch ($element) {
  525. case 'PublicationType':
  526. $xml->read();
  527. $value = $xml->value;
  528. $pub_cvterm = tripal_cv_get_cvterm_by_name($value, NULL, 'tripal_pub');
  529. if (!$pub_cvterm) {
  530. // see if this we can find the name using a synonym
  531. $pub_cvterm = tripal_cv_get_cvterm_by_synonym($value, NULL, 'tripal_pub');
  532. if (!$pub_cvterm) {
  533. tripal_report_error('tpub_pubmed', TRIPAL_ERROR, 'Cannot find a valid vocabulary term for the publication type: "%term".',
  534. array('%term' => $value));
  535. }
  536. }
  537. else {
  538. $pub['Publication Type'][] = $pub_cvterm->name;
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. }
  546. }
  547. /**
  548. * @stephen
  549. *
  550. * @param $xml
  551. * @param $pub
  552. *
  553. * @ingroup tripal_pub
  554. */
  555. function tripal_pub_PMID_parse_abstract($xml, &$pub) {
  556. $abstract = '';
  557. while ($xml->read()) {
  558. $element = $xml->name;
  559. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Abstract') {
  560. // we've reached the </Abstract> element so return
  561. $pub['Abstract'] = $abstract;
  562. return;
  563. }
  564. // the abstract text can be just a singe paragraph or be broken into multiple
  565. // abstract texts for structured abstracts. Here we will just combine then
  566. // into a single element in the order that they arrive in HTML format
  567. if ($xml->nodeType == XMLReader::ELEMENT) {
  568. switch ($element) {
  569. case 'AbstractText':
  570. $label = $xml->getAttribute('Label');
  571. $xml->read();
  572. if ($label) {
  573. $part = "<p><b>$label</b></br>" . $xml->value . '</p>';
  574. $abstract .= $part;
  575. $pub['Structured Abstract Part'][] = $part;
  576. }
  577. else {
  578. $abstract .= '<p>' . $xml->value . '</p>';
  579. }
  580. break;
  581. case 'CopyrightInformation':
  582. $xml->read();
  583. $pub['Copyright'] = $xml->value;
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. }
  590. }
  591. /**
  592. * @stephen
  593. *
  594. * @param $xml
  595. * @param $pub
  596. *
  597. * @ingroup tripal_pub
  598. */
  599. function tripal_pub_PMID_parse_pagination($xml, &$pub) {
  600. while ($xml->read()) {
  601. $element = $xml->name;
  602. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Pagination') {
  603. // we've reached the </Pagination> element so we're done.
  604. return;
  605. }
  606. if ($xml->nodeType == XMLReader::ELEMENT) {
  607. switch ($element) {
  608. case 'MedlinePgn':
  609. $xml->read();
  610. if(trim($xml->value)) {
  611. $pub['Pages'] = $xml->value;
  612. }
  613. break;
  614. default:
  615. break;
  616. }
  617. }
  618. }
  619. }
  620. /**
  621. * @stephen
  622. *
  623. * @param $xml
  624. * @param $pub
  625. *
  626. * @ingroup tripal_pub
  627. */
  628. function tripal_pub_PMID_parse_journal($xml, &$pub) {
  629. while ($xml->read()) {
  630. $element = $xml->name;
  631. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Journal') {
  632. return;
  633. }
  634. if ($xml->nodeType == XMLReader::ELEMENT) {
  635. switch ($element) {
  636. case 'ISSN':
  637. $issn_type = $xml->getAttribute('IssnType');
  638. $xml->read();
  639. $issn = $xml->value;
  640. $pub['ISSN'] = $issn;
  641. if ($issn_type == 'Electronic') {
  642. $pub['eISSN'] = $issn;
  643. }
  644. if ($issn_type == 'Print') {
  645. $pub['pISSN'] = $issn;
  646. }
  647. break;
  648. case 'JournalIssue':
  649. // valid values of cited_medium are 'Internet' and 'Print'
  650. $cited_medium = $xml->getAttribute('CitedMedium');
  651. tripal_pub_PMID_parse_journal_issue($xml, $pub);
  652. break;
  653. case 'Title':
  654. $xml->read();
  655. $pub['Journal Name'] = $xml->value;
  656. break;
  657. case 'ISOAbbreviation':
  658. $xml->read();
  659. $pub['Journal Abbreviation'] = $xml->value;
  660. break;
  661. default:
  662. break;
  663. }
  664. }
  665. }
  666. }
  667. /**
  668. * @stephen
  669. *
  670. * @param $xml
  671. * @param $pub
  672. *
  673. * @ingroup tripal_pub
  674. */
  675. function tripal_pub_PMID_parse_journal_issue($xml, &$pub) {
  676. while ($xml->read()) {
  677. $element = $xml->name;
  678. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'JournalIssue'){
  679. // if we're at the </JournalIssue> element then we're done
  680. return;
  681. }
  682. if ($xml->nodeType == XMLReader::ELEMENT) {
  683. switch ($element) {
  684. case 'Volume':
  685. $xml->read();
  686. $pub['Volume'] = $xml->value;
  687. break;
  688. case 'Issue':
  689. $xml->read();
  690. $pub['Issue'] = $xml->value;
  691. break;
  692. case 'PubDate':
  693. $date = tripal_pub_PMID_parse_date($xml, 'PubDate');
  694. $year = $date['year'];
  695. $month = array_key_exists('month', $date) ? $date['month'] : '';
  696. $day = array_key_exists('day', $date) ? $date['day'] : '';
  697. $medline = array_key_exists('medline', $date) ? $date['medline'] : '';
  698. $pub['Year'] = $year;
  699. if ($month and $day and $year) {
  700. $pub['Publication Date'] = "$year $month $day";
  701. }
  702. elseif ($month and !$day and $year) {
  703. $pub['Publication Date'] = "$year $month";
  704. }
  705. elseif (!$month and !$day and $year) {
  706. $pub['Publication Date'] = $year;
  707. }
  708. elseif ($medline) {
  709. $pub['Publication Date'] = $medline;
  710. }
  711. else {
  712. $pub['Publication Date'] = "Date Unknown";
  713. }
  714. break;
  715. default:
  716. break;
  717. }
  718. }
  719. }
  720. }
  721. /**
  722. * @stephen
  723. *
  724. * @param $xml
  725. * @param $element_name
  726. *
  727. * @ingroup tripal_pub
  728. */
  729. function tripal_pub_PMID_parse_date($xml, $element_name) {
  730. $date = array();
  731. while ($xml->read()) {
  732. $element = $xml->name;
  733. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == $element_name){
  734. // if we're at the </$element_name> then we're done
  735. return $date;
  736. }
  737. if ($xml->nodeType == XMLReader::ELEMENT) {
  738. switch ($element) {
  739. case 'Year':
  740. $xml->read();
  741. $date['year'] = $xml->value;
  742. break;
  743. case 'Month':
  744. $xml->read();
  745. $month =
  746. $date['month'] = $xml->value;
  747. break;
  748. case 'Day':
  749. $xml->read();
  750. $date['day'] = $xml->value;
  751. break;
  752. case 'MedlineDate':
  753. // the medline date is when the date cannot be broken into distinct month day year.
  754. $xml->read();
  755. $date['year'] = preg_replace('/^(\d{4}).*$/', '\1', $xml->value);
  756. $date['medline'] = $xml->value;
  757. break;
  758. default:
  759. break;
  760. }
  761. }
  762. }
  763. }
  764. /**
  765. * @stephen
  766. *
  767. * @param $xml
  768. * @param $pub
  769. *
  770. * @ingroup tripal_pub
  771. */
  772. function tripal_pub_PMID_parse_authorlist($xml, &$pub) {
  773. $num_authors = 0;
  774. while ($xml->read()) {
  775. $element = $xml->name;
  776. if ($xml->nodeType == XMLReader::END_ELEMENT){
  777. // if we're at the </AuthorList> element then we're done with the article...
  778. if($element == 'AuthorList') {
  779. // build the author list before returning
  780. $authors = '';
  781. foreach ($pub['Author List'] as $author) {
  782. if ($author['valid'] == 'N') {
  783. // skip non-valid entries. A non-valid entry should have
  784. // a corresponding corrected entry so we can saftely skip it.
  785. continue;
  786. }
  787. if (array_key_exists('Collective', $author)) {
  788. $authors .= $author['Collective'] . ', ';
  789. }
  790. else {
  791. $authors .= $author['Surname'] . ' ' . $author['First Initials'] . ', ';
  792. }
  793. }
  794. $authors = substr($authors, 0, -2);
  795. $pub['Authors'] = $authors;
  796. return;
  797. }
  798. // if we're at the end </Author> element then we're done with the author and we can
  799. // start a new one.
  800. if($element == 'Author') {
  801. $num_authors++;
  802. }
  803. }
  804. if ($xml->nodeType == XMLReader::ELEMENT) {
  805. switch ($element) {
  806. case 'Author':
  807. $valid = $xml->getAttribute('ValidYN');
  808. $pub['Author List'][$num_authors]['valid'] = $valid;
  809. break;
  810. case 'LastName':
  811. $xml->read();
  812. $pub['Author List'][$num_authors]['Surname'] = $xml->value;
  813. break;
  814. case 'ForeName':
  815. $xml->read();
  816. $pub['Author List'][$num_authors]['Given Name'] = $xml->value;
  817. break;
  818. case 'Initials':
  819. $xml->read();
  820. $pub['Author List'][$num_authors]['First Initials'] = $xml->value;
  821. break;
  822. case 'Suffix':
  823. $xml->read();
  824. $pub['Author List'][$num_authors]['Suffix'] = $xml->value;
  825. break;
  826. case 'CollectiveName':
  827. $xml->read();
  828. $pub['Author List'][$num_authors]['Collective'] = $xml->value;
  829. break;
  830. case 'Identifier':
  831. // according to the specification, this element is not yet used.
  832. break;
  833. default:
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. /**
  840. * Get the name of the language based on an abbreviation
  841. *
  842. * Language abbreviations were obtained here:
  843. * http://www.nlm.nih.gov/bsd/language_table.html
  844. *
  845. * @param $lang_abbr
  846. * The abbreviation of the language to return
  847. *
  848. * @return
  849. * The full name of the language
  850. *
  851. * @ingroup tripal_pub
  852. */
  853. function tripal_pub_remote_search_get_language($lang_abbr) {
  854. $languages = array(
  855. 'afr' => 'Afrikaans',
  856. 'alb' => 'Albanian',
  857. 'amh' => 'Amharic',
  858. 'ara' => 'Arabic',
  859. 'arm' => 'Armenian',
  860. 'aze' => 'Azerbaijani',
  861. 'ben' => 'Bengali',
  862. 'bos' => 'Bosnian',
  863. 'bul' => 'Bulgarian',
  864. 'cat' => 'Catalan',
  865. 'chi' => 'Chinese',
  866. 'cze' => 'Czech',
  867. 'dan' => 'Danish',
  868. 'dut' => 'Dutch',
  869. 'eng' => 'English',
  870. 'epo' => 'Esperanto',
  871. 'est' => 'Estonian',
  872. 'fin' => 'Finnish',
  873. 'fre' => 'French',
  874. 'geo' => 'Georgian',
  875. 'ger' => 'German',
  876. 'gla' => 'Scottish Gaelic',
  877. 'gre' => 'Greek, Modern',
  878. 'heb' => 'Hebrew',
  879. 'hin' => 'Hindi',
  880. 'hrv' => 'Croatian',
  881. 'hun' => 'Hungarian',
  882. 'ice' => 'Icelandic',
  883. 'ind' => 'Indonesian',
  884. 'ita' => 'Italian',
  885. 'jpn' => 'Japanese',
  886. 'kin' => 'Kinyarwanda',
  887. 'kor' => 'Korean',
  888. 'lat' => 'Latin',
  889. 'lav' => 'Latvian',
  890. 'lit' => 'Lithuanian',
  891. 'mac' => 'Macedonian',
  892. 'mal' => 'Malayalam',
  893. 'mao' => 'Maori',
  894. 'may' => 'Malay',
  895. 'mul' => 'Multiple languages',
  896. 'nor' => 'Norwegian',
  897. 'per' => 'Persian',
  898. 'pol' => 'Polish',
  899. 'por' => 'Portuguese',
  900. 'pus' => 'Pushto',
  901. 'rum' => 'Romanian, Rumanian, Moldovan',
  902. 'rus' => 'Russian',
  903. 'san' => 'Sanskrit',
  904. 'slo' => 'Slovak',
  905. 'slv' => 'Slovenian',
  906. 'spa' => 'Spanish',
  907. 'srp' => 'Serbian',
  908. 'swe' => 'Swedish',
  909. 'tha' => 'Thai',
  910. 'tur' => 'Turkish',
  911. 'ukr' => 'Ukrainian',
  912. 'und' => 'Undetermined',
  913. 'urd' => 'Urdu',
  914. 'vie' => 'Vietnamese',
  915. 'wel' => 'Welsh',
  916. );
  917. return $languages[strtolower($lang_abbr)];
  918. }