PMID.inc 27 KB

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