tripal_chado.pub_importer_PMID.inc 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. }
  82. $search_str = '';
  83. for ($i = 1; $i <= $num_criteria; $i++) {
  84. $search_terms = trim($search_array['criteria'][$i]['search_terms']);
  85. $scope = $search_array['criteria'][$i]['scope'];
  86. $is_phrase = $search_array['criteria'][$i]['is_phrase'];
  87. $op = $search_array['criteria'][$i]['operation'];
  88. if ($op) {
  89. $search_str .= "$op ";
  90. }
  91. // if this is phrase make sure the search terms are surrounded by quotes
  92. if ($is_phrase) {
  93. $search_str .= "(\"$search_terms\" |SCOPE|)";
  94. }
  95. // if this is not a phase then we want to separate each 'OR or 'AND' into a unique criteria
  96. else {
  97. $search_str .= "(";
  98. if (preg_match('/and/i', $search_terms)) {
  99. $elements = preg_split('/\s+and+\s/i', $search_terms);
  100. foreach ($elements as $element) {
  101. $search_str .= "($element |SCOPE|) AND ";
  102. }
  103. $search_str = substr($search_str, 0, -5); // remove trailing 'AND '
  104. }
  105. elseif (preg_match('/or/i', $search_terms)) {
  106. $elements = preg_split('/\s+or+\s/i', $search_terms);
  107. foreach ($elements as $element) {
  108. $search_str .= "($element |SCOPE|) OR ";
  109. }
  110. $search_str = substr($search_str, 0, -4); // remove trailing 'OR '
  111. }
  112. else {
  113. $search_str .= "($search_terms |SCOPE|)";
  114. }
  115. $search_str .= ')';
  116. }
  117. if ($scope == 'title') {
  118. $search_str = preg_replace('/\|SCOPE\|/', '[Title]', $search_str);
  119. }
  120. elseif ($scope == 'author') {
  121. $search_str = preg_replace('/\|SCOPE\|/', '[Author]', $search_str);
  122. }
  123. elseif ($scope == 'abstract') {
  124. $search_str = preg_replace('/\|SCOPE\|/', '[Title/Abstract]', $search_str);
  125. }
  126. elseif ($scope == 'journal') {
  127. $search_str = preg_replace('/\|SCOPE\|/', '[Journal]', $search_str);
  128. }
  129. elseif ($scope == 'id') {
  130. $search_str = preg_replace('/PMID:([^\s]*)/', '$1', $search_str);
  131. $search_str = preg_replace('/\|SCOPE\|/', '[Uid]', $search_str);
  132. }
  133. else {
  134. $search_str = preg_replace('/\|SCOPE\|/', '', $search_str);
  135. }
  136. }
  137. if ($days) {
  138. // get the date of the day suggested
  139. $past_timestamp = time() - ($days * 86400);
  140. $past_date = getdate($past_timestamp);
  141. $search_str .= " AND (\"" . sprintf("%04d/%02d/%02d", $past_date['year'], $past_date['mon'], $past_date['mday']) . "\"[Date - Create] : \"3000\"[Date - Create]))";
  142. }
  143. // now initialize the query
  144. $results = tripal_pub_PMID_search_init($search_str, $num_to_retrieve);
  145. $total_records = $results['Count'];
  146. $query_key = $results['QueryKey'];
  147. $web_env = $results['WebEnv'];
  148. // initialize the pager
  149. $start = $page * $num_to_retrieve;
  150. // if we have no records then return an empty array
  151. if ($total_records == 0) {
  152. return [
  153. 'total_records' => $total_records,
  154. 'search_str' => $search_str,
  155. 'pubs' => [],
  156. ];
  157. }
  158. // now get the list of PMIDs from the initialized search
  159. $pmids_txt = tripal_pub_PMID_fetch($query_key, $web_env, 'uilist', 'text', $start, $num_to_retrieve);
  160. // iterate through each PMID and get the publication record. This requires a new search and new fetch
  161. $pmids = explode("\n", trim($pmids_txt));
  162. $pubs = [];
  163. foreach ($pmids as $pmid) {
  164. // now retrieve the individual record
  165. $pub_xml = tripal_pub_PMID_fetch($query_key, $web_env, 'null', 'xml', 0, 1, ['id' => $pmid]);
  166. $pub = tripal_pub_PMID_parse_pubxml($pub_xml);
  167. $pubs[] = $pub;
  168. }
  169. return [
  170. 'total_records' => $total_records,
  171. 'search_str' => $search_str,
  172. 'pubs' => $pubs,
  173. ];
  174. }
  175. /**
  176. * Initailizes a PubMed Search using a given search string
  177. *
  178. * @param $search_str
  179. * The PubMed Search string
  180. * @param $retmax
  181. * The maximum number of records to return
  182. *
  183. * @return
  184. * An array containing the Count, WebEnv and QueryKey as return
  185. * by PubMed's esearch utility
  186. *
  187. * @ingroup tripal_pub
  188. */
  189. function tripal_pub_PMID_search_init($search_str, $retmax) {
  190. // do a search for a single result so that we can establish a history, and get
  191. // the number of records. Once we have the number of records we can retrieve
  192. // those requested in the range.
  193. $query_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" .
  194. "db=Pubmed" .
  195. "&retmax=$retmax" .
  196. "&usehistory=y" .
  197. "&term=" . urlencode($search_str);
  198. $rfh = fopen($query_url, "r");
  199. if (!$rfh) {
  200. drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
  201. tripal_report_error('tripal_pubmed', TRIPAL_ERROR, "Could not perform Pubmed query. Cannot connect to Entrez.",
  202. []);
  203. return 0;
  204. }
  205. // retrieve the XML results
  206. $query_xml = '';
  207. while (!feof($rfh)) {
  208. $query_xml .= fread($rfh, 255);
  209. }
  210. fclose($rfh);
  211. $xml = new XMLReader();
  212. $xml->xml($query_xml);
  213. // iterate though the child nodes of the <eSearchResult> tag and get the count, history and query_id
  214. $result = [];
  215. while ($xml->read()) {
  216. $element = $xml->name;
  217. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'WebEnv') {
  218. // we've read as much as we need. If we go too much further our counts
  219. // will get messed up by other 'Count' elements. so we're done.
  220. break;
  221. }
  222. if ($xml->nodeType == XMLReader::ELEMENT) {
  223. switch ($element) {
  224. case 'Count':
  225. $xml->read();
  226. $result['Count'] = $xml->value;
  227. break;
  228. case 'WebEnv':
  229. $xml->read();
  230. $result['WebEnv'] = $xml->value;
  231. break;
  232. case 'QueryKey':
  233. $xml->read();
  234. $result['QueryKey'] = $xml->value;
  235. break;
  236. }
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Retrieves from PubMed a set of publications from the
  243. * previously initiated query.
  244. *
  245. * @param $query_key
  246. * The esearch QueryKey
  247. * @param $web_env
  248. * The esearch WebEnv
  249. * @param $rettype
  250. * The efetch return type
  251. * @param $retmod
  252. * The efetch return mode
  253. * @param $start
  254. * The start of the range to retrieve
  255. * @param $limit
  256. * The number of publications to retrieve
  257. * @param $args
  258. * Any additional arguments to add the efetch query URL
  259. *
  260. * @return
  261. * An array containing the total_records in the dataaset, the search string
  262. * and an array of the publications that were retreived.
  263. *
  264. * @ingroup tripal_pub
  265. */
  266. function tripal_pub_PMID_fetch($query_key, $web_env, $rettype = 'null',
  267. $retmod = 'null', $start = 0, $limit = 10, $args = []) {
  268. // repeat the search performed previously (using WebEnv & QueryKey) to retrieve
  269. // the PMID's within the range specied. The PMIDs will be returned as a text list
  270. $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?" .
  271. "rettype=$rettype" .
  272. "&retmode=$retmod" .
  273. "&retstart=$start" .
  274. "&retmax=$limit" .
  275. "&db=Pubmed" .
  276. "&query_key=$query_key" .
  277. "&WebEnv=$web_env";
  278. foreach ($args as $key => $value) {
  279. if (is_array($value)) {
  280. $fetch_url .= "&$key=";
  281. foreach ($value as $item) {
  282. $fetch_url .= "$item,";
  283. }
  284. $fetch_url = substr($fetch_url, 0, -1); // remove trailing comma
  285. }
  286. else {
  287. $fetch_url .= "&$key=$value";
  288. }
  289. }
  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. ['%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 = [];
  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", ['%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'] = chado_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. $pub['Title'] = $xml->readString();
  480. break;
  481. case 'Abstract':
  482. tripal_pub_PMID_parse_abstract($xml, $pub);
  483. break;
  484. case 'Pagination':
  485. tripal_pub_PMID_parse_pagination($xml, $pub);
  486. break;
  487. case 'ELocationID':
  488. $type = $xml->getAttribute('EIdType');
  489. $valid = $xml->getAttribute('ValidYN');
  490. $xml->read();
  491. $elocation = $xml->value;
  492. if ($type == 'doi' and $valid == 'Y') {
  493. $pub['DOI'] = $elocation;
  494. }
  495. if ($type == 'pii' and $valid == 'Y') {
  496. $pub['PII'] = $elocation;
  497. }
  498. $pub['Elocation'] = $elocation;
  499. break;
  500. case 'Affiliation':
  501. // the affiliation tag at this level is meant solely for the first author
  502. $xml->read();
  503. $pub['Author List'][0]['Affiliation'] = $xml->value;
  504. break;
  505. case 'AuthorList':
  506. $complete = $xml->getAttribute('CompleteYN');
  507. tripal_pub_PMID_parse_authorlist($xml, $pub);
  508. break;
  509. case 'InvestigatorList':
  510. // TODO: perhaps handle this one day. The investigator list is to list the names of people who
  511. // are members of a collective or corporate group that is an author in the paper.
  512. break;
  513. case 'Language':
  514. $xml->read();
  515. $lang_abbr = $xml->value;
  516. // there may be multiple languages so we store these in an array
  517. $pub['Language'][] = tripal_pub_remote_search_get_language($lang_abbr);
  518. $pub['Language Abbr'][] = $lang_abbr;
  519. break;
  520. case 'DataBankList':
  521. // TODO: handle this case
  522. break;
  523. case 'GrantList':
  524. // TODO: handle this case
  525. break;
  526. case 'PublicationTypeList':
  527. tripal_pub_PMID_parse_publication_type($xml, $pub);
  528. break;
  529. case 'VernacularTitle':
  530. $xml->read();
  531. $pub['Vernacular Title'][] = $xml->value;
  532. break;
  533. case 'ArticleDate':
  534. // TODO: figure out what to do with this element. We already have the
  535. // published date in the <PubDate> field, but this date should be in numeric
  536. // form and may have more information.
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. }
  543. }
  544. /**
  545. * Parses the section from the XML returned from PubMed that contains
  546. * information about a publication
  547. *
  548. * A full list of publication types can be found here:
  549. * http://www.nlm.nih.gov/mesh/pubtypes.html.
  550. *
  551. * The Tripal Pub ontology doesn't yet have terms for all of the
  552. * publication types so we store the value in the 'publication_type' term.
  553. *
  554. * @param $xml
  555. * The XML to parse
  556. * @param $pub
  557. * The publication object to which additional details will be added
  558. *
  559. * @ingroup tripal_pub
  560. */
  561. function tripal_pub_PMID_parse_publication_type($xml, &$pub) {
  562. while ($xml->read()) {
  563. $element = $xml->name;
  564. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'PublicationTypeList') {
  565. // we've reached the </PublicationTypeList> element so we're done.
  566. return;
  567. }
  568. if ($xml->nodeType == XMLReader::ELEMENT) {
  569. switch ($element) {
  570. case 'PublicationType':
  571. $xml->read();
  572. $value = $xml->value;
  573. $identifiers = [
  574. 'name' => $value,
  575. 'cv_id' => [
  576. 'name' => 'tripal_pub',
  577. ],
  578. ];
  579. $options = ['case_insensitive_columns' => ['name']];
  580. $pub_cvterm = chado_get_cvterm($identifiers, $options);
  581. if (!$pub_cvterm) {
  582. // see if this we can find the name using a synonym
  583. $identifiers = [
  584. 'synonym' => [
  585. 'name' => $value,
  586. 'cv_name' => 'tripal_pub',
  587. ],
  588. ];
  589. $pub_cvterm = chado_get_cvterm($identifiers, $options);
  590. if (!$pub_cvterm) {
  591. tripal_report_error('tripal_pubmed', TRIPAL_ERROR,
  592. 'Cannot find a valid vocabulary term for the publication type: "%term".',
  593. ['%term' => $value]);
  594. }
  595. }
  596. else {
  597. $pub['Publication Type'][] = $pub_cvterm->name;
  598. }
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. }
  605. }
  606. /**
  607. * Parses the section from the XML returned from PubMed that contains
  608. * information about the abstract
  609. *
  610. * @param $xml
  611. * The XML to parse
  612. * @param $pub
  613. * The publication object to which additional details will be added
  614. *
  615. * @ingroup tripal_pub
  616. */
  617. function tripal_pub_PMID_parse_abstract($xml, &$pub) {
  618. $abstract = '';
  619. while ($xml->read()) {
  620. $element = $xml->name;
  621. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Abstract') {
  622. // we've reached the </Abstract> element so return
  623. $pub['Abstract'] = $abstract;
  624. return;
  625. }
  626. // the abstract text can be just a singe paragraph or be broken into multiple
  627. // abstract texts for structured abstracts. Here we will just combine then
  628. // into a single element in the order that they arrive in HTML format
  629. if ($xml->nodeType == XMLReader::ELEMENT) {
  630. switch ($element) {
  631. case 'AbstractText':
  632. $label = $xml->getAttribute('Label');
  633. $value = $xml->readString();
  634. if ($label) {
  635. $part = "<p><b>$label</b></br>" . $value . '</p>';
  636. $abstract .= $part;
  637. $pub['Structured Abstract Part'][] = $part;
  638. }
  639. else {
  640. $abstract .= "<p>" . $value . "</p>";
  641. }
  642. break;
  643. case 'CopyrightInformation':
  644. $xml->read();
  645. $pub['Copyright'] = $xml->value;
  646. break;
  647. default:
  648. break;
  649. }
  650. }
  651. }
  652. }
  653. /**
  654. * Parses the section from the XML returned from PubMed that contains
  655. * information about pagination
  656. *
  657. * @param $xml
  658. * The XML to parse
  659. * @param $pub
  660. * The publication object to which additional details will be added
  661. *
  662. * @ingroup tripal_pub
  663. */
  664. function tripal_pub_PMID_parse_pagination($xml, &$pub) {
  665. while ($xml->read()) {
  666. $element = $xml->name;
  667. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Pagination') {
  668. // we've reached the </Pagination> element so we're done.
  669. return;
  670. }
  671. if ($xml->nodeType == XMLReader::ELEMENT) {
  672. switch ($element) {
  673. case 'MedlinePgn':
  674. $xml->read();
  675. if (trim($xml->value)) {
  676. $pub['Pages'] = $xml->value;
  677. }
  678. break;
  679. default:
  680. break;
  681. }
  682. }
  683. }
  684. }
  685. /**
  686. * Parses the section from the XML returned from PubMed that contains
  687. * information about a journal
  688. *
  689. * @param $xml
  690. * The XML to parse
  691. * @param $pub
  692. * The publication object to which additional details will be added
  693. *
  694. * @ingroup tripal_pub
  695. */
  696. function tripal_pub_PMID_parse_journal($xml, &$pub) {
  697. while ($xml->read()) {
  698. $element = $xml->name;
  699. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Journal') {
  700. return;
  701. }
  702. if ($xml->nodeType == XMLReader::ELEMENT) {
  703. switch ($element) {
  704. case 'ISSN':
  705. $issn_type = $xml->getAttribute('IssnType');
  706. $xml->read();
  707. $issn = $xml->value;
  708. $pub['ISSN'] = $issn;
  709. if ($issn_type == 'Electronic') {
  710. $pub['eISSN'] = $issn;
  711. }
  712. if ($issn_type == 'Print') {
  713. $pub['pISSN'] = $issn;
  714. }
  715. break;
  716. case 'JournalIssue':
  717. // valid values of cited_medium are 'Internet' and 'Print'
  718. $cited_medium = $xml->getAttribute('CitedMedium');
  719. tripal_pub_PMID_parse_journal_issue($xml, $pub);
  720. break;
  721. case 'Title':
  722. $xml->read();
  723. $pub['Journal Name'] = $xml->value;
  724. break;
  725. case 'ISOAbbreviation':
  726. $xml->read();
  727. $pub['Journal Abbreviation'] = $xml->value;
  728. break;
  729. default:
  730. break;
  731. }
  732. }
  733. }
  734. }
  735. /**
  736. * Parses the section from the XML returned from PubMed that contains
  737. * information about a journal issue
  738. *
  739. * @param $xml
  740. * The XML to parse
  741. * @param $pub
  742. * The publication object to which additional details will be added
  743. *
  744. * @ingroup tripal_pub
  745. */
  746. function tripal_pub_PMID_parse_journal_issue($xml, &$pub) {
  747. while ($xml->read()) {
  748. $element = $xml->name;
  749. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'JournalIssue') {
  750. // if we're at the </JournalIssue> element then we're done
  751. return;
  752. }
  753. if ($xml->nodeType == XMLReader::ELEMENT) {
  754. switch ($element) {
  755. case 'Volume':
  756. $xml->read();
  757. $pub['Volume'] = $xml->value;
  758. break;
  759. case 'Issue':
  760. $xml->read();
  761. $pub['Issue'] = $xml->value;
  762. break;
  763. case 'PubDate':
  764. $date = tripal_pub_PMID_parse_date($xml, 'PubDate');
  765. $year = $date['year'];
  766. $month = array_key_exists('month', $date) ? $date['month'] : '';
  767. $day = array_key_exists('day', $date) ? $date['day'] : '';
  768. $medline = array_key_exists('medline', $date) ? $date['medline'] : '';
  769. $pub['Year'] = $year;
  770. if ($month and $day and $year) {
  771. $pub['Publication Date'] = "$year $month $day";
  772. }
  773. elseif ($month and !$day and $year) {
  774. $pub['Publication Date'] = "$year $month";
  775. }
  776. elseif (!$month and !$day and $year) {
  777. $pub['Publication Date'] = $year;
  778. }
  779. elseif ($medline) {
  780. $pub['Publication Date'] = $medline;
  781. }
  782. else {
  783. $pub['Publication Date'] = "Date Unknown";
  784. }
  785. break;
  786. default:
  787. break;
  788. }
  789. }
  790. }
  791. }
  792. /**
  793. * Parses the section from the XML returned from PubMed that contains
  794. * information regarding to dates
  795. *
  796. * @param $xml
  797. * The XML to parse
  798. * @param $pub
  799. * The publication object to which additional details will be added
  800. *
  801. * @ingroup tripal_pub
  802. */
  803. function tripal_pub_PMID_parse_date($xml, $element_name) {
  804. $date = [];
  805. while ($xml->read()) {
  806. $element = $xml->name;
  807. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == $element_name) {
  808. // if we're at the </$element_name> then we're done
  809. return $date;
  810. }
  811. if ($xml->nodeType == XMLReader::ELEMENT) {
  812. switch ($element) {
  813. case 'Year':
  814. $xml->read();
  815. $date['year'] = $xml->value;
  816. break;
  817. case 'Month':
  818. $xml->read();
  819. $month =
  820. $date['month'] = $xml->value;
  821. break;
  822. case 'Day':
  823. $xml->read();
  824. $date['day'] = $xml->value;
  825. break;
  826. case 'MedlineDate':
  827. // the medline date is when the date cannot be broken into distinct month day year.
  828. $xml->read();
  829. $date['year'] = preg_replace('/^(\d{4}).*$/', '\1', $xml->value);
  830. $date['medline'] = $xml->value;
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. }
  837. }
  838. /**
  839. * Parses the section from the XML returned from PubMed that contains
  840. * information about the author list for a publication
  841. *
  842. * @param $xml
  843. * The XML to parse
  844. * @param $pub
  845. * The publication object to which additional details will be added
  846. *
  847. * @ingroup tripal_pub
  848. */
  849. function tripal_pub_PMID_parse_authorlist($xml, &$pub) {
  850. $num_authors = 0;
  851. while ($xml->read()) {
  852. $element = $xml->name;
  853. if ($xml->nodeType == XMLReader::END_ELEMENT) {
  854. // if we're at the </AuthorList> element then we're done with the article...
  855. if ($element == 'AuthorList') {
  856. // build the author list before returning
  857. $authors = '';
  858. foreach ($pub['Author List'] as $author) {
  859. if ($author['valid'] == 'N') {
  860. // skip non-valid entries. A non-valid entry should have
  861. // a corresponding corrected entry so we can saftely skip it.
  862. continue;
  863. }
  864. if (array_key_exists('Collective', $author)) {
  865. $authors .= $author['Collective'] . ', ';
  866. }
  867. else {
  868. $authors .= $author['Surname'] . ' ' . $author['First Initials'] . ', ';
  869. }
  870. }
  871. $authors = substr($authors, 0, -2);
  872. $pub['Authors'] = $authors;
  873. return;
  874. }
  875. // if we're at the end </Author> element then we're done with the author and we can
  876. // start a new one.
  877. if ($element == 'Author') {
  878. $num_authors++;
  879. }
  880. }
  881. if ($xml->nodeType == XMLReader::ELEMENT) {
  882. switch ($element) {
  883. case 'Author':
  884. $valid = $xml->getAttribute('ValidYN');
  885. $pub['Author List'][$num_authors]['valid'] = $valid;
  886. break;
  887. case 'LastName':
  888. $xml->read();
  889. $pub['Author List'][$num_authors]['Surname'] = $xml->value;
  890. break;
  891. case 'ForeName':
  892. $xml->read();
  893. $pub['Author List'][$num_authors]['Given Name'] = $xml->value;
  894. break;
  895. case 'Initials':
  896. $xml->read();
  897. $pub['Author List'][$num_authors]['First Initials'] = $xml->value;
  898. break;
  899. case 'Suffix':
  900. $xml->read();
  901. $pub['Author List'][$num_authors]['Suffix'] = $xml->value;
  902. break;
  903. case 'CollectiveName':
  904. $xml->read();
  905. $pub['Author List'][$num_authors]['Collective'] = $xml->value;
  906. break;
  907. case 'Identifier':
  908. // according to the specification, this element is not yet used.
  909. break;
  910. default:
  911. break;
  912. }
  913. }
  914. }
  915. }
  916. /**
  917. * Get the name of the language based on an abbreviation
  918. *
  919. * Language abbreviations were obtained here:
  920. * http://www.nlm.nih.gov/bsd/language_table.html
  921. *
  922. * @param $lang_abbr
  923. * The abbreviation of the language to return
  924. *
  925. * @return
  926. * The full name of the language
  927. *
  928. * @ingroup tripal_pub
  929. */
  930. function tripal_pub_remote_search_get_language($lang_abbr) {
  931. $languages = [
  932. 'afr' => 'Afrikaans',
  933. 'alb' => 'Albanian',
  934. 'amh' => 'Amharic',
  935. 'ara' => 'Arabic',
  936. 'arm' => 'Armenian',
  937. 'aze' => 'Azerbaijani',
  938. 'ben' => 'Bengali',
  939. 'bos' => 'Bosnian',
  940. 'bul' => 'Bulgarian',
  941. 'cat' => 'Catalan',
  942. 'chi' => 'Chinese',
  943. 'cze' => 'Czech',
  944. 'dan' => 'Danish',
  945. 'dut' => 'Dutch',
  946. 'eng' => 'English',
  947. 'epo' => 'Esperanto',
  948. 'est' => 'Estonian',
  949. 'fin' => 'Finnish',
  950. 'fre' => 'French',
  951. 'geo' => 'Georgian',
  952. 'ger' => 'German',
  953. 'gla' => 'Scottish Gaelic',
  954. 'gre' => 'Greek, Modern',
  955. 'heb' => 'Hebrew',
  956. 'hin' => 'Hindi',
  957. 'hrv' => 'Croatian',
  958. 'hun' => 'Hungarian',
  959. 'ice' => 'Icelandic',
  960. 'ind' => 'Indonesian',
  961. 'ita' => 'Italian',
  962. 'jpn' => 'Japanese',
  963. 'kin' => 'Kinyarwanda',
  964. 'kor' => 'Korean',
  965. 'lat' => 'Latin',
  966. 'lav' => 'Latvian',
  967. 'lit' => 'Lithuanian',
  968. 'mac' => 'Macedonian',
  969. 'mal' => 'Malayalam',
  970. 'mao' => 'Maori',
  971. 'may' => 'Malay',
  972. 'mul' => 'Multiple languages',
  973. 'nor' => 'Norwegian',
  974. 'per' => 'Persian',
  975. 'pol' => 'Polish',
  976. 'por' => 'Portuguese',
  977. 'pus' => 'Pushto',
  978. 'rum' => 'Romanian, Rumanian, Moldovan',
  979. 'rus' => 'Russian',
  980. 'san' => 'Sanskrit',
  981. 'slo' => 'Slovak',
  982. 'slv' => 'Slovenian',
  983. 'spa' => 'Spanish',
  984. 'srp' => 'Serbian',
  985. 'swe' => 'Swedish',
  986. 'tha' => 'Thai',
  987. 'tur' => 'Turkish',
  988. 'ukr' => 'Ukrainian',
  989. 'und' => 'Undetermined',
  990. 'urd' => 'Urdu',
  991. 'vie' => 'Vietnamese',
  992. 'wel' => 'Welsh',
  993. ];
  994. return $languages[strtolower($lang_abbr)];
  995. }