tripal_pub.api.inc 50 KB

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