PMID.inc 26 KB

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