tripal_chado.pub.api.inc 43 KB

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