tripal_chado.pub.api.inc 43 KB

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