PMID.inc 27 KB

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