tripal_pub.api.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. // manually set the $_GET['page'] parameter to trick the pager
  58. // into giving us the requested page
  59. if (is_int($page) and $page > 0) {
  60. $_GET['page'] = $page;
  61. }
  62. // now call the callback function to get the results
  63. $callback = "tripal_pub_remote_search_$remote_db";
  64. $pubs = array();
  65. if (function_exists($callback)) {
  66. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $pager_id);
  67. }
  68. return $pubs;
  69. }
  70. /*
  71. * @ingroup tripal_pub_api
  72. */
  73. function tripal_pub_get_raw_data($dbxref) {
  74. if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  75. $remote_db = $matches[1];
  76. $accession = $matches[2];
  77. // check that the database is supported
  78. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  79. if(!in_array($remote_db, $supported_dbs)) {
  80. return "Unsupported database: $dbxref";
  81. }
  82. // build the search criteria
  83. $search = array(
  84. 'remote_db' => $remote_db,
  85. 'criteria' => array(
  86. array(
  87. 'search_terms' => $accession,
  88. 'scope' => 'id',
  89. ),
  90. ),
  91. );
  92. $search['num_criteria'] = 0;
  93. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  94. return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
  95. }
  96. return 'Invalid DB xref';
  97. }
  98. /*
  99. * @ingroup tripal_pub_api
  100. */
  101. function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
  102. // get a persistent connection
  103. $connection = tripal_db_persistent_chado();
  104. if (!$connection) {
  105. print "A persistant connection was not obtained. Loading will be slow\n";
  106. }
  107. // if we cannot get a connection then let the user know the loading will be slow
  108. tripal_db_start_transaction();
  109. if ($connection) {
  110. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  111. "If the load fails or is terminated prematurely then the entire set of \n" .
  112. "insertions/updates is rolled back and will not be found in the database\n\n";
  113. }
  114. // get a list of all publications by their Dbxrefs that have supported databases
  115. $sql = "
  116. SELECT DB.name as db_name, DBX.accession
  117. FROM pub P
  118. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  119. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  120. INNER JOIN db DB ON DB.db_id = DBX.db_id
  121. ";
  122. $args = array();
  123. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  124. $dbname = $matches[1];
  125. $accession = $matches[2];
  126. $sql .= "WHERE DBX.accession = '%s' and DB.name = '%s' ";
  127. $args[] = $accession;
  128. $args[] = $dbname;
  129. }
  130. elseif ($db) {
  131. $sql .= " WHERE DB.name = '%s' ";
  132. $args[] = $db;
  133. }
  134. $sql .= "ORDER BY DB.name, P.pub_id";
  135. $results = chado_query($sql, $args);
  136. $num_to_retrieve = 100;
  137. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query
  138. $curr_db = ''; // keeps track of the current current database
  139. $ids = array(); // the list of IDs for the database
  140. $search = array(); // the search array passed to the search function
  141. // iterate through the pub IDs
  142. while ($pub = db_fetch_object($results)) {
  143. $accession = $pub->accession;
  144. $remote_db = $pub->db_name;
  145. // here we need to only update publications for databases we support
  146. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  147. if(!in_array($remote_db, $supported_dbs)) {
  148. continue;
  149. }
  150. $search = array(
  151. 'num_criteria' => 1,
  152. 'remote_db' => $remote_db,
  153. 'criteria' => array(
  154. '1' => array(
  155. 'search_terms' => "$remote_db:$accession",
  156. 'scope' => 'id',
  157. 'operation' => '',
  158. 'is_phrase' => 0,
  159. ),
  160. ),
  161. );
  162. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  163. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  164. $i++;
  165. }
  166. // transaction is complete
  167. tripal_db_commit_transaction();
  168. print "Transaction Complete\n";
  169. // sync the newly added publications with Drupal
  170. print "Syncing publications with Drupal...\n";
  171. tripal_pub_sync_pubs();
  172. // if the caller wants to create contacts then we should sync them
  173. if ($do_contact) {
  174. print "Syncing contacts with Drupal...\n";
  175. tripal_contact_sync_contacts();
  176. }
  177. print "Done.\n";
  178. }
  179. /*
  180. * @ingroup tripal_pub_api
  181. */
  182. function tripal_pub_import_publications($pub_import_id = NULL) {
  183. $num_to_retrieve = 100;
  184. $pager_id = 0;
  185. $page = 0;
  186. $num_pubs = 0;
  187. // get a persistent connection
  188. $connection = tripal_db_persistent_chado();
  189. if (!$connection) {
  190. print "A persistant connection was not obtained. Loading will be slow\n";
  191. }
  192. // if we cannot get a connection then let the user know the loading will be slow
  193. tripal_db_start_transaction();
  194. if ($connection) {
  195. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  196. "If the load fails or is terminated prematurely then the entire set of \n" .
  197. "insertions/updates is rolled back and will not be found in the database\n\n";
  198. }
  199. // get all of the loaders
  200. $args = array();
  201. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
  202. if ($pub_import_id) {
  203. $sql .= " AND pub_import_id = %d";
  204. $args[] = $pub_import_id;
  205. }
  206. $results = db_query($sql, $args);
  207. $do_contact = FALSE;
  208. while ($import = db_fetch_object($results)) {
  209. print "Importing: " . $import->name . "\n";
  210. // keep track if any of the importers want to create contacts from authors
  211. if ($import->do_contact == 1) {
  212. $do_contact = TRUE;
  213. }
  214. $criteria = unserialize($import->criteria);
  215. $remote_db = $criteria['remote_db'];
  216. do {
  217. // retrieve the pubs for this page. We'll retreive 10 at a time
  218. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
  219. tripal_pub_add_publications($pubs, $import->do_contact);
  220. $page++;
  221. }
  222. // continue looping until we have a $pubs array that does not have
  223. // our requested numer of records. This means we've hit the end
  224. while (count($pubs) == $num_to_retrieve);
  225. }
  226. // transaction is complete
  227. tripal_db_commit_transaction();
  228. print "Transaction Complete\n";
  229. // sync the newly added publications with Drupal
  230. print "Syncing publications with Drupal...\n";
  231. tripal_pub_sync_pubs();
  232. // if any of the importers wanted to create contacts from the authors then sync them
  233. if($do_contact) {
  234. print "Syncing contacts with Drupal...\n";
  235. tripal_contact_sync_contacts();
  236. }
  237. print "Done.\n";
  238. }
  239. /*
  240. * @ingroup tripal_pub_api
  241. */
  242. function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE) {
  243. $num_to_retrieve = 1;
  244. $pager_id = 0;
  245. $page = 0;
  246. $num_pubs = 0;
  247. // get a persistent connection
  248. $connection = tripal_db_persistent_chado();
  249. if (!$connection) {
  250. print "A persistant connection was not obtained. Loading will be slow\n";
  251. }
  252. // if we cannot get a connection then let the user know the loading will be slow
  253. tripal_db_start_transaction();
  254. if ($connection) {
  255. print "\nNOTE: Loading of the publication is performed using a database transaction. \n" .
  256. "If the load fails or is terminated prematurely then the entire set of \n" .
  257. "insertions/updates is rolled back and will not be found in the database\n\n";
  258. }
  259. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  260. $dbname = $matches[1];
  261. $accession = $matches[2];
  262. $criteria['remote_db'] = $dbname;
  263. $criteria['num_criteria'] = 1;
  264. $criteria['criteria'][1]['search_terms'] = $accession;
  265. $criteria['criteria'][1]['scope'] = 'id';
  266. $criteria['criteria'][1]['is_phrase'] = 0;
  267. $criteria['criteria'][1]['operation'] = '';
  268. $remote_db = $criteria['remote_db'];
  269. $pubs = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $pager_id, $page);
  270. $pub_id = tripal_pub_add_publications($pubs, $do_contact);
  271. }
  272. // transaction is complete
  273. tripal_db_commit_transaction();
  274. print "Transaction Complete\n";
  275. // sync the newly added publications with Drupal
  276. print "Syncing publications with Drupal...\n";
  277. tripal_pub_sync_pubs();
  278. // if any of the importers wanted to create contacts from the authors then sync them
  279. if($do_contact) {
  280. print "Syncing contacts with Drupal...\n";
  281. tripal_contact_sync_contacts();
  282. }
  283. print "Done.\n";
  284. }
  285. /*
  286. *
  287. */
  288. function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
  289. // iterate through the publications and add each one
  290. foreach ($pubs as $pub) {
  291. // add the publication to Chado and sync it with Chado
  292. $pub_id = tripal_pub_add_publication($pub, $do_contact, $update);
  293. if ($pub_id){
  294. // add the publication cross reference (e.g. to PubMed)
  295. if ($pub_id and $pub['Publication Dbxref']) {
  296. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
  297. }
  298. $num_pubs++;
  299. print "Done: " . $pub['Publication Dbxref'] . "\n";
  300. }
  301. else {
  302. print "Failed: " . $pub['Publication Dbxref'] . "\n";
  303. }
  304. }
  305. }
  306. /*
  307. *
  308. */
  309. function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
  310. // break apart the dbxref
  311. $dbname = '';
  312. $accession = '';
  313. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  314. $dbname = $matches[1];
  315. $accession = $matches[2];
  316. }
  317. else {
  318. return FALSE;
  319. }
  320. // check to see if the pub_dbxref record already exist
  321. $values = array(
  322. 'dbxref_id' => array(
  323. 'accession' => $accession,
  324. 'db_id' => array(
  325. 'name' => $dbname,
  326. ),
  327. ),
  328. 'pub_id' => $pub_id,
  329. );
  330. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  331. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  332. // if the pub_dbxref record exist then we don't need to re-add it.
  333. if(count($results) > 0) {
  334. return $results[0];
  335. }
  336. // make sure our database already exists
  337. $db = tripal_db_add_db($dbname);
  338. // get the database cross-reference
  339. $dbxvalues = array(
  340. 'accession' => $accession,
  341. 'db_id' => $db->db_id,
  342. );
  343. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  344. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  345. // if the accession doesn't exist then add it
  346. if(count($results) == 0){
  347. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  348. }
  349. else {
  350. $dbxref = $results[0];
  351. }
  352. // now add the record
  353. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  354. $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
  355. if (!$results) {
  356. watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
  357. array('%db' => $dbname, '%accession' => $accession). WATCHDOG_ERROR);
  358. return FALSE;
  359. }
  360. return $results;
  361. }
  362. /**
  363. * Returns the list of publications that are assigned the database
  364. * cross-reference provided
  365. *
  366. * @param $pub_dbxref
  367. * The database cross reference accession. It should be in the form
  368. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  369. * unique publication identifier (e.g. PMID:4382934)
  370. *
  371. * @return
  372. * Returns an array of all the publications that have the provided
  373. * cross reference. If no publications match, then an empty array
  374. * is returned.
  375. *
  376. * @ingroup tripal_pub_api
  377. *
  378. */
  379. function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
  380. $return = array();
  381. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  382. $dbname = $matches[1];
  383. $accession = $matches[2];
  384. $values = array(
  385. 'dbxref_id' => array (
  386. 'accession' => $accession,
  387. 'db_id' => array(
  388. 'name' => $dbname
  389. ),
  390. ),
  391. );
  392. $options = array('statement_name' => 'sel_pubdbxref_db');
  393. $results = tripal_core_chado_select('pub_dbxref', array('pub_id'), $values, $options);
  394. foreach ($results as $index => $pub) {
  395. $return[] = $pub->pub_id;
  396. }
  397. }
  398. return $return;
  399. }
  400. /**
  401. * Returns the list of publications that match a given title and year
  402. *
  403. * @param title
  404. * The title of the publication to look for
  405. * @param type
  406. * The publication type. The value of this field should come from
  407. * the Tripal Pub vocabulary
  408. * @param year
  409. * Optional. The year the publication was published.
  410. *
  411. * @return
  412. * Returns an array of all the publications that have the provided
  413. * cross reference. If no publications match, then an empty array
  414. * is returned.
  415. *
  416. * @ingroup tripal_pub_api
  417. *
  418. */
  419. function tripal_pub_get_pubs_by_title_type_pyear($title, $type, $pyear = '') {
  420. $return = array();
  421. // build the values array for the query. The $pyear is not required.
  422. $values = array(
  423. 'title' => $title,
  424. 'type_id' => array(
  425. 'name' => $type,
  426. 'cv_id' => array(
  427. 'name' => 'tripal_pub'
  428. )
  429. )
  430. );
  431. $stmnt_suffix = 'tity';
  432. if ($pub_details['Year']) {
  433. $values['pyear'] = $pyear;
  434. $stmnt_suffix .= 'py';
  435. }
  436. $options = array('statement_name' => 'sel_pub_' . $stmnt_suffix);
  437. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  438. // iterate through any matches and pull out the pub_id
  439. foreach ($results as $index => $pub) {
  440. $return[] = $pub->pub_id;
  441. }
  442. return $return;
  443. }
  444. /**
  445. * Adds a new publication to the Chado, along with all properties and
  446. * database cross-references. If the publication does not already exist
  447. * in Chado then it is added. If it does exist nothing is done. If
  448. * the $update parameter is TRUE then the publication is updated if it exists.
  449. *
  450. * @param $pub_details
  451. * An associative array containing all of the details about the publication.
  452. * @param $do_contact
  453. * Optional. Set to TRUE if a contact entry should be added to the Chado contact table
  454. * for authors of the publication.
  455. * @param $update
  456. * Optional. If the publication already exists then this function will return
  457. * without adding a new publication. However, set this value to TRUE to force
  458. * the function to pudate the publication using the $pub_details that are provided.
  459. *
  460. * @return
  461. * On successful addition of the publication, the new publication ID is returned. If
  462. * the publication already exists but $update is FALSE then TRUE is returned indicating
  463. * that the publication is there already. If $update is TRUE and the publication
  464. * exists then the publication ID is returned.
  465. *
  466. */
  467. function tripal_pub_add_publication($pub_details, $do_contact = FALSE, $update = FALSE) {
  468. $pub_id = 0;
  469. // first try to find the publication using the accession number. It will have
  470. // one if the pub has already been loaded for the publication database
  471. if ($pub_details['Publication Dbxref']) {
  472. $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
  473. if(count($results) == 1) {
  474. $pub_id = $results[0];
  475. }
  476. elseif (count($results) > 1) {
  477. watchdog('tripal_pub', "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
  478. array('%db' => $dbname, '%accession' => $accession), WATCHDOG_ERROR);
  479. return FALSE;
  480. }
  481. }
  482. // if we couldn't find a publication by the accession (which means it doesn't
  483. // yet exist or it has been added using a different publication database) then
  484. // try to find it using the title and publication year.
  485. if (!$pub_id and $pub_details['Title']) {
  486. $results = tripal_pub_get_pubs_by_title_type_pyear($pub_details['Title'], $pub_details['Publication Type'], $pub_details['Year']);
  487. if (count($results) == 1) {
  488. $pub_id = $results[0];
  489. }
  490. elseif (count($results) > 1) {
  491. watchdog('tripal_pub', "The publication with the same title, type and year is present multiple times. Cannot ".
  492. "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
  493. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_ERROR);
  494. return FALSE;
  495. }
  496. }
  497. // if there is a pub id and we've been told not to update then return
  498. if ($pub_id and !$update) {
  499. return $pub_id;
  500. }
  501. // get the publication type (use the first publication type, any others will get stored as properties)
  502. if (is_array($pub_details['Publication Type'])) {
  503. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  504. }
  505. elseif ($pub_details['Publication Type']) {
  506. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
  507. }
  508. else {
  509. watchdog('tripal_pub', "The Publication Type is a required property but is missing", array(), WATCHDOG_ERROR);
  510. return FALSE;
  511. }
  512. if (!$pub_type) {
  513. watchdog('tripal_pub', "Cannot find publication type: '%type'",
  514. array('%type' => $pub_details['Publication Type'][0]), WATCHDOG_ERROR);
  515. return FALSE;
  516. }
  517. // build the values array for inserting or updating
  518. $values = array(
  519. 'title' => $pub_details['Title'],
  520. 'volume' => $pub_details['Volume'],
  521. 'series_name' => $pub_details['Journal Name'],
  522. 'issue' => $pub_details['Issue'],
  523. 'pyear' => $pub_details['Year'],
  524. 'pages' => $pub_details['Pages'],
  525. 'uniquename' => $pub_details['Citation'],
  526. 'type_id' => $pub_type->cvterm_id,
  527. );
  528. // if there is no pub_id then we need to do an insert.
  529. if (!$pub_id) {
  530. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  531. $pub = tripal_core_chado_insert('pub', $values, $options);
  532. if (!$pub) {
  533. watchdog('tripal_pub', "Cannot insert the publication with title: %title",
  534. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  535. return FALSE;
  536. }
  537. $pub_id = $pub['pub_id'];
  538. }
  539. // if there is a pub_id and we've been told to update, then do the update
  540. if ($pub_id and $update) {
  541. $match = array('pub_id' => $pub_id);
  542. $options = array('statement_name' => 'up_pub_tivoseispypaunty');
  543. $success = tripal_core_chado_update('pub', $match, $values, $options);
  544. if (!$success) {
  545. watchdog('tripal_pub', "Cannot update the publication with title: %title",
  546. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  547. return FALSE;
  548. }
  549. }
  550. // before we add any new properties we need to remove those that are there if this
  551. // is an update. The only thing we don't want to remove are the 'Publication Dbxref'
  552. if ($update) {
  553. $sql = "
  554. DELETE FROM {pubprop}
  555. WHERE
  556. pub_id = %d AND
  557. NOT type_id in (
  558. SELECT cvterm_id
  559. FROM {cvterm}
  560. WHERE name = 'Publication Dbxref'
  561. )
  562. ";
  563. chado_query($sql, $pub_id);
  564. }
  565. // iterate through the properties and add them
  566. foreach ($pub_details as $key => $value) {
  567. // the pub_details may have the raw search data (e.g. in XML from PubMed. We'll irgnore this for now
  568. if($key == 'raw') {
  569. continue;
  570. }
  571. // get the cvterm by name or synonym
  572. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  573. if (!$cvterm) {
  574. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  575. }
  576. if (!$cvterm) {
  577. watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
  578. continue;
  579. }
  580. // skip details that won't be stored as properties
  581. if ($key == 'Author List') {
  582. tripal_pub_add_authors($pub_id, $value, $do_contact);
  583. continue;
  584. }
  585. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  586. $key == 'Year' or $key == 'Pages') {
  587. continue;
  588. }
  589. $success = 0;
  590. if (is_array($value)) {
  591. foreach ($value as $subkey => $subvalue) {
  592. // if the key is an integer then this array is a simple list and
  593. // we will insert using the primary key. Otheriwse, use the new key
  594. if(is_int($subkey)) {
  595. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
  596. }
  597. else {
  598. $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
  599. }
  600. }
  601. }
  602. else {
  603. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  604. }
  605. if (!$success) {
  606. watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
  607. array('%prop' => $key), WATCHDOG_ERROR);
  608. continue;
  609. }
  610. }
  611. return $pub_id;
  612. }
  613. /*
  614. *
  615. */
  616. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  617. $rank = 0;
  618. // first remove any of the existing pubauthor entires
  619. $sql = "DELETE FROM {pubauthor} WHERE pub_id = %d";
  620. chado_query($sql, $pub_id);
  621. // iterate through the authors and add them to the pubauthors and contact
  622. // tables of chado, then link them through the custom pubauthors_contact table
  623. foreach ($authors as $author) {
  624. // skip invalid author entires
  625. if ($author['valid'] == 'N') {
  626. continue;
  627. }
  628. // remove the 'valid' property as we don't have a CV term for it
  629. unset($author['valid']);
  630. // construct the contact.name field using the author information
  631. $name = '';
  632. $type = 'Person';
  633. if ($author['Given Name']) {
  634. $name .= $author['Given Name'];
  635. }
  636. if ($author['Surname']) {
  637. $name .= ' ' . $author['Surname'];
  638. }
  639. if ($author['Suffix']) {
  640. $name .= ' ' . $author['Suffix'];
  641. }
  642. if ($author['Collective']) {
  643. $name = $author['Collective'];
  644. $type = 'Collective';
  645. }
  646. $name = trim($name);
  647. // add an entry to the pubauthors table
  648. $values = array(
  649. 'pub_id' => $pub_id,
  650. 'rank' => $rank,
  651. 'surname' => $author['Surname'] ? $author['Surname'] : $author['Collective'],
  652. 'givennames' => $author['Given Name'],
  653. 'suffix' => $author['Suffix'],
  654. );
  655. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  656. $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
  657. // if the user wants us to create a contact for each author then do it.
  658. if ($do_contact) {
  659. // Add the contact
  660. $contact = tripal_contact_add_contact($name, '', $type, $author);
  661. // if we have succesfully added the contact and the pubauthor entries then we want to
  662. // link them together
  663. if ($contact and $pubauthor) {
  664. // link the pubauthor entry to the contact
  665. $values = array(
  666. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  667. 'contact_id' => $contact['contact_id'],
  668. );
  669. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  670. $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
  671. if (!$pubauthor_contact) {
  672. watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
  673. }
  674. }
  675. }
  676. $rank++;
  677. }
  678. }
  679. /**
  680. * Retrieve properties of a given type for a given pub
  681. *
  682. * @param $pub_id
  683. * The pub_id of the properties you would like to retrieve
  684. * @param $property
  685. * The cvterm name of the properties to retrieve
  686. *
  687. * @return
  688. * An pub chado variable with the specified properties expanded
  689. *
  690. * @ingroup tripal_pub_api
  691. */
  692. function tripal_pub_get_property($pub_id, $property) {
  693. return tripal_core_get_property('pub', $pub_id, $property, 'tripal');
  694. }
  695. /**
  696. * Insert a given property
  697. *
  698. * @param $pub_id
  699. * The pub_id of the property to insert
  700. * @param $property
  701. * The cvterm name of the property to insert
  702. * @param $value
  703. * The value of the property to insert
  704. * @param $update_if_present
  705. * A boolean indicated whether to update the record if it's already present
  706. *
  707. * @return
  708. * True of success, False otherwise
  709. *
  710. * @ingroup tripal_pub_api
  711. */
  712. function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
  713. return tripal_core_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
  714. }
  715. /**
  716. * Update a given property
  717. *
  718. * @param $pub_id
  719. * The pub_id of the property to update
  720. * @param $property
  721. * The cvterm name of the property to update
  722. * @param $value
  723. * The value of the property to update
  724. * @param $insert_if_missing
  725. * A boolean indicated whether to insert the record if it's absent
  726. *
  727. * Note: The property will be identified using the unique combination of the $pub_id and $property
  728. * and then it will be updated with the supplied value
  729. *
  730. * @return
  731. * True of success, False otherwise
  732. *
  733. * @ingroup tripal_pub_api
  734. */
  735. function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
  736. return tripal_core_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
  737. }
  738. /**
  739. * Delete a given property
  740. *
  741. * @param $pub_id
  742. * The pub_id of the property to delete
  743. * @param $property
  744. * The cvterm name of the property to delete
  745. *
  746. * Note: The property will be identified using the unique combination of the $pub_id and $property
  747. * and then it will be deleted
  748. *
  749. * @return
  750. * True of success, False otherwise
  751. *
  752. * @ingroup tripal_pub_api
  753. */
  754. function tripal_pub_delete_property($pub_id, $property) {
  755. return tripal_core_delete_property('pub', $pub_id, $property, 'tripal');
  756. }
  757. /*
  758. *
  759. */
  760. function tripal_pub_create_citation($pub) {
  761. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  762. if ($pub['Journal Name']) {
  763. $citation .= $pub['Journal Name'] . '. ';
  764. }
  765. elseif ($pub['Journal Abbreviation']) {
  766. $citation .= $pub['Journal Abbreviation'] . '. ';
  767. }
  768. $citation .= $pub['Publication Date'];
  769. if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
  770. $citation .= '; ';
  771. }
  772. if ($pub['Volume']) {
  773. $citation .= $pub['Volume'];
  774. }
  775. if ($pub['Issue']) {
  776. $citation .= '(' . $pub['Issue'] . ')';
  777. }
  778. if ($pub['Pages']) {
  779. if($pub['Volume']) {
  780. $citation .= ':';
  781. }
  782. $citation .= $pub['Pages'];
  783. }
  784. $citation .= '.';
  785. return $citation;
  786. }