tripal_pub.PMID.inc 26 KB

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