PMID.inc 27 KB

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