tripal_pub.api.inc 43 KB

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