tripal_chado.pub.api.inc 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 $publish
  312. * Set to TRUE if publications should be published after import. For Tripal
  313. * v3 this value can be set to the string 'sync' or 'both' in the event that
  314. * the site is in "legacy" mode. Setting this value to 'sync' will create
  315. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  316. * only entities are created.
  317. * @param $do_update
  318. * If set to TRUE then the publication will be updated if it already exists
  319. * in the database.
  320. *
  321. * @ingroup tripal_pub_api
  322. */
  323. function chado_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE,
  324. $publish = TRUE, $do_update = TRUE) {
  325. $num_to_retrieve = 1;
  326. $pager_id = 0;
  327. $page = 0;
  328. $num_pubs = 0;
  329. $pub_id = NULL;
  330. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.pub_importers');
  331. // These are options for the tripal_report_error function. We do not
  332. // want to log messages to the watchdog but we do for the job and to
  333. // the terminal
  334. $message_type = 'pub_import';
  335. $message_opts = [
  336. 'watchdog' == FALSE,
  337. 'print' => TRUE,
  338. ];
  339. $message = "Importing of publications is performed using a database transaction. " .
  340. "If the load fails or is terminated prematurely then the entire set of " .
  341. "deletions is rolled back and will not be found in the database";
  342. tripal_report_error($message_type, TRIPAL_INFO, $message, [], $message_opts);
  343. $transaction = db_transaction();
  344. try {
  345. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  346. $dbname = $matches[1];
  347. $accession = $matches[2];
  348. $criteria = array(
  349. 'num_criteria' => 1,
  350. 'remote_db' => $dbname,
  351. 'criteria' => array(
  352. '1' => array(
  353. 'search_terms' => "$dbname:$accession",
  354. 'scope' => 'id',
  355. 'operation' => '',
  356. 'is_phrase' => 0,
  357. ),
  358. ),
  359. );
  360. $remote_db = $criteria['remote_db'];
  361. $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
  362. $pubs = $results['pubs'];
  363. $search_str = $results['search_str'];
  364. $total_records = $results['total_records'];
  365. tripal_pub_add_publications($pubs, $do_contact, $do_update);
  366. }
  367. // Publish as requested by the caller.
  368. _chado_execute_pub_importer_publish($publish, NULL, $message_type, $message_opts);
  369. }
  370. catch (Exception $e) {
  371. $transaction->rollback();
  372. print "\n"; // make sure we start errors on new line
  373. watchdog_exception('T_pub_import', $e);
  374. print "FAILED: Rolling back database changes...\n";
  375. return;
  376. }
  377. }
  378. /**
  379. * Imports all publications for all active import setups.
  380. *
  381. * @param $report_email
  382. * A list of email address, separated by commas, that should be notified
  383. * once importing has completed.
  384. * @param $publish
  385. * Set to TRUE if publications should be published after import. For Tripal
  386. * v3 this value can be set to the string 'sync' or 'both' in the event that
  387. * the site is in "legacy" mode. Setting this value to 'sync' will create
  388. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  389. * only entities are created.
  390. * @param $do_update
  391. * If set to TRUE then publications that already exist in the Chado database
  392. * will be updated, whereas if FALSE only new publications will be added.
  393. *
  394. * @ingroup tripal_pub_api
  395. */
  396. function chado_execute_active_pub_importers($report_email = FALSE,
  397. $publish = TRUE, $do_update = FALSE) {
  398. // Get all of the loaders.
  399. $args = array();
  400. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
  401. $importers = db_query($sql, $args);
  402. $do_contact = FALSE;
  403. $reports = array();
  404. while ($import = $importers->fetchObject()) {
  405. chado_execute_pub_importer($import->pub_import_id, $publish, $do_update);
  406. }
  407. // Iterate through each of the reports and generate a final report with HTML
  408. // links.
  409. $HTML_report = '';
  410. if ($report_email) {
  411. $HTML_report .= "<html>";
  412. global $base_url;
  413. foreach ($reports as $importer => $report) {
  414. $total = count($report['inserted']);
  415. $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
  416. foreach ($report['inserted'] as $pub) {
  417. $item = $pub['Title'];
  418. if (array_key_exists('pub_id', $pub)) {
  419. $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
  420. }
  421. $HTML_report .= "<li>$item</li>\n";
  422. }
  423. $HTML_report .= "</ol>\n";
  424. }
  425. $HTML_report .= "</html>";
  426. $site_email = variable_get('site_mail', '');
  427. $params = array(
  428. 'message' => $HTML_report
  429. );
  430. drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
  431. }
  432. print "Done.\n";
  433. }
  434. /**
  435. * Imports all publications for a given publication import setup.
  436. *
  437. * @param $import_id
  438. * The ID of the import setup to use
  439. * @param $publish
  440. * Set to TRUE if publications should be published after import. For Tripal
  441. * v3 this value can be set to the string 'sync' or 'both' in the event that
  442. * the site is in "legacy" mode. Setting this value to 'sync' will create
  443. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  444. * only entities are created.
  445. * @param $do_update
  446. * If set to TRUE then publications that already exist in the Chado database
  447. * will be updated, whereas if FALSE only new publications will be added.
  448. * @param $job
  449. * The jobs management object for the job if this function is run as a job.
  450. * This argument is added by Tripal during a job run and is not needed if
  451. * this function is run directly.
  452. *
  453. * @return
  454. * TRUE if importing occured with on errors. FALSE otherwise. If failed,
  455. * all database changes are rolled back.
  456. *
  457. * @ingroup tripal_pub
  458. */
  459. function chado_execute_pub_importer($import_id, $publish = TRUE, $do_update = FALSE, $job = NULL) {
  460. // These are options for the tripal_report_error function. We do not
  461. // want to log messages to the watchdog but we do for the job and to
  462. // the terminal
  463. $message_type = 'pub_import';
  464. $message_opts = [
  465. 'watchdog' == FALSE,
  466. 'job' => $job,
  467. 'print' => TRUE,
  468. ];
  469. $message = "Importing of publications for this importer is performed using a database transaction. " .
  470. "If the load fails or is terminated prematurely then the entire set of " .
  471. "deletions is rolled back and will not be found in the database";
  472. tripal_report_error($message_type, TRIPAL_INFO, $message, [], $message_opts);
  473. // start the transaction
  474. $transaction = db_transaction();
  475. try {
  476. $page = 0;
  477. $do_contact = FALSE;
  478. $num_to_retrieve = 100;
  479. // get all of the loaders
  480. $args = array(':import_id' => $import_id);
  481. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
  482. $import = db_query($sql, $args)->fetchObject();
  483. tripal_report_error($message_type, TRIPAL_INFO,
  484. "Executing Importer: !name.", ['!name' => $import->name], $message_opts);
  485. $criteria = unserialize($import->criteria);
  486. $remote_db = $criteria['remote_db'];
  487. $total_pubs = 0;
  488. // Loop until we have a $pubs array that does not have
  489. // our requested numer of records. This means we've hit the end
  490. do {
  491. // retrieve the pubs for this page. We'll retreive 100 at a time
  492. tripal_report_error($message_type, TRIPAL_INFO,
  493. "Querying !remote_db for up to !num pubs that match the criteria.",
  494. ['!num' => $num_to_retrieve, '!remote_db' => $remote_db], $message_opts);
  495. $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
  496. $pubs = $results['pubs'];
  497. $num_pubs = $results['total_records'];
  498. $total_pubs += $num_pubs;
  499. tripal_report_error($message_type, TRIPAL_INFO,
  500. "Adding %num new publications.",
  501. ['%num' => $num_pubs], $message_opts);
  502. tripal_pub_add_publications($pubs, $import->do_contact, $do_update, $job);
  503. $page++;
  504. }
  505. while (count($pubs) == $num_to_retrieve);
  506. // Publish as requested by the caller.
  507. _chado_execute_pub_importer_publish($publish, $job, $message_type, $message_opts);
  508. if ($job) {
  509. $job->setProgress(100);
  510. }
  511. }
  512. catch (Exception $e) {
  513. $transaction->rollback();
  514. watchdog_exception('T_pub_import', $e);
  515. tripal_report_error($message_type, TRIPAL_ERROR,
  516. "Rolling back database changes... !message",
  517. ['!message' => $e->getMessage()], $message_opts);
  518. return FALSE;
  519. }
  520. tripal_report_error($message_type, TRIPAL_INFO,
  521. "Done.", [], $message_opts);
  522. return TRUE;
  523. }
  524. /**
  525. * A helper function to dermine if imported publications should be published.
  526. *
  527. * It supports backwards compatibility with Tripal v2 legacy mode.
  528. *
  529. * @param $publish
  530. * Set to TRUE if publications should be published after import. For Tripal
  531. * v3 this value can be set to the string 'sync' or 'both' in the event that
  532. * the site is in "legacy" mode. Setting this value to 'sync' will create
  533. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  534. * only entities are created.
  535. */
  536. function _chado_execute_pub_importer_publish($publish, $job, $message_type, $message_opts) {
  537. // If the user wants to publish then do so.
  538. if ($publish === TRUE or $publish === 'both') {
  539. // Get the bundle for the Publication content type.
  540. $bundle_term = tripal_load_term_entity(['vocabulary' => 'TPUB', 'accession' => '0000002']);
  541. $bundle = tripal_load_bundle_entity(['term_id' => $bundle_term->id]);
  542. if ($bundle) {
  543. tripal_report_error($message_type, TRIPAL_INFO,
  544. "Publishing publications with Drupal...", [], $message_opts);
  545. chado_publish_records(['bundle_name' => $bundle->name], $job);
  546. }
  547. // Note: we won't publish contacts as Tripal v2 did because there is
  548. // no consisten way to do that. Each site my use a different term for
  549. // different contact content types (e.g. all as one 'Contact' type or
  550. // specific such as 'Person', 'Organization', etc.).
  551. }
  552. // For backwords compatibility with legacy module do a sync.
  553. if ($publish === 'sync' or $publish === 'both') {
  554. if (module_exists('tripal_pub')) {
  555. tripal_report_error($message_type, TRIPAL_INFO,
  556. "Syncing publications with Drupal...", [], $message_opts);
  557. chado_node_sync_records('pub');
  558. if($import->do_contact) {
  559. tripal_report_error($message_type, TRIPAL_INFO,
  560. "Syncing contacts with Drupal...", [], $message_opts);
  561. chado_node_sync_records('contact');
  562. }
  563. }
  564. }
  565. }
  566. /**
  567. * Updates publication records.
  568. *
  569. * Updates publication records that currently exist in the Chado pub table
  570. * with the most recent data in the remote database.
  571. *
  572. * @param $do_contact
  573. * Set to TRUE if authors should automatically have a contact record added
  574. * to Chado. Contacts are added using the name provided by the remote
  575. * database.
  576. * @param $dbxref
  577. * The unique database ID for the record to update. This value must
  578. * be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
  579. * database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
  580. * for the record in the database.
  581. * @param $db
  582. * The name of the remote database to update. If this value is provided and
  583. * no dbxref then all of the publications currently in the Chado database
  584. * for this remote database will be updated.
  585. * @param $publish
  586. * Set to TRUE if publications should be published after import. For Tripal
  587. * v3 this value can be set to the string 'sync' or 'both' in the event that
  588. * the site is in "legacy" mode. Setting this value to 'sync' will create
  589. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  590. * only entities are created.
  591. * @ingroup tripal_pub_api
  592. */
  593. function chado_reimport_publications($do_contact = FALSE, $dbxref = NULL,
  594. $db = NULL, $publish = TRUE) {
  595. // These are options for the tripal_report_error function. We do not
  596. // want to log messages to the watchdog but we do for the job and to
  597. // the terminal
  598. $message_type = 'pub_import';
  599. $message_opts = [
  600. 'watchdog' == FALSE,
  601. 'print' => TRUE,
  602. ];
  603. $message = "Importing of publications for this importer is performed using a database transaction. " .
  604. "If the load fails or is terminated prematurely then the entire set of " .
  605. "deletions is rolled back and will not be found in the database";
  606. tripal_report_error($message_type, TRIPAL_INFO, $message, [], $message_opts);
  607. $transaction = db_transaction();
  608. try {
  609. // Get a list of all publications by their Dbxrefs that have supported
  610. // databases.
  611. $sql = "
  612. SELECT DB.name as db_name, DBX.accession
  613. FROM {pub} P
  614. INNER JOIN {pub_dbxref} PDBX ON P.pub_id = PDBX.pub_id
  615. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = PDBX.dbxref_id
  616. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  617. ";
  618. $args = array();
  619. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  620. $dbname = $matches[1];
  621. $accession = $matches[2];
  622. $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
  623. $args[':accession'] = $accession;
  624. $args[':dbname'] = $dbname;
  625. }
  626. elseif ($db) {
  627. $sql .= " WHERE DB.name = :dbname ";
  628. $args[':dbname'] = $db;
  629. }
  630. $sql .= "ORDER BY DB.name, P.pub_id";
  631. $results = chado_query($sql, $args);
  632. $num_to_retrieve = 100;
  633. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query.
  634. $curr_db = ''; // keeps track of the current current database.
  635. $ids = array(); // the list of IDs for the database.
  636. $search = array(); // the search array passed to the search function.
  637. // Iterate through the pub IDs.
  638. while ($pub = $results->fetchObject()) {
  639. $accession = $pub->accession;
  640. $remote_db = $pub->db_name;
  641. // Here we need to only update publications for databases we support.
  642. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  643. if(!in_array($remote_db, $supported_dbs)) {
  644. continue;
  645. }
  646. $search = array(
  647. 'num_criteria' => 1,
  648. 'remote_db' => $remote_db,
  649. 'criteria' => array(
  650. '1' => array(
  651. 'search_terms' => "$remote_db:$accession",
  652. 'scope' => 'id',
  653. 'operation' => '',
  654. 'is_phrase' => 0,
  655. ),
  656. ),
  657. );
  658. $qresults = tripal_get_remote_pubs($remote_db, $search, 1, 0);
  659. $pubs = $qresults['pubs'];
  660. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  661. $i++;
  662. }
  663. // Publish as requested by the caller.
  664. _chado_execute_pub_importer_publish($publish, NULL, $message_type, $message_opts);
  665. }
  666. catch (Exception $e) {
  667. $transaction->rollback();
  668. watchdog_exception('T_pub_import', $e);
  669. tripal_report_error($message_type, TRIPAL_ERROR,
  670. "Rolling back database changes... !message",
  671. ['!message' => $e->getMessage()], $message_opts);return;
  672. }
  673. print "Done.\n";
  674. }
  675. /**
  676. * Launch the Tripal job to generate citations.
  677. *
  678. * This function will recreate citations for all publications currently
  679. * loaded into Tripal. This is useful to create a consistent format for
  680. * all citations.
  681. *
  682. * @param $options
  683. * Options pertaining to what publications to generate citations for.
  684. * One of the following must be present:
  685. * - all: Create and replace citation for all pubs.
  686. * - new: Create citation for pubs that don't already have one.
  687. *
  688. * @ingroup tripal_pub_api
  689. */
  690. function chado_pub_create_citations($options) {
  691. $skip_existing = TRUE;
  692. $sql = "
  693. SELECT cvterm_id
  694. FROM {cvterm}
  695. WHERE
  696. name = 'Citation' AND
  697. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  698. ";
  699. $citation_type_id = chado_query($sql)->fetchField();
  700. // Create and replace citation for all pubs.
  701. if ($options == 'all') {
  702. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  703. $skip_existing = FALSE;
  704. }
  705. // Create citation for pubs that don't already have one.
  706. else if ($options == 'new') {
  707. $sql = "
  708. SELECT pub_id
  709. FROM {pub} P
  710. WHERE
  711. (SELECT value
  712. FROM {pubprop} PB
  713. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  714. AND pub_id <> 1
  715. ";
  716. $skip_existing = TRUE;
  717. }
  718. $result = chado_query($sql, array(':type_id' => $citation_type_id));
  719. $counter_updated = 0;
  720. $counter_generated = 0;
  721. while ($pub = $result->fetchObject()) {
  722. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  723. if ($pub_arr) {
  724. $citation = chado_pub_create_citation($pub_arr);
  725. print $citation . "\n\n";
  726. // Replace if citation exists. This condition is never TRUE if
  727. // $skip_existing is TRUE.
  728. if ($pub_arr['Citation']) {
  729. $sql = "
  730. UPDATE {pubprop} SET value = :value
  731. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  732. ";
  733. chado_query($sql, array(':value' => $citation, ':pub_id' => $pub->pub_id,
  734. ':type_id' => $citation_type_id, ':rank' => 0));
  735. $counter_updated ++;
  736. // Generate a new citation.
  737. } else {
  738. $sql = "
  739. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  740. VALUES (:pub_id, :type_id, :value, :rank)
  741. ";
  742. chado_query($sql, array(':pub_id' => $pub->pub_id, ':type_id' => $citation_type_id,
  743. ':value' => $citation, ':rank' => 0));
  744. $counter_generated ++;
  745. }
  746. }
  747. }
  748. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  749. }
  750. /**
  751. * This function generates citations for publications. It requires
  752. * an array structure with keys being the terms in the Tripal
  753. * publication ontology. This function is intended to be used
  754. * for any function that needs to generate a citation.
  755. *
  756. * @param $pub
  757. * An array structure containing publication details where the keys
  758. * are the publication ontology term names and values are the
  759. * corresponding details. The pub array can contain the following
  760. * keys with corresponding values:
  761. * - Publication Type: an array of publication types. a publication can
  762. * have more than one type.
  763. * - Authors: a string containing all of the authors of a publication.
  764. * - Journal Name: a string containing the journal name.
  765. * - Journal Abbreviation: a string containing the journal name abbreviation.
  766. * - Series Name: a string containing the series (e.g. conference
  767. * proceedings) name.
  768. * - Series Abbreviation: a string containing the series name abbreviation
  769. * - Volume: the serives volume number.
  770. * - Issue: the series issue number.
  771. * - Pages: the page numbers for the publication.
  772. * - Publication Date: A date in the format "Year Month Day".
  773. *
  774. * @return
  775. * A text string containing the citation.
  776. *
  777. * @ingroup tripal_pub_api
  778. */
  779. function chado_pub_create_citation($pub) {
  780. $citation = '';
  781. $pub_type = '';
  782. // An article may have more than one publication type. For example,
  783. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  784. // Therefore, we need to select the type that makes most sense for
  785. // construction of the citation. Here we'll iterate through them all
  786. // and select the one that matches best.
  787. if (is_array($pub['Publication Type'])) {
  788. foreach ($pub['Publication Type'] as $ptype) {
  789. if ($ptype == 'Journal Article' ) {
  790. $pub_type = $ptype;
  791. break;
  792. }
  793. else if ($ptype == 'Conference Proceedings'){
  794. $pub_type = $ptype;
  795. break;
  796. }
  797. else if ($ptype == 'Review') {
  798. $pub_type = $ptype;
  799. break;
  800. }
  801. else if ($ptype == 'Book') {
  802. $pub_type = $ptype;
  803. break;
  804. }
  805. else if ($ptype == 'Letter') {
  806. $pub_type = $ptype;
  807. break;
  808. }
  809. else if ($ptype == 'Book Chapter') {
  810. $pub_type = $ptype;
  811. break;
  812. }
  813. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  814. $pub_type = $ptype;
  815. // We don't break because if the article is also a Journal Article
  816. // we prefer that type.
  817. }
  818. }
  819. // If we don't have a recognized publication type, then just use the
  820. // first one in the list.
  821. if (!$pub_type) {
  822. $pub_type = $pub['Publication Type'][0];
  823. }
  824. }
  825. else {
  826. $pub_type = $pub['Publication Type'];
  827. }
  828. //----------------------
  829. // Journal Article
  830. //----------------------
  831. if ($pub_type == 'Journal Article') {
  832. if (array_key_exists('Authors', $pub)) {
  833. $citation = $pub['Authors'] . '. ';
  834. }
  835. $citation .= $pub['Title'] . '. ';
  836. if (array_key_exists('Journal Name', $pub)) {
  837. $citation .= $pub['Journal Name'] . '. ';
  838. }
  839. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  840. $citation .= $pub['Journal Abbreviation'] . '. ';
  841. }
  842. elseif (array_key_exists('Series Name', $pub)) {
  843. $citation .= $pub['Series Name'] . '. ';
  844. }
  845. elseif (array_key_exists('Series Abbreviation', $pub)) {
  846. $citation .= $pub['Series Abbreviation'] . '. ';
  847. }
  848. if (array_key_exists('Publication Date', $pub)) {
  849. $citation .= $pub['Publication Date'];
  850. }
  851. elseif (array_key_exists('Year', $pub)) {
  852. $citation .= $pub['Year'];
  853. }
  854. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  855. $citation .= '; ';
  856. }
  857. if (array_key_exists('Volume', $pub)) {
  858. $citation .= $pub['Volume'];
  859. }
  860. if (array_key_exists('Issue', $pub)) {
  861. $citation .= '(' . $pub['Issue'] . ')';
  862. }
  863. if (array_key_exists('Pages', $pub)) {
  864. if (array_key_exists('Volume', $pub)) {
  865. $citation .= ':';
  866. }
  867. $citation .= $pub['Pages'];
  868. }
  869. $citation .= '.';
  870. }
  871. //----------------------
  872. // Review
  873. //----------------------
  874. else if ($pub_type == 'Review') {
  875. if (array_key_exists('Authors', $pub)) {
  876. $citation = $pub['Authors'] . '. ';
  877. }
  878. $citation .= $pub['Title'] . '. ';
  879. if (array_key_exists('Journal Name', $pub)) {
  880. $citation .= $pub['Journal Name'] . '. ';
  881. }
  882. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  883. $citation .= $pub['Journal Abbreviation'] . '. ';
  884. }
  885. elseif (array_key_exists('Series Name', $pub)) {
  886. $citation .= $pub['Series Name'] . '. ';
  887. }
  888. elseif (array_key_exists('Series Abbreviation', $pub)) {
  889. $citation .= $pub['Series Abbreviation'] . '. ';
  890. }
  891. if (array_key_exists('Publication Date', $pub)) {
  892. $citation .= $pub['Publication Date'];
  893. }
  894. elseif (array_key_exists('Year', $pub)) {
  895. $citation .= $pub['Year'];
  896. }
  897. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  898. $citation .= '; ';
  899. }
  900. if (array_key_exists('Volume', $pub)) {
  901. $citation .= $pub['Volume'];
  902. }
  903. if (array_key_exists('Issue', $pub)) {
  904. $citation .= '(' . $pub['Issue'] . ')';
  905. }
  906. if (array_key_exists('Pages', $pub)) {
  907. if (array_key_exists('Volume', $pub)) {
  908. $citation .= ':';
  909. }
  910. $citation .= $pub['Pages'];
  911. }
  912. $citation .= '.';
  913. }
  914. //----------------------
  915. // Research Support, Non-U.S. Gov't
  916. //----------------------
  917. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  918. if (array_key_exists('Authors', $pub)) {
  919. $citation = $pub['Authors'] . '. ';
  920. }
  921. $citation .= $pub['Title'] . '. ';
  922. if (array_key_exists('Journal Name', $pub)) {
  923. $citation .= $pub['Journal Name'] . '. ';
  924. }
  925. if (array_key_exists('Publication Date', $pub)) {
  926. $citation .= $pub['Publication Date'];
  927. }
  928. elseif (array_key_exists('Year', $pub)) {
  929. $citation .= $pub['Year'];
  930. }
  931. $citation .= '.';
  932. }
  933. //----------------------
  934. // Letter
  935. //----------------------
  936. elseif ($pub_type == 'Letter') {
  937. if (array_key_exists('Authors', $pub)) {
  938. $citation = $pub['Authors'] . '. ';
  939. }
  940. $citation .= $pub['Title'] . '. ';
  941. if (array_key_exists('Journal Name', $pub)) {
  942. $citation .= $pub['Journal Name'] . '. ';
  943. }
  944. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  945. $citation .= $pub['Journal Abbreviation'] . '. ';
  946. }
  947. elseif (array_key_exists('Series Name', $pub)) {
  948. $citation .= $pub['Series Name'] . '. ';
  949. }
  950. elseif (array_key_exists('Series Abbreviation', $pub)) {
  951. $citation .= $pub['Series Abbreviation'] . '. ';
  952. }
  953. if (array_key_exists('Publication Date', $pub)) {
  954. $citation .= $pub['Publication Date'];
  955. }
  956. elseif (array_key_exists('Year', $pub)) {
  957. $citation .= $pub['Year'];
  958. }
  959. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  960. $citation .= '; ';
  961. }
  962. if (array_key_exists('Volume', $pub)) {
  963. $citation .= $pub['Volume'];
  964. }
  965. if (array_key_exists('Issue', $pub)) {
  966. $citation .= '(' . $pub['Issue'] . ')';
  967. }
  968. if (array_key_exists('Pages', $pub)) {
  969. if (array_key_exists('Volume', $pub)) {
  970. $citation .= ':';
  971. }
  972. $citation .= $pub['Pages'];
  973. }
  974. $citation .= '.';
  975. }
  976. //-----------------------
  977. // Conference Proceedings
  978. //-----------------------
  979. elseif ($pub_type == 'Conference Proceedings') {
  980. if (array_key_exists('Authors', $pub)) {
  981. $citation = $pub['Authors'] . '. ';
  982. }
  983. $citation .= $pub['Title'] . '. ';
  984. if (array_key_exists('Conference Name', $pub)) {
  985. $citation .= $pub['Conference Name'] . '. ';
  986. }
  987. elseif (array_key_exists('Series Name', $pub)) {
  988. $citation .= $pub['Series Name'] . '. ';
  989. }
  990. elseif (array_key_exists('Series Abbreviation', $pub)) {
  991. $citation .= $pub['Series Abbreviation'] . '. ';
  992. }
  993. if (array_key_exists('Publication Date', $pub)) {
  994. $citation .= $pub['Publication Date'];
  995. }
  996. elseif (array_key_exists('Year', $pub)) {
  997. $citation .= $pub['Year'];
  998. }
  999. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1000. $citation .= '; ';
  1001. }
  1002. if (array_key_exists('Volume', $pub)) {
  1003. $citation .= $pub['Volume'];
  1004. }
  1005. if (array_key_exists('Issue', $pub)) {
  1006. $citation .= '(' . $pub['Issue'] . ')';
  1007. }
  1008. if (array_key_exists('Pages', $pub)) {
  1009. if (array_key_exists('Volume', $pub)) {
  1010. $citation .= ':';
  1011. }
  1012. $citation .= $pub['Pages'];
  1013. }
  1014. $citation .= '.';
  1015. }
  1016. //-----------------------
  1017. // Default
  1018. //-----------------------
  1019. else {
  1020. if (array_key_exists('Authors', $pub)) {
  1021. $citation = $pub['Authors'] . '. ';
  1022. }
  1023. $citation .= $pub['Title'] . '. ';
  1024. if (array_key_exists('Series Name', $pub)) {
  1025. $citation .= $pub['Series Name'] . '. ';
  1026. }
  1027. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1028. $citation .= $pub['Series Abbreviation'] . '. ';
  1029. }
  1030. if (array_key_exists('Publication Date', $pub)) {
  1031. $citation .= $pub['Publication Date'];
  1032. }
  1033. elseif (array_key_exists('Year', $pub)) {
  1034. $citation .= $pub['Year'];
  1035. }
  1036. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1037. $citation .= '; ';
  1038. }
  1039. if (array_key_exists('Volume', $pub)) {
  1040. $citation .= $pub['Volume'];
  1041. }
  1042. if (array_key_exists('Issue', $pub)) {
  1043. $citation .= '(' . $pub['Issue'] . ')';
  1044. }
  1045. if (array_key_exists('Pages', $pub)) {
  1046. if (array_key_exists('Volume', $pub)) {
  1047. $citation .= ':';
  1048. }
  1049. $citation .= $pub['Pages'];
  1050. }
  1051. $citation .= '.';
  1052. }
  1053. return $citation;
  1054. }
  1055. /**
  1056. * Retrieves the minimal information to uniquely describe any publication.
  1057. *
  1058. * The returned array is an associative array where the keys are
  1059. * the controlled vocabulary terms in the form [vocab]:[accession].
  1060. *
  1061. * @param $pub
  1062. * A publication object as created by chado_generate_var().
  1063. *
  1064. * @return
  1065. * An array with the following keys: 'Citation', 'Abstract', 'Authors',
  1066. * 'URL'. All keys are term names in the Tripal Publication Ontology :TPUB.
  1067. *
  1068. * @ingroup tripal_pub_api
  1069. */
  1070. function chado_get_minimal_pub_info($pub) {
  1071. if (!$pub) {
  1072. return array();
  1073. }
  1074. // Chado has a null pub as default. We don't return anything for this.
  1075. if (isset($pub->uniquename) && $pub->uniquename == 'null') {
  1076. return array();
  1077. }
  1078. // Expand the title.
  1079. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1080. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1081. // Get the abstract.
  1082. $values = array(
  1083. 'pub_id' => $pub->pub_id,
  1084. 'type_id' => array(
  1085. 'name' => 'Abstract',
  1086. ),
  1087. );
  1088. $options = array(
  1089. 'include_fk' => array(
  1090. ),
  1091. );
  1092. $abstract = chado_generate_var('pubprop', $values, $options);
  1093. $abstract = chado_expand_var($abstract, 'field', 'pubprop.value');
  1094. $abstract_text = '';
  1095. if ($abstract) {
  1096. $abstract_text = htmlspecialchars($abstract->value);
  1097. }
  1098. // Get the author list.
  1099. $values = array(
  1100. 'pub_id' => $pub->pub_id,
  1101. 'type_id' => array(
  1102. 'name' => 'Authors',
  1103. ),
  1104. );
  1105. $options = array(
  1106. 'include_fk' => array(
  1107. ),
  1108. );
  1109. $authors = chado_generate_var('pubprop', $values, $options);
  1110. $authors = chado_expand_var($authors, 'field', 'pubprop.value');
  1111. $authors_list = 'N/A';
  1112. if ($authors) {
  1113. $authors_list = $authors->value;
  1114. }
  1115. // Get the first database cross-reference with a url.
  1116. $options = array('return_array' => 1);
  1117. $pub = chado_expand_var($pub, 'table', 'pub_dbxref', $options);
  1118. $dbxref = NULL;
  1119. if ($pub->pub_dbxref) {
  1120. foreach ($pub->pub_dbxref as $index => $pub_dbxref) {
  1121. if ($pub_dbxref->dbxref_id->db_id->urlprefix) {
  1122. $dbxref = $pub_dbxref->dbxref_id;
  1123. }
  1124. }
  1125. }
  1126. // Get the URL.
  1127. $values = array(
  1128. 'pub_id' => $pub->pub_id,
  1129. 'type_id' => array(
  1130. 'name' => 'URL',
  1131. ),
  1132. );
  1133. $options = array(
  1134. 'return_array' => 1,
  1135. 'include_fk' => array(),
  1136. );
  1137. $url = '';
  1138. $urls = chado_generate_var('pubprop', $values, $options);
  1139. if ($urls) {
  1140. $urls = chado_expand_var($urls, 'field', 'pubprop.value');
  1141. if (count($urls) > 0) {
  1142. $url = $urls[0]->value;
  1143. }
  1144. }
  1145. // Get the list of database cross references.
  1146. $values = array(
  1147. 'pub_id' => $pub->pub_id,
  1148. );
  1149. $options = array(
  1150. 'return_array' => 1,
  1151. );
  1152. $pub_dbxrefs = chado_generate_var('pub_dbxref', $values, $options);
  1153. $dbxrefs = array();
  1154. foreach ($pub_dbxrefs as $pub_dbxref) {
  1155. $dbxrefs[] = $pub_dbxref->dbxref_id->db_id->name . ':' . $pub_dbxref->dbxref_id->accession;
  1156. }
  1157. // Get the citation.
  1158. $values = array(
  1159. 'pub_id' => $pub->pub_id,
  1160. 'type_id' => array(
  1161. 'name' => 'Citation',
  1162. ),
  1163. );
  1164. $options = array(
  1165. 'include_fk' => array(
  1166. ),
  1167. );
  1168. $citation = chado_generate_var('pubprop', $values, $options);
  1169. if ($citation) {
  1170. $citation = chado_expand_var($citation, 'field', 'pubprop.value');
  1171. $citation = $citation->value;
  1172. }
  1173. else {
  1174. $pub_info = array(
  1175. 'Title' => $pub->title,
  1176. 'Publication Type' => $pub->type_id->name,
  1177. 'Authors' => $authors_list,
  1178. 'Series Name' => $pub->series_name,
  1179. 'Volume' => $pub->volume,
  1180. 'Issue' => $pub->issue,
  1181. 'Pages' => $pub->pages,
  1182. 'Publication Date' => $pub->pyear,
  1183. );
  1184. $citation = chado_pub_create_citation($pub_info);
  1185. }
  1186. return array(
  1187. 'TPUB:0000039' => $pub->title,
  1188. 'TPUB:0000003' => $citation,
  1189. 'TPUB:0000050' => $abstract_text,
  1190. 'TPUB:0000047' => $authors_list,
  1191. 'TPUB:0000052' => $url,
  1192. 'SBO:0000554' => $dbxrefs,
  1193. );
  1194. }