Browse Source

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

spficklin 11 years ago
parent
commit
be1ceb56a3

+ 137 - 51
tripal_pub/api/tripal_pub.api.inc

@@ -974,54 +974,87 @@ function tripal_pub_delete_property($pub_id, $property) {
 
 function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
 
-	$options = array('return_array' => 1);
-	 
+  $options = array('return_array' => 1);
+
   // ---------------------------------
   // get the publication
   // ---------------------------------
-	$values = array('pub_id' => $pub_id);
-	$pub = tripal_core_generate_chado_var($pub, $values);	
-	
-	// expand the title
-	$pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
-	$pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle', $options);
+  $values = array('pub_id' => $pub_id);
+  $pub = tripal_core_generate_chado_var('pub', $values);
+
+  // expand the title
+  $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
+  $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
+  $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
+  $pub_array = array();
+  if (trim($pub->title)) {
+    $pub_array['Title'] = $pub->title;
+  }
+  if (trim($pub->volumetitle)) {
+    $pub_array['Volume Title'] = $pub->volumetitle;
+  }
+  if (trim($pub->volume)) {
+    $pub_array['Volume'] = $pub->volume;
+  }
+  if (trim($pub->series_name)) {
+    $pub_array['Series Name'] = $pub->series_name;
+  }
+  if (trim($pub->issue)) {
+    $pub_array['Issue'] = $pub->issue;
+  }
+  if (trim($pub->pyear)) {
+    $pub_array['Year'] = $pub->pyear;
+  }
+  if (trim($pub->pages)) {
+    $pub_array['Pages'] = $pub->pages;
+  }
+  if (trim($pub->miniref)) {
+    $pub_array['Mini Ref'] = $pub->miniref;
+  }
+  if (trim($pub->uniquename)) {
+    $pub_array['Uniquename'] = $pub->uniquename;
+  }
+  $pub_array['Publication Type'][] = $pub->type_id->name;
 
   // ---------------------------------
   // get the citation
   // ---------------------------------
-	$values = array(
-	  'pub_id' => $pub->pub_id, 
-	  'type_id' => array(
-	    'name' => 'Citation',
-	  ),
-	);
-	$citation = tripal_core_generate_chado_var('pubprop', $values); 
-	$citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value', $options);
-  if (count($citation > 1)) {
-    watchdog('tripal_pub', "Publication has multiple citations already: %pub_id", 
-      array('%pub_id' => $pubid), WATCHDOG_ERROR);
-    return FALSE;
-  }
-  elseif (count($citation) == 1 and $skip_existing == TRUE) {
-    // skip this publication, it already has a citation
-    return FALSE;
+  $values = array(
+    'pub_id' => $pub->pub_id, 
+    'type_id' => array(
+      'name' => 'Citation',
+    ),
+  );
+  $citation = tripal_core_generate_chado_var('pubprop', $values);
+  if ($citation) {
+    $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value', $options);
+    if (count($citation) > 1) {
+    	watchdog('tripal_pub', "Publication has multiple citations already: %pub_id",
+    	array('%pub_id' => $pubid), WATCHDOG_ERROR);
+    	return FALSE;
+    }
+    elseif (count($citation) == 1 and $skip_existing == TRUE) {
+    	// skip this publication, it already has a citation
+    	return FALSE;
+    }
   }  
-	
-	// ---------------------------------
-	// get the publication types
-	// ---------------------------------
+
+  // ---------------------------------
+  // get the publication types
+  // ---------------------------------
   $values = array(
     'pub_id' => $pub->pub_id, 
     'type_id' => array(
       'name' => 'Publication Type',
     ),
   );
-  $ptypes = tripal_core_generate_chado_var('pubprop', $values); 
-  $ptypes = tripal_core_expand_chado_vars($ptypes, 'field', 'pubprop.value', $options);
-  foreach ($ptypes as $ptype) {
-  	$pub['Publication Type'][] = $ptype->value;
+  $ptypes = tripal_core_generate_chado_var('pubprop', $values, $options);
+  if ($ptypes) {
+    $ptypes = tripal_core_expand_chado_vars($ptypes, 'field', 'pubprop.value', $options);
+    foreach ($ptypes as $ptype) {
+     $pub_array['Publication Type'][] = $ptype->value;
+    }
   }
- 
 
   // ---------------------------------
   // get the authors list
@@ -1032,22 +1065,68 @@ function tripal_pub_get_publication_array($pub_id, $skip_existing = TRUE) {
       'name' => 'Authors',
     ),
   );
-  $authors = tripal_core_generate_chado_var('pubprop', $values); 
+  $authors = tripal_core_generate_chado_var('pubprop', $values);
   $authors = tripal_core_expand_chado_vars($authors, 'field', 'pubprop.value', $options);
-  if (count($authors > 1)) {
-    watchdog('tripal_pub', "Publication has multiple author lists. It should have only one list: %pub_id", 
-      array('%pub_id' => $pubid), WATCHDOG_ERROR);
-    return FALSE;
+  if (count($authors) > 1) {
+   watchdog('tripal_pub', "Publication has multiple author lists. It should have only one list: %pub_id",
+   array('%pub_id' => $pubid), WATCHDOG_ERROR);
+   return FALSE;
   }
-  else {
-    $pub['Authors'] = $authors->value;
+  else if (trim($authors->value)) {
+    $pub_array['Authors'] = $authors->value;
   }
-  
   // if there is no 'Author's property then try to retreive authors from the pubauthor table
+  else {
+    $sql = "SELECT string_agg(surname || ' ' || givennames, ', ') FROM {pubauthor} WHERE pub_id = %d GROUP BY pub_id";
+    $au = db_result(chado_query($sql));
+    if ($au) {
+    	$pub_array['Authors'] = $au;
+    }
+  }
 
-  
-  return $pub;
-  
+  //Get other props
+  $props = array(
+    'Journal Abbreviation',
+    'Elocation',
+    'Media Code',
+    'Conference Name',
+    'Keywords',
+    'Series Name',
+    'pISSN',
+    'Publication Date',
+    'Journal Code',
+    'Journal Alias',
+    'Journal Country',
+    'Published Location',
+    'Publication Model',
+    'Language Abbr',
+    'Alias',
+    'Publication Dbxref',
+    'Copyright',
+    'Abstract',
+    'Notes',
+    'Citation',
+    'Language',
+    'URL',
+    'eISSN',
+    'DOI',
+    'ISSN',
+    'Publication Code',
+    'Comments',
+    'Publisher',
+    'Media Alias',
+    'Original Title');
+  foreach ($props AS $prop) {
+    $sql = 
+      "SELECT value FROM {pubprop} 
+       WHERE type_id = (SELECT cvterm_id FROM {cvterm} WHERE name = '%s' AND cv_id = (SELECT cv_id FROM cv WHERE name = 'tripal_pub')) 
+       AND pub_id = %d";
+    $val = trim(db_result(chado_query($sql, $prop, $pub->pub_id)));
+    if ($val) {
+      $pub_array[$prop] =$val;
+    } 
+  }
+  return $pub_array;  
 }
 
 
@@ -1085,13 +1164,20 @@ function tripal_pub_create_citation($pub) {
 	// Therefore, we need to select the type that makes most sense for 
 	// construction of the citation. Here we'll iterate through them all
 	// and select the one that matches best.
-	if(is_array($pub['Publication Type'])) {		
+	if(is_array($pub['Publication Type'])) {
 	  foreach ($pub['Publication Type'] as $ptype) {
-	    if ($ptype == 'Journal Article' or
-	        $ptype == 'Book' or
-	        $ptype == 'Book Chapter' or
-	        $ptype == 'Conference Proceedings'){ 
-	      $pub_type = $ptype;   
+	    if ($ptype == 'Journal Article' ) {
+	      $pub_type = $ptype;
+	      break;
+	    } else if ($ptype == 'Conference Proceedings'){ 
+	      $pub_type = $ptype;
+	      break;
+	    } else if ($ptype == 'Book') {
+	      $pub_type = $ptype;
+	      break;
+	    } else if ($ptype == 'Book Chapter') {
+	      $pub_type = $ptype;
+	      break;
 	    }
 	  }
 	  if (!$pub_type) {
@@ -1103,7 +1189,7 @@ function tripal_pub_create_citation($pub) {
 	else {
 	  $pub_type = $pub['Publication Type'];
 	}		
-	
+  print "[$pub_type]\n";
 	//----------------------
   // Journal Article
   //----------------------

+ 6 - 4
tripal_pub/includes/importers/AGL.inc

@@ -2,7 +2,8 @@
 /**
  *
  */
-function tripal_pub_remote_alter_form_AGL(&$form, $form_state) {
+function tripal_pub_remote_alter_form_AGL($form, $form_state) {
+
   $num_criteria = $form['num_criteria']['#default_value'];
 
   // So far we haven't been able to get AGL to filter results to only
@@ -15,12 +16,12 @@ function tripal_pub_remote_alter_form_AGL(&$form, $form_state) {
   for($i = 1; $i <= $num_criteria; $i++) {
     unset($form['criteria'][$i]["scope-$i"]['#options']['journal']);
   }
-
+  return $form;
 }
 /**
  *
  */
-function tripal_pub_remote_validate_form_AGL(&$form, $form_state) {
+function tripal_pub_remote_validate_form_AGL($form, $form_state) {
   $days =  trim($form_state['values']["days"]);
   $num_criteria = $form['num_criteria']['#default_value'];
 
@@ -42,6 +43,7 @@ function tripal_pub_remote_validate_form_AGL(&$form, $form_state) {
       form_set_error("search_terms-$i", "Unfortuantely, the AGL importer can only support a single accession at a time. Please remove the others.");
     }
   }
+  return $form;
 }
 /**
  *
@@ -872,4 +874,4 @@ function tripal_pub_remote_search_AGL_get_author($xml, $ind1) {
     }
   }
   return $author;
-}
+}

+ 6 - 3
tripal_pub/includes/importers/PMID.inc

@@ -9,7 +9,7 @@
 /**
  *
  */
-function tripal_pub_remote_alter_form_PMID(&$form, $form_state) {
+function tripal_pub_remote_alter_form_PMID($form, $form_state) {
   $num_criteria = $form['num_criteria']['#default_value'];
 
   // PubMed doesn't have an 'Abstract' field, so we need to convert the criteria
@@ -17,11 +17,13 @@ function tripal_pub_remote_alter_form_PMID(&$form, $form_state) {
   for($i = 1; $i <= $num_criteria; $i++) {
     $form['criteria'][$i]["scope-$i"]['#options']['abstract'] = 'Abstract/Title';
   }
+
+  return $form;
 }
 /**
  *
  */
-function tripal_pub_remote_validate_form_PMID(&$form, $form_state) {
+function tripal_pub_remote_validate_form_PMID($form, $form_state) {
   $num_criteria = $form['num_criteria']['#default_value'];
 
   for ($i = 1; $i <= $num_criteria; $i++) {
@@ -31,6 +33,7 @@ function tripal_pub_remote_validate_form_PMID(&$form, $form_state) {
       form_set_error("search_terms-$i", "The PubMed accession must be a numeric value, prefixed with 'PMID:' (e.g. PMID:23024789).");
     }
   }
+  return $form;
 }
 /**
  *
@@ -896,4 +899,4 @@ function tripal_pub_remote_search_get_language($lang_abbr) {
     'wel' => 'Welsh',
   );
   return $languages[strtolower($lang_abbr)];
-}
+}

+ 82 - 0
tripal_pub/includes/pub_citation.inc

@@ -0,0 +1,82 @@
+<?php
+/*
+ * The admin form for submitting job to create citations
+*/
+function tripal_pub_citation_form($form_state) {
+
+  $form['create_all'] = array(
+    '#type' => 'item',
+    '#value' => t('Submit a job to create citation for the publications in chado. '),
+  );
+  
+  $form['options'] = array(
+    '#type' => 'radios',
+    '#options' => 
+      array('all' => 'Create citation for all publications. Replace the existing citation if it exists.', 
+                'new' => 'Create citation for publication only if it does not already have one.'),
+    '#default_value' => 'all'
+  );
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#weight' => 10,
+    '#value' => t('Submit')
+  );
+
+  return $form;
+}
+
+/*
+ * Submit form. Create Tripal job for citations
+ */
+
+function tripal_pub_citation_form_submit(&$form_state) {
+  $options [0] = $form_state['options']['#value'];
+  tripal_add_job("Create citations ($options[0])", 'tripal_pub',
+       'tripal_pub_create_citations', $options, $user->uid);
+}
+
+/*
+ * Launch the Tripal job to generate citations
+ */
+function tripal_pub_create_citations ($options) {
+  $skip_existing = TRUE;
+  $citation_type_id = db_result(chado_query("SELECT cvterm_id FROM {cvterm} WHERE name = 'Citation' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal_pub')"));
+  // Create and replace citation for all pubs
+  if ($options == 'all') {
+  	$sql = "SELECT pub_id FROM {pub} P WHERE pub_id <> 1";
+    $skip_existing = FALSE;
+  // Create citation for pubs that don't already have one
+  } else if ($options == 'new') {
+  	$sql = "SELECT pub_id FROM {pub} P WHERE (SELECT value FROM {pubprop} PB WHERE type_id = $citation_type_id AND P.pub_id = PB.pub_id AND rank = 0) IS NULL AND  pub_id <> 1";
+  	$skip_existing = TRUE;
+  }
+
+  $result = chado_query($sql);
+  $counter_updated = 0;
+  $counter_generated = 0;
+  while ($pub = db_fetch_object($result)) {
+    $pub_arr = tripal_pub_get_publication_array($pub->pub_id, $skip_existing);
+    if ($pub_arr) {
+      $citation = tripal_pub_create_citation ($pub_arr);
+      print $citation . "\n\n";
+      // Replace if citation exists. This condition is never TRUE if $skip_existing is TRUE
+      if ($pub_arr['Citation']) {
+        $sql = 
+            "UPDATE {pubprop} 
+             SET value = '%s' 
+             WHERE pub_id = %d 
+             AND type_id = %d
+             AND rank = %d";
+        chado_query($sql, $citation, $pub->pub_id, $citation_type_id, 0);
+        $counter_updated ++;
+      // Generate a new citation
+      } else {
+      	$sql = "INSERT INTO {pubprop} (pub_id, type_id, value, rank) VALUES (%d, %d, '%s', %d)";
+      	chado_query($sql, $pub->pub_id, $citation_type_id, $citation, 0);
+      	$counter_generated ++;
+      }
+    }
+  }
+  print "$counter_generated citations generated. $counter_updated citations updated.\n";
+}

+ 3 - 3
tripal_pub/includes/pub_importers.inc

@@ -431,7 +431,7 @@ function tripal_pub_importer_setup_form(&$form_state = NULL, $pub_import_id = NU
 
     // allow the selected remote database to make changes to the form if needed
     $callback = "tripal_pub_remote_alter_form_$remote_db";
-    call_user_func($callback, $form, $form_state);
+    $form = call_user_func($callback, $form, $form_state);
 
     return $form;
 }
@@ -467,7 +467,7 @@ function tripal_pub_importer_setup_form_validate($form, &$form_state) {
   }
   // allow the selected remote database to validate any changes to the form if needed
   $callback = "tripal_pub_remote_validate_form_$remote_db";
-  call_user_func($callback, $form, $form_state);
+  $form = call_user_func($callback, $form, $form_state);
 }
 
 /**
@@ -623,4 +623,4 @@ function tripal_pub_importer_setup_page_update_criteria($action, $i) {
       'settings' => $settings,
   )
   );
-}
+}

+ 9 - 0
tripal_pub/tripal_pub.module

@@ -6,6 +6,7 @@ require_once "includes/pub_sync.inc";
 require_once "includes/pub_form.inc";
 require_once "includes/pub_importers.inc";
 require_once "includes/pub_search.inc";
+require_once "includes/pub_citation.inc";
 require_once "includes/importers/PMID.inc";
 require_once "includes/importers/AGL.inc";
 /**
@@ -174,6 +175,14 @@ function tripal_pub_menu() {
     'type ' => MENU_CALLBACK,
   );
 
+  $items['admin/tripal/tripal_pub/citation'] = array(
+      'title' => ' Create Citations',
+      'description' => 'Create citations in Chado with Drupal',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_pub_citation_form'),
+      'access arguments' => array('administer tripal pubs'),
+      'type' => MENU_NORMAL_ITEM,
+  );
 
   return $items;
 }