tripal_chado.pub_importer_PMID.inc 31 KB

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