tripal_pub.api.inc 44 KB

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