tripal_chado.pub.api.inc 42 KB

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