PMID.inc 27 KB

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