tripal_pub.api.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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 = :accession and DB.name = :dbname ";
  128. $args[':accession'] = $accession;
  129. $args[':dbname'] = $dbname;
  130. }
  131. elseif ($db) {
  132. $sql .= " WHERE DB.name = :dbname ";
  133. $args[':dbname'] = $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 = $results->fetchObject()) {
  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 = $results->fetchObject()) {
  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. $pub_id = tripal_pub_add_publication($pub, $action, $do_contact, $update);
  332. if ($pub_id){
  333. // add the publication cross reference (e.g. to PubMed)
  334. if ($pub_id and $pub['Publication Dbxref']) {
  335. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub['Publication Dbxref']);
  336. }
  337. $pub['pub_id'] = $pub_id;
  338. }
  339. switch ($action) {
  340. case 'error':
  341. $report['error']++;
  342. break;
  343. case 'inserted':
  344. $report['inserted'][] = $pub;
  345. break;
  346. case 'updated':
  347. $report['updated'][] = $pub;
  348. break;
  349. case 'skipped':
  350. $report['skipped'][] = $pub;
  351. break;
  352. }
  353. $i++;
  354. }
  355. print "\n";
  356. return $report;
  357. }
  358. /*
  359. *
  360. */
  361. function tripal_pub_add_pub_dbxref($pub_id, $pub_dbxref) {
  362. // break apart the dbxref
  363. $dbname = '';
  364. $accession = '';
  365. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  366. $dbname = $matches[1];
  367. $accession = $matches[2];
  368. }
  369. else {
  370. return FALSE;
  371. }
  372. // check to see if the pub_dbxref record already exist
  373. $values = array(
  374. 'dbxref_id' => array(
  375. 'accession' => $accession,
  376. 'db_id' => array(
  377. 'name' => $dbname,
  378. ),
  379. ),
  380. 'pub_id' => $pub_id,
  381. );
  382. $options = array('statement_name' => 'sel_pubdbxref_dbpu');
  383. $results = tripal_core_chado_select('pub_dbxref', array('*'), $values, $options);
  384. // if the pub_dbxref record exist then we don't need to re-add it.
  385. if(count($results) > 0) {
  386. return $results[0];
  387. }
  388. // make sure our database already exists
  389. $db = tripal_db_add_db($dbname);
  390. // get the database cross-reference
  391. $dbxvalues = array(
  392. 'accession' => $accession,
  393. 'db_id' => $db->db_id,
  394. );
  395. $dbxoptions = array('statement_name' => 'sel_dbxref_acdb');
  396. $results = tripal_core_chado_select('dbxref', array('dbxref_id'), $dbxvalues, $dbxoptions);
  397. // if the accession doesn't exist then add it
  398. if(count($results) == 0){
  399. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  400. }
  401. else {
  402. $dbxref = $results[0];
  403. }
  404. // now add the record
  405. $options = array('statement_name' => 'ins_pubdbxref_dbpu');
  406. $results = tripal_core_chado_insert('pub_dbxref', $values, $options);
  407. if (!$results) {
  408. watchdog('tripal_pub', "Cannot add publication dbxref: %db:%accession.",
  409. array('%db' => $dbname, '%accession' => $accession). WATCHDOG_ERROR);
  410. return FALSE;
  411. }
  412. return $results;
  413. }
  414. /**
  415. * Returns the list of publications that are assigned the database
  416. * cross-reference provided
  417. *
  418. * @param $pub_dbxref
  419. * The database cross reference accession. It should be in the form
  420. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  421. * unique publication identifier (e.g. PMID:4382934)
  422. *
  423. * @return
  424. * Returns an array of all the publications that have the provided
  425. * cross reference. If no publications match, then an empty array
  426. * is returned.
  427. *
  428. * @ingroup tripal_pub_api
  429. *
  430. */
  431. function tripal_pub_get_pubs_by_dbxref($pub_dbxref) {
  432. $return = array();
  433. if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
  434. $dbname = $matches[1];
  435. $accession = $matches[2];
  436. $values = array(
  437. 'dbxref_id' => array (
  438. 'accession' => $accession,
  439. 'db_id' => array(
  440. 'name' => $dbname
  441. ),
  442. ),
  443. );
  444. $options = array('statement_name' => 'sel_pubdbxref_db');
  445. $results = tripal_core_chado_select('pub_dbxref', array('pub_id'), $values, $options);
  446. foreach ($results as $index => $pub) {
  447. $return[] = $pub->pub_id;
  448. }
  449. }
  450. return $return;
  451. }
  452. /**
  453. * Returns the list of publications that match a given title, type and year
  454. *
  455. * @param title
  456. * The title of the publication to look for
  457. * @param type
  458. * Optional. The publication type. The value of this field should come from
  459. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  460. * @param year
  461. * Optional. The year the publication was published.
  462. * @param series_name
  463. * Optional. The name of the series (e.g. Journal name)
  464. *
  465. * @return
  466. * Returns an array of all the publications that have the provided
  467. * cross reference. If no publications match, then an empty array
  468. * is returned.
  469. *
  470. * @ingroup tripal_pub_api
  471. *
  472. */
  473. function tripal_pub_get_pubs_by_title_type_pyear_series($title, $type = NULL, $pyear = NULL, $series_name = NULL) {
  474. $return = array();
  475. // build the values array for the query.
  476. $values = array(
  477. 'title' => $title,
  478. );
  479. $stmnt_suffix = 'ti';
  480. if ($type) {
  481. $values['type_id'] = array(
  482. 'name' => $type,
  483. 'cv_id' => array(
  484. 'name' => 'tripal_pub'
  485. )
  486. );
  487. $stmnt_suffix .= 'ty';
  488. }
  489. if ($pyear) {
  490. $values['pyear'] = $pyear;
  491. $stmnt_suffix .= 'py';
  492. }
  493. if ($series_name) {
  494. $values['series_name'] = strtolower($series_name);
  495. $stmnt_suffix .= 'se';
  496. }
  497. $options = array(
  498. 'statement_name' => 'sel_pub_' . $stmnt_suffix,
  499. 'case_insensitive_columns' => array('title', 'series_name'),
  500. );
  501. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  502. // iterate through any matches and pull out the pub_id
  503. foreach ($results as $index => $pub) {
  504. $return[] = $pub->pub_id;
  505. }
  506. return $return;
  507. }
  508. /**
  509. * Returns the list of publications that match a given title, type and year
  510. *
  511. * @param title
  512. * The title of the publication to look for
  513. * @param type
  514. * Optional. The publication type. The value of this field should come from
  515. * the Tripal Pub vocabulary. This should be the type name (e.g. cvterm.name)
  516. * @param year
  517. * Optional. The year the publication was published.
  518. * @param series_name
  519. * Optional. The name of the series (e.g. Journal name)
  520. *
  521. * @return
  522. * Returns an array of all the publications that have the provided
  523. * cross reference. If no publications match, then an empty array
  524. * is returned.
  525. *
  526. * @ingroup tripal_pub_api
  527. *
  528. */
  529. function tripal_pub_get_pub_by_uniquename($name) {
  530. $return = array();
  531. // build the values array for the query.
  532. $values = array(
  533. 'uniquename' => $name,
  534. );
  535. $options = array(
  536. 'statement_name' => 'sel_pub_un',
  537. 'case_insensitive_columns' => array('uniquename'),
  538. );
  539. $results = tripal_core_chado_select('pub', array('pub_id'), $values, $options);
  540. // iterate through any matches and pull out the pub_id
  541. foreach ($results as $index => $pub) {
  542. $return[] = $pub->pub_id;
  543. }
  544. return $return;
  545. }
  546. /**
  547. * Adds a new publication to the Chado, along with all properties and
  548. * database cross-references. If the publication does not already exist
  549. * in Chado then it is added. If it does exist nothing is done. If
  550. * the $update parameter is TRUE then the publication is updated if it exists.
  551. *
  552. * @param $pub_details
  553. * An associative array containing all of the details about the publication.
  554. * @param $action
  555. * This variable will get set to a text value indicating the action that was
  556. * performed. The values include 'skipped', 'inserted', 'updated' or 'error'.
  557. * @param $do_contact
  558. * Optional. Set to TRUE if a contact entry should be added to the Chado contact table
  559. * for authors of the publication.
  560. * @param $update_if_exists
  561. * Optional. If the publication already exists then this function will return
  562. * without adding a new publication. However, set this value to TRUE to force
  563. * the function to pudate the publication using the $pub_details that are provided.
  564. *
  565. * @return
  566. * If the publication already exists, is inserted or updated then the publication
  567. * ID is returned, otherwise FALSE is returned. If the publication already exists
  568. * and $update_if_exists is not TRUE then the $action variable is set to 'skipped'.
  569. * If the publication already exists and $update_if_exists is TRUE and if the update
  570. * was successful then $action is set to 'updated'. Otherwise on successful insert
  571. * the $action variable is set to 'inserted'. If the function failes then the
  572. * $action variable is set to 'error'
  573. *
  574. */
  575. function tripal_pub_add_publication($pub_details, &$action, $do_contact = FALSE, $update_if_exists = FALSE) {
  576. $pub_id = 0;
  577. // first try to find the publication using the accession number. It will have
  578. // one if the pub has already been loaded for the publication database
  579. if ($pub_details['Publication Dbxref']) {
  580. $results = tripal_pub_get_pubs_by_dbxref($pub_details['Publication Dbxref']);
  581. if(count($results) == 1) {
  582. $pub_id = $results[0];
  583. if ($pub_id and !$update_if_exists) {
  584. watchdog('tripal_pub', "A publication with this Dbxref already exists... Skipping: %dbxref",
  585. array('%dbxref' => $pub_details['Publication Dbxref']), WATCHDOG_WARNING);
  586. $action = 'skipped';
  587. return $pub_id;
  588. }
  589. }
  590. elseif (count($results) > 1) {
  591. watchdog('tripal_pub', "There are two publications with this accession: %db:%accession. Cannot determine which to update.",
  592. array('%db' => $dbname, '%accession' => $accession), WATCHDOG_ERROR);
  593. $action = 'error';
  594. return FALSE;
  595. }
  596. }
  597. // if we couldn't find a publication by the accession (which means it doesn't
  598. // yet exist or it has been added using a different publication database) then
  599. // try to find it using the title and publication year.
  600. if (!$pub_id and $pub_details['Title']) {
  601. $results = tripal_pub_get_pubs_by_title_type_pyear_series($pub_details['Title'], NULL, $pub_details['Year']);
  602. if (count($results) == 1) {
  603. $pub_id = $results[0];
  604. if ($pub_id and !$update_if_exists) {
  605. watchdog('tripal_pub', "The publication with the same title, type and year already exists. Skipping. ".
  606. " Title: '%title'. Type: '%type'. Year: '%year'",
  607. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_WARNING);
  608. $action = 'skipped';
  609. return $pub_id;
  610. }
  611. }
  612. elseif (count($results) > 1) {
  613. watchdog('tripal_pub', "The publication with the same title, type and year is present multiple times. Cannot ".
  614. "determine which to use. Title: '%title'. Type: '%type'. Year: '%year'",
  615. array('%title' => $pub_details['Title'], '%type' => $pub_details['Publication Type'], '%year' => $pub_details['Year']), WATCHDOG_ERROR);
  616. $action = 'error';
  617. return FALSE;
  618. }
  619. }
  620. // get the publication type (use the first publication type, any others will get stored as properties)
  621. if (is_array($pub_details['Publication Type'])) {
  622. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'][0], NULL, 'tripal_pub');
  623. }
  624. elseif ($pub_details['Publication Type']) {
  625. $pub_type = tripal_cv_get_cvterm_by_name($pub_details['Publication Type'], NULL, 'tripal_pub');
  626. }
  627. else {
  628. watchdog('tripal_pub', "The Publication Type is a required property but is missing", array(), WATCHDOG_ERROR);
  629. $action = 'error';
  630. return FALSE;
  631. }
  632. if (!$pub_type) {
  633. watchdog('tripal_pub', "Cannot find publication type: '%type'",
  634. array('%type' => $pub_details['Publication Type'][0]), WATCHDOG_ERROR);
  635. $action = 'error';
  636. return FALSE;
  637. }
  638. // build the values array for inserting or updating
  639. $values = array(
  640. 'title' => $pub_details['Title'],
  641. 'volume' => $pub_details['Volume'],
  642. 'series_name' => substr($pub_details['Journal Name'], 0, 255),
  643. 'issue' => $pub_details['Issue'],
  644. 'pyear' => $pub_details['Year'],
  645. 'pages' => $pub_details['Pages'],
  646. 'uniquename' => $pub_details['Citation'],
  647. 'type_id' => $pub_type->cvterm_id,
  648. );
  649. // if there is no pub_id then we need to do an insert.
  650. if (!$pub_id) {
  651. $options = array('statement_name' => 'ins_pub_tivoseispypaunty');
  652. $pub = tripal_core_chado_insert('pub', $values, $options);
  653. if (!$pub) {
  654. watchdog('tripal_pub', "Cannot insert the publication with title: %title",
  655. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  656. $action = 'error';
  657. return FALSE;
  658. }
  659. $pub_id = $pub['pub_id'];
  660. $action = 'inserted';
  661. }
  662. // if there is a pub_id and we've been told to update, then do the update
  663. if ($pub_id and $update_if_exists) {
  664. $match = array('pub_id' => $pub_id);
  665. $options = array('statement_name' => 'up_pub_tivoseispypaunty');
  666. $success = tripal_core_chado_update('pub', $match, $values, $options);
  667. if (!$success) {
  668. watchdog('tripal_pub', "Cannot update the publication with title: %title",
  669. array('%title' => $pub_details['Title']), WATCHDOG_ERROR);
  670. $action = 'error';
  671. return FALSE;
  672. }
  673. $action = 'updated';
  674. }
  675. // before we add any new properties we need to remove those that are there if this
  676. // is an update. The only thing we don't want to remove are the 'Publication Dbxref'
  677. if ($update_if_exists) {
  678. $sql = "
  679. DELETE FROM {pubprop}
  680. WHERE
  681. pub_id = :pub_id AND
  682. NOT type_id in (
  683. SELECT cvterm_id
  684. FROM {cvterm}
  685. WHERE name = 'Publication Dbxref'
  686. )
  687. ";
  688. chado_query($sql, array(':pub_id' => $pub_id));
  689. }
  690. // iterate through the properties and add them
  691. foreach ($pub_details as $key => $value) {
  692. // the pub_details may have the raw search data (e.g. in XML from PubMed. We'll irgnore this for now
  693. if($key == 'raw') {
  694. continue;
  695. }
  696. // get the cvterm by name or synonym
  697. $cvterm = tripal_cv_get_cvterm_by_name($key, NULL, 'tripal_pub');
  698. if (!$cvterm) {
  699. $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
  700. }
  701. if (!$cvterm) {
  702. watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
  703. continue;
  704. }
  705. // skip details that won't be stored as properties
  706. if ($key == 'Author List') {
  707. tripal_pub_add_authors($pub_id, $value, $do_contact);
  708. continue;
  709. }
  710. if ($key == 'Title' or $key == 'Volume' or $key == 'Journal Name' or $key == 'Issue' or
  711. $key == 'Year' or $key == 'Pages') {
  712. continue;
  713. }
  714. $success = 0;
  715. if (is_array($value)) {
  716. foreach ($value as $subkey => $subvalue) {
  717. // if the key is an integer then this array is a simple list and
  718. // we will insert using the primary key. Otheriwse, use the new key
  719. if(is_int($subkey)) {
  720. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $subvalue, FALSE);
  721. }
  722. else {
  723. $success = tripal_core_insert_property('pub', $pub_id, $subkey, 'tripal_pub', $subvalue, FALSE);
  724. }
  725. }
  726. }
  727. else {
  728. $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
  729. }
  730. if (!$success) {
  731. watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.",
  732. array('%prop' => $key), WATCHDOG_ERROR);
  733. continue;
  734. }
  735. }
  736. return $pub_id;
  737. }
  738. /*
  739. *
  740. */
  741. function tripal_pub_add_authors($pub_id, $authors, $do_contact) {
  742. $rank = 0;
  743. // first remove any of the existing pubauthor entires
  744. $sql = "DELETE FROM {pubauthor} WHERE pub_id = :pub_id";
  745. chado_query($sql, array(':pub_id' => $pub_id));
  746. // iterate through the authors and add them to the pubauthors and contact
  747. // tables of chado, then link them through the custom pubauthors_contact table
  748. foreach ($authors as $author) {
  749. // skip invalid author entires
  750. if ($author['valid'] == 'N') {
  751. continue;
  752. }
  753. // remove the 'valid' property as we don't have a CV term for it
  754. unset($author['valid']);
  755. // construct the contact.name field using the author information
  756. $name = '';
  757. $type = 'Person';
  758. if ($author['Given Name']) {
  759. $name .= $author['Given Name'];
  760. }
  761. if ($author['Surname']) {
  762. $name .= ' ' . $author['Surname'];
  763. }
  764. if ($author['Suffix']) {
  765. $name .= ' ' . $author['Suffix'];
  766. }
  767. if ($author['Collective']) {
  768. $name = $author['Collective'];
  769. $type = 'Collective';
  770. }
  771. $name = trim($name);
  772. // add an entry to the pubauthors table
  773. $values = array(
  774. 'pub_id' => $pub_id,
  775. 'rank' => $rank,
  776. 'surname' => $author['Surname'] ? substr($author['Surname'], 0, 100) : substr($author['Collective'], 0, 100),
  777. 'givennames' => $author['Given Name'],
  778. 'suffix' => $author['Suffix'],
  779. );
  780. $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
  781. $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
  782. // if the user wants us to create a contact for each author then do it.
  783. if ($do_contact) {
  784. // Add the contact
  785. $contact = tripal_contact_add_contact($name, '', $type, $author);
  786. // if we have succesfully added the contact and the pubauthor entries then we want to
  787. // link them together
  788. if ($contact and $pubauthor) {
  789. // link the pubauthor entry to the contact
  790. $values = array(
  791. 'pubauthor_id' => $pubauthor['pubauthor_id'],
  792. 'contact_id' => $contact['contact_id'],
  793. );
  794. $options = array('statement_name' => 'ins_pubauthorcontact_puco');
  795. $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
  796. if (!$pubauthor_contact) {
  797. watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
  798. }
  799. }
  800. }
  801. $rank++;
  802. }
  803. }
  804. /**
  805. * Retrieve properties of a given type for a given pub
  806. *
  807. * @param $pub_id
  808. * The pub_id of the properties you would like to retrieve
  809. * @param $property
  810. * The cvterm name of the properties to retrieve
  811. *
  812. * @return
  813. * An pub chado variable with the specified properties expanded
  814. *
  815. * @ingroup tripal_pub_api
  816. */
  817. function tripal_pub_get_property($pub_id, $property) {
  818. return tripal_core_get_property('pub', $pub_id, $property, 'tripal_pub');
  819. }
  820. /**
  821. * Insert a given property
  822. *
  823. * @param $pub_id
  824. * The pub_id of the property to insert
  825. * @param $property
  826. * The cvterm name of the property to insert
  827. * @param $value
  828. * The value of the property to insert
  829. * @param $update_if_present
  830. * A boolean indicated whether to update the record if it's already present
  831. *
  832. * @return
  833. * True of success, False otherwise
  834. *
  835. * @ingroup tripal_pub_api
  836. */
  837. function tripal_pub_insert_property($pub_id, $property, $value, $update_if_present = 0) {
  838. return tripal_core_insert_property('pub', $pub_id, $property, 'tripal_pub', $value, $update_if_present);
  839. }
  840. /**
  841. * Update a given property
  842. *
  843. * @param $pub_id
  844. * The pub_id of the property to update
  845. * @param $property
  846. * The cvterm name of the property to update
  847. * @param $value
  848. * The value of the property to update
  849. * @param $insert_if_missing
  850. * A boolean indicated whether to insert the record if it's absent
  851. *
  852. * Note: The property will be identified using the unique combination of the $pub_id and $property
  853. * and then it will be updated with the supplied value
  854. *
  855. * @return
  856. * True of success, False otherwise
  857. *
  858. * @ingroup tripal_pub_api
  859. */
  860. function tripal_pub_update_property($pub_id, $property, $value, $insert_if_missing = 0) {
  861. return tripal_core_update_property('pub', $pub_id, $property, 'tripal_pub', $value, $insert_if_missing);
  862. }
  863. /**
  864. * Delete a given property
  865. *
  866. * @param $pub_id
  867. * The pub_id of the property to delete
  868. * @param $property
  869. * The cvterm name of the property to delete
  870. *
  871. * Note: The property will be identified using the unique combination of the $pub_id and $property
  872. * and then it will be deleted
  873. *
  874. * @return
  875. * True of success, False otherwise
  876. *
  877. * @ingroup tripal_pub_api
  878. */
  879. function tripal_pub_delete_property($pub_id, $property) {
  880. return tripal_core_delete_property('pub', $pub_id, $property, 'tripal_pub');
  881. }
  882. /**
  883. * This function generates an array suitable for use with the
  884. * tripal_pub_create_citation function for any publication
  885. * already stored in the Chado tables.
  886. *
  887. * @param $pub_id
  888. * The publication ID
  889. * @param $skip_existing
  890. * Set to TRUE to skip publications that already have a citation
  891. * in the pubprop table. Set to FALSE to generate a citation
  892. * regardless if the citation already exists.
  893. *
  894. * @return
  895. * An array suitable for the trpial_pub_create_citation function. On
  896. * failure returns FALSE.
  897. */
  898. function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
  899. $options = array('return_array' => 1);
  900. // ---------------------------------
  901. // get the publication
  902. // ---------------------------------
  903. $values = array('pub_id' => $pub_id);
  904. $pub = tripal_core_generate_chado_var('pub', $values);
  905. // expand the title
  906. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  907. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  908. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  909. $pub_array = array();
  910. if (trim($pub->title)) {
  911. $pub_array['Title'] = $pub->title;
  912. }
  913. if (trim($pub->volumetitle)) {
  914. $pub_array['Volume Title'] = $pub->volumetitle;
  915. }
  916. if (trim($pub->volume)) {
  917. $pub_array['Volume'] = $pub->volume;
  918. }
  919. if (trim($pub->series_name)) {
  920. $pub_array['Series Name'] = $pub->series_name;
  921. }
  922. if (trim($pub->issue)) {
  923. $pub_array['Issue'] = $pub->issue;
  924. }
  925. if (trim($pub->pyear)) {
  926. $pub_array['Year'] = $pub->pyear;
  927. }
  928. if (trim($pub->pages)) {
  929. $pub_array['Pages'] = $pub->pages;
  930. }
  931. if (trim($pub->miniref)) {
  932. $pub_array['Mini Ref'] = $pub->miniref;
  933. }
  934. if (trim($pub->uniquename)) {
  935. $pub_array['Uniquename'] = $pub->uniquename;
  936. }
  937. $pub_array['Publication Type'][] = $pub->type_id->name;
  938. // ---------------------------------
  939. // get the citation
  940. // ---------------------------------
  941. $values = array(
  942. 'pub_id' => $pub->pub_id,
  943. 'type_id' => array(
  944. 'name' => 'Citation',
  945. ),
  946. );
  947. $citation = tripal_core_generate_chado_var('pubprop', $values);
  948. if ($citation) {
  949. $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value', $options);
  950. if (count($citation) > 1) {
  951. watchdog('tripal_pub', "Publication has multiple citations already: %pub_id",
  952. array('%pub_id' => $pubid), WATCHDOG_ERROR);
  953. return FALSE;
  954. }
  955. elseif (count($citation) == 1 and $skip_existing == TRUE) {
  956. // skip this publication, it already has a citation
  957. return FALSE;
  958. }
  959. }
  960. // ---------------------------------
  961. // get the publication types
  962. // ---------------------------------
  963. $values = array(
  964. 'pub_id' => $pub->pub_id,
  965. 'type_id' => array(
  966. 'name' => 'Publication Type',
  967. ),
  968. );
  969. $ptypes = tripal_core_generate_chado_var('pubprop', $values, $options);
  970. if ($ptypes) {
  971. $ptypes = tripal_core_expand_chado_vars($ptypes, 'field', 'pubprop.value', $options);
  972. foreach ($ptypes as $ptype) {
  973. $pub_array['Publication Type'][] = $ptype->value;
  974. }
  975. }
  976. // ---------------------------------
  977. // get the authors list
  978. // ---------------------------------
  979. $values = array(
  980. 'pub_id' => $pub->pub_id,
  981. 'type_id' => array(
  982. 'name' => 'Authors',
  983. ),
  984. );
  985. $authors = tripal_core_generate_chado_var('pubprop', $values);
  986. $authors = tripal_core_expand_chado_vars($authors, 'field', 'pubprop.value', $options);
  987. if (count($authors) > 1) {
  988. watchdog('tripal_pub', "Publication has multiple author lists. It should have only one list: %pub_id",
  989. array('%pub_id' => $pubid), WATCHDOG_ERROR);
  990. return FALSE;
  991. }
  992. else if (trim($authors->value)) {
  993. $pub_array['Authors'] = $authors->value;
  994. }
  995. // if there is no 'Author's property then try to retreive authors from the pubauthor table
  996. else {
  997. $sql = "
  998. SELECT string_agg(surname || ' ' || givennames, ', ')
  999. FROM {pubauthor}
  1000. WHERE pub_id = :pub_id
  1001. GROUP BY pub_id
  1002. ";
  1003. $au = chado_query($sql, array(':pub_id' => $pub_id))->fetchField();
  1004. if ($au) {
  1005. $pub_array['Authors'] = $au;
  1006. }
  1007. }
  1008. //Get other props
  1009. $props = array(
  1010. 'Journal Abbreviation',
  1011. 'Elocation',
  1012. 'Media Code',
  1013. 'Conference Name',
  1014. 'Keywords',
  1015. 'Series Name',
  1016. 'pISSN',
  1017. 'Publication Date',
  1018. 'Journal Code',
  1019. 'Journal Alias',
  1020. 'Journal Country',
  1021. 'Published Location',
  1022. 'Publication Model',
  1023. 'Language Abbr',
  1024. 'Alias',
  1025. 'Publication Dbxref',
  1026. 'Copyright',
  1027. 'Abstract',
  1028. 'Notes',
  1029. 'Citation',
  1030. 'Language',
  1031. 'URL',
  1032. 'eISSN',
  1033. 'DOI',
  1034. 'ISSN',
  1035. 'Publication Code',
  1036. 'Comments',
  1037. 'Publisher',
  1038. 'Media Alias',
  1039. 'Original Title');
  1040. foreach ($props AS $prop) {
  1041. $sql =
  1042. "SELECT value FROM {pubprop}
  1043. WHERE type_id =
  1044. (SELECT cvterm_id
  1045. FROM {cvterm}
  1046. WHERE name = :cvtname AND cv_id =
  1047. (SELECT cv_id
  1048. FROM cv
  1049. WHERE name = 'tripal_pub'
  1050. )
  1051. )
  1052. AND pub_id = :pub_id
  1053. ";
  1054. $val = trim(chado_query($sql, array(':cvtname' => $prop, ':pub_id' => $pub->pub_id))->fetchField());
  1055. if ($val) {
  1056. $pub_array[$prop] =$val;
  1057. }
  1058. }
  1059. return $pub_array;
  1060. }
  1061. /**
  1062. * This function generates citations for publications. It requires
  1063. * an array structure with keys being the terms in the Tripal
  1064. * publication ontology. This function is intended to be used
  1065. * for any function that needs to generate a citation.
  1066. *
  1067. * @param $pub
  1068. * An array structure containing publication details where the keys
  1069. * are the publication ontology term names and values are the
  1070. * corresponding details. The pub array can contain the following
  1071. * keys with corresponding values:
  1072. * - Publication Type: an array of publication types. a publication can have more than one type
  1073. * - Authors: a string containing all of the authors of a publication
  1074. * - Journal Name: a string containing the journal name
  1075. * - Journal Abbreviation: a string containing the journal name abbreviation
  1076. * - Series Name: a string containing the series (e.g. conference proceedings) name
  1077. * - Series Abbreviation: a string containing the series name abbreviation
  1078. * - Volume: the serives volume number
  1079. * - Issue: the series issue number
  1080. * - Pages: the page numbers for the publication
  1081. * - Publication Date: A date in the format "Year Month Day"
  1082. *
  1083. * @return
  1084. * A text string containing the citation
  1085. */
  1086. function tripal_pub_create_citation($pub) {
  1087. $citation = '';
  1088. $pub_type = '';
  1089. // An article may have more than one publication type. For example,
  1090. // a publication type can be 'Journal Article' but also a 'Clinical Trial'.
  1091. // Therefore, we need to select the type that makes most sense for
  1092. // construction of the citation. Here we'll iterate through them all
  1093. // and select the one that matches best.
  1094. if(is_array($pub['Publication Type'])) {
  1095. foreach ($pub['Publication Type'] as $ptype) {
  1096. if ($ptype == 'Journal Article' ) {
  1097. $pub_type = $ptype;
  1098. break;
  1099. }
  1100. else if ($ptype == 'Conference Proceedings'){
  1101. $pub_type = $ptype;
  1102. break;
  1103. }
  1104. else if ($ptype == 'Book') {
  1105. $pub_type = $ptype;
  1106. break;
  1107. }
  1108. else if ($ptype == 'Letter') {
  1109. $pub_type = $ptype;
  1110. break;
  1111. }
  1112. else if ($ptype == 'Book Chapter') {
  1113. $pub_type = $ptype;
  1114. break;
  1115. }
  1116. else if ($ptype == "Research Support, Non-U.S. Gov't") {
  1117. $pub_type = $ptype;
  1118. // we don't break because if the article is also a Journal Article
  1119. // we prefer that type
  1120. }
  1121. }
  1122. if (!$pub_type) {
  1123. watchdog('tripal_pub', "Cannot generate citation for publication type: %types",
  1124. array('%types' => print_r($pub['Publication Type'], TRUE)), WATCHDOG_ERROR);
  1125. return FALSE;
  1126. }
  1127. }
  1128. else {
  1129. $pub_type = $pub['Publication Type'];
  1130. }
  1131. //----------------------
  1132. // Journal Article
  1133. //----------------------
  1134. if ($pub_type == 'Journal Article') {
  1135. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1136. if ($pub['Journal Name']) {
  1137. $citation .= $pub['Journal Name'] . '. ';
  1138. }
  1139. elseif ($pub['Journal Abbreviation']) {
  1140. $citation .= $pub['Journal Abbreviation'] . '. ';
  1141. }
  1142. elseif ($pub['Series Name']) {
  1143. $citation .= $pub['Series Name'] . '. ';
  1144. }
  1145. elseif ($pub['Series Abbreviation']) {
  1146. $citation .= $pub['Series Abbreviation'] . '. ';
  1147. }
  1148. if ($pub['Publication Date']) {
  1149. $citation .= $pub['Publication Date'];
  1150. }
  1151. elseif ($pub['Year']) {
  1152. $citation .= $pub['Year'];
  1153. }
  1154. if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
  1155. $citation .= '; ';
  1156. }
  1157. if ($pub['Volume']) {
  1158. $citation .= $pub['Volume'];
  1159. }
  1160. if ($pub['Issue']) {
  1161. $citation .= '(' . $pub['Issue'] . ')';
  1162. }
  1163. if ($pub['Pages']) {
  1164. if($pub['Volume']) {
  1165. $citation .= ':';
  1166. }
  1167. $citation .= $pub['Pages'];
  1168. }
  1169. $citation .= '.';
  1170. }
  1171. //----------------------
  1172. // Research Support, Non-U.S. Gov't
  1173. //----------------------
  1174. elseif ($pub_type == "Research Support, Non-U.S. Gov't") {
  1175. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1176. if ($pub['Journal Name']) {
  1177. $citation .= $pub['Journal Name'] . '. ';
  1178. }
  1179. if ($pub['Publication Date']) {
  1180. $citation .= $pub['Publication Date'];
  1181. }
  1182. elseif ($pub['Year']) {
  1183. $citation .= $pub['Year'];
  1184. }
  1185. $citation .= '.';
  1186. }
  1187. //----------------------
  1188. // Letter
  1189. //----------------------
  1190. elseif ($pub_type == 'Letter') {
  1191. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1192. if ($pub['Journal Name']) {
  1193. $citation .= $pub['Journal Name'] . '. ';
  1194. }
  1195. elseif ($pub['Journal Abbreviation']) {
  1196. $citation .= $pub['Journal Abbreviation'] . '. ';
  1197. }
  1198. elseif ($pub['Series Name']) {
  1199. $citation .= $pub['Series Name'] . '. ';
  1200. }
  1201. elseif ($pub['Series Abbreviation']) {
  1202. $citation .= $pub['Series Abbreviation'] . '. ';
  1203. }
  1204. if ($pub['Publication Date']) {
  1205. $citation .= $pub['Publication Date'];
  1206. }
  1207. elseif ($pub['Year']) {
  1208. $citation .= $pub['Year'];
  1209. }
  1210. if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
  1211. $citation .= '; ';
  1212. }
  1213. if ($pub['Volume']) {
  1214. $citation .= $pub['Volume'];
  1215. }
  1216. if ($pub['Issue']) {
  1217. $citation .= '(' . $pub['Issue'] . ')';
  1218. }
  1219. if ($pub['Pages']) {
  1220. if($pub['Volume']) {
  1221. $citation .= ':';
  1222. }
  1223. $citation .= $pub['Pages'];
  1224. }
  1225. $citation .= '.';
  1226. }
  1227. //----------------------
  1228. // Book
  1229. //----------------------
  1230. elseif ($pub_type == 'Book') {
  1231. }
  1232. //----------------------
  1233. // Book Chapter
  1234. //----------------------
  1235. elseif ($pub_type == 'Book Chapter') {
  1236. }
  1237. //----------------------
  1238. // Conference Proceedings
  1239. //----------------------
  1240. elseif ($pub_type == 'Conference Proceedings') {
  1241. $citation = $pub['Authors'] . '. ' . $pub['Title'] . '. ';
  1242. if ($pub['Conference Name']) {
  1243. $citation .= $pub['Conference Name'] . '. ';
  1244. }
  1245. elseif ($pub['Series Name']) {
  1246. $citation .= $pub['Series Name'] . '. ';
  1247. }
  1248. elseif ($pub['Series Abbreviation']) {
  1249. $citation .= $pub['Series Abbreviation'] . '. ';
  1250. }
  1251. if ($pub['Publication Date']) {
  1252. $citation .= $pub['Publication Date'];
  1253. }
  1254. elseif ($pub['Year']) {
  1255. $citation .= $pub['Year'];
  1256. }
  1257. if ($pub['Volume'] or $pub['Issue'] or $pub['Pages']) {
  1258. $citation .= '; ';
  1259. }
  1260. if ($pub['Volume']) {
  1261. $citation .= $pub['Volume'];
  1262. }
  1263. if ($pub['Issue']) {
  1264. $citation .= '(' . $pub['Issue'] . ')';
  1265. }
  1266. if ($pub['Pages']) {
  1267. if($pub['Volume']) {
  1268. $citation .= ':';
  1269. }
  1270. $citation .= $pub['Pages'];
  1271. }
  1272. $citation .= '.';
  1273. }
  1274. return $citation;
  1275. }