tripal_pub.api.inc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. /**
  3. * @defgroup tripal_pub_api Publication Module API
  4. * @ingroup tripal_api
  5. * @{
  6. * Provides an application programming interface (API) to manage chado publications
  7. * @}
  8. */
  9. /*
  10. * Retrieves a list of publications as an associated array where
  11. * keys correspond directly with Tripal Pub CV terms.
  12. *
  13. * @param remote_db
  14. * The name of the remote publication database to query. These names should
  15. * match the name of the databases in the Chado 'db' table. Currently
  16. * supported databass include
  17. * 'PMID': PubMed
  18. *
  19. * @param search_array
  20. * An associate array containing the search criteria. The following key
  21. * are expected
  22. * 'remote_db': Specifies the name of the remote publication database
  23. * 'num_criteria': Specifies the number of criteria present in the search array
  24. * 'days': The number of days to include in the search starting from today
  25. * 'criteria': An associate array containing the search critiera. There should
  26. * be no less than 'num_criteria' elements in this array.
  27. *
  28. * The following keys are expected in the 'criteria' array
  29. * 'search_terms': A list of terms to search on, separated by spaces.
  30. * 'scope': The fields to search in the remote database. Valid values
  31. * include: 'title', 'abstract', 'author' and 'any'
  32. * 'operation': The logical operation to use for this criteria. Valid
  33. * values include: 'AND', 'OR' and 'NOT'.
  34. * @param $num_to_retrieve
  35. * The number of records to retrieve. In cases with large numbers of
  36. * records to retrieve, the remote database may limit the size of each
  37. * retrieval.
  38. * @param $page
  39. * Optional. If this function is called where the
  40. * page for the pager cannot be set using the $_GET variable, use this
  41. * argument to specify the page to retrieve.
  42. *
  43. * @return
  44. * Returns an array of pubs where each element is
  45. * an associative array where the keys are Tripal Pub CV terms.
  46. *
  47. * @ingroup tripal_pub_api
  48. */
  49. function tripal_pub_get_remote_search_results($remote_db, $search_array, $num_to_retrieve, $page = 0) {
  50. // now call the callback function to get the results
  51. $callback = "tripal_pub_remote_search_$remote_db";
  52. $pubs = array(
  53. 'total_records' => 0,
  54. 'search_str' => '',
  55. 'pubs' => array(),
  56. );
  57. if (function_exists($callback)) {
  58. $pubs = call_user_func($callback, $search_array, $num_to_retrieve, $page);
  59. }
  60. return $pubs;
  61. }
  62. /*
  63. * @ingroup tripal_pub_api
  64. */
  65. function tripal_pub_get_raw_data($dbxref) {
  66. if(preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  67. $remote_db = $matches[1];
  68. $accession = $matches[2];
  69. // check that the database is supported
  70. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  71. if(!in_array($remote_db, $supported_dbs)) {
  72. return "Unsupported database: $dbxref";
  73. }
  74. $search = array(
  75. 'num_criteria' => 1,
  76. 'remote_db' => $remote_db,
  77. 'criteria' => array(
  78. '1' => array(
  79. 'search_terms' => "$remote_db:$accession",
  80. 'scope' => 'id',
  81. 'operation' => '',
  82. 'is_phrase' => 0,
  83. ),
  84. ),
  85. );
  86. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  87. return '<textarea cols=80 rows=20>' . $pubs[0]['raw'] . '</textarea>';
  88. }
  89. return 'Invalid DB xref';
  90. }
  91. /*
  92. * @ingroup tripal_pub_api
  93. */
  94. function tripal_pub_update_publications($do_contact = FALSE, $dbxref = NULL, $db = NULL) {
  95. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  96. "If the load fails or is terminated prematurely then the entire set of \n" .
  97. "insertions/updates is rolled back and will not be found in the database\n\n";
  98. $transaction = db_transaction();
  99. try {
  100. // get a list of all publications by their Dbxrefs that have supported databases
  101. $sql = "
  102. SELECT DB.name as db_name, DBX.accession
  103. FROM pub P
  104. INNER JOIN pub_dbxref PDBX ON P.pub_id = PDBX.pub_id
  105. INNER JOIN dbxref DBX ON DBX.dbxref_id = PDBX.dbxref_id
  106. INNER JOIN db DB ON DB.db_id = DBX.db_id
  107. ";
  108. $args = array();
  109. if ($dbxref and preg_match('/^(.*?):(.*?)$/', $dbxref, $matches)) {
  110. $dbname = $matches[1];
  111. $accession = $matches[2];
  112. $sql .= "WHERE DBX.accession = :accession and DB.name = :dbname ";
  113. $args[':accession'] = $accession;
  114. $args[':dbname'] = $dbname;
  115. }
  116. elseif ($db) {
  117. $sql .= " WHERE DB.name = :dbname ";
  118. $args[':dbname'] = $db;
  119. }
  120. $sql .= "ORDER BY DB.name, P.pub_id";
  121. $results = chado_query($sql, $args);
  122. $num_to_retrieve = 100;
  123. $i = 0; // count the number of IDs. When we hit $num_to_retrieve we'll do the query
  124. $curr_db = ''; // keeps track of the current current database
  125. $ids = array(); // the list of IDs for the database
  126. $search = array(); // the search array passed to the search function
  127. // iterate through the pub IDs
  128. while ($pub = $results->fetchObject()) {
  129. $accession = $pub->accession;
  130. $remote_db = $pub->db_name;
  131. // here we need to only update publications for databases we support
  132. $supported_dbs = variable_get('tripal_pub_supported_dbs', array());
  133. if(!in_array($remote_db, $supported_dbs)) {
  134. continue;
  135. }
  136. $search = array(
  137. 'num_criteria' => 1,
  138. 'remote_db' => $remote_db,
  139. 'criteria' => array(
  140. '1' => array(
  141. 'search_terms' => "$remote_db:$accession",
  142. 'scope' => 'id',
  143. 'operation' => '',
  144. 'is_phrase' => 0,
  145. ),
  146. ),
  147. );
  148. $pubs = tripal_pub_get_remote_search_results($remote_db, $search, 1, 0);
  149. tripal_pub_add_publications($pubs, $do_contact, TRUE);
  150. $i++;
  151. }
  152. // sync the newly added publications with Drupal
  153. print "Syncing publications with Drupal...\n";
  154. tripal_pub_sync_pubs();
  155. // if the caller wants to create contacts then we should sync them
  156. if ($do_contact) {
  157. print "Syncing contacts with Drupal...\n";
  158. tripal_contact_sync_contacts();
  159. }
  160. }
  161. catch (Exception $e) {
  162. print "\n"; // make sure we start errors on new line
  163. watchdog_exception('T_pub_import', $e);
  164. $transaction->rollback();
  165. print "FAILED: Rolling back database changes...\n";
  166. return;
  167. }
  168. print "Done.\n";
  169. }
  170. /**
  171. *
  172. * @param $pub_importer_id
  173. */
  174. function tripal_pub_import_publications_by_import_id($import_id, $job_id = NULL) {
  175. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  176. "If the load fails or is terminated prematurely then the entire set of \n" .
  177. "insertions/updates is rolled back and will not be found in the database\n\n";
  178. // start the transaction
  179. $transaction = db_transaction();
  180. try {
  181. $page = 0;
  182. $do_contact = FALSE;
  183. $num_to_retrieve = 100;
  184. // get all of the loaders
  185. $args = array(':import_id' => $import_id);
  186. $sql = "SELECT * FROM {tripal_pub_import} WHERE pub_import_id = :import_id ";
  187. $import = db_query($sql, $args)->fetchObject();
  188. print "Importing: " . $import->name . "\n";
  189. $criteria = unserialize($import->criteria);
  190. $remote_db = $criteria['remote_db'];
  191. $total_pubs = 0;
  192. do {
  193. // retrieve the pubs for this page. We'll retreive 100 at a time
  194. $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
  195. $pubs = $results['pubs'];
  196. $num_pubs = $rseults['total_records'];
  197. $total_pubs += $num_pubs;
  198. tripal_pub_add_publications($pubs, $import->do_contact);
  199. $page++;
  200. }
  201. // continue looping until we have a $pubs array that does not have
  202. // our requested numer of records. This means we've hit the end
  203. while (count($pubs) == $num_to_retrieve);
  204. // sync the newly added publications with Drupal. If the user
  205. // requested a report then we don't want to print any syncing information
  206. // so pass 'FALSE' to the sync call
  207. print "Syncing publications with Drupal...\n";
  208. tripal_pub_sync_pubs();
  209. // if any of the importers wanted to create contacts from the authors then sync them
  210. if($import->do_contact) {
  211. print "Syncing contacts with Drupal...\n";
  212. tripal_contact_sync_contacts();
  213. }
  214. tripal_set_job_progress($job_id, '100');
  215. }
  216. catch (Exception $e) {
  217. print "\n"; // make sure we start errors on new line
  218. watchdog_exception('T_pub_import', $e);
  219. $transaction->rollback();
  220. print "FAILED: Rolling back database changes...\n";
  221. return;
  222. }
  223. print "Done.\n";
  224. }
  225. /*
  226. * @ingroup tripal_pub_api
  227. */
  228. function tripal_pub_import_publications($report_email = FALSE, $do_update = FALSE) {
  229. $num_to_retrieve = 100;
  230. $page = 0;
  231. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  232. "If the load fails or is terminated prematurely then the entire set of \n" .
  233. "insertions/updates is rolled back and will not be found in the database\n\n";
  234. // start the transaction
  235. $transaction = db_transaction();
  236. try {
  237. // get all of the loaders
  238. $args = array();
  239. $sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
  240. $results = db_query($sql, $args);
  241. $do_contact = FALSE;
  242. $reports = array();
  243. foreach ($results as $import) {
  244. $page = 0;
  245. print "Importing: " . $import->name . "\n";
  246. // keep track if any of the importers want to create contacts from authors
  247. if ($import->do_contact == 1) {
  248. $do_contact = TRUE;
  249. }
  250. $criteria = unserialize($import->criteria);
  251. $remote_db = $criteria['remote_db'];
  252. do {
  253. // retrieve the pubs for this page. We'll retreive 100 at a time
  254. $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
  255. $pubs = $results['pubs'];
  256. $reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
  257. $page++;
  258. }
  259. // continue looping until we have a $pubs array that does not have
  260. // our requested numer of records. This means we've hit the end
  261. while (count($pubs) == $num_to_retrieve);
  262. }
  263. // sync the newly added publications with Drupal. If the user
  264. // requested a report then we don't want to print any syncing information
  265. // so pass 'FALSE' to the sync call
  266. print "Syncing publications with Drupal...\n";
  267. tripal_pub_sync_pubs();
  268. // iterate through each of the reports and generate a final report with HTML links
  269. $HTML_report = '';
  270. if ($report_email) {
  271. $HTML_report .= "<html>";
  272. global $base_url;
  273. foreach ($reports as $importer => $report) {
  274. $total = count($report['inserted']);
  275. $HTML_report .= "<b>$total new publications from importer: $importer</b><br><ol>\n";
  276. foreach ($report['inserted'] as $pub) {
  277. $item = $pub['Title'];
  278. if (array_key_exists('pub_id', $pub)) {
  279. $item = l($pub['Title'], "$base_url/pub/" . $pub['pub_id']);
  280. }
  281. $HTML_report .= "<li>$item</li>\n";
  282. }
  283. $HTML_report .= "</ol>\n";
  284. }
  285. $HTML_report .= "</html>";
  286. $site_email = variable_get('site_mail', '');
  287. $params = array(
  288. 'message' => $HTML_report
  289. );
  290. drupal_mail('tripal_pub', 'import_report', $report_email, language_default(), $params, $site_email, TRUE);
  291. }
  292. // if any of the importers wanted to create contacts from the authors then sync them
  293. if($do_contact) {
  294. print "Syncing contacts with Drupal...\n";
  295. tripal_contact_sync_contacts();
  296. }
  297. }
  298. catch (Exception $e) {
  299. print "\n"; // make sure we start errors on new line
  300. watchdog_exception('T_pub_import', $e);
  301. $transaction->rollback();
  302. print "FAILED: Rolling back database changes...\n";
  303. return;
  304. }
  305. print "Done.\n";
  306. }
  307. /*
  308. * @ingroup tripal_pub_api
  309. */
  310. function tripal_pub_import_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update) {
  311. $num_to_retrieve = 1;
  312. $pager_id = 0;
  313. $page = 0;
  314. $num_pubs = 0;
  315. print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
  316. "If the load fails or is terminated prematurely then the entire set of \n" .
  317. "insertions/updates is rolled back and will not be found in the database\n\n";
  318. $transaction = db_transaction();
  319. try {
  320. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  321. $dbname = $matches[1];
  322. $accession = $matches[2];
  323. $criteria = array(
  324. 'num_criteria' => 1,
  325. 'remote_db' => $dbname,
  326. 'criteria' => array(
  327. '1' => array(
  328. 'search_terms' => "$dbname:$accession",
  329. 'scope' => 'id',
  330. 'operation' => '',
  331. 'is_phrase' => 0,
  332. ),
  333. ),
  334. );
  335. $remote_db = $criteria['remote_db'];
  336. $results = tripal_pub_get_remote_search_results($remote_db, $criteria, $num_to_retrieve, $page);
  337. $pubs = $results['pubs'];
  338. $search_str = $results['search_str'];
  339. $total_records = $results['total_records'];
  340. $pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
  341. }
  342. // sync the newly added publications with Drupal
  343. print "Syncing publications with Drupal...\n";
  344. tripal_pub_sync_pubs();
  345. // if any of the importers wanted to create contacts from the authors then sync them
  346. if($do_contact) {
  347. print "Syncing contacts with Drupal...\n";
  348. tripal_contact_sync_contacts();
  349. }
  350. }
  351. catch (Exception $e) {
  352. print "\n"; // make sure we start errors on new line
  353. watchdog_exception('T_pub_import', $e);
  354. $transaction->rollback();
  355. print "FAILED: Rolling back database changes...\n";
  356. return;
  357. }
  358. print "Done.\n";
  359. }
  360. /**
  361. *
  362. * @param $pubs
  363. * @param $do_contact
  364. * @param $update
  365. */
  366. function tripal_pub_add_publications($pubs, $do_contact, $update = FALSE) {
  367. $report = array();
  368. $report['error'] = 0;
  369. $report['inserted'] = array();
  370. $report['skipped'] = array();
  371. $total_pubs = count($pubs);
  372. // iterate through the publications and add each one
  373. $i = 1;
  374. foreach ($pubs as $pub) {
  375. $memory = number_format(memory_get_usage()) . " bytes";
  376. print "Processing $i of $total_pubs. Memory usage: $memory.\r";
  377. // add the publication to Chado
  378. $action = '';
  379. $pub_id = tripal_pub_add_publication($pub, $action, $do_contact, $update);
  380. if ($pub_id){
  381. // add the publication cross reference (e.g. to PubMed)
  382. if ($pub_id and $pub['Publication Dbxref']) {
  383. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
  384. }
  385. $pub['pub_id'] = $pub_id;
  386. }
  387. switch ($action) {
  388. case 'error':
  389. $report['error']++;
  390. break;
  391. case 'inserted':
  392. $report['inserted'][] = $pub;
  393. break;
  394. case 'updated':
  395. $report['updated'][] = $pub;
  396. break;
  397. case 'skipped':
  398. $report['skipped'][] = $pub;
  399. break;
  400. }
  401. $i++;
  402. }
  403. print "\n";
  404. return $report;
  405. }
  406. /**
  407. * Adds a dbxref record to a publication
  408. */
  409. function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
  410. // break apart the dbxref
  411. $dbname = '';
  412. $accession = '';
  413. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  414. $dbname = $matches[1];
  415. $accession = $matches[2];
  416. }
  417. else {
  418. return FALSE;
  419. }
  420. // check to see if the pub_dbxref record already exist
  421. $values = array(
  422. 'dbxref_id' => array(
  423. 'accession' => $accession,
  424. 'db_id' => array(
  425. 'name' => $dbname,
  426. ),
  427. ),
  428. 'pub_id' => $pub_id,
  429. );
  430. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  431. $results = chado_select_record('pub_dbxref', array('*'), $values, $options);
  432. // if the pub_dbxref record exist then we don't need to re-add it.
  433. if(count($results) > 0) {
  434. return $results[0];
  435. }
  436. // make sure our database already exists
  437. $db = tripal_db_add_db($dbname);
  438. // get the database cross-reference
  439. $dbxvalues = array(
  440. 'accession' => $accession,
  441. 'db_id' => $db->db_id,
  442. );
  443. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  444. $results = chado_select_record('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  445. // if the accession doesn't exist then add it
  446. if(count($results) == 0){
  447. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  448. }
  449. else {
  450. $dbxref = $results[0];
  451. }
  452. // now add the record
  453. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  454. $results = chado_insert_record('pub_dbxref', $values, $options);
  455. if (!$results) {
  456. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot add publication dbxref: %db:%accession.",
  457. array('%db' => $dbname, '%accession' => $accession));
  458. return FALSE;
  459. }
  460. return $results;
  461. }
  462. /**
  463. * Returns the list of publications that are assigned the database
  464. * cross-reference provided
  465. *
  466. * @param $pub_dbxref
  467. * The database cross reference accession. It should be in the form
  468. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  469. * unique publication identifier (e.g. PMID:4382934)
  470. *
  471. * @return
  472. * Returns an array of all the publications that have the provided
  473. * cross reference. If no publications match, then an empty array
  474. * is returned.
  475. *
  476. * @ingroup tripal_pub_api
  477. *
  478. */
  479. function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
  480. $return = array();
  481. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  482. $dbname = $matches[1];
  483. $accession = $matches[2];
  484. $values = array(
  485. 'dbxref_id' => array (
  486. 'accession' => $accession,
  487. 'db_id' => array(
  488. 'name' => $dbname
  489. ),
  490. ),
  491. );
  492. $options = array('statement_name' => 'sel_pubdbxref_db');
  493. $results = chado_select_record('pub_dbxref', array('pub_id'), $values, $options);
  494. foreach ($results as $index => $pub) {
  495. $return[] = $pub->pub_id;
  496. }
  497. }
  498. return $return;
  499. }
  500. /**
  501. * Returns the list of publications that match a given title, type and year
  502. *
  503. * @param title
  504. * The title of the publication to look for
  505. * @param type
  506. * Optional. The publication type. The value of this field should come from
  507. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  508. * @param year
  509. * Optional. The year the publication was published.
  510. * @param series_name
  511. * Optional. The name of the series (e.g. Journal name)
  512. *
  513. * @return
  514. * Returns an array of all the publications that have the provided
  515. * cross reference. If no publications match, then an empty array
  516. * is returned.
  517. *
  518. * @ingroup tripal_pub_api
  519. *
  520. */
  521. function tripal_pub_get_pubs_by_title_type_pyear_series($title, $type = NULL, $pyear = NULL, $series_name = NULL) {
  522. $return = array();
  523. // build the values array for the query.
  524. $values = array(
  525. 'title' => $title,
  526. );
  527. $stmnt_suffix = 'ti';
  528. if ($type) {
  529. $values['type_id'] = array(
  530. 'name' => $type,
  531. 'cv_id' => array(
  532. 'name' => 'tripal_pub'
  533. )
  534. );
  535. $stmnt_suffix .= 'ty';
  536. }
  537. if ($pyear) {
  538. $values['pyear'] = $pyear;
  539. $stmnt_suffix .= 'py';
  540. }
  541. if ($series_name) {
  542. $values['series_name'] = strtolower($series_name);
  543. $stmnt_suffix .= 'se';
  544. }
  545. $options = array(
  546. 'statement_name' => 'sel_pub_' . $stmnt_suffix,
  547. 'case_insensitive_columns' => array('title', 'series_name'),
  548. );
  549. $results = chado_select_record('pub', array('pub_id'), $values, $options);
  550. // iterate through any matches and pull out the pub_id
  551. foreach ($results as $index => $pub) {
  552. $return[] = $pub->pub_id;
  553. }
  554. return $return;
  555. }
  556. /**
  557. * Returns the list of publications that match a given title, type and year
  558. *
  559. * @param title
  560. * The title of the publication to look for
  561. * @param type
  562. * Optional. The publication type. The value of this field should come from
  563. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  564. * @param year
  565. * Optional. The year the publication was published.
  566. * @param series_name
  567. * Optional. The name of the series (e.g. Journal name)
  568. *
  569. * @return
  570. * Returns an array of all the publications that have the provided
  571. * cross reference. If no publications match, then an empty array
  572. * is returned.
  573. *
  574. * @ingroup tripal_pub_api
  575. *
  576. */
  577. function tripal_pub_get_pub_by_uniquename($name) {
  578. $return = array();
  579. // build the values array for the query.
  580. $values = array(
  581. 'uniquename' => $name,
  582. );
  583. $options = array(
  584. 'statement_name' => 'sel_pub_un',
  585. 'case_insensitive_columns' => array('uniquename'),
  586. );
  587. $results = chado_select_record('pub', array('pub_id'), $values, $options);
  588. // iterate through any matches and pull out the pub_id
  589. foreach ($results as $index => $pub) {
  590. $return[] = $pub->pub_id;
  591. }
  592. return $return;
  593. }
  594. /**
  595. * Adds a new publication to the Chado, along with all properties and
  596. * database cross-references. If the publication does not already exist
  597. * in Chado then it is added. If it does exist nothing is done. If
  598. * the $update parameter is TRUE then the publication is updated if it exists.
  599. *
  600. * @param $pub_details
  601. * An associative array containing all of the details about the publication.
  602. * @param $action
  603. * This variable will get set to a text value indicating the action that was
  604. * performed. The values include 'skipped', 'inserted', 'updated' or 'error'.
  605. * @param $do_contact
  606. * Optional. Set to TRUE if a contact entry should be added to the Chado contact table
  607. * for authors of the publication.
  608. * @param $update_if_exists
  609. * Optional. If the publication already exists then this function will return
  610. * without adding a new publication. However, set this value to TRUE to force
  611. * the function to pudate the publication using the $pub_details that are provided.
  612. *
  613. * @return
  614. * If the publication already exists, is inserted or updated then the publication
  615. * ID is returned, otherwise FALSE is returned. If the publication already exists
  616. * and $update_if_exists is not TRUE then the $action variable is set to 'skipped'.
  617. * If the publication already exists and $update_if_exists is TRUE and if the update
  618. * was successful then $action is set to 'updated'. Otherwise on successful insert
  619. * the $action variable is set to 'inserted'. If the function failes then the
  620. * $action variable is set to 'error'
  621. *
  622. */
  623. function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE) {
  624. $pub_id = 0;
  625. if (!is_array($pub_details)) {
  626. return FALSE;
  627. }
  628. // first try to find the publication using the accession number. It will have
  629. // one if the pub has already been loaded for the publication database
  630. if (array_key_exists('Publication Dbxref', $pub_details)) {
  631. $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
  632. if(count($results) == 1) {
  633. $pub_id = $results[0];
  634. if ($pub_id and !$update_if_exists) {
  635. //tripal_core_report_error('tripal_pub', TRIPAL_WARNING, "A publication with this Dbxref already exists... Skipping: %dbxref",
  636. //array('%dbxref' => $pub_details['Publication Dbxref']));
  637. $action = 'skipped';
  638. return $pub_id;
  639. }
  640. }
  641. elseif (count($results) > 1) {
  642. tripal_report_error('tripal_pub', TRIPAL_ERROR, "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
  643. array('%db' => $dbname, '%accession' => $accession));
  644. $action = 'error';
  645. return FALSE;
  646. }
  647. }
  648. // if we couldn't find a publication by the accession (which means it doesn't
  649. // yet exist or it has been added using a different publication database) then
  650. // try to find it using the title and publication year.
  651. if (!$pub_id and array_key_exists('Title', $pub_details)) {
  652. $results = tripal_pub_get_pubs_by_title_type_pyear_series($pub_details['Title'], NULL, $pub_details['Year']);
  653. if (count($results) == 1) {
  654. $pub_id = $results[0];
  655. if ($pub_id and !$update_if_exists) {
  656. tripal_report_error('tripal_pub', TRIPAL_WARNING, "The publication with the same title, type and year already exists. Skipping. ".
  657. " Title: '%title'. Type: '%type'. Year: '%year'",
  658. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']));
  659. $action = 'skipped';
  660. return $pub_id;
  661. }
  662. }
  663. elseif (count($results) > 1) {
  664. tripal_report_error('tripal_pub', TRIPAL_ERROR, "The publication with the same title, type and year is present multiple times. Cannot ".
  665. "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
  666. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']));
  667. $action = 'error';
  668. return FALSE;
  669. }
  670. }
  671. // get the publication type (use the first publication type, any others will get stored as properties)
  672. if (array_key_exists('Publication Type', $pub_details)) {
  673. if(is_array($pub_details['Publication Type'])) {
  674. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  675. }
  676. else {
  677. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
  678. }
  679. }
  680. else {
  681. tripal_report_error('tripal_pub', TRIPAL_ERROR, "The Publication Type is a required property but is missing", array());
  682. $action = 'error';
  683. return FALSE;
  684. }
  685. if (!$pub_type) {
  686. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot find publication type: '%type'",
  687. array('%type' => $pub_details['Publication Type'][0]));
  688. $action = 'error';
  689. return FALSE;
  690. }
  691. // build the values array for inserting or updating
  692. $values = array(
  693. 'title' => $pub_details['Title'],
  694. 'volume' => $pub_details['Volume'],
  695. 'series_name' => substr($pub_details['Journal Name'], 0, 255),
  696. 'issue' => $pub_details['Issue'],
  697. 'pyear' => $pub_details['Year'],
  698. 'pages' => $pub_details['Pages'],
  699. 'uniquename' => $pub_details['Citation'],
  700. 'type_id' => $pub_type->cvterm_id,
  701. );
  702. // if there is no pub_id then we need to do an insert.
  703. if (!$pub_id) {
  704. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  705. $pub = chado_insert_record('pub', $values, $options);
  706. if (!$pub) {
  707. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot insert the publication with title: %title",
  708. array('%title' => $pub_details['Title']));
  709. $action = 'error';
  710. return FALSE;
  711. }
  712. $pub_id = $pub['pub_id'];
  713. $action = 'inserted';
  714. }
  715. // if there is a pub_id and we've been told to update, then do the update
  716. if ($pub_id and $update_if_exists) {
  717. $match = array('pub_id' => $pub_id);
  718. $options = array('statement_name' => 'up_pub_tivoseispypaunty');
  719. $success = chado_update_record('pub', $match, $values, $options);
  720. if (!$success) {
  721. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot update the publication with title: %title",
  722. array('%title' => $pub_details['Title']));
  723. $action = 'error';
  724. return FALSE;
  725. }
  726. $action = 'updated';
  727. }
  728. // before we add any new properties we need to remove those that are there if this
  729. // is an update. The only thing we don't want to remove are the 'Publication Dbxref'
  730. if ($update_if_exists) {
  731. $sql = "
  732. DELETE FROM {pubprop}
  733. WHERE
  734. pub_id = :pub_id AND
  735. NOT type_id in (
  736. SELECT cvterm_id
  737. FROM {cvterm}
  738. WHERE name = 'Publication Dbxref'
  739. )
  740. ";
  741. chado_query($sql, array(':pub_id' => $pub_id));
  742. }
  743. // iterate through the properties and add them
  744. foreach ($pub_details as $key => $value) {
  745. // the pub_details may have the raw search data (e.g. in XML from PubMed. We'll irgnore this for now
  746. if($key == 'raw') {
  747. continue;
  748. }
  749. // get the cvterm by name or synonym
  750. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  751. if (!$cvterm) {
  752. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  753. }
  754. if (!$cvterm) {
  755. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot find term: '%prop'. Skipping.", array('%prop' => $key));
  756. continue;
  757. }
  758. // skip details that won't be stored as properties
  759. if ($key == 'Author List') {
  760. tripal_pub_add_authors($pub_id, $value, $do_contact);
  761. continue;
  762. }
  763. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  764. $key == 'Year' or $key == 'Pages') {
  765. continue;
  766. }
  767. $success = 0;
  768. if (is_array($value)) {
  769. foreach ($value as $subkey => $subvalue) {
  770. // if the key is an integer then this array is a simple list and
  771. // we will insert using the primary key. Otheriwse, use the new key
  772. if(is_int($subkey)) {
  773. $success = chado_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
  774. }
  775. else {
  776. $success = chado_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
  777. }
  778. }
  779. }
  780. else {
  781. $success = chado_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  782. }
  783. if (!$success) {
  784. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot add property '%prop' to publication. Skipping.",
  785. array('%prop' => $key));
  786. continue;
  787. }
  788. }
  789. return $pub_id;
  790. }
  791. /*
  792. *
  793. */
  794. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  795. $rank = 0;
  796. // first remove any of the existing pubauthor entires
  797. $sql = "DELETE FROM {pubauthor} WHERE pub_id = :pub_id";
  798. chado_query($sql, array(':pub_id' => $pub_id));
  799. // iterate through the authors and add them to the pubauthors and contact
  800. // tables of chado, then link them through the custom pubauthors_contact table
  801. foreach ($authors as $author) {
  802. // skip invalid author entires
  803. if ($author['valid'] == 'N') {
  804. continue;
  805. }
  806. // remove the 'valid' property as we don't have a CV term for it
  807. unset($author['valid']);
  808. // construct the contact.name field using the author information
  809. $name = '';
  810. $type = 'Person';
  811. if ($author['Given Name']) {
  812. $name .= $author['Given Name'];
  813. }
  814. if ($author['Surname']) {
  815. $name .= ' ' . $author['Surname'];
  816. }
  817. if ($author['Suffix']) {
  818. $name .= ' ' . $author['Suffix'];
  819. }
  820. if ($author['Collective']) {
  821. $name = $author['Collective'];
  822. $type = 'Collective';
  823. }
  824. $name = trim($name);
  825. // add an entry to the pubauthors table
  826. $values = array(
  827. 'pub_id' => $pub_id,
  828. 'rank' => $rank,
  829. 'surname' => $author['Surname'] ? substr($author['Surname'], 0, 100) : substr($author['Collective'], 0, 100),
  830. 'givennames' => $author['Given Name'],
  831. 'suffix' => $author['Suffix'],
  832. );
  833. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  834. $pubauthor = chado_insert_record('pubauthor', $values, $options);
  835. // if the user wants us to create a contact for each author then do it.
  836. if ($do_contact) {
  837. // Add the contact
  838. $contact = tripal_contact_add_contact($name, '', $type, $author);
  839. // if we have succesfully added the contact and the pubauthor entries then we want to
  840. // link them together
  841. if ($contact and $pubauthor) {
  842. // link the pubauthor entry to the contact
  843. $values = array(
  844. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  845. 'contact_id' => $contact['contact_id'],
  846. );
  847. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  848. $pubauthor_contact = chado_insert_record('pubauthor_contact', $values, $options);
  849. if (!$pubauthor_contact) {
  850. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot link pub authro and contact.", array());
  851. }
  852. }
  853. }
  854. $rank++;
  855. }
  856. }
  857. /**
  858. * Retrieve properties of a given type for a given pub
  859. *
  860. * @param $pub_id
  861. * The pub_id of the properties you would like to retrieve
  862. * @param $property
  863. * The cvterm name of the properties to retrieve
  864. *
  865. * @return
  866. * An pub chado variable with the specified properties expanded
  867. *
  868. * @ingroup tripal_pub_api
  869. */
  870. function tripal_pub_get_property($pub_id, $property) {
  871. return chado_get_property('pub', $pub_id, $property, 'tripal_pub');
  872. }
  873. /**
  874. * Insert a given property
  875. *
  876. * @param $pub_id
  877. * The pub_id of the property to insert
  878. * @param $property
  879. * The cvterm name of the property to insert
  880. * @param $value
  881. * The value of the property to insert
  882. * @param $update_if_present
  883. * A boolean indicated whether to update the record if it's already present
  884. *
  885. * @return
  886. * True of success, False otherwise
  887. *
  888. * @ingroup tripal_pub_api
  889. */
  890. function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
  891. return chado_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
  892. }
  893. /**
  894. * Update a given property
  895. *
  896. * @param $pub_id
  897. * The pub_id of the property to update
  898. * @param $property
  899. * The cvterm name of the property to update
  900. * @param $value
  901. * The value of the property to update
  902. * @param $insert_if_missing
  903. * A boolean indicated whether to insert the record if it's absent
  904. *
  905. * Note: The property will be identified using the unique combination of the $pub_id and $property
  906. * and then it will be updated with the supplied value
  907. *
  908. * @return
  909. * True of success, False otherwise
  910. *
  911. * @ingroup tripal_pub_api
  912. */
  913. function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
  914. return chado_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
  915. }
  916. /**
  917. * Delete a given property
  918. *
  919. * @param $pub_id
  920. * The pub_id of the property to delete
  921. * @param $property
  922. * The cvterm name of the property to delete
  923. *
  924. * Note: The property will be identified using the unique combination of the $pub_id and $property
  925. * and then it will be deleted
  926. *
  927. * @return
  928. * True of success, False otherwise
  929. *
  930. * @ingroup tripal_pub_api
  931. */
  932. function tripal_pub_delete_property($pub_id, $property) {
  933. return chado_delete_property('pub', $pub_id, $property, 'tripal_pub');
  934. }
  935. /**
  936. * This function generates an array suitable for use with the
  937. * tripal_pub_create_citation function for any publication
  938. * already stored in the Chado tables.
  939. *
  940. * @param $pub_id
  941. * The publication ID
  942. * @param $skip_existing
  943. * Set to TRUE to skip publications that already have a citation
  944. * in the pubprop table. Set to FALSE to generate a citation
  945. * regardless if the citation already exists.
  946. *
  947. * @return
  948. * An array suitable for the trpial_pub_create_citation function. On
  949. * failure returns FALSE.
  950. */
  951. function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
  952. $options = array('return_array' => 1);
  953. // ---------------------------------
  954. // get the publication
  955. // ---------------------------------
  956. $values = array('pub_id' => $pub_id);
  957. $pub = chado_generate_var('pub', $values);
  958. // expand the title
  959. $pub = chado_expand_var($pub, 'field', 'pub.title');
  960. $pub = chado_expand_var($pub, 'field', 'pub.volumetitle');
  961. $pub = chado_expand_var($pub, 'field', 'pub.uniquename');
  962. $pub_array = array();
  963. if (trim($pub->title)) {
  964. $pub_array['Title'] = $pub->title;
  965. }
  966. if (trim($pub->volumetitle)) {
  967. $pub_array['Volume Title'] = $pub->volumetitle;
  968. }
  969. if (trim($pub->volume)) {
  970. $pub_array['Volume'] = $pub->volume;
  971. }
  972. if (trim($pub->series_name)) {
  973. $pub_array['Series Name'] = $pub->series_name;
  974. }
  975. if (trim($pub->issue)) {
  976. $pub_array['Issue'] = $pub->issue;
  977. }
  978. if (trim($pub->pyear)) {
  979. $pub_array['Year'] = $pub->pyear;
  980. }
  981. if (trim($pub->pages)) {
  982. $pub_array['Pages'] = $pub->pages;
  983. }
  984. if (trim($pub->miniref)) {
  985. $pub_array['Mini Ref'] = $pub->miniref;
  986. }
  987. if (trim($pub->uniquename)) {
  988. $pub_array['Uniquename'] = $pub->uniquename;
  989. }
  990. $pub_array['Publication Type'][] = $pub->type_id->name;
  991. // ---------------------------------
  992. // get the citation
  993. // ---------------------------------
  994. $values = array(
  995. 'pub_id' => $pub->pub_id,
  996. 'type_id' => array(
  997. 'name' => 'Citation',
  998. ),
  999. );
  1000. $citation = chado_generate_var('pubprop', $values);
  1001. if ($citation) {
  1002. $citation = chado_expand_var($citation, 'field', 'pubprop.value', $options);
  1003. if (count($citation) > 1) {
  1004. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Publication has multiple citations already: %pub_id",
  1005. array('%pub_id' => $pubid));
  1006. return FALSE;
  1007. }
  1008. elseif (count($citation) == 1 and $skip_existing == TRUE) {
  1009. // skip this publication, it already has a citation
  1010. return FALSE;
  1011. }
  1012. }
  1013. // ---------------------------------
  1014. // get the publication types
  1015. // ---------------------------------
  1016. $values = array(
  1017. 'pub_id' => $pub->pub_id,
  1018. 'type_id' => array(
  1019. 'name' => 'Publication Type',
  1020. ),
  1021. );
  1022. $ptypes = chado_generate_var('pubprop', $values, $options);
  1023. if ($ptypes) {
  1024. $ptypes = chado_expand_var($ptypes, 'field', 'pubprop.value', $options);
  1025. foreach ($ptypes as $ptype) {
  1026. $pub_array['Publication Type'][] = $ptype->value;
  1027. }
  1028. }
  1029. // ---------------------------------
  1030. // get the authors list
  1031. // ---------------------------------
  1032. $values = array(
  1033. 'pub_id' => $pub->pub_id,
  1034. 'type_id' => array(
  1035. 'name' => 'Authors',
  1036. ),
  1037. );
  1038. $authors = chado_generate_var('pubprop', $values);
  1039. $authors = chado_expand_var($authors, 'field', 'pubprop.value', $options);
  1040. if (count($authors) > 1) {
  1041. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Publication has multiple author lists. It should have only one list: %pub_id",
  1042. array('%pub_id' => $pubid));
  1043. return FALSE;
  1044. }
  1045. else if (trim($authors->value)) {
  1046. $pub_array['Authors'] = $authors->value;
  1047. }
  1048. // if there is no 'Author's property then try to retreive authors from the pubauthor table
  1049. else {
  1050. $sql = "
  1051. SELECT string_agg(surname || ' ' || givennames, ', ')
  1052. FROM {pubauthor}
  1053. WHERE pub_id = :pub_id
  1054. GROUP BY pub_id
  1055. ";
  1056. $au = chado_query($sql, array(':pub_id' => $pub_id))->fetchField();
  1057. if ($au) {
  1058. $pub_array['Authors'] = $au;
  1059. }
  1060. }
  1061. //Get other props
  1062. $props = array(
  1063. 'Journal Abbreviation',
  1064. 'Elocation',
  1065. 'Media Code',
  1066. 'Conference Name',
  1067. 'Keywords',
  1068. 'Series Name',
  1069. 'pISSN',
  1070. 'Publication Date',
  1071. 'Journal Code',
  1072. 'Journal Alias',
  1073. 'Journal Country',
  1074. 'Published Location',
  1075. 'Publication Model',
  1076. 'Language Abbr',
  1077. 'Alias',
  1078. 'Publication Dbxref',
  1079. 'Copyright',
  1080. 'Abstract',
  1081. 'Notes',
  1082. 'Citation',
  1083. 'Language',
  1084. 'URL',
  1085. 'eISSN',
  1086. 'DOI',
  1087. 'ISSN',
  1088. 'Publication Code',
  1089. 'Comments',
  1090. 'Publisher',
  1091. 'Media Alias',
  1092. 'Original Title');
  1093. foreach ($props AS $prop) {
  1094. $sql =
  1095. "SELECT value FROM {pubprop}
  1096. WHERE type_id =
  1097. (SELECT cvterm_id
  1098. FROM {cvterm}
  1099. WHERE name = :cvtname AND cv_id =
  1100. (SELECT cv_id
  1101. FROM {cv}
  1102. WHERE name = 'tripal_pub'
  1103. )
  1104. )
  1105. AND pub_id = :pub_id
  1106. ";
  1107. $val = trim(chado_query($sql, array(':cvtname' => $prop, ':pub_id' => $pub->pub_id))->fetchField());
  1108. if ($val) {
  1109. $pub_array[$prop] =$val;
  1110. }
  1111. }
  1112. return $pub_array;
  1113. }
  1114. /**
  1115. * This function generates citations for publications. It requires
  1116. * an array structure with keys being the terms in the Tripal
  1117. * publication ontology. This function is intended to be used
  1118. * for any function that needs to generate a citation.
  1119. *
  1120. * @param $pub
  1121. * An array structure containing publication details where the keys
  1122. * are the publication ontology term names and values are the
  1123. * corresponding details. The pub array can contain the following
  1124. * keys with corresponding values:
  1125. * - Publication Type: an array of publication types. a publication can have more than one type
  1126. * - Authors: a string containing all of the authors of a publication
  1127. * - Journal Name: a string containing the journal name
  1128. * - Journal Abbreviation: a string containing the journal name abbreviation
  1129. * - Series Name: a string containing the series (e.g. conference proceedings) name
  1130. * - Series Abbreviation: a string containing the series name abbreviation
  1131. * - Volume: the serives volume number
  1132. * - Issue: the series issue number
  1133. * - Pages: the page numbers for the publication
  1134. * - Publication Date: A date in the format "Year Month Day"
  1135. *
  1136. * @return
  1137. * A text string containing the citation
  1138. */
  1139. function tripal_pub_create_citation($pub) {
  1140. $citation = '';
  1141. $pub_type = '';
  1142. // An article may have more than one publication type. For example,
  1143. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  1144. // Therefore, we need to select the type that makes most sense for
  1145. // construction of the citation. Here we'll iterate through them all
  1146. // and select the one that matches best.
  1147. if(is_array($pub['Publication Type'])) {
  1148. foreach ($pub['Publication Type'] as $ptype) {
  1149. if ($ptype == 'Journal Article' ) {
  1150. $pub_type = $ptype;
  1151. break;
  1152. }
  1153. else if ($ptype == 'Conference Proceedings'){
  1154. $pub_type = $ptype;
  1155. break;
  1156. }
  1157. else if ($ptype == 'Book') {
  1158. $pub_type = $ptype;
  1159. break;
  1160. }
  1161. else if ($ptype == 'Letter') {
  1162. $pub_type = $ptype;
  1163. break;
  1164. }
  1165. else if ($ptype == 'Book Chapter') {
  1166. $pub_type = $ptype;
  1167. break;
  1168. }
  1169. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  1170. $pub_type = $ptype;
  1171. // we don't break because if the article is also a Journal Article
  1172. // we prefer that type
  1173. }
  1174. }
  1175. if (!$pub_type) {
  1176. tripal_report_error('tripal_pub', TRIPAL_ERROR, "Cannot generate citation for publication type: %types",
  1177. array('%types' => print_r($pub['Publication Type'], TRUE)));
  1178. return FALSE;
  1179. }
  1180. }
  1181. else {
  1182. $pub_type = $pub['Publication Type'];
  1183. }
  1184. //----------------------
  1185. // Journal Article
  1186. //----------------------
  1187. if ($pub_type == 'Journal Article') {
  1188. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1189. if (array_key_exists('Journal Name', $pub)) {
  1190. $citation .= $pub['Journal Name'] . '. ';
  1191. }
  1192. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  1193. $citation .= $pub['Journal Abbreviation'] . '. ';
  1194. }
  1195. elseif (array_key_exists('Series Name', $pub)) {
  1196. $citation .= $pub['Series Name'] . '. ';
  1197. }
  1198. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1199. $citation .= $pub['Series Abbreviation'] . '. ';
  1200. }
  1201. if (array_key_exists('Publication Date', $pub)) {
  1202. $citation .= $pub['Publication Date'];
  1203. }
  1204. elseif (array_key_exists('Year', $pub)) {
  1205. $citation .= $pub['Year'];
  1206. }
  1207. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1208. $citation .= '; ';
  1209. }
  1210. if (array_key_exists('Volume', $pub)) {
  1211. $citation .= $pub['Volume'];
  1212. }
  1213. if (array_key_exists('Issue', $pub)) {
  1214. $citation .= '(' . $pub['Issue'] . ')';
  1215. }
  1216. if (array_key_exists('Pages', $pub)) {
  1217. if (array_key_exists('Volume', $pub)) {
  1218. $citation .= ':';
  1219. }
  1220. $citation .= $pub['Pages'];
  1221. }
  1222. $citation .= '.';
  1223. }
  1224. //----------------------
  1225. // Research Support, Non-U.S. Gov't
  1226. //----------------------
  1227. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  1228. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1229. if (array_key_exists('Journal Name', $pub)) {
  1230. $citation .= $pub['Journal Name'] . '. ';
  1231. }
  1232. if (array_key_exists('Publication Date', $pub)) {
  1233. $citation .= $pub['Publication Date'];
  1234. }
  1235. elseif (array_key_exists('Year', $pub)) {
  1236. $citation .= $pub['Year'];
  1237. }
  1238. $citation .= '.';
  1239. }
  1240. //----------------------
  1241. // Letter
  1242. //----------------------
  1243. elseif ($pub_type == 'Letter') {
  1244. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1245. if (array_key_exists('Journal Name', $pub)) {
  1246. $citation .= $pub['Journal Name'] . '. ';
  1247. }
  1248. elseif (array_key_exists('Journal Abbreviation', $pub)) {
  1249. $citation .= $pub['Journal Abbreviation'] . '. ';
  1250. }
  1251. elseif (array_key_exists('Series Name', $pub)) {
  1252. $citation .= $pub['Series Name'] . '. ';
  1253. }
  1254. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1255. $citation .= $pub['Series Abbreviation'] . '. ';
  1256. }
  1257. if (array_key_exists('Publication Date', $pub)) {
  1258. $citation .= $pub['Publication Date'];
  1259. }
  1260. elseif (array_key_exists('Year', $pub)) {
  1261. $citation .= $pub['Year'];
  1262. }
  1263. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1264. $citation .= '; ';
  1265. }
  1266. if (array_key_exists('Volume', $pub)) {
  1267. $citation .= $pub['Volume'];
  1268. }
  1269. if (array_key_exists('Issue', $pub)) {
  1270. $citation .= '(' . $pub['Issue'] . ')';
  1271. }
  1272. if (array_key_exists('Pages', $pub)) {
  1273. if (array_key_exists('Volume', $pub)) {
  1274. $citation .= ':';
  1275. }
  1276. $citation .= $pub['Pages'];
  1277. }
  1278. $citation .= '.';
  1279. }
  1280. //----------------------
  1281. // Book
  1282. //----------------------
  1283. elseif ($pub_type == 'Book') {
  1284. }
  1285. //----------------------
  1286. // Book Chapter
  1287. //----------------------
  1288. elseif ($pub_type == 'Book Chapter') {
  1289. }
  1290. //----------------------
  1291. // Conference Proceedings
  1292. //----------------------
  1293. elseif ($pub_type == 'Conference Proceedings') {
  1294. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1295. if (array_key_exists('Conference Name', $pub)) {
  1296. $citation .= $pub['Conference Name'] . '. ';
  1297. }
  1298. elseif (array_key_exists('Series Name', $pub)) {
  1299. $citation .= $pub['Series Name'] . '. ';
  1300. }
  1301. elseif (array_key_exists('Series Abbreviation', $pub)) {
  1302. $citation .= $pub['Series Abbreviation'] . '. ';
  1303. }
  1304. if (array_key_exists('Publication Date', $pub)) {
  1305. $citation .= $pub['Publication Date'];
  1306. }
  1307. elseif (array_key_exists('Year', $pub)) {
  1308. $citation .= $pub['Year'];
  1309. }
  1310. if (array_key_exists('Volume', $pub) or array_key_exists('Issue', $pub) or array_key_exists('Pages',$pub)) {
  1311. $citation .= '; ';
  1312. }
  1313. if (array_key_exists('Volume', $pub)) {
  1314. $citation .= $pub['Volume'];
  1315. }
  1316. if (array_key_exists('Issue', $pub)) {
  1317. $citation .= '(' . $pub['Issue'] . ')';
  1318. }
  1319. if (array_key_exists('Pages', $pub)) {
  1320. if (array_key_exists('Volume', $pub)) {
  1321. $citation .= ':';
  1322. }
  1323. $citation .= $pub['Pages'];
  1324. }
  1325. $citation .= '.';
  1326. }
  1327. return $citation;
  1328. }