tripal_chado.pub.api.inc 43 KB

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