tripal_chado.pub_importer_PMID.inc 31 KB

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