tripal_chado.pub_importer_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 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 = "https://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" .
  194. "db=Pubmed" .
  195. "&retmax=$retmax" .
  196. "&usehistory=y" .
  197. "&term=" . urlencode($search_str);
  198. usleep(333334); // 1/3 of a second delay, NCBI limits requests to 3 / second without API key
  199. $rfh = fopen($query_url, "r");
  200. if (!$rfh) {
  201. drupal_set_message('Could not perform Pubmed query. Cannot connect to Entrez.', 'error');
  202. tripal_report_error('tripal_pubmed', TRIPAL_ERROR, "Could not perform Pubmed query. Cannot connect to Entrez.",
  203. []);
  204. return 0;
  205. }
  206. // retrieve the XML results
  207. $query_xml = '';
  208. while (!feof($rfh)) {
  209. $query_xml .= fread($rfh, 255);
  210. }
  211. fclose($rfh);
  212. $xml = new XMLReader();
  213. $xml->xml($query_xml);
  214. // iterate though the child nodes of the <eSearchResult> tag and get the count, history and query_id
  215. $result = [];
  216. while ($xml->read()) {
  217. $element = $xml->name;
  218. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'WebEnv') {
  219. // we've read as much as we need. If we go too much further our counts
  220. // will get messed up by other 'Count' elements. so we're done.
  221. break;
  222. }
  223. if ($xml->nodeType == XMLReader::ELEMENT) {
  224. switch ($element) {
  225. case 'Count':
  226. $xml->read();
  227. $result['Count'] = $xml->value;
  228. break;
  229. case 'WebEnv':
  230. $xml->read();
  231. $result['WebEnv'] = $xml->value;
  232. break;
  233. case 'QueryKey':
  234. $xml->read();
  235. $result['QueryKey'] = $xml->value;
  236. break;
  237. }
  238. }
  239. }
  240. return $result;
  241. }
  242. /**
  243. * Retrieves from PubMed a set of publications from the
  244. * previously initiated query.
  245. *
  246. * @param $query_key
  247. * The esearch QueryKey
  248. * @param $web_env
  249. * The esearch WebEnv
  250. * @param $rettype
  251. * The efetch return type
  252. * @param $retmod
  253. * The efetch return mode
  254. * @param $start
  255. * The start of the range to retrieve
  256. * @param $limit
  257. * The number of publications to retrieve
  258. * @param $args
  259. * Any additional arguments to add the efetch query URL
  260. *
  261. * @return
  262. * An array containing the total_records in the dataaset, the search string
  263. * and an array of the publications that were retreived.
  264. *
  265. * @ingroup tripal_pub
  266. */
  267. function tripal_pub_PMID_fetch($query_key, $web_env, $rettype = 'null',
  268. $retmod = 'null', $start = 0, $limit = 10, $args = []) {
  269. // repeat the search performed previously (using WebEnv & QueryKey) to retrieve
  270. // the PMID's within the range specied. The PMIDs will be returned as a text list
  271. $fetch_url = "https://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?" .
  272. "rettype=$rettype" .
  273. "&retmode=$retmod" .
  274. "&retstart=$start" .
  275. "&retmax=$limit" .
  276. "&db=Pubmed" .
  277. "&query_key=$query_key" .
  278. "&WebEnv=$web_env";
  279. foreach ($args as $key => $value) {
  280. if (is_array($value)) {
  281. $fetch_url .= "&$key=";
  282. foreach ($value as $item) {
  283. $fetch_url .= "$item,";
  284. }
  285. $fetch_url = substr($fetch_url, 0, -1); // remove trailing comma
  286. }
  287. else {
  288. $fetch_url .= "&$key=$value";
  289. }
  290. }
  291. usleep(333334); // 1/3 of a second delay, NCBI limits requests to 3 / second without API key
  292. $rfh = fopen($fetch_url, "r");
  293. if (!$rfh) {
  294. drupal_set_message('ERROR: Could not perform PubMed query.', 'error');
  295. tripal_report_error('tripal_pubmed', TRIPAL_ERROR, "Could not perform PubMed query: %fetch_url.",
  296. ['%fetch_url' => $fetch_url]);
  297. return '';
  298. }
  299. $results = '';
  300. if ($rfh) {
  301. while (!feof($rfh)) {
  302. $results .= fread($rfh, 255);
  303. }
  304. fclose($rfh);
  305. }
  306. return $results;
  307. }
  308. /**
  309. * This function parses the XML containing details of a publication and
  310. * converts it into an associative array of where keys are Tripal Pub
  311. * ontology terms and the values are extracted from the XML. The
  312. * XML should contain only a single publication record.
  313. *
  314. * Information about the valid elements in the PubMed XML can be found here:
  315. * https://www.nlm.nih.gov/bsd/licensee/elements_descriptions.html
  316. *
  317. * Information about PubMed's citation format can be found here
  318. * https://www.nlm.nih.gov/bsd/policy/cit_format.html
  319. *
  320. * @param $pub_xml
  321. * An XML string describing a single publication
  322. *
  323. * @return
  324. * An array describing the publication
  325. *
  326. * @ingroup tripal_pub
  327. */
  328. function tripal_pub_PMID_parse_pubxml($pub_xml) {
  329. $pub = [];
  330. if (!$pub_xml) {
  331. return $pub;
  332. }
  333. // read the XML and iterate through it.
  334. $xml = new XMLReader();
  335. $xml->xml(trim($pub_xml));
  336. while ($xml->read()) {
  337. $element = $xml->name;
  338. if ($xml->nodeType == XMLReader::ELEMENT) {
  339. switch ($element) {
  340. case 'ERROR':
  341. $xml->read(); // get the value for this element
  342. tripal_report_error('tripal_pubmed', TRIPAL_ERROR, "Error: %err", ['%err' => $xml->value]);
  343. break;
  344. case 'PMID':
  345. // thre are multiple places where a PMID is present in the XML and
  346. // since this code does not descend into every branch of the XML tree
  347. // we will encounter many of them here. Therefore, we only want the
  348. // PMID that we first encounter. If we already have the PMID we will
  349. // just skip it. Examples of other PMIDs are in the articles that
  350. // cite this one.
  351. $xml->read(); // get the value for this element
  352. if (!array_key_exists('Publication Dbxref', $pub)) {
  353. $pub['Publication Dbxref'] = 'PMID:' . $xml->value;
  354. }
  355. break;
  356. case 'Article':
  357. $pub_model = $xml->getAttribute('PubModel');
  358. $pub['Publication Model'] = $pub_model;
  359. tripal_pub_PMID_parse_article($xml, $pub);
  360. break;
  361. case 'MedlineJournalInfo':
  362. tripal_pub_PMID_parse_medline_journal_info($xml, $pub);
  363. break;
  364. case 'ChemicalList':
  365. // TODO: handle this
  366. break;
  367. case 'SupplMeshList':
  368. // TODO: meant for protocol list
  369. break;
  370. case 'CitationSubset':
  371. // TODO: not sure this is needed.
  372. break;
  373. case 'CommentsCorrections':
  374. // TODO: handle this
  375. break;
  376. case 'GeneSymbolList':
  377. // TODO: handle this
  378. break;
  379. case 'MeshHeadingList':
  380. // TODO: Medical subject headings
  381. break;
  382. case 'NumberOfReferences':
  383. // TODO: not sure we should keep this as it changes frequently.
  384. break;
  385. case 'PersonalNameSubjectList':
  386. // TODO: for works about an individual or with biographical note/obituary.
  387. break;
  388. case 'OtherID':
  389. // TODO: ID's from another NLM partner.
  390. break;
  391. case 'OtherAbstract':
  392. // TODO: when the journal does not contain an abstract for the publication.
  393. break;
  394. case 'KeywordList':
  395. // TODO: handle this
  396. break;
  397. case 'InvestigatorList':
  398. // TODO: personal names of individuals who are not authors (can be used with collection)
  399. break;
  400. case 'GeneralNote':
  401. // TODO: handle this
  402. break;
  403. case 'DeleteCitation':
  404. // TODO: need to know how to handle this
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. }
  411. $pub['Citation'] = chado_pub_create_citation($pub);
  412. $pub['raw'] = $pub_xml;
  413. return $pub;
  414. }
  415. /**
  416. * Parses the section from the XML returned from PubMed that contains
  417. * information about the Journal
  418. *
  419. * @param $xml
  420. * The XML to parse
  421. * @param $pub
  422. * The publication object to which additional details will be added
  423. *
  424. * @ingroup tripal_pub
  425. */
  426. function tripal_pub_PMID_parse_medline_journal_info($xml, &$pub) {
  427. while ($xml->read()) {
  428. // get this element name
  429. $element = $xml->name;
  430. // if we're at the </Article> element then we're done with the article...
  431. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'MedlineJournalInfo') {
  432. return;
  433. }
  434. if ($xml->nodeType == XMLReader::ELEMENT) {
  435. switch ($element) {
  436. case 'Country':
  437. // the place of publication of the journal
  438. $xml->read();
  439. $pub['Journal Country'] = $xml->value;
  440. break;
  441. case 'MedlineTA':
  442. // TODO: not sure how this is different from ISOAbbreviation
  443. break;
  444. case 'NlmUniqueID':
  445. // TODO: the journal's unique ID in medline
  446. break;
  447. case 'ISSNLinking':
  448. // TODO: not sure how this is different from ISSN
  449. break;
  450. default:
  451. break;
  452. }
  453. }
  454. }
  455. }
  456. /**
  457. * Parses the section from the XML returned from PubMed that contains
  458. * information about an article.
  459. *
  460. * @param $xml
  461. * The XML to parse
  462. * @param $pub
  463. * The publication object to which additional details will be added
  464. *
  465. * @ingroup tripal_pub
  466. */
  467. function tripal_pub_PMID_parse_article($xml, &$pub) {
  468. while ($xml->read()) {
  469. // get this element name
  470. $element = $xml->name;
  471. // if we're at the </Article> element then we're done with the article...
  472. if ($xml->nodeType == XMLReader::END_ELEMENT and $element == 'Article') {
  473. return;
  474. }
  475. if ($xml->nodeType == XMLReader::ELEMENT) {
  476. switch ($element) {
  477. case 'Journal':
  478. tripal_pub_PMID_parse_journal($xml, $pub);
  479. break;
  480. case 'ArticleTitle':
  481. $pub['Title'] = $xml->readString();
  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 = [
  576. 'name' => $value,
  577. 'cv_id' => [
  578. 'name' => 'tripal_pub',
  579. ],
  580. ];
  581. $options = ['case_insensitive_columns' => ['name']];
  582. $pub_cvterm = chado_get_cvterm($identifiers, $options);
  583. if (!$pub_cvterm) {
  584. // see if this we can find the name using a synonym
  585. $identifiers = [
  586. 'synonym' => [
  587. 'name' => $value,
  588. 'cv_name' => 'tripal_pub',
  589. ],
  590. ];
  591. $pub_cvterm = chado_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. ['%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. $value = $xml->readString();
  636. if ($label) {
  637. $part = "<p><b>$label</b></br>" . $value . '</p>';
  638. $abstract .= $part;
  639. $pub['Structured Abstract Part'][] = $part;
  640. }
  641. else {
  642. $abstract .= "<p>" . $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 = [];
  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 = [
  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. }