瀏覽代碼

Achieved multiple instance for one organism, still need to parse analysis properly and more test.

Jiu9Shen 5 年之前
父節點
當前提交
4f045ca519

+ 14 - 1
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt.api.inc

@@ -92,7 +92,7 @@ function tripal_jbrowse_mgmt_get_instances($conditions = NULL) {
  */
 function tripal_jbrowse_mgmt_create_instance($data) {
   global $user;
-
+  dpm($data, '$data in tripal_jbrowse_mgmt_create_instance');
   // Check that required fields are provided.
   $required = ['organism_id', 'created_at'];
   foreach ($required as $key) {
@@ -111,6 +111,7 @@ function tripal_jbrowse_mgmt_create_instance($data) {
   return db_insert('tripal_jbrowse_mgmt_instances')->fields([
     'uid' => $user->uid,
     'organism_id' => $data['organism_id'],
+    'analysis_id' => $data['analysis_id'],
     'title' => $data['title'],
     'description' => isset($data['description']) ? $data['description'] : '',
     'created_at' => $data['created_at'],
@@ -282,6 +283,18 @@ function tripal_jbrowse_mgmt_get_organisms_list() {
     ->fetchAll();
 }
 
+/**
+ * parse analysis to obtain analysis_id
+ *
+ * @return mixed
+ */
+function tripal_jbrowse_mgmt_analysis_parse_4id($name_id) {
+  
+
+  return $analysis_id;
+}
+
+
 /**
  * Format the name of the organism to `Genus species (Common Name)`.
  *

+ 29 - 5
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_add.form.inc

@@ -61,6 +61,13 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     '#default_value' => ($edit_form) ? $instance->organism_id : NULL,
   ];
 
+  $form['analysis'] = [
+    '#title' => t('Analysis'),
+    '#description' => t('Select the analysis to which this instance will be related.'),
+    '#type' => 'textfield',
+    '#autocomplete_path' => 'admin/tripal/extension/tripal_jbrowse/management/instances/analysis/autocomplete',
+  ];
+
   $form['description'] = [
     '#title' => t('Description'),
     '#description' => t('Optional description for the instance.'),
@@ -170,6 +177,7 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
  * @param $form_state
  */
 function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
+  dpm($form_state, '$form_state in validation');
   $values = $form_state['values'];
   $organism = isset($values['organism']) ? $values['organism'] : NULL;
 
@@ -207,15 +215,28 @@ function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
     }
   }
 
+  //  if selected organism already exist, analysis_id is required
+  //  also make sure organism+analysis does not exist
   $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
-  if (!empty($instances)) {
-    if (!$edit_form OR ($edit_form AND $instances[0]->id != $instance_id)) {
+  dpm($instances, '$instances after tripal_jbrowse_mgmt_get_instances([\'organism_id\' => $organism]) ');
+  if (empty($values['analysis'])) {
+    if (!empty($instances)){
       form_set_error(
-        'organism',
-        'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
+        'analysis',
+        'A JBrowse instance for the selected organism already exists. Please choose one analysis for this instance.'
       );
     }
   }
+  else{
+    foreach ($instances as $instance){
+      if ($values['analysis'] == $instance->analysis_id){
+        form_set_error(
+          ['organism', 'analysis'],
+          'The analysis for selected organism is taken. Please choose another analysis/organism for this instance.'
+        );
+      }
+    }
+  }
 
   $organism = db_select('chado.organism', 'CO')
     ->fields('CO')
@@ -236,9 +257,10 @@ function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
  */
 function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
   global $user;
-
+  dpm($form_state, '$form_state in tripal_jbrowse_mgmt_add_form_submit');
   $values = $form_state['values'];
   $organism_id = $values['organism'];
+  $analysis_id = $values['analysis_id'];
   $description = isset($values['description']) ? $values['description'] : '';
 
   // Check if this is an add or edit form.
@@ -260,6 +282,7 @@ function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
 
     $data = [
       'organism_id' => $organism_id,
+      'analysis_id' => $analysis_id,
       'title' => $title,
       'description' => $description,
     ];
@@ -290,6 +313,7 @@ function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
     $instance_id = tripal_jbrowse_mgmt_create_instance(
       [
         'organism_id' => $organism_id,
+        'analysis_id' => $analysis_id,
         'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
         'description' => $description,
         'created_at' => time(),

+ 17 - 1
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_commands.inc

@@ -10,11 +10,15 @@
  */
 function tripal_jbrowse_mgmt_cmd_prepare_refseq($instance) {
   $settings = tripal_jbrowse_mgmt_get_settings();
-
+  dpm($settings,'$settings in tripal_jbrowse_mgmt_cmd_prepare_refseq');
   $data = $settings['data_dir'];
   $bin = $settings['bin_path'];
 
   $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
+  if(isset($instance->analysis_id)){
+    $path = $path . '_' . $instance->analysis_id;
+  }
+
   if (!file_exists($path)) {
     if (!mkdir($path)) {
       throw new Exception(
@@ -58,6 +62,10 @@ function tripal_jbrowse_mgmt_cmd_add_track($track) {
   $menu_template = $settings['menu_template'];
 
   $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
+  if(isset($instance->analysis_id)){
+    $path = $path . '_' . $instance->analysis_id;
+  }
+
   $out = $path . '/data';
 
   if (!file_exists($out)) {
@@ -158,6 +166,10 @@ function tripal_jbrowse_mgmt_cmd_generate_names($instance) {
   $bin = $settings['bin_path'];
 
   $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
+  if(isset($instance->analysis_id)){
+    $path = $path . '_' . $instance->analysis_id;
+  }
+
   if (!file_exists($path)) {
     if (!mkdir($path)) {
       throw new Exception(
@@ -198,6 +210,10 @@ function tripal_jbrowse_mgmt_cmd_delete_track($track) {
   $instance = $track->instance;
 
   $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
+  if(isset($instance->analysis_id)){
+    $path = $path . '_' . $instance->analysis_id;
+  }
+
   $out = $path . '/data';
 
   if (!file_exists($out)) {

+ 4 - 0
tripal_jbrowse_mgmt/tripal_jbrowse_mgmt.install

@@ -25,6 +25,10 @@ function tripal_jbrowse_mgmt_schema() {
         'type' => 'int',
         'not null' => TRUE,
       ],
+      'analysis_id' => [
+        'type' => 'int',
+        'not null' => FALSE,
+      ],
       'title' => [
         'type' => 'varchar',
         'length' => 255,

+ 26 - 0
tripal_jbrowse_mgmt/tripal_jbrowse_mgmt.module

@@ -124,6 +124,13 @@ function tripal_jbrowse_mgmt_menu() {
     'type' => MENU_LOCAL_ACTION,
   ];
 
+  $items['admin/tripal/extension/tripal_jbrowse/management/instances/analysis/autocomplete'] = [
+    'title' => 'autocomplete for JBrowse instance analysis',
+    'page callback' => '_analysis_autocomplete',
+    'access arguments' => ['administer tripal_jbrowse_mgmt'],
+    'type' => MENU_CALLBACK,
+  ];
+
   return $items;
 }
 
@@ -143,3 +150,22 @@ function tripal_jbrowse_mgmt_permission() {
 
   return $items;
 }
+
+/**
+ * autocomplete helper
+ * $string = string for autocomplete search
+ * to provide analysis names for form in tripal_jbrowse_mgmt_add.form.inc
+ */
+ function _analysis_autocomplete($string){
+   $analysis_4match =array();
+   $analysis = db_select('chado.analysis', 'CA')
+     ->fields('CA', ['analysis_id', 'name'])
+     ->condition('name','%'.db_like($string).'%','LIKE')
+     ->execute();
+
+    foreach($analysis as $one_analysis){
+      $key = $one_analysis->name . ' (' . $one_analysis->analysis_id . ')';
+      $analysis_4match[$key] = $one_analysis->name;
+    }
+    drupal_json_output($analysis_4match);
+ }