tripal_pub.api.inc 27 KB

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