tripal_chado.pub.api.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. "insertions/updates 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. "insertions/updates 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 retrieve 100 at a time
  487. $npages = isset($num_pubs)?(intval($num_pubs/$num_to_retrieve)+1):'?'; // will be 0 to 99 in last page
  488. tripal_report_error($message_type, TRIPAL_INFO,
  489. "Page ".($page+1)." of $npages. Querying !remote_db for up to !num pubs that match the criteria.",
  490. [
  491. '!num' => $num_to_retrieve,
  492. '!remote_db' => $remote_db,
  493. ], $message_opts);
  494. $results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
  495. $pubs = $results['pubs'];
  496. $num_pubs = $results['total_records'];
  497. $total_pubs += $num_pubs;
  498. tripal_report_error($message_type, TRIPAL_INFO,
  499. "Found %num publications.",
  500. ['%num' => $num_pubs], $message_opts);
  501. $subset_report = tripal_pub_add_publications($pubs, $import->do_contact, $do_update, $job);
  502. $countpubs = count($pubs); // the following merge resets count($pubs) so save it
  503. foreach ($subset_report as $action => $pubs) {
  504. $report[$action] = array_merge($report[$action], $pubs);
  505. }
  506. $page++;
  507. } while ($countpubs == $num_to_retrieve);
  508. // Publish as requested by the caller.
  509. _chado_execute_pub_importer_publish($publish, $job, $message_type, $message_opts);
  510. if ($job) {
  511. $job->setProgress(100);
  512. }
  513. } catch (Exception $e) {
  514. $transaction->rollback();
  515. watchdog_exception('T_pub_import', $e);
  516. tripal_report_error($message_type, TRIPAL_ERROR,
  517. "Rolling back database changes... !message",
  518. ['!message' => $e->getMessage()], $message_opts);
  519. return FALSE;
  520. }
  521. tripal_report_error($message_type, TRIPAL_INFO,
  522. "Done.", [], $message_opts);
  523. return $report;
  524. }
  525. /**
  526. * A helper function to dermine if imported publications should be published.
  527. *
  528. * It supports backwards compatibility with Tripal v2 legacy mode.
  529. *
  530. * @param $publish
  531. * Set to TRUE if publications should be published after import. For Tripal
  532. * v3 this value can be set to the string 'sync' or 'both' in the event that
  533. * the site is in "legacy" mode. Setting this value to 'sync' will create
  534. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  535. * only entities are created.
  536. */
  537. function _chado_execute_pub_importer_publish($publish, $job, $message_type, $message_opts) {
  538. // If the user wants to publish then do so.
  539. if ($publish === TRUE or $publish === 'both') {
  540. // Get the bundle for the Publication content type.
  541. $bundle_term = tripal_load_term_entity([
  542. 'vocabulary' => 'TPUB',
  543. 'accession' => '0000002',
  544. ]);
  545. $bundle = tripal_load_bundle_entity(['term_id' => $bundle_term->id]);
  546. if ($bundle) {
  547. tripal_report_error($message_type, TRIPAL_INFO,
  548. "Publishing publications with Drupal...", [], $message_opts);
  549. chado_publish_records(['bundle_name' => $bundle->name], $job);
  550. }
  551. // Note: we won't publish contacts as Tripal v2 did because there is
  552. // no consisten way to do that. Each site my use a different term for
  553. // different contact content types (e.g. all as one 'Contact' type or
  554. // specific such as 'Person', 'Organization', etc.).
  555. }
  556. // For backwords compatibility with legacy module do a sync.
  557. if ($publish === 'sync' or $publish === 'both') {
  558. if (module_exists('tripal_pub')) {
  559. tripal_report_error($message_type, TRIPAL_INFO,
  560. "Syncing publications with Drupal...", [], $message_opts);
  561. chado_node_sync_records('pub');
  562. if ($import->do_contact) {
  563. tripal_report_error($message_type, TRIPAL_INFO,
  564. "Syncing contacts with Drupal...", [], $message_opts);
  565. chado_node_sync_records('contact');
  566. }
  567. }
  568. }
  569. }
  570. /**
  571. * Updates publication records.
  572. *
  573. * Updates publication records that currently exist in the Chado pub table
  574. * with the most recent data in the remote database.
  575. *
  576. * @param $do_contact
  577. * Set to TRUE if authors should automatically have a contact record added
  578. * to Chado. Contacts are added using the name provided by the remote
  579. * database.
  580. * @param $dbxref
  581. * The unique database ID for the record to update. This value must
  582. * be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
  583. * database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
  584. * for the record in the database.
  585. * @param $db
  586. * The name of the remote database to update. If this value is provided and
  587. * no dbxref then all of the publications currently in the Chado database
  588. * for this remote database will be updated.
  589. * @param $publish
  590. * Set to TRUE if publications should be published after import. For Tripal
  591. * v3 this value can be set to the string 'sync' or 'both' in the event that
  592. * the site is in "legacy" mode. Setting this value to 'sync' will create
  593. * nodes, setting to 'both' will create nodes and entities. If set to TRUE
  594. * only entities are created.
  595. *
  596. * @ingroup tripal_pub_api
  597. */
  598. function chado_reimport_publications($do_contact = FALSE, $dbxref = NULL,
  599. $db = NULL, $publish = TRUE) {
  600. // These are options for the tripal_report_error function. We do not
  601. // want to log messages to the watchdog but we do for the job and to
  602. // the terminal
  603. $message_type = 'pub_import';
  604. $message_opts = [
  605. 'watchdog' == FALSE,
  606. 'print' => TRUE,
  607. ];
  608. $message = "Importing of publications for this importer is performed using a database transaction. " .
  609. "If the load fails or is terminated prematurely then the entire set of " .
  610. "insertions/updates is rolled back and will not be found in the database";
  611. tripal_report_error($message_type, TRIPAL_INFO, $message, [], $message_opts);
  612. $transaction = db_transaction();
  613. try {
  614. // Get a list of all publications by their Dbxrefs that have supported
  615. // databases.
  616. $sql = "
  617. SELECT DB.name as db_name, DBX.accession
  618. FROM {pub} P
  619. INNER JOIN {pub_dbxref} PDBX ON P.pub_id = PDBX.pub_id
  620. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = PDBX.dbxref_id
  621. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  622. ";
  623. $args = [];
  624. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  625. $dbname = $matches[1];
  626. $accession = $matches[2];
  627. $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
  628. $args[':accession'] = $accession;
  629. $args[':dbname'] = $dbname;
  630. }
  631. elseif ($db) {
  632. $sql .= " WHERE DB.name = :dbname ";
  633. $args[':dbname'] = $db;
  634. }
  635. $sql .= "ORDER BY DB.name, P.pub_id";
  636. $results = chado_query($sql, $args);
  637. $num_to_retrieve = 100;
  638. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query.
  639. $curr_db = ''; // keeps track of the current current database.
  640. $ids = []; // the list of IDs for the database.
  641. $search = []; // the search array passed to the search function.
  642. // Iterate through the pub IDs.
  643. while ($pub = $results->fetchObject()) {
  644. $accession = $pub->accession;
  645. $remote_db = $pub->db_name;
  646. // Here we need to only update publications for databases we support.
  647. $supported_dbs = variable_get('tripal_pub_supported_dbs', []);
  648. if (!in_array($remote_db, $supported_dbs)) {
  649. continue;
  650. }
  651. $search = [
  652. 'num_criteria' => 1,
  653. 'remote_db' => $remote_db,
  654. 'criteria' => [
  655. '1' => [
  656. 'search_terms' => "$remote_db:$accession",
  657. 'scope' => 'id',
  658. 'operation' => '',
  659. 'is_phrase' => 0,
  660. ],
  661. ],
  662. ];
  663. $qresults = tripal_get_remote_pubs($remote_db, $search, 1, 0);
  664. $pubs = $qresults['pubs'];
  665. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  666. $i++;
  667. }
  668. // Publish as requested by the caller.
  669. _chado_execute_pub_importer_publish($publish, NULL, $message_type, $message_opts);
  670. } catch (Exception $e) {
  671. $transaction->rollback();
  672. watchdog_exception('T_pub_import', $e);
  673. tripal_report_error($message_type, TRIPAL_ERROR,
  674. "Rolling back database changes... !message",
  675. ['!message' => $e->getMessage()], $message_opts);
  676. return;
  677. }
  678. print "Done.\n";
  679. }
  680. /**
  681. * Launch the Tripal job to generate citations.
  682. *
  683. * This function will recreate citations for all publications currently
  684. * loaded into Tripal. This is useful to create a consistent format for
  685. * all citations.
  686. *
  687. * @param $options
  688. * Options pertaining to what publications to generate citations for.
  689. * One of the following must be present:
  690. * - all: Create and replace citation for all pubs.
  691. * - new: Create citation for pubs that don't already have one.
  692. *
  693. * @ingroup tripal_pub_api
  694. */
  695. function chado_pub_create_citations($options) {
  696. $skip_existing = TRUE;
  697. $sql = "
  698. SELECT cvterm_id
  699. FROM {cvterm}
  700. WHERE
  701. name = 'Citation' AND
  702. cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')
  703. ";
  704. $citation_type_id = chado_query($sql)->fetchField();
  705. // Create and replace citation for all pubs.
  706. if ($options == 'all') {
  707. $sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
  708. $skip_existing = FALSE;
  709. }
  710. // Create citation for pubs that don't already have one.
  711. else {
  712. if ($options == 'new') {
  713. $sql = "
  714. SELECT pub_id
  715. FROM {pub} P
  716. WHERE
  717. (SELECT value
  718. FROM {pubprop} PB
  719. WHERE type_id = :type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL
  720. AND pub_id <> 1
  721. ";
  722. $skip_existing = TRUE;
  723. }
  724. }
  725. $result = chado_query($sql, [':type_id' => $citation_type_id]);
  726. $counter_updated = 0;
  727. $counter_generated = 0;
  728. while ($pub = $result->fetchObject()) {
  729. $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
  730. if ($pub_arr) {
  731. $citation = chado_pub_create_citation($pub_arr);
  732. print $citation . "\n\n";
  733. // Replace if citation exists. This condition is never TRUE if
  734. // $skip_existing is TRUE.
  735. if ($pub_arr['Citation']) {
  736. $sql = "
  737. UPDATE {pubprop} SET value = :value
  738. WHERE pub_id = :pub_id AND type_id = :type_id AND rank = :rank
  739. ";
  740. chado_query($sql, [
  741. ':value' => $citation,
  742. ':pub_id' => $pub->pub_id,
  743. ':type_id' => $citation_type_id,
  744. ':rank' => 0,
  745. ]);
  746. $counter_updated++;
  747. // Generate a new citation.
  748. }
  749. else {
  750. $sql = "
  751. INSERT INTO {pubprop} (pub_id, type_id, value, rank)
  752. VALUES (:pub_id, :type_id, :value, :rank)
  753. ";
  754. chado_query($sql, [
  755. ':pub_id' => $pub->pub_id,
  756. ':type_id' => $citation_type_id,
  757. ':value' => $citation,
  758. ':rank' => 0,
  759. ]);
  760. $counter_generated++;
  761. }
  762. }
  763. }
  764. print "$counter_generated citations generated. $counter_updated citations updated.\n";
  765. }
  766. /**
  767. * This function generates citations for publications. It requires
  768. * an array structure with keys being the terms in the Tripal
  769. * publication ontology. This function is intended to be used
  770. * for any function that needs to generate a citation.
  771. *
  772. * @param $pub
  773. * An array structure containing publication details where the keys
  774. * are the publication ontology term names and values are the
  775. * corresponding details. The pub array can contain the following
  776. * keys with corresponding values:
  777. * - Publication Type: an array of publication types. a publication can
  778. * have more than one type.
  779. * - Authors: a string containing all of the authors of a publication.
  780. * - Journal Name: a string containing the journal name.
  781. * - Journal Abbreviation: a string containing the journal name
  782. * abbreviation.
  783. * - Series Name: a string containing the series (e.g. conference
  784. * proceedings) name.
  785. * - Series Abbreviation: a string containing the series name abbreviation
  786. * - Volume: the serives volume number.
  787. * - Issue: the series issue number.
  788. * - Pages: the page numbers for the publication.
  789. * - Publication Date: A date in the format "Year Month Day".
  790. *
  791. * @return
  792. * A text string containing the citation.
  793. *
  794. * @ingroup tripal_pub_api
  795. */
  796. function chado_pub_create_citation($pub) {
  797. $citation = '';
  798. $pub_type = '';
  799. // An article may have more than one publication type. For example,
  800. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  801. // Therefore, we need to select the type that makes most sense for
  802. // construction of the citation. Here we'll iterate through them all
  803. // and select the one that matches best.
  804. if (is_array($pub['Publication Type'])) {
  805. foreach ($pub['Publication Type'] as $ptype) {
  806. if ($ptype == 'Journal Article') {
  807. $pub_type = $ptype;
  808. break;
  809. }
  810. else {
  811. if ($ptype == 'Conference Proceedings') {
  812. $pub_type = $ptype;
  813. break;
  814. }
  815. else {
  816. if ($ptype == 'Review') {
  817. $pub_type = $ptype;
  818. break;
  819. }
  820. else {
  821. if ($ptype == 'Book') {
  822. $pub_type = $ptype;
  823. break;
  824. }
  825. else {
  826. if ($ptype == 'Letter') {
  827. $pub_type = $ptype;
  828. break;
  829. }
  830. else {
  831. if ($ptype == 'Book Chapter') {
  832. $pub_type = $ptype;
  833. break;
  834. }
  835. else {
  836. if ($ptype == "Research Support, Non-U.S. Gov't") {
  837. $pub_type = $ptype;
  838. // We don't break because if the article is also a Journal Article
  839. // we prefer that type.
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. }
  847. }
  848. // If we don't have a recognized publication type, then just use the
  849. // first one in the list.
  850. if (!$pub_type) {
  851. $pub_type = $pub['Publication Type'][0];
  852. }
  853. }
  854. else {
  855. $pub_type = $pub['Publication Type'];
  856. }
  857. //----------------------
  858. // Journal Article
  859. //----------------------
  860. if ($pub_type == 'Journal Article') {
  861. if (array_key_exists('Authors', $pub)) {
  862. $citation = $pub['Authors'] . '. ';
  863. }
  864. $citation .= $pub['Title'] . '. ';
  865. if (array_key_exists('Journal Name', $pub)) {
  866. $citation .= $pub['Journal Name'] . '. ';
  867. }
  868. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  869. $citation .= $pub['Journal Abbreviation'] . '. ';
  870. }
  871. elseif (array_key_exists('Series Name', $pub)) {
  872. $citation .= $pub['Series Name'] . '. ';
  873. }
  874. elseif (array_key_exists('Series Abbreviation', $pub)) {
  875. $citation .= $pub['Series Abbreviation'] . '. ';
  876. }
  877. if (array_key_exists('Publication Date', $pub)) {
  878. $citation .= $pub['Publication Date'];
  879. }
  880. elseif (array_key_exists('Year', $pub)) {
  881. $citation .= $pub['Year'];
  882. }
  883. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages', $pub)) {
  884. $citation .= '; ';
  885. }
  886. if (array_key_exists('Volume', $pub)) {
  887. $citation .= $pub['Volume'];
  888. }
  889. if (array_key_exists('Issue', $pub)) {
  890. $citation .= '(' . $pub['Issue'] . ')';
  891. }
  892. if (array_key_exists('Pages', $pub)) {
  893. if (array_key_exists('Volume', $pub)) {
  894. $citation .= ':';
  895. }
  896. $citation .= $pub['Pages'];
  897. }
  898. $citation .= '.';
  899. }
  900. //----------------------
  901. // Review
  902. //----------------------
  903. else {
  904. if ($pub_type == 'Review') {
  905. if (array_key_exists('Authors', $pub)) {
  906. $citation = $pub['Authors'] . '. ';
  907. }
  908. $citation .= $pub['Title'] . '. ';
  909. if (array_key_exists('Journal Name', $pub)) {
  910. $citation .= $pub['Journal Name'] . '. ';
  911. }
  912. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  913. $citation .= $pub['Journal Abbreviation'] . '. ';
  914. }
  915. elseif (array_key_exists('Series Name', $pub)) {
  916. $citation .= $pub['Series Name'] . '. ';
  917. }
  918. elseif (array_key_exists('Series Abbreviation', $pub)) {
  919. $citation .= $pub['Series Abbreviation'] . '. ';
  920. }
  921. if (array_key_exists('Publication Date', $pub)) {
  922. $citation .= $pub['Publication Date'];
  923. }
  924. elseif (array_key_exists('Year', $pub)) {
  925. $citation .= $pub['Year'];
  926. }
  927. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages', $pub)) {
  928. $citation .= '; ';
  929. }
  930. if (array_key_exists('Volume', $pub)) {
  931. $citation .= $pub['Volume'];
  932. }
  933. if (array_key_exists('Issue', $pub)) {
  934. $citation .= '(' . $pub['Issue'] . ')';
  935. }
  936. if (array_key_exists('Pages', $pub)) {
  937. if (array_key_exists('Volume', $pub)) {
  938. $citation .= ':';
  939. }
  940. $citation .= $pub['Pages'];
  941. }
  942. $citation .= '.';
  943. }
  944. //----------------------
  945. // Research Support, Non-U.S. Gov't
  946. //----------------------
  947. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  948. if (array_key_exists('Authors', $pub)) {
  949. $citation = $pub['Authors'] . '. ';
  950. }
  951. $citation .= $pub['Title'] . '. ';
  952. if (array_key_exists('Journal Name', $pub)) {
  953. $citation .= $pub['Journal Name'] . '. ';
  954. }
  955. if (array_key_exists('Publication Date', $pub)) {
  956. $citation .= $pub['Publication Date'];
  957. }
  958. elseif (array_key_exists('Year', $pub)) {
  959. $citation .= $pub['Year'];
  960. }
  961. $citation .= '.';
  962. }
  963. //----------------------
  964. // Letter
  965. //----------------------
  966. elseif ($pub_type == 'Letter') {
  967. if (array_key_exists('Authors', $pub)) {
  968. $citation = $pub['Authors'] . '. ';
  969. }
  970. $citation .= $pub['Title'] . '. ';
  971. if (array_key_exists('Journal Name', $pub)) {
  972. $citation .= $pub['Journal Name'] . '. ';
  973. }
  974. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  975. $citation .= $pub['Journal Abbreviation'] . '. ';
  976. }
  977. elseif (array_key_exists('Series Name', $pub)) {
  978. $citation .= $pub['Series Name'] . '. ';
  979. }
  980. elseif (array_key_exists('Series Abbreviation', $pub)) {
  981. $citation .= $pub['Series Abbreviation'] . '. ';
  982. }
  983. if (array_key_exists('Publication Date', $pub)) {
  984. $citation .= $pub['Publication Date'];
  985. }
  986. elseif (array_key_exists('Year', $pub)) {
  987. $citation .= $pub['Year'];
  988. }
  989. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages', $pub)) {
  990. $citation .= '; ';
  991. }
  992. if (array_key_exists('Volume', $pub)) {
  993. $citation .= $pub['Volume'];
  994. }
  995. if (array_key_exists('Issue', $pub)) {
  996. $citation .= '(' . $pub['Issue'] . ')';
  997. }
  998. if (array_key_exists('Pages', $pub)) {
  999. if (array_key_exists('Volume', $pub)) {
  1000. $citation .= ':';
  1001. }
  1002. $citation .= $pub['Pages'];
  1003. }
  1004. $citation .= '.';
  1005. }
  1006. //-----------------------
  1007. // Conference Proceedings
  1008. //-----------------------
  1009. elseif ($pub_type == 'Conference Proceedings') {
  1010. if (array_key_exists('Authors', $pub)) {
  1011. $citation = $pub['Authors'] . '. ';
  1012. }
  1013. $citation .= $pub['Title'] . '. ';
  1014. if (array_key_exists('Conference Name', $pub)) {
  1015. $citation .= $pub['Conference Name'] . '. ';
  1016. }
  1017. elseif (array_key_exists('Series Name', $pub)) {
  1018. $citation .= $pub['Series Name'] . '. ';
  1019. }
  1020. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1021. $citation .= $pub['Series Abbreviation'] . '. ';
  1022. }
  1023. if (array_key_exists('Publication Date', $pub)) {
  1024. $citation .= $pub['Publication Date'];
  1025. }
  1026. elseif (array_key_exists('Year', $pub)) {
  1027. $citation .= $pub['Year'];
  1028. }
  1029. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages', $pub)) {
  1030. $citation .= '; ';
  1031. }
  1032. if (array_key_exists('Volume', $pub)) {
  1033. $citation .= $pub['Volume'];
  1034. }
  1035. if (array_key_exists('Issue', $pub)) {
  1036. $citation .= '(' . $pub['Issue'] . ')';
  1037. }
  1038. if (array_key_exists('Pages', $pub)) {
  1039. if (array_key_exists('Volume', $pub)) {
  1040. $citation .= ':';
  1041. }
  1042. $citation .= $pub['Pages'];
  1043. }
  1044. $citation .= '.';
  1045. }
  1046. //-----------------------
  1047. // Default
  1048. //-----------------------
  1049. else {
  1050. if (array_key_exists('Authors', $pub)) {
  1051. $citation = $pub['Authors'] . '. ';
  1052. }
  1053. $citation .= $pub['Title'] . '. ';
  1054. if (array_key_exists('Series Name', $pub)) {
  1055. $citation .= $pub['Series Name'] . '. ';
  1056. }
  1057. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1058. $citation .= $pub['Series Abbreviation'] . '. ';
  1059. }
  1060. if (array_key_exists('Publication Date', $pub)) {
  1061. $citation .= $pub['Publication Date'];
  1062. }
  1063. elseif (array_key_exists('Year', $pub)) {
  1064. $citation .= $pub['Year'];
  1065. }
  1066. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages', $pub)) {
  1067. $citation .= '; ';
  1068. }
  1069. if (array_key_exists('Volume', $pub)) {
  1070. $citation .= $pub['Volume'];
  1071. }
  1072. if (array_key_exists('Issue', $pub)) {
  1073. $citation .= '(' . $pub['Issue'] . ')';
  1074. }
  1075. if (array_key_exists('Pages', $pub)) {
  1076. if (array_key_exists('Volume', $pub)) {
  1077. $citation .= ':';
  1078. }
  1079. $citation .= $pub['Pages'];
  1080. }
  1081. $citation .= '.';
  1082. }
  1083. }
  1084. return $citation;
  1085. }
  1086. /**
  1087. * Retrieves the minimal information to uniquely describe any publication.
  1088. *
  1089. * The returned array is an associative array where the keys are
  1090. * the controlled vocabulary terms in the form [vocab]:[accession].
  1091. *
  1092. * @param $pub
  1093. * A publication object as created by chado_generate_var().
  1094. *
  1095. * @return
  1096. * An array with the following keys: 'Citation', 'Abstract', 'Authors',
  1097. * 'URL'. All keys are term names in the Tripal Publication Ontology :TPUB.
  1098. *
  1099. * @ingroup tripal_pub_api
  1100. */
  1101. function chado_get_minimal_pub_info($pub) {
  1102. if (!$pub) {
  1103. return [];
  1104. }
  1105. // Chado has a null pub as default. We don't return anything for this.
  1106. if (isset($pub->uniquename) && $pub->uniquename == 'null') {
  1107. return [];
  1108. }
  1109. // Expand the title.
  1110. $pub = chado_expand_var($pub, 'field', 'pub.title');
  1111. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  1112. // Get the abstract.
  1113. $values = [
  1114. 'pub_id' => $pub->pub_id,
  1115. 'type_id' => [
  1116. 'name' => 'Abstract',
  1117. ],
  1118. ];
  1119. $options = [
  1120. 'include_fk' => [
  1121. ],
  1122. ];
  1123. $abstract = chado_generate_var('pubprop', $values, $options);
  1124. $abstract_text = '';
  1125. if($abstract) {
  1126. $abstract = chado_expand_var($abstract, 'field', 'pubprop.value');
  1127. if ($abstract) {
  1128. $abstract_text = htmlspecialchars($abstract->value);
  1129. }
  1130. }
  1131. // Get the author list.
  1132. $values = [
  1133. 'pub_id' => $pub->pub_id,
  1134. 'type_id' => [
  1135. 'name' => 'Authors',
  1136. ],
  1137. ];
  1138. $options = [
  1139. 'include_fk' => [
  1140. ],
  1141. ];
  1142. $authors = chado_generate_var('pubprop', $values, $options);
  1143. $authors_list = 'N/A';
  1144. if($authors) {
  1145. $authors = chado_expand_var($authors, 'field', 'pubprop.value');
  1146. if ($authors) {
  1147. $authors_list = $authors->value;
  1148. }
  1149. }
  1150. // Get the first database cross-reference with a url.
  1151. $options = ['return_array' => 1];
  1152. $pub = chado_expand_var($pub, 'table', 'pub_dbxref', $options);
  1153. $dbxref = NULL;
  1154. if ($pub->pub_dbxref) {
  1155. foreach ($pub->pub_dbxref as $index => $pub_dbxref) {
  1156. if ($pub_dbxref->dbxref_id->db_id->urlprefix) {
  1157. $dbxref = $pub_dbxref->dbxref_id;
  1158. }
  1159. }
  1160. }
  1161. // Get the URL.
  1162. $values = [
  1163. 'pub_id' => $pub->pub_id,
  1164. 'type_id' => [
  1165. 'name' => 'URL',
  1166. ],
  1167. ];
  1168. $options = [
  1169. 'return_array' => 1,
  1170. 'include_fk' => [],
  1171. ];
  1172. $url = '';
  1173. $urls = chado_generate_var('pubprop', $values, $options);
  1174. if ($urls) {
  1175. $urls = chado_expand_var($urls, 'field', 'pubprop.value');
  1176. if (count($urls) > 0) {
  1177. $url = $urls[0]->value;
  1178. }
  1179. }
  1180. // Get the list of database cross references.
  1181. $values = [
  1182. 'pub_id' => $pub->pub_id,
  1183. ];
  1184. $options = [
  1185. 'return_array' => 1,
  1186. ];
  1187. $pub_dbxrefs = chado_generate_var('pub_dbxref', $values, $options);
  1188. $dbxrefs = [];
  1189. foreach ($pub_dbxrefs as $pub_dbxref) {
  1190. $dbxrefs[] = $pub_dbxref->dbxref_id->db_id->name . ':' . $pub_dbxref->dbxref_id->accession;
  1191. }
  1192. // Get the citation.
  1193. $values = [
  1194. 'pub_id' => $pub->pub_id,
  1195. 'type_id' => [
  1196. 'name' => 'Citation',
  1197. ],
  1198. ];
  1199. $options = [
  1200. 'include_fk' => [
  1201. ],
  1202. ];
  1203. $citation = chado_generate_var('pubprop', $values, $options);
  1204. if ($citation) {
  1205. $citation = chado_expand_var($citation, 'field', 'pubprop.value');
  1206. $citation = $citation->value;
  1207. }
  1208. else {
  1209. $pub_info = [
  1210. 'Title' => $pub->title,
  1211. 'Publication Type' => $pub->type_id->name,
  1212. 'Authors' => $authors_list,
  1213. 'Series Name' => $pub->series_name,
  1214. 'Volume' => $pub->volume,
  1215. 'Issue' => $pub->issue,
  1216. 'Pages' => $pub->pages,
  1217. 'Publication Date' => $pub->pyear,
  1218. ];
  1219. $citation = chado_pub_create_citation($pub_info);
  1220. }
  1221. return [
  1222. 'TPUB:0000039' => $pub->title,
  1223. 'TPUB:0000003' => $citation,
  1224. 'TPUB:0000050' => $abstract_text,
  1225. 'TPUB:0000047' => $authors_list,
  1226. 'TPUB:0000052' => $url,
  1227. 'SBO:0000554' => $dbxrefs,
  1228. ];
  1229. }