Преглед изворни кода

Merge pull request #58 from tripal/46-alignments

46 alignments
Lacey-Anne Sanderson пре 4 година
родитељ
комит
30be1232d9

+ 110 - 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,111 @@ 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_directory)) {
+     if (!file_exists($local_directory)) {
+       form_set_error('The directory provided does not exist.');
+     }
+     else {
+       if (!is_dir($local_directory)) {
+         form_set_error('file_path',
+           'The file provided is not a directory.');
+       }
+       else {
+         // Retrieve an array of data file and index
+         // to ensure there is only one of each.
+
+         foreach (scandir($local_directory) as $f) {
+           $fparts = pathinfo($f);
+
+           if (in_array($fparts['extension'], ['gz', 'bam', 'cram'])) {
+             $file_file[] = $local_directory . '/' . $f;
+           }
+           if (in_array($fparts['extension'], ['csi','tbi','idx','bai','crai'])) {
+             $file_index[] = $local_directory . '/' . $f;
+           }
+         }
+         // CHECK: Only a single data file and index.
+
+         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_file[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_index[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 index.');
+     }
+     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;
+ }

+ 42 - 1
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_commands.inc

@@ -78,7 +78,7 @@ function tripal_jbrowse_mgmt_cmd_add_track($track) {
       $file_name = $track->file;
       if (is_dir($track->file)) {
         $file_name = glob($track->file . '/' . '*.vcf.gz')[0];
-        $index_name = glob($track->file . '/' . '*.vcf.gz.[tci][bsd][ix]')[0];
+        $index_name = glob($track->file . '/' . '*.vcf.gz.[cti][sbd][ix]')[0];
       }
       $file_name = pathinfo($file_name)['basename'];
 
@@ -111,6 +111,47 @@ 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';
+      if (is_dir($track->file)) {
+        $file_name = glob($track->file . '/' . '*.bam')[0];
+        $index_file_name = glob($track->file . '/' . '*.bam.*')[0];
+        if (!$file_name) {
+          $file_name = glob($track->file . '/' . '*.cram')[0];
+          $index_file_name = glob($track->file . '/' . '*.cram.*')[0];
+         }
+      }
+
+      $track_in_json = [
+        'label' => tripal_jbrowse_mgmt_make_slug($track->label),
+        'key' => $track->label,
+        'urlTemplate' => $directory . '/' . pathinfo($file_name)['basename'],
+        'type' => 'JBrowse/View/Track/Alignments2',
+      ];
+      $index_path_info = pathinfo($index_file_name);
+      switch($index_path_info['extension']){
+        case "bai":
+        $track_in_json['baiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
+        break;
+
+        case "csi":
+        $track_in_json['csiUrlTemplate'] = $directory . '/' . $index_path_info['basename'];
+        break;
+      }
+      $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;
+      }
+      $json['tracks'][] = $track_in_json;
+      tripal_jbrowse_mgmt_save_json($instance, $json);
+      break;
+
     case 'XYPlot':
       $json = tripal_jbrowse_mgmt_get_json($instance);
       $basename = pathinfo($track->file)['basename'];

+ 28 - 86
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,
   ];
@@ -67,7 +67,9 @@ function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
     '#title' => t('Index File'),
     '#states' => [
       'visible' => [
-        ':input[name="file_type"]' => ['value' => 'vcf'],
+        [':input[name="file_type"]' => ['value' => 'vcf'],],
+        [':input[name="file_type"]' => ['value' => 'bam'],],
+        [':input[name="file_type"]' => ['value' => 'cram'],],
       ],
     ],
   ];
@@ -79,7 +81,9 @@ function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
     '#maxlength' => 255,
     '#states' => [
       'invisible' => [
-        ':input[name="file_type"]' => ['value' => 'vcf'],
+        [':input[name="file_type"]' => ['value' => 'vcf'],],
+        [':input[name="file_type"]' => ['value' => 'bam'],],
+        [':input[name="file_type"]' => ['value' => 'cram'],],
       ],
     ],
   ];
@@ -91,7 +95,9 @@ function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
     '#maxlength' => 255,
     '#states' => [
       'visible' => [
-        ':input[name="file_type"]' => ['value' => 'vcf'],
+        [':input[name="file_type"]' => ['value' => 'vcf'],],
+        [':input[name="file_type"]' => ['value' => 'bam'],],
+        [':input[name="file_type"]' => ['value' => 'cram'],],
       ],
     ],
   ];
@@ -99,6 +105,7 @@ function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
   $form['data']['symbolic_link'] = [
     '#type' => 'checkbox',
     '#title' => t('Symbolic Link'),
+    '#default_value' => true,
     '#description' => t('Create a symbolic link rather than make a copy of the file. This only applies when a path on the server is supplied.<br>Please have Symbolic Link selected if the same file is used for new track.'),
   ];
 
@@ -136,10 +143,7 @@ function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
   }
   $base_path = $base_path . '/data';
 
-  if ($file_type === 'vcf') {
-    $path = $base_path . '/vcf';
-  }
-  elseif ($file_type === 'bw') {
+  if ($file_type === 'bw') {
     $path = $base_path . '/wig';
   }
   else {
@@ -147,83 +151,21 @@ function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
   }
   switch ($file_type) {
     case 'vcf':
+      $path = $base_path . '/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':
+      $path = $base_path . '/bam';
+      $index = $_FILES['files']['tmp_name']['file2'];
+      $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
+      break;
 
+    case 'cram':
+      $path = $base_path . '/bam';
+      $index = $_FILES['files']['tmp_name']['file2'];
+      $form_state = tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state);
       break;
 
     default:
@@ -283,11 +225,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']);