tripal_chado.pub.api.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. <?php
  2. /**
  3. * @file
  4. * Provides API functions specificially for managing publication
  5. * records in Chado.
  6. */
  7. /**
  8. * @defgroup tripal_pub_api Chado Publication
  9. * @ingroup tripal_chado_api
  10. * @{
  11. * Provides API functions specificially for managing publication
  12. * records in Chado.
  13. * @}
  14. */
  15. /**
  16. * Retrieves a chado publication array.
  17. *
  18. * @param $identifier
  19. * An array used to uniquely identify a publication. This array has the same
  20. * format as that used by the chado_generate_var(). The following keys can be
  21. * useful for uniquely identifying a publication as they should be unique:
  22. * - pub_id: the chado pub.pub_id primary key.
  23. * - nid: the drupal nid of the publication.
  24. * - uniquename: A value to matach with the pub.uniquename field.
  25. * There are also some specially handled keys. They are:
  26. * - property: An array describing the property to select records for. It
  27. * should at least have either a 'type_name' key (if unique across cvs) or
  28. * 'type_id' key. Other supported keys include: 'cv_id', 'cv_name'
  29. * (of the type), 'value' and 'rank'
  30. * - dbxref: The database cross reference accession. It should be in the
  31. * form DB:ACCESSION, where DB is the database name and ACCESSION is the
  32. * unique publication identifier (e.g. PMID:4382934)
  33. * - dbxref_id: The dbxref.dbxref_id of the publication.
  34. * @param $options
  35. * An array of options. Supported keys include:
  36. * - Any keys supported by chado_generate_var(). See that function
  37. * definition for additional details.
  38. *
  39. * NOTE: the $identifier parameter can really be any array similar to $values
  40. * passed into chado_select_record(). It should fully specify the pub record to
  41. * be returned.
  42. *
  43. * @return
  44. * If a singe publication is retreived using the identifiers, then a
  45. * publication array will be returned. The array is of the same format
  46. * returned by the chado_generate_var() function. Otherwise, FALSE will be
  47. * returned.
  48. *
  49. * @ingroup tripal_pub_api
  50. */
  51. function chado_get_publication($identifiers, $options = array()) {
  52. // Error Checking of parameters
  53. if (!is_array($identifiers)) {
  54. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  55. "chado_get_publication: The identifier passed in is expected to be an array with the key
  56. matching a column name in the pub table (ie: pub_id or name). You passed in %identifier.",
  57. array('%identifier'=> print_r($identifiers, TRUE))
  58. );
  59. }
  60. elseif (empty($identifiers)) {
  61. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  62. "chado_get_publication: You did not pass in anything to identify the publication you want. The identifier
  63. is expected to be an array with the key matching a column name in the pub table
  64. (ie: pub_id or name). You passed in %identifier.",
  65. array('%identifier'=> print_r($identifiers, TRUE))
  66. );
  67. }
  68. // If one of the identifiers is property then use
  69. // chado_get_record_with_property().
  70. if (array_key_exists('property', $identifiers)) {
  71. $property = $identifiers['property'];
  72. unset($identifiers['property']);
  73. $pub = chado_get_record_with_property(
  74. array('table' => 'pub', 'base_records' => $identifiers),
  75. array('type_name' => $property),
  76. $options
  77. );
  78. }
  79. elseif (array_key_exists('dbxref', $identifiers)) {
  80. if(preg_match('/^(.*?):(.*?)$/', $identifiers['dbxref'], $matches)) {
  81. $dbname = $matches[1];
  82. $accession = $matches[2];
  83. // First make sure the dbxref is present.
  84. $values = array(
  85. 'accession' => $accession,
  86. 'db_id' => array(
  87. 'name' => $dbname
  88. ),
  89. );
  90. $dbxref = chado_select_record('dbxref', array('dbxref_id'), $values);
  91. if (count($dbxref) == 0) {
  92. return FALSE;
  93. }
  94. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), array('dbxref_id' => $dbxref[0]->dbxref_id));
  95. if (count($pub_dbxref) == 0) {
  96. return FALSE;
  97. }
  98. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  99. }
  100. else {
  101. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  102. "chado_get_publication: The dbxref identifier is not correctly formatted.",
  103. array('%identifier'=> print_r($identifiers, TRUE))
  104. );
  105. }
  106. }
  107. elseif (array_key_exists('dbxref_id', $identifiers)) {
  108. // First get the pub_dbxref record.
  109. $values = array('dbxref_id' => $identifiers['dbxref_id']);
  110. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
  111. // Now get the pub.
  112. if (count($pub_dbxref) > 0) {
  113. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  114. }
  115. else {
  116. return FALSE;
  117. }
  118. }
  119. // Else we have a simple case and we can just use chado_generate_var to get
  120. // the pub.
  121. else {
  122. // Try to get the pub.
  123. $pub = chado_generate_var('pub', $identifiers, $options);
  124. }
  125. // Ensure the pub is singular. If it's an array then it is not singular.
  126. if (is_array($pub)) {
  127. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  128. "chado_get_publication: The identifiers did not find a single unique record. Identifiers passed: %identifier.",
  129. array('%identifier'=> print_r($identifiers, TRUE))
  130. );
  131. }
  132. // Report an error if $pub is FALSE since then chado_generate_var has failed.
  133. elseif ($pub === FALSE) {
  134. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  135. "chado_get_publication: Could not find a publication using the identifiers
  136. provided. Check that the identifiers are correct. Identifiers passed: %identifier.",
  137. array('%identifier'=> print_r($identifiers, TRUE))
  138. );
  139. }
  140. // Else, as far we know, everything is fine so give them their pub :)
  141. else {
  142. return $pub;
  143. }
  144. }
  145. /**
  146. * The publication table of Chado only has a unique constraint for the
  147. * uniquename of the publiation, but in reality a publication can be considered
  148. * unique by a combination of the title, publication type, published year and
  149. * series name (e.g. journal name or conference name). The site administrator
  150. * can configure how publications are determined to be unique. This function
  151. * uses the configuration specified by the administrator to look for publications
  152. * that match the details specified by the $pub_details argument
  153. * and indicates if one ore more publications match the criteria.
  154. *
  155. * @param $pub_details
  156. * An associative array with details about the publications. The expected keys
  157. * are:
  158. * 'Title': The title of the publication.
  159. * 'Year': The published year of the publication.
  160. * 'Publication Type': An array of publication types. A publication can
  161. * have more than one type.
  162. * 'Series Name': The series name of the publication.
  163. * 'Journal Name': An alternative to 'Series Name'.
  164. * 'Conference Name': An alternative to 'Series Name'.
  165. * 'Citation': The publication citation (this is the value saved
  166. * in the pub.uniquename field and must be unique).
  167. *
  168. * If this key is present it will also be checked
  169. * 'Publication Dbxref': A database cross reference of the form DB:ACCESSION
  170. * where DB is the name of the database and ACCESSION
  171. * is the unique identifier (e.g PMID:3483139).
  172. *
  173. * @return
  174. * An array containing the pub_id's of matching publications. Returns an
  175. * empty array if no pubs match.
  176. *
  177. * @ingroup tripal_pub_api
  178. */
  179. function chado_publication_exists($pub_details) {
  180. // First try to find the publication using the accession number if that key
  181. // exists in the details array.
  182. if (array_key_exists('Publication Dbxref', $pub_details)) {
  183. $pub = chado_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
  184. if($pub) {
  185. return array($pub->pub_id);
  186. }
  187. }
  188. // Make sure the citation is unique.
  189. if (array_key_exists('Citation', $pub_details)) {
  190. $pub = chado_get_publication(array('uniquename' => $pub_details['Citation']));
  191. if($pub) {
  192. return array($pub->pub_id);
  193. }
  194. }
  195. // Get the publication type (use the first publication type).
  196. if (array_key_exists('Publication Type', $pub_details)) {
  197. $type_name = '';
  198. if(is_array($pub_details['Publication Type'])) {
  199. $type_name = $pub_details['Publication Type'][0];
  200. }
  201. else {
  202. $type_name = $pub_details['Publication Type'];
  203. }
  204. $identifiers = array(
  205. 'name' => $type_name,
  206. 'cv_id' => array(
  207. 'name' => 'tripal_pub',
  208. ),
  209. );
  210. $pub_type = chado_get_cvterm($identifiers);
  211. }
  212. else {
  213. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  214. "chado_publication_exists(): The Publication Type is a " .
  215. "required property but is missing", array());
  216. return array();
  217. }
  218. if (!$pub_type) {
  219. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  220. "chado_publication_exists(): Cannot find publication type: '%type'",
  221. array('%type' => $pub_details['Publication Type'][0]));
  222. return array();
  223. }
  224. // Get the series name. The pub.series_name field is only 255 chars so we
  225. // must truncate to be safe.
  226. $series_name = '';
  227. if (array_key_exists('Series Name', $pub_details)) {
  228. $series_name = substr($pub_details['Series Name'], 0, 255);
  229. }
  230. if (array_key_exists('Journal Name', $pub_details)) {
  231. $series_name = substr($pub_details['Journal Name'], 0, 255);
  232. }
  233. if (array_key_exists('Conference Name', $pub_details)) {
  234. $series_name = substr($pub_details['Conference Name'], 0, 255);
  235. }
  236. // Make sure the publication is unique using the prefereed import
  237. // duplication check.
  238. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  239. $pubs = array();
  240. switch ($import_dups_check) {
  241. case 'title_year':
  242. $identifiers = array(
  243. 'title' => $pub_details['Title'],
  244. 'pyear' => $pub_details['Year']
  245. );
  246. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  247. break;
  248. case 'title_year_type':
  249. $identifiers = array(
  250. 'title' => $pub_details['Title'],
  251. 'pyear' => $pub_details['Year'],
  252. 'type_id' => $pub_type->cvterm_id,
  253. );
  254. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  255. break;
  256. case 'title_year_media':
  257. $identifiers = array(
  258. 'title' => $pub_details['Title'],
  259. 'pyear' => $pub_details['Year'],
  260. 'series_name' => $series_name,
  261. );
  262. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  263. break;
  264. }
  265. $return = array();
  266. foreach ($pubs as $pub) {
  267. $return[] = $pub->pub_id;
  268. }
  269. return $return;
  270. }
  271. /**
  272. * Used for autocomplete in forms for identifying for publications.
  273. *
  274. * @param $field
  275. * The field in the publication to search on.
  276. * @param $string
  277. * The string to search for.
  278. *
  279. * @return
  280. * A json array of terms that begin with the provided string.
  281. *
  282. * @ingroup tripal_pub_api
  283. */
  284. function chado_autocomplete_pub($string = '') {
  285. $items = array();
  286. $sql = "
  287. SELECT pub_id, title, uniquename
  288. FROM {pub}
  289. WHERE lower(title) like lower(:str)
  290. ORDER by title
  291. LIMIT 25 OFFSET 0
  292. ";
  293. $pubs = chado_query($sql, array(':str' => $string . '%'));
  294. while ($pub = $pubs->fetchObject()) {
  295. $val = $pub->title . " [id:" . $pub->pub_id . "]";
  296. $items[$val] = $pub->title;
  297. }
  298. drupal_json_output($items);
  299. }
  300. /**
  301. * Imports a singe publication specified by a remote database cross reference.
  302. *
  303. * @param $pub_dbxref
  304. * The unique database ID for the record to update. This value must
  305. * be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
  306. * database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
  307. * for the record in the database.
  308. * @param $do_contact
  309. * Set to TRUE if authors should automatically have a contact record added
  310. * to Chado.
  311. * @param $do_update
  312. * If set to TRUE then the publication will be updated if it already exists
  313. * in the database.
  314. *
  315. * @ingroup tripal_pub_api
  316. */
  317. function chado_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update = TRUE) {
  318. $num_to_retrieve = 1;
  319. $pager_id = 0;
  320. $page = 0;
  321. $num_pubs = 0;
  322. $pub_id = NULL;
  323. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  324. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  325. "If the load fails or is terminated prematurely then the entire set of \n" .
  326. "insertions/updates is rolled back and will not be found in the database\n\n";
  327. $transaction = db_transaction();
  328. try {
  329. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  330. $dbname = $matches[1];
  331. $accession = $matches[2];
  332. $criteria = array(
  333. 'num_criteria' => 1,
  334. 'remote_db' => $dbname,
  335. 'criteria' => array(
  336. '1' => array(
  337. 'search_terms' => "$dbname:$accession",
  338. 'scope' => 'id',
  339. 'operation' => '',
  340. 'is_phrase' => 0,
  341. ),
  342. ),
  343. );
  344. $remote_db = $criteria['remote_db'];
  345. $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
  346. $pubs = $results['pubs'];
  347. $search_str = $results['search_str'];
  348. $total_records = $results['total_records'];
  349. tripal_pub_add_publications($pubs, $do_contact, $do_update);
  350. }
  351. // For backwards compatibility check to see if the legacy pub module
  352. // is enabled. If so, then sync the nodes.
  353. if (module_exists('tripal_pub')) {
  354. // Sync the newly added publications with Drupal.
  355. print "Syncing publications with Drupal...\n";
  356. chado_node_sync_records('pub');
  357. // If any of the importers wanted to create contacts from the authors
  358. // then sync them.
  359. if($do_contact) {
  360. print "Syncing contacts with Drupal...\n";
  361. chado_node_sync_records('contact');
  362. }
  363. }
  364. }
  365. catch (Exception $e) {
  366. $transaction->rollback();
  367. print "\n"; // make sure we start errors on new line
  368. watchdog_exception('T_pub_import', $e);
  369. print "FAILED: Rolling back database changes...\n";
  370. return;
  371. }
  372. }
  373. /**
  374. * Imports all publications for all active import setups.
  375. *
  376. * @param $report_email
  377. * A list of email address, separated by commas, that should be notified
  378. * once importing has completed.
  379. * @param $do_update
  380. * If set to TRUE then publications that already exist in the Chado database
  381. * will be updated, whereas if FALSE only new publications will be added.
  382. *
  383. * @ingroup tripal_pub_api
  384. */
  385. function chado_execute_active_pub_importers($report_email = FALSE, $do_update = FALSE) {
  386. $num_to_retrieve = 100;
  387. $page = 0;
  388. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  389. "If the load fails or is terminated prematurely then the entire set of \n" .
  390. "insertions/updates is rolled back and will not be found in the database\n\n";
  391. // start the transaction
  392. $transaction = db_transaction();
  393. try {
  394. // Get all of the loaders.
  395. $args = array();
  396. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
  397. $results = db_query($sql, $args);
  398. $do_contact = FALSE;
  399. $reports = array();
  400. foreach ($results as $import) {
  401. $page = 0;
  402. print "Executing importer: '" . $import->name . "'\n";
  403. // Keep track if any of the importers want to create contacts from authors.
  404. if ($import->do_contact == 1) {
  405. $do_contact = TRUE;
  406. }
  407. $criteria = unserialize($import->criteria);
  408. $remote_db = $criteria['remote_db'];
  409. do {
  410. // Retrieve the pubs for this page. We'll retreive 100 at a time.
  411. $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
  412. $pubs = $results['pubs'];
  413. $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
  414. $page++;
  415. }
  416. // Continue looping until we have a $pubs array that does not have
  417. // our requested numer of records. This means we've hit the end.
  418. while (count($pubs) == $num_to_retrieve);
  419. }
  420. // Sync the newly added publications with Drupal. If the user
  421. // requested a report then we don't want to print any syncing information
  422. // so pass 'FALSE' to the sync call.
  423. // For backwards compatibility check to see if the legacy pub module
  424. // is enabled. If so, then sync the nodes.
  425. if (module_exists('tripal_pub')) {
  426. print "Syncing publications with Drupal...\n";
  427. chado_node_sync_records('pub');
  428. }
  429. // Iterate through each of the reports and generate a final report with HTML
  430. // links.
  431. $HTML_report = '';
  432. if ($report_email) {
  433. $HTML_report .= "<html>";
  434. global $base_url;
  435. foreach ($reports as $importer => $report) {
  436. $total = count($report['inserted']);
  437. $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
  438. foreach ($report['inserted'] as $pub) {
  439. $item = $pub['Title'];
  440. if (array_key_exists('pub_id', $pub)) {
  441. $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
  442. }
  443. $HTML_report .= "<li>$item</li>\n";
  444. }
  445. $HTML_report .= "</ol>\n";
  446. }
  447. $HTML_report .= "</html>";
  448. $site_email = variable_get('site_mail', '');
  449. $params = array(
  450. 'message' => $HTML_report
  451. );
  452. drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
  453. }
  454. // For backwards compatibility check to see if the legacy pub module
  455. // is enabled. If so, then sync the nodes.
  456. if (module_exists('tripal_pub')) {
  457. // If any of the importers wanted to create contacts from the authors then
  458. // sync them.
  459. if($do_contact) {
  460. print "Syncing contacts with Drupal...\n";
  461. chado_node_sync_records('contact');
  462. }
  463. }
  464. }
  465. catch (Exception $e) {
  466. $transaction->rollback();
  467. print "\n"; // make sure we start errors on new line
  468. watchdog_exception('T_pub_import', $e);
  469. print "FAILED: Rolling back database changes...\n";
  470. return;
  471. }
  472. print "Done.\n";
  473. }
  474. /**
  475. * Updates publication records.
  476. *
  477. * Updates publication records that currently exist in the Chado pub table
  478. * with the most recent data in the remote database.
  479. *
  480. * @param $do_contact
  481. * Set to TRUE if authors should automatically have a contact record added
  482. * to Chado. Contacts are added using the name provided by the remote
  483. * database.
  484. * @param $dbxref
  485. * The unique database ID for the record to update. This value must
  486. * be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
  487. * database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
  488. * for the record in the database.
  489. * @param $db
  490. * The name of the remote database to update. If this value is provided and
  491. * no dbxref then all of the publications currently in the Chado database
  492. * for this remote database will be updated.
  493. *
  494. * @ingroup tripal_pub_api
  495. */
  496. function chado_reimport_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
  497. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  498. "If the load fails or is terminated prematurely then the entire set of \n" .
  499. "insertions/updates is rolled back and will not be found in the database\n\n";
  500. $transaction = db_transaction();
  501. try {
  502. // Get a list of all publications by their Dbxrefs that have supported
  503. // databases.
  504. $sql = "
  505. SELECT DB.name as db_name, DBX.accession
  506. FROM pub P
  507. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  508. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  509. INNER JOIN db DB ON DB.db_id = DBX.db_id
  510. ";
  511. $args = array();
  512. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  513. $dbname = $matches[1];
  514. $accession = $matches[2];
  515. $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
  516. $args[':accession'] = $accession;
  517. $args[':dbname'] = $dbname;
  518. }
  519. elseif ($db) {
  520. $sql .= " WHERE DB.name = :dbname ";
  521. $args[':dbname'] = $db;
  522. }
  523. $sql .= "ORDER BY DB.name, P.pub_id";
  524. $results = chado_query($sql, $args);
  525. $num_to_retrieve = 100;
  526. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query.
  527. $curr_db = ''; // keeps track of the current current database.
  528. $ids = array(); // the list of IDs for the database.
  529. $search = array(); // the search array passed to the search function.
  530. // Iterate through the pub IDs.
  531. while ($pub = $results->fetchObject()) {
  532. $accession = $pub->accession;
  533. $remote_db = $pub->db_name;
  534. // Here we need to only update publications for databases we support.
  535. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  536. if(!in_array($remote_db, $supported_dbs)) {
  537. continue;
  538. }
  539. $search = array(
  540. 'num_criteria' => 1,
  541. 'remote_db' => $remote_db,
  542. 'criteria' => array(
  543. '1' => array(
  544. 'search_terms' => "$remote_db:$accession",
  545. 'scope' => 'id',
  546. 'operation' => '',
  547. 'is_phrase' => 0,
  548. ),
  549. ),
  550. );
  551. $pubs = tripal_get_remote_pubs($remote_db, $search, 1, 0);
  552. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  553. $i++;
  554. }
  555. // For backwards compatibility check to see if the legacy pub module
  556. // is enabled. If so, then sync the nodes.
  557. if (module_exists('tripal_pub')) {
  558. // Sync the newly added publications with Drupal.
  559. print "Syncing publications with Drupal...\n";
  560. chado_node_sync_records('pub');
  561. // If the caller wants to create contacts then we should sync them.
  562. if ($do_contact) {
  563. print "Syncing contacts with Drupal...\n";
  564. chado_node_sync_records('contact');
  565. }
  566. }
  567. }
  568. catch (Exception $e) {
  569. $transaction->rollback();
  570. print "\n"; // make sure we start errors on new line
  571. watchdog_exception('T_pub_import', $e);
  572. print "FAILED: Rolling back database changes...\n";
  573. return;
  574. }
  575. print "Done.\n";
  576. }
  577. /**
  578. * Launch the Tripal job to generate citations.
  579. *
  580. * This function will recreate citations for all publications currently
  581. * loaded into Tripal. This is useful to create a consistent format for
  582. * all citations.
  583. *
  584. * @param $options
  585. * Options pertaining to what publications to generate citations for.
  586. * One of the following must be present:
  587. * - all: Create and replace citation for all pubs.
  588. * - new: Create citation for pubs that don't already have one.
  589. *
  590. * @ingroup tripal_pub_api
  591. */
  592. function chado_pub_create_citations($options) {
  593. $skip_existing = TRUE;
  594. $sql = "
  595. SELECT cvterm_id
  596. FROM {cvterm}
  597. WHERE
  598. name = 'Citation' AND
  599. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  600. ";
  601. $citation_type_id = chado_query($sql)->fetchField();
  602. // Create and replace citation for all pubs.
  603. if ($options == 'all') {
  604. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  605. $skip_existing = FALSE;
  606. }
  607. // Create citation for pubs that don't already have one.
  608. else if ($options == 'new') {
  609. $sql = "
  610. SELECT pub_id
  611. FROM {pub} P
  612. WHERE
  613. (SELECT value
  614. FROM {pubprop} PB
  615. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  616. AND pub_id <> 1
  617. ";
  618. $skip_existing = TRUE;
  619. }
  620. $result = chado_query($sql, array(':type_id' => $citation_type_id));
  621. $counter_updated = 0;
  622. $counter_generated = 0;
  623. while ($pub = $result->fetchObject()) {
  624. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  625. if ($pub_arr) {
  626. $citation = chado_pub_create_citation($pub_arr);
  627. print $citation . "\n\n";
  628. // Replace if citation exists. This condition is never TRUE if
  629. // $skip_existing is TRUE.
  630. if ($pub_arr['Citation']) {
  631. $sql = "
  632. UPDATE {pubprop} SET value = :value
  633. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  634. ";
  635. chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
  636. ':type_id' => $citation_type_id, ':rank' => 0));
  637. $counter_updated ++;
  638. // Generate a new citation.
  639. } else {
  640. $sql = "
  641. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  642. VALUES (:pub_id, :type_id, :value, :rank)
  643. ";
  644. chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
  645. ':value' => $citation, ':rank' => 0));
  646. $counter_generated ++;
  647. }
  648. }
  649. }
  650. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  651. }
  652. /**
  653. * This function generates citations for publications. It requires
  654. * an array structure with keys being the terms in the Tripal
  655. * publication ontology. This function is intended to be used
  656. * for any function that needs to generate a citation.
  657. *
  658. * @param $pub
  659. * An array structure containing publication details where the keys
  660. * are the publication ontology term names and values are the
  661. * corresponding details. The pub array can contain the following
  662. * keys with corresponding values:
  663. * - Publication Type: an array of publication types. a publication can
  664. * have more than one type.
  665. * - Authors: a string containing all of the authors of a publication.
  666. * - Journal Name: a string containing the journal name.
  667. * - Journal Abbreviation: a string containing the journal name abbreviation.
  668. * - Series Name: a string containing the series (e.g. conference
  669. * proceedings) name.
  670. * - Series Abbreviation: a string containing the series name abbreviation
  671. * - Volume: the serives volume number.
  672. * - Issue: the series issue number.
  673. * - Pages: the page numbers for the publication.
  674. * - Publication Date: A date in the format "Year Month Day".
  675. *
  676. * @return
  677. * A text string containing the citation.
  678. *
  679. * @ingroup tripal_pub_api
  680. */
  681. function chado_pub_create_citation($pub) {
  682. $citation = '';
  683. $pub_type = '';
  684. // An article may have more than one publication type. For example,
  685. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  686. // Therefore, we need to select the type that makes most sense for
  687. // construction of the citation. Here we'll iterate through them all
  688. // and select the one that matches best.
  689. if (is_array($pub['Publication Type'])) {
  690. foreach ($pub['Publication Type'] as $ptype) {
  691. if ($ptype == 'Journal Article' ) {
  692. $pub_type = $ptype;
  693. break;
  694. }
  695. else if ($ptype == 'Conference Proceedings'){
  696. $pub_type = $ptype;
  697. break;
  698. }
  699. else if ($ptype == 'Review') {
  700. $pub_type = $ptype;
  701. break;
  702. }
  703. else if ($ptype == 'Book') {
  704. $pub_type = $ptype;
  705. break;
  706. }
  707. else if ($ptype == 'Letter') {
  708. $pub_type = $ptype;
  709. break;
  710. }
  711. else if ($ptype == 'Book Chapter') {
  712. $pub_type = $ptype;
  713. break;
  714. }
  715. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  716. $pub_type = $ptype;
  717. // We don't break because if the article is also a Journal Article
  718. // we prefer that type.
  719. }
  720. }
  721. // If we don't have a recognized publication type, then just use the
  722. // first one in the list.
  723. if (!$pub_type) {
  724. $pub_type = $pub['Publication Type'][0];
  725. }
  726. }
  727. else {
  728. $pub_type = $pub['Publication Type'];
  729. }
  730. //----------------------
  731. // Journal Article
  732. //----------------------
  733. if ($pub_type == 'Journal Article') {
  734. if (array_key_exists('Authors', $pub)) {
  735. $citation = $pub['Authors'] . '. ';
  736. }
  737. $citation .= $pub['Title'] . '. ';
  738. if (array_key_exists('Journal Name', $pub)) {
  739. $citation .= $pub['Journal Name'] . '. ';
  740. }
  741. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  742. $citation .= $pub['Journal Abbreviation'] . '. ';
  743. }
  744. elseif (array_key_exists('Series Name', $pub)) {
  745. $citation .= $pub['Series Name'] . '. ';
  746. }
  747. elseif (array_key_exists('Series Abbreviation', $pub)) {
  748. $citation .= $pub['Series Abbreviation'] . '. ';
  749. }
  750. if (array_key_exists('Publication Date', $pub)) {
  751. $citation .= $pub['Publication Date'];
  752. }
  753. elseif (array_key_exists('Year', $pub)) {
  754. $citation .= $pub['Year'];
  755. }
  756. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  757. $citation .= '; ';
  758. }
  759. if (array_key_exists('Volume', $pub)) {
  760. $citation .= $pub['Volume'];
  761. }
  762. if (array_key_exists('Issue', $pub)) {
  763. $citation .= '(' . $pub['Issue'] . ')';
  764. }
  765. if (array_key_exists('Pages', $pub)) {
  766. if (array_key_exists('Volume', $pub)) {
  767. $citation .= ':';
  768. }
  769. $citation .= $pub['Pages'];
  770. }
  771. $citation .= '.';
  772. }
  773. //----------------------
  774. // Review
  775. //----------------------
  776. else if ($pub_type == 'Review') {
  777. if (array_key_exists('Authors', $pub)) {
  778. $citation = $pub['Authors'] . '. ';
  779. }
  780. $citation .= $pub['Title'] . '. ';
  781. if (array_key_exists('Journal Name', $pub)) {
  782. $citation .= $pub['Journal Name'] . '. ';
  783. }
  784. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  785. $citation .= $pub['Journal Abbreviation'] . '. ';
  786. }
  787. elseif (array_key_exists('Series Name', $pub)) {
  788. $citation .= $pub['Series Name'] . '. ';
  789. }
  790. elseif (array_key_exists('Series Abbreviation', $pub)) {
  791. $citation .= $pub['Series Abbreviation'] . '. ';
  792. }
  793. if (array_key_exists('Publication Date', $pub)) {
  794. $citation .= $pub['Publication Date'];
  795. }
  796. elseif (array_key_exists('Year', $pub)) {
  797. $citation .= $pub['Year'];
  798. }
  799. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  800. $citation .= '; ';
  801. }
  802. if (array_key_exists('Volume', $pub)) {
  803. $citation .= $pub['Volume'];
  804. }
  805. if (array_key_exists('Issue', $pub)) {
  806. $citation .= '(' . $pub['Issue'] . ')';
  807. }
  808. if (array_key_exists('Pages', $pub)) {
  809. if (array_key_exists('Volume', $pub)) {
  810. $citation .= ':';
  811. }
  812. $citation .= $pub['Pages'];
  813. }
  814. $citation .= '.';
  815. }
  816. //----------------------
  817. // Research Support, Non-U.S. Gov't
  818. //----------------------
  819. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  820. if (array_key_exists('Authors', $pub)) {
  821. $citation = $pub['Authors'] . '. ';
  822. }
  823. $citation .= $pub['Title'] . '. ';
  824. if (array_key_exists('Journal Name', $pub)) {
  825. $citation .= $pub['Journal Name'] . '. ';
  826. }
  827. if (array_key_exists('Publication Date', $pub)) {
  828. $citation .= $pub['Publication Date'];
  829. }
  830. elseif (array_key_exists('Year', $pub)) {
  831. $citation .= $pub['Year'];
  832. }
  833. $citation .= '.';
  834. }
  835. //----------------------
  836. // Letter
  837. //----------------------
  838. elseif ($pub_type == 'Letter') {
  839. if (array_key_exists('Authors', $pub)) {
  840. $citation = $pub['Authors'] . '. ';
  841. }
  842. $citation .= $pub['Title'] . '. ';
  843. if (array_key_exists('Journal Name', $pub)) {
  844. $citation .= $pub['Journal Name'] . '. ';
  845. }
  846. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  847. $citation .= $pub['Journal Abbreviation'] . '. ';
  848. }
  849. elseif (array_key_exists('Series Name', $pub)) {
  850. $citation .= $pub['Series Name'] . '. ';
  851. }
  852. elseif (array_key_exists('Series Abbreviation', $pub)) {
  853. $citation .= $pub['Series Abbreviation'] . '. ';
  854. }
  855. if (array_key_exists('Publication Date', $pub)) {
  856. $citation .= $pub['Publication Date'];
  857. }
  858. elseif (array_key_exists('Year', $pub)) {
  859. $citation .= $pub['Year'];
  860. }
  861. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  862. $citation .= '; ';
  863. }
  864. if (array_key_exists('Volume', $pub)) {
  865. $citation .= $pub['Volume'];
  866. }
  867. if (array_key_exists('Issue', $pub)) {
  868. $citation .= '(' . $pub['Issue'] . ')';
  869. }
  870. if (array_key_exists('Pages', $pub)) {
  871. if (array_key_exists('Volume', $pub)) {
  872. $citation .= ':';
  873. }
  874. $citation .= $pub['Pages'];
  875. }
  876. $citation .= '.';
  877. }
  878. //-----------------------
  879. // Conference Proceedings
  880. //-----------------------
  881. elseif ($pub_type == 'Conference Proceedings') {
  882. if (array_key_exists('Authors', $pub)) {
  883. $citation = $pub['Authors'] . '. ';
  884. }
  885. $citation .= $pub['Title'] . '. ';
  886. if (array_key_exists('Conference Name', $pub)) {
  887. $citation .= $pub['Conference Name'] . '. ';
  888. }
  889. elseif (array_key_exists('Series Name', $pub)) {
  890. $citation .= $pub['Series Name'] . '. ';
  891. }
  892. elseif (array_key_exists('Series Abbreviation', $pub)) {
  893. $citation .= $pub['Series Abbreviation'] . '. ';
  894. }
  895. if (array_key_exists('Publication Date', $pub)) {
  896. $citation .= $pub['Publication Date'];
  897. }
  898. elseif (array_key_exists('Year', $pub)) {
  899. $citation .= $pub['Year'];
  900. }
  901. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  902. $citation .= '; ';
  903. }
  904. if (array_key_exists('Volume', $pub)) {
  905. $citation .= $pub['Volume'];
  906. }
  907. if (array_key_exists('Issue', $pub)) {
  908. $citation .= '(' . $pub['Issue'] . ')';
  909. }
  910. if (array_key_exists('Pages', $pub)) {
  911. if (array_key_exists('Volume', $pub)) {
  912. $citation .= ':';
  913. }
  914. $citation .= $pub['Pages'];
  915. }
  916. $citation .= '.';
  917. }
  918. //-----------------------
  919. // Default
  920. //-----------------------
  921. else {
  922. if (array_key_exists('Authors', $pub)) {
  923. $citation = $pub['Authors'] . '. ';
  924. }
  925. $citation .= $pub['Title'] . '. ';
  926. if (array_key_exists('Series Name', $pub)) {
  927. $citation .= $pub['Series Name'] . '. ';
  928. }
  929. elseif (array_key_exists('Series Abbreviation', $pub)) {
  930. $citation .= $pub['Series Abbreviation'] . '. ';
  931. }
  932. if (array_key_exists('Publication Date', $pub)) {
  933. $citation .= $pub['Publication Date'];
  934. }
  935. elseif (array_key_exists('Year', $pub)) {
  936. $citation .= $pub['Year'];
  937. }
  938. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  939. $citation .= '; ';
  940. }
  941. if (array_key_exists('Volume', $pub)) {
  942. $citation .= $pub['Volume'];
  943. }
  944. if (array_key_exists('Issue', $pub)) {
  945. $citation .= '(' . $pub['Issue'] . ')';
  946. }
  947. if (array_key_exists('Pages', $pub)) {
  948. if (array_key_exists('Volume', $pub)) {
  949. $citation .= ':';
  950. }
  951. $citation .= $pub['Pages'];
  952. }
  953. $citation .= '.';
  954. }
  955. return $citation;
  956. }
  957. /**
  958. * Retrieves the minimal information to uniquely describe any publication.
  959. *
  960. * The returned array is an associative array where the keys are
  961. * the controlled vocabulary terms in the form [vocab]:[accession].
  962. *
  963. * @param $pub
  964. * A publication object as created by chado_generate_var().
  965. *
  966. * @return
  967. * An array with the following keys: 'Citation', 'Abstract', 'Authors',
  968. * 'URL'. All keys are term names in the Tripal Publication Ontology :TPUB.
  969. *
  970. * @ingroup tripal_pub_api
  971. */
  972. function chado_get_minimal_pub_info($pub) {
  973. if (!$pub) {
  974. return array();
  975. }
  976. // Chado has a null pub as default. We don't return anything for this.
  977. if (isset($pub->uniquename) && $pub->uniquename == 'null') {
  978. return array();
  979. }
  980. // Expand the title.
  981. $pub = chado_expand_var($pub, 'field', 'pub.title');
  982. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  983. // Get the abstract.
  984. $values = array(
  985. 'pub_id' => $pub->pub_id,
  986. 'type_id' => array(
  987. 'name' => 'Abstract',
  988. ),
  989. );
  990. $options = array(
  991. 'include_fk' => array(
  992. ),
  993. );
  994. $abstract = chado_generate_var('pubprop', $values, $options);
  995. $abstract = chado_expand_var($abstract, 'field', 'pubprop.value');
  996. $abstract_text = '';
  997. if ($abstract) {
  998. $abstract_text = htmlspecialchars($abstract->value);
  999. }
  1000. // Get the author list.
  1001. $values = array(
  1002. 'pub_id' => $pub->pub_id,
  1003. 'type_id' => array(
  1004. 'name' => 'Authors',
  1005. ),
  1006. );
  1007. $options = array(
  1008. 'include_fk' => array(
  1009. ),
  1010. );
  1011. $authors = chado_generate_var('pubprop', $values, $options);
  1012. $authors = chado_expand_var($authors, 'field', 'pubprop.value');
  1013. $authors_list = 'N/A';
  1014. if ($authors) {
  1015. $authors_list = $authors->value;
  1016. }
  1017. // Get the first database cross-reference with a url.
  1018. $options = array('return_array' => 1);
  1019. $pub = chado_expand_var($pub, 'table', 'pub_dbxref', $options);
  1020. $dbxref = NULL;
  1021. if ($pub->pub_dbxref) {
  1022. foreach ($pub->pub_dbxref as $index => $pub_dbxref) {
  1023. if ($pub_dbxref->dbxref_id->db_id->urlprefix) {
  1024. $dbxref = $pub_dbxref->dbxref_id;
  1025. }
  1026. }
  1027. }
  1028. // Get the URL.
  1029. $values = array(
  1030. 'pub_id' => $pub->pub_id,
  1031. 'type_id' => array(
  1032. 'name' => 'URL',
  1033. ),
  1034. );
  1035. $options = array(
  1036. 'return_array' => 1,
  1037. 'include_fk' => array(),
  1038. );
  1039. $url = '';
  1040. $urls = chado_generate_var('pubprop', $values, $options);
  1041. if ($urls) {
  1042. $urls = chado_expand_var($urls, 'field', 'pubprop.value');
  1043. if (count($urls) > 0) {
  1044. $url = $urls[0]->value;
  1045. }
  1046. }
  1047. // Get the list of database cross references.
  1048. $values = array(
  1049. 'pub_id' => $pub->pub_id,
  1050. );
  1051. $options = array(
  1052. 'return_array' => 1,
  1053. );
  1054. $pub_dbxrefs = chado_generate_var('pub_dbxref', $values, $options);
  1055. $dbxrefs = array();
  1056. foreach ($pub_dbxrefs as $pub_dbxref) {
  1057. $dbxrefs[] = $pub_dbxref->dbxref_id->db_id->name . ':' . $pub_dbxref->dbxref_id->accession;
  1058. }
  1059. // Get the citation.
  1060. $values = array(
  1061. 'pub_id' => $pub->pub_id,
  1062. 'type_id' => array(
  1063. 'name' => 'Citation',
  1064. ),
  1065. );
  1066. $options = array(
  1067. 'include_fk' => array(
  1068. ),
  1069. );
  1070. $citation = chado_generate_var('pubprop', $values, $options);
  1071. if ($citation) {
  1072. $citation = chado_expand_var($citation, 'field', 'pubprop.value');
  1073. $citation = $citation->value;
  1074. }
  1075. else {
  1076. $pub_info = array(
  1077. 'Title' => $pub->title,
  1078. 'Publication Type' => $pub->type_id->name,
  1079. 'Authors' => $authors_list,
  1080. 'Series Name' => $pub->series_name,
  1081. 'Volume' => $pub->volume,
  1082. 'Issue' => $pub->issue,
  1083. 'Pages' => $pub->pages,
  1084. 'Publication Date' => $pub->pyear,
  1085. );
  1086. $citation = chado_pub_create_citation($pub_info);
  1087. }
  1088. return array(
  1089. 'TPUB:0000039' => $pub->title,
  1090. 'TPUB:0000003' => $citation,
  1091. 'TPUB:0000050' => $abstract_text,
  1092. 'TPUB:0000047' => $authors_list,
  1093. 'TPUB:0000052' => $url,
  1094. 'SBO:0000554' => $dbxrefs,
  1095. );
  1096. }