Переглянути джерело

Merge pull request #57 from Jiu9Shen/46-alignment_tracks

Alignment tracks
Lacey-Anne Sanderson 4 роки тому
батько
коміт
fa102c744b

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

@@ -375,7 +375,7 @@ function tripal_jbrowse_mgmt_make_slug($string) {
  */
 function tripal_jbrowse_mgmt_upload_file($field) {
   $file = file_save_upload($field, [
-    'file_validate_extensions' => ['fasta faa fna fastq txt gff vcf wig gz tbi bw'],
+    'file_validate_extensions' => ['fasta faa fna fastq txt gff vcf wig gz tbi bw bam bai cram'],
     // Make it 20 GB max.
     'file_validate_size' => [1024 * 1024 * 1024 * 20],
   ]);
@@ -612,6 +612,7 @@ function tripal_jbrowse_mgmt_get_track_types() {
   return [
     'FeatureTrack',
     'CanvasFeatures',
+    'Alignment',
     'HTMLFeatures',
     'HTMLVariants',
     'XYPlot',
@@ -709,3 +710,98 @@ function tripal_jbrowse_mgmt_get_instance_properties($id) {
     ->condition('instance_id', $id)
     ->execute()->fetchAllKeyed(0,1);
 }
+
+
+/**
+ * validate folder upload (file with index)
+ * eg. vcf, bam, cram
+ *
+ * @param $file
+ *   the path and file name of upload file
+ * @param $index
+ *   the path and file name of upload file
+ * @param $local_directory
+ *   the path and file name of upload file
+ * @return
+ *   warning message or NULL if no warning
+ */
+
+ function tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state) {
+   $values = $form_state['values'];
+   $local_directory = isset($values['dir_path']) ? $values['dir_path'] : NULL;
+   $symbolic_link = $values['symbolic_link'];
+
+   if (empty($file) && empty($index) && empty($local_directory)) {
+     form_set_error('Please provide a local directory path or upload files.');
+   }
+   elseif (empty($file) && empty($index) && !empty($local_dir)) {
+     if (!file_exists($local_dir)) {
+       form_set_error('The directory provided does not exist.');
+     }
+     else {
+       if (!is_dir($local_dir)) {
+         form_set_error('file_path',
+           'The file provided is not a directory.');
+       }
+       else {
+         $file_file = glob($local_dir . '/*.[vcf.gz][bam][cram]');
+         $file_index = glob($local_dir . '/*.[csi][tbi][idx][bai][cram][crai]');
+         if (count($file_file) != 1 || count($file_index) != 1) {
+           form_set_error('file_path',
+             'Please provide a directory with exactly one data file and one index file.');
+         }
+         else {
+           try {
+             if (!tripal_jbrowse_mgmt_copy_file($file_file[0], $path, $symbolic_link)) {
+               form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
+             }
+             else {
+               if (!tripal_jbrowse_mgmt_copy_file($file_index[0], $path, $symbolic_link)) {
+                 form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
+               }
+             }
+           } catch (Exception $exception) {
+             form_set_error($exception->getMessage());
+           }
+
+         }
+       }
+     }
+   }
+   elseif (empty($file) && !empty($index)) {
+     form_set_error('file', 'Please upload both a data file and an index file.');
+   }
+   elseif (!empty($file) && empty($index)) {
+     form_set_error('file2', 'Please upload both a data file and an index file.');
+   }
+   else {
+     $file_data_uploaded = tripal_jbrowse_mgmt_upload_file('file');
+     if (!$file_data_uploaded) {
+       form_set_error('file', 'Unable to upload file');
+     }
+     else {
+       $file_data_uploaded = tripal_jbrowse_mgmt_move_file($file_data_uploaded, $path);
+       if (!isset($file_data_uploaded)) {
+         form_set_error('file', 'Failed to move data file to ' . $path . '.');
+       }
+       else {
+         $form_state['values']['uploaded_file_data'] = $file_data_uploaded;
+       }
+     }
+
+     $index_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
+     if (!$index_uploaded) {
+       form_set_error('file2', 'Unable to upload file');
+     }
+     else {
+       $index_uploaded = tripal_jbrowse_mgmt_move_file($index_uploaded, $path);
+       if (!isset($index_uploaded)) {
+         form_set_error('file2', 'Failed to move index file to ' . $path . '.');
+       }
+       else {
+         $form_state['values']['uploaded_file_index'] = $index_uploaded;
+       }
+     }
+   }
+   return $form_state;
+ }

+ 28 - 0
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_commands.inc

@@ -111,6 +111,34 @@ function tripal_jbrowse_mgmt_cmd_add_track($track) {
       tripal_jbrowse_mgmt_save_json($instance, $json);
       break;
 
+    case 'Alignment':
+      $json = tripal_jbrowse_mgmt_get_json($instance);
+      $directory = 'bam';
+      $file_name = $track->file;
+      if (is_dir($track->file)) {
+        $file_name = glob($track->file . '/' . '*.[bam][cram]')[0];
+      }
+      $file_name = pathinfo($file_name)['basename'];
+
+      $track_in_json = [
+        'label' => tripal_jbrowse_mgmt_make_slug($track->label),
+        'key' => $track->label,
+        'urlTemplate' => $directory . '/' . $file_name,
+        'type' => 'JBrowse/View/Track/Alignment2',
+      ];
+
+      $extension = pathinfo($file_name)['extension'];
+      switch($extension){
+        case 'bam':
+          $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/BAM';
+          break;
+        case 'cram':
+          $track_in_json['storeClass'] = 'JBrowse/Store/SeqFeature/CRAM';
+          break;
+      }
+      tripal_jbrowse_mgmt_save_json($instance, $json);
+      break;
+
     case 'XYPlot':
       $json = tripal_jbrowse_mgmt_get_json($instance);
       $basename = pathinfo($track->file)['basename'];

+ 17 - 79
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_tracks.form.inc

@@ -52,7 +52,7 @@ function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
   $form['data']['file_type'] = [
     '#type' => 'select',
     '#title' => t('File Type'),
-    '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'vcf', 'bw']),
+    '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'bam', 'cram', 'vcf', 'bw']),
     '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
     '#required' => TRUE,
   ];
@@ -139,6 +139,9 @@ function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
   if ($file_type === 'vcf') {
     $path = $base_path . '/vcf';
   }
+  if (($file_type === 'bam') OR ($file_type === 'cram')){
+    $path = $base_path . '/bam';
+  }
   elseif ($file_type === 'bw') {
     $path = $base_path . '/wig';
   }
@@ -148,82 +151,17 @@ function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
   switch ($file_type) {
     case 'vcf':
       $index = $_FILES['files']['tmp_name']['file2'];
-      $local_dir = isset($values['dir_path']) ? $values['dir_path'] : NULL;
-
-      if (empty($file) && empty($index) && empty($local_dir)) {
-        form_set_error('file',
-          'Please provide a local directory path or upload files.');
-      }
-      elseif (empty($file) && empty($index) && !empty($local_dir)) {
-        if (!file_exists($local_dir)) {
-          form_set_error('file_path', 'The directory provided does not exist.');
-        }
-        else {
-          if (!is_dir($local_dir)) {
-            form_set_error('file_path',
-              'The file provided is not a directory.');
-          }
-          else {
-            $file_gz = glob($local_dir . '/*.vcf.gz');
-            $file_index = glob($local_dir . '/*.vcf.gz.[cti][sbd][ix]');
-            if (count($file_gz) != 1 || count($file_index) != 1) {
-              form_set_error('file_path',
-                'Please provide a directory with exactly one gz and one index file.');
-            }
-            else {
-              try {
-                if (!tripal_jbrowse_mgmt_copy_file($file_gz[0], $path, $symbolic_link)) {
-                  form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
-                }
-                else {
-                  if (!tripal_jbrowse_mgmt_copy_file($file_index[0], $path, $symbolic_link)) {
-                    form_set_error('file_path', 'Failed to copy file' . $file_gz[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
-                  }
-                }
-              } catch (Exception $exception) {
-                form_set_error($exception->getMessage());
-              }
-
-            }
-          }
-        }
-      }
-      elseif (empty($file) && !empty($index)) {
-        form_set_error('file', 'Please upload both a gz and an index file.');
-      }
-      elseif (!empty($file) && empty($index)) {
-        form_set_error('file2', 'Please upload both a gz and an index file.');
-      }
-      else {
-        $gz_uploaded = tripal_jbrowse_mgmt_upload_file('file');
-        if (!$gz_uploaded) {
-          form_set_error('file', 'Unable to upload file');
-        }
-        else {
-          $gz_uploaded = tripal_jbrowse_mgmt_move_file($gz_uploaded, $path);
-          if (!isset($gz_uploaded)) {
-            form_set_error('file', 'Failed to move gz file to ' . $path . '.');
-          }
-          else {
-            $form_state['values']['uploaded_gz'] = $gz_uploaded;
-          }
-        }
+      $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
+      break;
 
-        $index_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
-        if (!$index_uploaded) {
-          form_set_error('file2', 'Unable to upload file');
-        }
-        else {
-          $index_uploaded = tripal_jbrowse_mgmt_move_file($index_uploaded, $path);
-          if (!isset($index_uploaded)) {
-            form_set_error('file2', 'Failed to move index file to ' . $path . '.');
-          }
-          else {
-            $form_state['values']['uploaded_tbi'] = $index_uploaded;
-          }
-        }
-      }
+    case 'bam':
+      $index = $_FILES['files']['tmp_name']['file2'];
+      $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
+      break;
 
+    case 'carm':
+      $index = $_FILES['files']['tmp_name']['file2'];
+      $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
       break;
 
     default:
@@ -283,11 +221,11 @@ function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
   if (!empty($values['dir_path'])) {
     $file = $values['dir_path'];
   }
-  if (!empty($values['uploaded_gz'])) {
-    $file = $values['uploaded_gz'];
+  if (!empty($values['uploaded_file_index'])) {
+    $file = $values['uploaded_file_index'];
   }
-  if (!empty($values['uploaded_file'])) {
-    $file = $values['uploaded_file'];
+  if (!empty($values['uploaded_file_data'])) {
+    $file = $values['uploaded_file_data'];
   }
 
   $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);