tripal_pub.PMID.inc 31 KB

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