PMID.inc 26 KB

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