tripal_chado.pub.api.inc 43 KB

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