Pārlūkot izejas kodu

Continued development on contact and pub modules

spficklin 12 gadi atpakaļ
vecāks
revīzija
4cce871896

+ 69 - 0
tripal_contact/api/tripal_contact.api.inc

@@ -87,3 +87,72 @@ function tripal_contact_update_property($contact_id, $property, $value, $insert_
 function tripal_contact_delete_property($contact_id, $property) {
   return tripal_core_delete_property('contact', $contact_id, $property, 'tripal_contact');
 }
+
+/**
+ * Adds a contact to the Chado contact table
+ * 
+ * @param $name
+ *   The name of the contact
+ * @param $description
+ *   Text describing the contact
+ * @param $type
+ *   The type of contact.  Must be a term in the tripal_contact vocabulary
+ * @param $properties
+ *   An associative array containing a list of key value pairs for the properites.
+ *   The key's must be valid terms in the tripal_contact vocabulary (e.g. Affiliation,
+ *   Address, etc).
+ *   
+ * @return
+ *   On success, an array is returned containing the fields of the contact 
+ *   record including the newly added contact_id. On failure, FALSE is 
+ *   returned
+ *   
+ * @ingroup tripal_contact_api
+ * 
+ */
+function tripal_contact_add_contact($name, $description, $type, $properties) {
+
+  // check to see if this contact name already exists.    
+  $values =  array('name' => $name);
+  $options = array('statement_name' => 'sel_contact_na');
+  $contact = tripal_core_chado_select('contact', array('contact_id'), $values, $options);
+  
+  if (count($contact) == 0) {    
+    $cvterm = tripal_cv_get_cvterm_by_name($type, NULL, 'tripal_contact');
+    if (!$cvterm) {
+      watchdog('tripal_contact',"Cannot find contact type '%type'",
+        array('%type' => $type), WATCHDOG_ERROR);
+      return FALSE;  
+    }
+    $values =  array(
+      'name' => $name,
+      'description' => '',
+      'type_id' => $cvterm->cvterm_id,
+    );
+    $options = array('statement_name' => 'ins_contact_nadety');
+    $contact = tripal_core_chado_insert('contact', $values, $options);
+    if (!$contact) {
+      watchdog('tripal_contact','Could not add the contact', array(), WATCHDOG_ERROR);
+      return FALSE; 
+    }
+  }
+  else {
+    $contact = (array) $contact[0];
+  }
+ 
+  // add the description property. We don't store this in the contact.description
+  // field because it is only 255 characters long and may not be enough  
+  if ($description) {
+    tripal_contact_insert_property($contact['contact_id'], 'contact_description', $description, TRUE);
+  }
+    
+  // add in the other properties provided
+  foreach ($properties as $key => $value) {
+    $success = tripal_contact_insert_property($contact['contact_id'], $key,$value, TRUE);
+    if (!$success) {
+      watchdog('tripal_contact',"Could not add the contact property '%prop'", array('%prop' => $key), WATCHDOG_ERROR);
+      return FALSE;  
+    }  
+  }
+  return $contact; 
+}

+ 90 - 0
tripal_contact/includes/contact_sync.inc

@@ -0,0 +1,90 @@
+<?php
+/*
+ * 
+ */
+function tripal_contact_sync_form() {
+
+  $form['sync_all'] = array(
+    '#type' => 'item',
+    '#value' => t('Syncing a contact will create a Drupal page for every contact record in the Chado database. Click the button below to sync all contacts in Chado that currently are not already synced with Drupal.'),
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#weight' => 10,
+    '#value' => t('Sync contacts')
+  );
+
+  return $form;
+}
+
+
+/*
+ * 
+ */
+function tripal_contact_sync_form_submit($form, $form_state) {
+  global $user;    //needed to make the current users details available so access of user id is available
+  $job_args = array();
+  $job_id = tripal_add_job('Sync contacts', 'tripal_contact', 'tripal_contact_sync_contacts', $job_args, $user->uid);
+
+}
+/**
+ *
+ *
+ * @ingroup tripal_contact
+ */
+function tripal_contact_sync_contacts($job_id = NULL) {
+  
+  // get the list of contacts that have not yet been synced
+  // and ignore the default 'NULL' contact. we don't want
+  // to sync that one.
+  $sql = "
+    SELECT C.*
+    FROM chado.contact C
+      LEFT JOIN {chado_contact} CP ON CP.contact_id = C.contact_id
+    WHERE CP.contact_id IS NULL
+  ";
+  $results = db_query($sql);
+
+  while ($contact = db_fetch_object($results)) {
+    $node = tripal_contact_sync_contact($contact);
+  }
+}
+/**
+ * @param $contact
+ *   A contactlication object
+ *   
+ * @return
+ *   A new Drupal node object on success. FALSE on failure
+ *   
+ * @ingroup tripal_contact
+ */
+function tripal_contact_sync_contact($contact) {
+  global $user;
+  
+  $new_node = new stdClass();
+  $new_node->contact_id = $contact->contact_id;
+  $new_node->type = 'chado_contact';
+  $new_node->title = $contact->name;
+  $new_node->description = $contact->description;
+  $new_node->type_id = $contact->type_id;
+
+  node_validate($new_node);
+  $errors = form_get_errors();
+  if (!$errors) {
+    $node = node_submit($new_node);
+    node_save($node);
+    if ($node->nid) {
+      print "Added " . $contact->contact_id . "\n";      
+    }
+    else {
+      print "ERROR: Unable to create " . $contact->name . "\n";
+      return FALSE;
+    }
+  }
+  else {
+    print "ERROR: Unable to create " . $contact->name . "\n" . print_r($errors, TRUE) . "\n";
+    return FALSE;
+  }
+  return $node; 
+}

+ 1 - 0
tripal_contact/includes/tripal_contact.admin.inc

@@ -0,0 +1 @@
+<?php

+ 13 - 2
tripal_contact/tripal_contact.module

@@ -10,6 +10,8 @@
  */
 
 require('api/tripal_contact.api.inc');
+require('includes/contact_sync.inc');
+require('includes/tripal_contact.admin.inc');
 
 /**
  * @defgroup tripal_contact Contact Module
@@ -90,7 +92,16 @@ function tripal_contact_menu() {
     'access arguments' => array('administer tripal contacts'),
     'type' => MENU_NORMAL_ITEM
   );
-
+  
+  $items['admin/tripal/tripal_contact/sync'] = array(
+    'title' => ' Sync Contacts',
+    'description' => 'Sync contacts in Chado with Drupal',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_contact_sync_form'),
+    'access arguments' => array('administer tripal contacts'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  
   return $items;
 }
   
@@ -328,7 +339,7 @@ function chado_contact_insert($node) {
    
     // add the description property
     tripal_contact_insert_property($contact['contact_id'], 'contact_description',
-      $node->contact_description);
+      $node->contact_description, TRUE);
     
     // make sure the entry for this contact doesn't already exist in the chado_contact table
     // if it doesn't exist then we want to add it.

+ 9 - 6
tripal_cv/api/tripal_cv.api.inc

@@ -194,11 +194,11 @@ function tripal_cv_get_cv_options() {
  *   the name of the CV
  *
  * @return
- *   cvterm object
+ *   cvterm array or FALSE on error
  *
  * @ingroup tripal_cv_api
  */
-function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
+function tripal_cv_get_cvterm_by_name($name, $cv_id = NULL, $cv_name = 'tripal') {
 
   if ($cv_id) {
     $values = array(
@@ -206,7 +206,7 @@ function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
        'cv_id' => $cv_id,
     );
     $options = array(
-      'statement_name' => 'sel_cvterm_nacv', 
+      'statement_name' => 'sel_cvterm_nacv',
       'case_insensitive_columns' => array('name')
     );
     $r = tripal_core_chado_select('cvterm', array('*'), $values, $options);
@@ -218,18 +218,19 @@ function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
         'name' => $cv_name,
       ),
     );    
+
     $options = array(
-      'statement_name' => 'sel_cvterm_nacv', 
+      'statement_name' => 'sel_cvterm_nacv',
       'case_insensitive_columns' => array('name')
     );
-    $r = tripal_core_chado_select('cvterm', array('*'), $values, $options);  
+    $r = tripal_core_chado_select('cvterm', array('*'), $values, $options);      
   }
   else {
     $values = array(
       'name' => $name,
     );
     $options = array(
-      'statement_name' => 'sel_cvterm_na', 
+      'statement_name' => 'sel_cvterm_na',
       'case_insensitive_columns' => array('name')
     );
     $r = tripal_core_chado_select('cvterm', array('*'), $values, $options);
@@ -239,6 +240,8 @@ function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
     return FALSE;
   }
   if (count($r) > 1) {
+    watchdog('tripal_cv', "Cannot find a unique term for the term '%name' in the vocabulary '%cv'. Multiple entries exist for this name",
+      array('%name' => $name, '%cv' => $cv_name ? $cv_name : $cv_id), WATCHDOG_ERROR);
     return FALSE;
   }
   return $r[0];

+ 78 - 6
tripal_pub/api/tripal_pub.api.inc

@@ -74,7 +74,7 @@ function tripal_pub_get_remote_search_results($remote_db, $search_array,
  * @ingroup tripal_pub_api
  */
 function tripal_pub_import_publications() {
-  $num_to_retrieve = 10;
+  $num_to_retrieve = 100;
   $pager_id = 0;
   $page = 0;
   $num_pubs = 0;
@@ -109,7 +109,10 @@ function tripal_pub_import_publications() {
        // now add the publications
        foreach ($pubs as $pub) {
                
+         // add the publication to Chado and sync it with Chado
          $pub_id = tripal_pub_add_publication($pub);
+         
+         // add the publication cross reference (e.g. to PubMed)
          if ($pub_id) {         
            $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $pub);
          }                                      
@@ -124,6 +127,12 @@ function tripal_pub_import_publications() {
      while (count($pubs) == $num_to_retrieve);    
   }
   
+  // sync the newly added publications with Drupal
+  print "Syncing publications with Drupal...\n";
+  tripal_pub_sync_pubs();
+  print "Syncing contacts with Drupal...\n";
+  tripal_contact_sync_contacts();
+  
   // transaction is complete
   tripal_db_commit_transaction();
   
@@ -243,8 +252,8 @@ function tripal_pub_add_publication($pub_details) {
        $cvterm = tripal_cv_get_cvterm_by_synonym($key, NULL, 'tripal_pub');
      }
      if (!$cvterm) {
-       watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", 
-         array('%prop' => $key), WATCHDOG_ERROR);
+       print_r($cvterm);
+       watchdog('tripal_pub', "Cannot find term: '%prop'. Skipping.", array('%prop' => $key), WATCHDOG_ERROR);
        continue;
      }
 
@@ -275,10 +284,9 @@ function tripal_pub_add_publication($pub_details) {
        $success = tripal_core_insert_property('pub', $pub_id, $key, 'tripal_pub', $value, TRUE);
      }
      if (!$success) { 
-       print_r($success); 
        watchdog('tripal_pub', "Cannot add property '%prop' to publication. Skipping.", 
          array('%prop' => $key), WATCHDOG_ERROR);
-         continue;
+       continue;
      }
   }
   
@@ -289,5 +297,69 @@ function tripal_pub_add_publication($pub_details) {
  * 
  */
 function tripal_pub_add_authors($pub_id, $authors) {
-  print_r($authors);
+  $rank = 0;  
+  
+  // first remove any of the existing pubauthor entires
+  $sql = "DELETE FROM {pubauthor} WHERE pub_id = %d";
+  chado_query($sql, $pub_id);
+  
+  // iterate through the authors and add them to the pubauthors and contact 
+  // tables of chado, then link them through the custom pubauthors_contact table
+  foreach ($authors as $author) {    
+    // skip invalid author entires
+    if ($author['valid'] == 'N') {
+      continue;
+    }
+    // remove the 'valid' property as we don't have a CV term for it
+    unset($author['valid']);
+    
+    // construct the contact.name field using the author information
+    $name = '';
+    $type = 'Person';
+    if ($author['Given Name']) {
+      $name .= $author['Given Name'];
+    }
+    if ($author['Surname']) { 
+      $name .= ' ' . $author['Surname'];
+    }
+    if ($author['Suffix']) { 
+      $name .= ' ' . $author['Suffix'];
+    }
+    if ($author['Collective']) {
+      $name = $author['Collective'];
+      $type = 'Collective';
+    }
+    $name = trim($name);    
+        
+    // Add the contact 
+    $contact = tripal_contact_add_contact($name, '', $type, $author);
+    
+    // add an entry to the pubauthors table
+    $values = array(
+      'pub_id' => $pub_id,
+      'rank' => $rank,
+      'surname' => $author['Surname'] ? $author['Surname'] : $author['Collective'],
+      'givennames' => $author['Given Name'],
+      'suffix' => $author['Suffix'],
+    );
+    $options = array('statement_name' => 'ins_pubauthor_idrasugisu');
+    $pubauthor = tripal_core_chado_insert('pubauthor', $values, $options);
+    
+    // if we have succesfully added the contact and the pubauthor entries then we want to
+    // link them together 
+    if ($contact and $pubauthor) {
+    
+      // link the pubauthor entry to the contact 
+      $values = array(
+        'pubauthor_id' => $pubauthor['pubauthor_id'],
+        'contact_id' => $contact['contact_id'],
+      );
+      $options = array('statement_name' => 'ins_pubauthorcontact_puco');
+      $pubauthor_contact = tripal_core_chado_insert('pubauthor_contact', $values, $options);
+      if (!$pubauthor_contact) {
+        watchdog('tripal_pub', "Cannot link pub authro and contact.", array(), WATCHDOG_ERROR);
+      }
+    }
+    $rank++;   
+  }
 }

+ 40 - 27
tripal_pub/includes/pub_sync.inc

@@ -34,10 +34,7 @@ function tripal_pub_sync_form_submit($form, $form_state) {
  * @ingroup tripal_pub
  */
 function tripal_pub_sync_pubs($job_id = NULL) {
-
-  global $user;
-  $page_content = '';
-
+  
   // get the list of pubs that have not yet been synced
   // and ignore the default 'NULL' pub. we don't want
   // to sync that one.
@@ -51,30 +48,46 @@ function tripal_pub_sync_pubs($job_id = NULL) {
 
 
   while ($pub = db_fetch_object($results)) {
+    $node = tripal_pub_sync_pub($pub);
+  }
+}
+/**
+ * @param $pub
+ *   A publication object
+ *   
+ * @return
+ *   A new Drupal node object on success. FALSE on failure
+ *   
+ * @ingroup tripal_pub
+ */
+function tripal_pub_sync_pub($pub) {
+  global $user;
+  
+  $new_node = new stdClass();
+  $new_node->pub_id = $pub->pub_id;
+  $new_node->type = 'chado_pub';
+  $new_node->uid = $user->uid;
+  $new_node->title = $pub->title;
+  $new_node->pyear = $pub->pyear;
+  $new_node->uniquename = $pub->uniquename;
+  $new_node->type_id = $pub->type_id;
 
-    $new_node = new stdClass();
-    $new_node->pub_id = $pub->pub_id;
-    $new_node->type = 'chado_pub';
-    $new_node->uid = $user->uid;
-    $new_node->title = $pub->title;
-    $new_node->pyear = $pub->pyear;
-    $new_node->uniquename = $pub->uniquename;
-    $new_node->type_id = $pub->type_id;
-
-    node_validate($new_node);
-    $errors = form_get_errors();
-    if (!$errors) {
-      $node = node_submit($new_node);
-      node_save($node);
-      if ($node->nid) {
-        print "Added " . $pub->pub_id . "\n";
-      }
-      else {
-        print "ERROR: Unable to create " . $pub->name . "\n";
-      }
+  node_validate($new_node);
+  $errors = form_get_errors();
+  if (!$errors) {
+    $node = node_submit($new_node);
+    node_save($node);
+    if ($node->nid) {
+      print "Added " . $pub->pub_id . "\n";      
     }
     else {
-      print "ERROR: Unable to create " . $pub->name . "\n" . print_r($errors, TRUE) . "\n";
-    }   
+      print "ERROR: Unable to create " . $pub->name . "\n";
+      return FALSE;
+    }
+  }
+  else {
+    print "ERROR: Unable to create " . $pub->name . "\n" . print_r($errors, TRUE) . "\n";
+    return FALSE;
   }
-}
+  return $node; 
+}

+ 1 - 1
tripal_pub/tripal_pub.module

@@ -69,7 +69,7 @@ function tripal_pub_menu() {
     'type' => MENU_NORMAL_ITEM
   );
   
-    $items['admin/tripal/tripal_pub/sync'] = array(
+  $items['admin/tripal/tripal_pub/sync'] = array(
     'title' => ' Sync Publications',
     'description' => 'Sync publications in Chado with Drupal',
     'page callback' => 'drupal_get_form',