tripal_pub.api.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Pub API
  5. *
  6. * @defgroup tripal_pub_api Publication Module API
  7. * @ingroup tripal_api
  8. */
  9. /*
  10. * Retrieves a list of publications as an associated array where
  11. * keys correspond directly with Tripal Pub CV terms.
  12. *
  13. * @param remote_db
  14. * The name of the remote publication database to query. These names should
  15. * match the name of the databases in the Chado 'db' table. Currently
  16. * supported databass include
  17. * 'PMID': PubMed
  18. *
  19. * @param search_array
  20. * An associate array containing the search criteria. The following key
  21. * are expected
  22. * 'remote_db': Specifies the name of the remote publication database
  23. * 'num_criteria': Specifies the number of criteria present in the search array
  24. * 'days': The number of days to include in the search starting from today
  25. * 'criteria': An associate array containing the search critiera. There should
  26. * be no less than 'num_criteria' elements in this array.
  27. *
  28. * The following keys are expected in the 'criteria' array
  29. * 'search_terms': A list of terms to search on, separated by spaces.
  30. * 'scope': The fields to search in the remote database. Valid values
  31. * include: 'title', 'abstract', 'author' and 'any'
  32. * 'operation': The logical operation to use for this criteria. Valid
  33. * values include: 'AND', 'OR' and 'NOT'.
  34. * @param $num_to_retrieve
  35. * The number of records to retrieve. In cases with large numbers of
  36. * records to retrieve, the remote database may limit the size of each
  37. * retrieval.
  38. * @param $pager_id
  39. * Optional. This function uses the 'tripal_pager_callback' function
  40. * to page a set of results. This is helpful when generating results to
  41. * be view online. The pager works identical to the pager_query function
  42. * of drupal. Simply provide a unique integer value for this argument. Each
  43. * form on a single page should have a unique $pager_id.
  44. * @param $page
  45. * Optional. If this function is called where the
  46. * page for the pager cannot be set using the $_GET variable, use this
  47. * argument to specify the page to retrieve.
  48. *
  49. * @return
  50. * Returns an array of pubs where each element is
  51. * an associative array where the keys are Tripal Pub CV terms.
  52. *
  53. * @ingroup tripal_pub_api
  54. */
  55. function tripal_pub_get_remote_search_results($remote_db, $search_array,
  56. $num_to_retrieve, $pager_id = 0, $page = 0) {
  57. // construct the callback function using the remote database name
  58. $callback = 'tripal_pub_remote_search_' . strtolower($remote_db);
  59. // manually set the $_GET['page'] parameter to trick the pager
  60. // into giving us the requested page
  61. if (is_int($page) and $page > 0) {
  62. $_GET['page'] = $page;
  63. }
  64. // now call the callback function to get the rsults
  65. $pubs = array();
  66. if (function_exists($callback)) {
  67. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  68. }
  69. return $pubs;
  70. }
  71. /*
  72. * @ingroup tripal_pub_api
  73. */
  74. function tripal_pub_update_publications() {
  75. // get a persistent connection
  76. $connection = tripal_db_persistent_chado();
  77. if (!$connection) {
  78. print "A persistant connection was not obtained. Loading will be slow\n";
  79. }
  80. // if we cannot get a connection then let the user know the loading will be slow
  81. tripal_db_start_transaction();
  82. if ($connection) {
  83. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  84. "If the load fails or is terminated prematurely then the entire set of \n" .
  85. "insertions/updates is rolled back and will not be found in the database\n\n";
  86. }
  87. // get a list of all publications that have
  88. // supported databases
  89. $sql = "
  90. SELECT DB.name as db_name, DBX.accession
  91. FROM pub P
  92. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  93. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  94. INNER JOIN db DB ON DB.db_id = DBX.db_id
  95. ORDER BY DB.name
  96. ";
  97. $results = chado_query($sql);
  98. $num_to_retrieve = 100;
  99. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query
  100. $curr_db = ''; // keeps track of the current current database
  101. $ids = array(); // the list of IDs for the database
  102. $search = array(); // the search array passed to the search function
  103. // iterate through the pub IDs
  104. while ($pub = db_fetch_object($results)) {
  105. $accession = $pub->accession;
  106. $remote_db = $pub->db_name;
  107. // if we're switching databases then reset the search array
  108. if($remote_db != $curr_db) {
  109. // if we had a previous DB then do the update.
  110. if ($curr_db) {
  111. $search['num_criteria'] = $i - 1;
  112. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  113. tripal_pub_add_publications($pubs);
  114. }
  115. $curr_db = $remote_db;
  116. $search = array(
  117. 'remote_db' => $remote_db,
  118. 'criteria' => array(),
  119. );
  120. $ids = array();
  121. $i = 0;
  122. }
  123. // if we've hit the maximum number to retrieve then do the search
  124. if($i == $num_to_retrieve) {
  125. $search['num_criteria'] = $i - 1;
  126. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  127. tripal_pub_add_publications($pubs);
  128. $search['criteria'] = array();
  129. $i = 0;
  130. }
  131. // add each accession to the search criteria
  132. $search['criteria'][] = array(
  133. 'search_terms' => $accession,
  134. 'scope' => 'id',
  135. 'operation' => 'OR'
  136. );
  137. $i++;
  138. }
  139. // now update any remaining in the search criteria array
  140. $search['num_criteria'] = $i - 1;
  141. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, $i, 0);
  142. tripal_pub_add_publications($pubs);
  143. // sync the newly added publications with Drupal
  144. print "Syncing publications with Drupal...\n";
  145. tripal_pub_sync_pubs();
  146. print "Syncing contacts with Drupal...\n";
  147. tripal_contact_sync_contacts();
  148. // transaction is complete
  149. tripal_db_commit_transaction();
  150. print "Done.\n";
  151. }
  152. /*
  153. * @ingroup tripal_pub_api
  154. */
  155. function tripal_pub_import_publications() {
  156. $num_to_retrieve = 100;
  157. $pager_id = 0;
  158. $page = 0;
  159. $num_pubs = 0;
  160. // get a persistent connection
  161. $connection = tripal_db_persistent_chado();
  162. if (!$connection) {
  163. print "A persistant connection was not obtained. Loading will be slow\n";
  164. }
  165. // if we cannot get a connection then let the user know the loading will be slow
  166. tripal_db_start_transaction();
  167. if ($connection) {
  168. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  169. "If the load fails or is terminated prematurely then the entire set of \n" .
  170. "insertions/updates is rolled back and will not be found in the database\n\n";
  171. }
  172. // get all of the loaders
  173. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0";
  174. $results = db_query($sql);
  175. while ($import = db_fetch_object($results)) {
  176. $criteria = unserialize($import->criteria);
  177. $remote_db = $criteria['remote_db'];
  178. do {
  179. // retrieve the pubs for this page. We'll retreive 10 at a time
  180. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
  181. $page++;
  182. }
  183. // continue looping until we have a $pubs array that does not have
  184. // our requested numer of records. This means we've hit the end
  185. while (count($pubs) == $num_to_retrieve);
  186. }
  187. // sync the newly added publications with Drupal
  188. print "Syncing publications with Drupal...\n";
  189. tripal_pub_sync_pubs();
  190. print "Syncing contacts with Drupal...\n";
  191. tripal_contact_sync_contacts();
  192. // transaction is complete
  193. tripal_db_commit_transaction();
  194. print "Done.\n";
  195. }
  196. /*
  197. *
  198. */
  199. function tripal_pub_add_publications($pubs) {
  200. // iterate through the publications and add each one
  201. foreach ($pubs as $pub) {
  202. // add the publication to Chado and sync it with Chado
  203. $pub_id = tripal_pub_add_publication($pub);
  204. // add the publication cross reference (e.g. to PubMed)
  205. if ($pub_id) {
  206. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub);
  207. }
  208. $num_pubs++;
  209. print $num_pubs . ". " . $pub['Publication Database'] . ' ' . $pub['Pub Accession'] . "\n";
  210. } // end for loop
  211. }
  212. /*
  213. *
  214. */
  215. function tripal_pub_add_pub_dbxref($pub_id, $pub) {
  216. // check to see if the pub_dbxref record already exist
  217. $values = array(
  218. 'dbxref_id' => array(
  219. 'accession' => $pub['Pub Accession'],
  220. 'db_id' => array(
  221. 'name' => $pub['Publication Database'],
  222. ),
  223. ),
  224. 'pub_id' => $pub_id,
  225. );
  226. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  227. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  228. // if the pub_dbxref record exist then we don't need to re-add it.
  229. if(count($results) > 0) {
  230. return $results[0];
  231. }
  232. // make sure our database already exists
  233. $db = tripal_db_add_db($pub['Publication Database']);
  234. // get the database cross-reference
  235. $dbxvalues = array(
  236. 'accession' => $pub['Pub Accession'],
  237. 'db_id' => $db->db_id,
  238. );
  239. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  240. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  241. // if the accession doesn't exist then add it
  242. if(count($results) == 0){
  243. $dbxref = tripal_db_add_dbxref($db->db_id, $pub['Pub Accession']);
  244. }
  245. else {
  246. $dbxref = $results[0];
  247. }
  248. // now add the record
  249. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  250. $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
  251. if (!$results) {
  252. watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
  253. array('%db' => $pub['Publication Database'], '%accession' => $pub['Pub Accession']). WATCHDOG_ERROR);
  254. return FALSE;
  255. }
  256. return $results;
  257. }
  258. /*
  259. *
  260. */
  261. function tripal_pub_add_publication($pub_details) {
  262. // check to see if the publication already exists
  263. $pub_id = 0;
  264. $values = array(
  265. 'title' => $pub_details['Title'],
  266. 'pyear' => $pub_details['Year'],
  267. );
  268. $options = array('statement_name' => 'sel_pub_tipy');
  269. $results = tripal_core_chado_select('pub', array('*'), $values, $options);
  270. if (count($results) == 1) {
  271. $pub_id = $results[0]->pub_id;
  272. }
  273. elseif (count($results) > 1) {
  274. watchdog('tripal_pub', "The publication with the same title is present multiple times. Cannot ".
  275. "determine which to use. Title: %title", array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  276. return FALSE;
  277. }
  278. // add the publication if it doens't exist
  279. elseif(count($results) == 0) {
  280. // get the publication type (use the first publication type, any others will get stored as properties)
  281. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  282. if (!$pub_type) {
  283. watchdog('tripal_pub', "Cannot find publication type: %type",
  284. array('%type' => $pub_type), WATCHDOG_ERROR);
  285. return FALSE;
  286. }
  287. // if the publication does not exist then create it.
  288. $values = array(
  289. 'title' => $pub_details['Title'],
  290. 'volume' => $pub_details['Volume'],
  291. 'series_name' => $pub_details['Journal Name'],
  292. 'issue' => $pub_details['Issue'],
  293. 'pyear' => $pub_details['Year'],
  294. 'pages' => $pub_details['Pages'],
  295. 'uniquename' => $pub_details['Citation'],
  296. 'type_id' => $pub_type->cvterm_id,
  297. );
  298. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  299. $pub = tripal_core_chado_insert('pub', $values, $options);
  300. if (!$pub) {
  301. watchdog('tripal_pub', "Cannot insert the publication with title: %title",
  302. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  303. return FALSE;
  304. }
  305. $pub_id = $pub['pub_id'];
  306. }
  307. // now add in any other items that remain as properties of the publication
  308. foreach ($pub_details as $key => $value) {
  309. // get the cvterm by name or synonym
  310. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  311. if (!$cvterm) {
  312. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  313. }
  314. if (!$cvterm) {
  315. print_r($cvterm);
  316. watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
  317. continue;
  318. }
  319. // skip details that won't be stored as properties
  320. if ($key == 'Authors') {
  321. tripal_pub_add_authors($pub_id, $value);
  322. continue;
  323. }
  324. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  325. $key == 'Year' or $key == 'Pages') {
  326. continue;
  327. }
  328. $success = 0;
  329. if (is_array($value)) {
  330. foreach ($value as $subkey => $subvalue) {
  331. // if the key is an integer then this array is a simple list and
  332. // we will insert using the primary key. Otheriwse, use the new key
  333. if(is_int($subkey)) {
  334. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, TRUE);
  335. }
  336. else {
  337. $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, TRUE);
  338. }
  339. }
  340. }
  341. else {
  342. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  343. }
  344. if (!$success) {
  345. watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
  346. array('%prop' => $key), WATCHDOG_ERROR);
  347. continue;
  348. }
  349. }
  350. return $pub_id;
  351. }
  352. /*
  353. *
  354. */
  355. function tripal_pub_add_authors($pub_id, $authors) {
  356. $rank = 0;
  357. // first remove any of the existing pubauthor entires
  358. $sql = "DELETE FROM {pubauthor} WHERE pub_id = %d";
  359. chado_query($sql, $pub_id);
  360. // iterate through the authors and add them to the pubauthors and contact
  361. // tables of chado, then link them through the custom pubauthors_contact table
  362. foreach ($authors as $author) {
  363. // skip invalid author entires
  364. if ($author['valid'] == 'N') {
  365. continue;
  366. }
  367. // remove the 'valid' property as we don't have a CV term for it
  368. unset($author['valid']);
  369. // construct the contact.name field using the author information
  370. $name = '';
  371. $type = 'Person';
  372. if ($author['Given Name']) {
  373. $name .= $author['Given Name'];
  374. }
  375. if ($author['Surname']) {
  376. $name .= ' ' . $author['Surname'];
  377. }
  378. if ($author['Suffix']) {
  379. $name .= ' ' . $author['Suffix'];
  380. }
  381. if ($author['Collective']) {
  382. $name = $author['Collective'];
  383. $type = 'Collective';
  384. }
  385. $name = trim($name);
  386. // Add the contact
  387. $contact = tripal_contact_add_contact($name, '', $type, $author);
  388. // add an entry to the pubauthors table
  389. $values = array(
  390. 'pub_id' => $pub_id,
  391. 'rank' => $rank,
  392. 'surname' => $author['Surname'] ? $author['Surname'] : $author['Collective'],
  393. 'givennames' => $author['Given Name'],
  394. 'suffix' => $author['Suffix'],
  395. );
  396. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  397. $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
  398. // if we have succesfully added the contact and the pubauthor entries then we want to
  399. // link them together
  400. if ($contact and $pubauthor) {
  401. // link the pubauthor entry to the contact
  402. $values = array(
  403. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  404. 'contact_id' => $contact['contact_id'],
  405. );
  406. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  407. $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
  408. if (!$pubauthor_contact) {
  409. watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
  410. }
  411. }
  412. $rank++;
  413. }
  414. }