print_r($identifiers, TRUE))
);
}
elseif (empty($identifiers)) {
tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
"chado_get_publication: You did not pass in anything to identify the publication you want. The identifier
is expected to be an array with the key matching a column name in the pub table
(ie: pub_id or name). You passed in %identifier.",
array('%identifier'=> print_r($identifiers, TRUE))
);
}
// If one of the identifiers is property then use chado_get_record_with_property()
if (array_key_exists('property', $identifiers)) {
$property = $identifiers['property'];
unset($identifiers['property']);
$pub = chado_get_record_with_property(
array('table' => 'pub', 'base_records' => $identifiers),
array('type_name' => $property),
$options
);
}
elseif (array_key_exists('dbxref', $identifiers)) {
if(preg_match('/^(.*?):(.*?)$/', $identifiers['dbxref'], $matches)) {
$dbname = $matches[1];
$accession = $matches[2];
// First make sure the dbxref is present.
$values = array(
'accession' => $accession,
'db_id' => array(
'name' => $dbname
),
);
$dbxref = chado_select_record('dbxref', array('dbxref_id'), $values);
if (count($dbxref) == 0) {
return FALSE;
}
$pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), array('dbxref_id' => $dbxref[0]->dbxref_id));
if (count($pub_dbxref) == 0) {
return FALSE;
}
$pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
}
else {
tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
"chado_get_publication: The dbxref identifier is not correctly formatted.",
array('%identifier'=> print_r($identifiers, TRUE))
);
}
}
elseif (array_key_exists('dbxref_id', $identifiers)) {
// first get the pub_dbxref record
$values = array('dbxref_id' => $identifiers['dbxref_id']);
$pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
// now get the pub
if (count($pub_dbxref) > 0) {
$pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
}
else {
return FALSE;
}
}
// Else we have a simple case and we can just use chado_generate_var to get the pub
else {
// Try to get the pub
$pub = chado_generate_var('pub', $identifiers, $options);
}
// Ensure the pub is singular. If it's an array then it is not singular
if (is_array($pub)) {
tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
"chado_get_publication: The identifiers did not find a single unique record. Identifiers passed: %identifier.",
array('%identifier'=> print_r($identifiers, TRUE))
);
}
// Report an error if $pub is FALSE since then chado_generate_var has failed
elseif ($pub === FALSE) {
tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
"chado_get_publication: Could not find a publication using the identifiers
provided. Check that the identifiers are correct. Identifiers passed: %identifier.",
array('%identifier'=> print_r($identifiers, TRUE))
);
}
// Else, as far we know, everything is fine so give them their pub :)
else {
return $pub;
}
}
/**
* The publication table of Chado only has a unique constraint for the
* uniquename of the publiation, but in reality a publication can be considered
* unique by a combination of the title, publication type, published year and
* series name (e.g. journal name or conference name). The site administrator
* can configure how publications are determined to be unique. This function
* uses the configuration specified by the administrator to look for publications
* that match the details specified by the $pub_details argument
* and indicates if one ore more publications match the criteria.
*
* @param $pub_details
* An associative array with details about the publications. The expected keys
* are:
* 'Title': The title of the publication
* 'Year': The published year of the publication
* 'Publication Type': An array of publication types. A publication can have more than one type.
* 'Series Name': The series name of the publication
* 'Journal Name': An alternative to 'Series Name'
* 'Conference Name': An alternative to 'Series Name'
* 'Citation': The publication citation (this is the value saved in the pub.uniquename field and must be unique)
* If this key is present it will also be checked
* 'Publication Dbxref': A database cross reference of the form DB:ACCESSION where DB is the name
* of the database and ACCESSION is the unique identifier (e.g PMID:3483139)
*
* @return
* An array containing the pub_id's of matching publications. Returns an
* empty array if no pubs match
*
* @ingroup tripal_pub_api
*/
function tripal_publication_exists($pub_details) {
// first try to find the publication using the accession number if that key exists in the details array
if (array_key_exists('Publication Dbxref', $pub_details)) {
$pub = tripal_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
if($pub) {
return array($pub->pub_id);
}
}
// make sure the citation is unique
if (array_key_exists('Citation', $pub_details)) {
$pub = tripal_get_publication(array('uniquename' => $pub_details['Citation']));
if($pub) {
return array($pub->pub_id);
}
}
// get the publication type (use the first publication type)
if (array_key_exists('Publication Type', $pub_details)) {
$type_name = '';
if(is_array($pub_details['Publication Type'])) {
$type_name = $pub_details['Publication Type'][0];
}
else {
$type_name = $pub_details['Publication Type'];
}
$identifiers = array(
'name' => $type_name,
'cv_id' => array(
'name' => 'tripal_pub',
),
);
$pub_type = tripal_get_cvterm($identifiers);
}
else {
tripal_report_error('tripal_pub', TRIPAL_ERROR,
"tripal_publication_exists(): The Publication Type is a " .
"required property but is missing", array());
return array();
}
if (!$pub_type) {
tripal_report_error('tripal_pub', TRIPAL_ERROR,
"tripal_publication_exists(): Cannot find publication type: '%type'",
array('%type' => $pub_details['Publication Type'][0]));
return array();
}
// get the series name. The pub.series_name field is only 255 chars so we must truncate to be safe
$series_name = '';
if (array_key_exists('Series Name', $pub_details)) {
$series_name = substr($pub_details['Series Name'], 0, 255);
}
if (array_key_exists('Journal Name', $pub_details)) {
$series_name = substr($pub_details['Journal Name'], 0, 255);
}
if (array_key_exists('Conference Name', $pub_details)) {
$series_name = substr($pub_details['Conference Name'], 0, 255);
}
// make sure the publication is unique using the prefereed import duplication check
$import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
$pubs = array();
switch ($import_dups_check) {
case 'title_year':
$identifiers = array(
'title' => $pub_details['Title'],
'pyear' => $pub_details['Year']
);
$pubs = chado_select_record('pub', array('pub_id'), $identifiers);
break;
case 'title_year_type':
$identifiers = array(
'title' => $pub_details['Title'],
'pyear' => $pub_details['Year'],
'type_id' => $pub_type->cvterm_id,
);
$pubs = chado_select_record('pub', array('pub_id'), $identifiers);
break;
case 'title_year_media':
$identifiers = array(
'title' => $pub_details['Title'],
'pyear' => $pub_details['Year'],
'series_name' => $series_name,
);
$pubs = chado_select_record('pub', array('pub_id'), $identifiers);
break;
}
$return = array();
foreach ($pubs as $pub) {
$return[] = $pub->pub_id;
}
return $return;
}
/**
* Used for autocomplete in forms for identifying for publications.
*
* @param $field
* The field in the publication to search on.
* @param $string
* The string to search for
*
* @return
* A json array of terms that begin with the provided string
*
* @ingroup tripal_chado_api
*/
function tripal_autocomplete_pub($string = '') {
$items = array();
$sql = "
SELECT pub_id, title, uniquename
FROM {pub}
WHERE lower(title) like lower(:str)
ORDER by title
LIMIT 25 OFFSET 0
";
$pubs = chado_query($sql, array(':str' => $string . '%'));
while ($pub = $pubs->fetchObject()) {
$items[$pub->uniquename] = $pub->uniquename;
}
drupal_json_output($items);
}
/**
* Imports a singe publication specified by a remote database cross reference.
*
* @param $pub_dbxref
* The unique database ID for the record to update. This value must
* be of the format DB_NAME:ACCESSION where DB_NAME is the name of the
* database (e.g. PMID or AGL) and the ACCESSION is the unique identifier
* for the record in the database.
* @param $do_contact
* Set to TRUE if authors should automatically have a contact record added
* to Chado.
* @param $do_update
* If set to TRUE then the publication will be updated if it already exists
* in the database.
*
* @ingroup tripal_pub
*/
function tripal_import_pub_by_dbxref($pub_dbxref, $do_contact = FALSE, $do_update) {
$num_to_retrieve = 1;
$pager_id = 0;
$page = 0;
$num_pubs = 0;
print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
"If the load fails or is terminated prematurely then the entire set of \n" .
"insertions/updates is rolled back and will not be found in the database\n\n";
$transaction = db_transaction();
try {
if(preg_match('/^(.*?):(.*?)$/', $pub_dbxref, $matches)) {
$dbname = $matches[1];
$accession = $matches[2];
$criteria = array(
'num_criteria' => 1,
'remote_db' => $dbname,
'criteria' => array(
'1' => array(
'search_terms' => "$dbname:$accession",
'scope' => 'id',
'operation' => '',
'is_phrase' => 0,
),
),
);
$remote_db = $criteria['remote_db'];
$results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
$pubs = $results['pubs'];
$search_str = $results['search_str'];
$total_records = $results['total_records'];
$pub_id = tripal_pub_add_publications($pubs, $do_contact, $do_update);
}
// For backwards compatibility check to see if the legacy pub module
// is enabled. If so, then sync the nodes.
if (module_exists('tripal_pub')) {
// sync the newly added publications with Drupal
print "Syncing publications with Drupal...\n";
chado_node_sync_records('pub');
// if any of the importers wanted to create contacts from the authors then sync them
if($do_contact) {
print "Syncing contacts with Drupal...\n";
chado_node_sync_records('contact');
}
}
}
catch (Exception $e) {
$transaction->rollback();
print "\n"; // make sure we start errors on new line
watchdog_exception('T_pub_import', $e);
print "FAILED: Rolling back database changes...\n";
return;
}
print "Done.\n";
}
/**
* Imports all publications for all active import setups.
*
* @param $report_email
* A list of email address, separated by commas, that should be notified
* once importing has completed
* @param $do_update
* If set to TRUE then publications that already exist in the Chado database
* will be updated, whereas if FALSE only new publications will be added
*
* @ingroup tripal_pub
*/
function tripal_execute_active_pub_importers($report_email = FALSE, $do_update = FALSE) {
$num_to_retrieve = 100;
$page = 0;
print "\nNOTE: Loading of publications is performed using a database transaction. \n" .
"If the load fails or is terminated prematurely then the entire set of \n" .
"insertions/updates is rolled back and will not be found in the database\n\n";
// start the transaction
$transaction = db_transaction();
try {
// get all of the loaders
$args = array();
$sql = "SELECT * FROM {tripal_pub_import} WHERE disabled = 0 ";
$results = db_query($sql, $args);
$do_contact = FALSE;
$reports = array();
foreach ($results as $import) {
$page = 0;
print "Executing importer: '" . $import->name . "'\n";
// keep track if any of the importers want to create contacts from authors
if ($import->do_contact == 1) {
$do_contact = TRUE;
}
$criteria = unserialize($import->criteria);
$remote_db = $criteria['remote_db'];
do {
// retrieve the pubs for this page. We'll retreive 100 at a time
$results = tripal_get_remote_pubs($remote_db, $criteria, $num_to_retrieve, $page);
$pubs = $results['pubs'];
$reports[$import->name] = tripal_pub_add_publications($pubs, $import->do_contact, $do_update);
$page++;
}
// continue looping until we have a $pubs array that does not have
// our requested numer of records. This means we've hit the end
while (count($pubs) == $num_to_retrieve);
}
// sync the newly added publications with Drupal. If the user
// requested a report then we don't want to print any syncing information
// so pass 'FALSE' to the sync call
// For backwards compatibility check to see if the legacy pub module
// is enabled. If so, then sync the nodes.
if (module_exists('tripal_pub')) {
print "Syncing publications with Drupal...\n";
chado_node_sync_records('pub');
}
// iterate through each of the reports and generate a final report with HTML links
$HTML_report = '';
if ($report_email) {
$HTML_report .= "";
global $base_url;
foreach ($reports as $importer => $report) {
$total = count($report['inserted']);
$HTML_report .= "$total new publications from importer: $importer