changeset 1:93f2e3337a33

Update sam_to_bam to use the fasta_indexes data table.
author Dave Bouvier <dave@bx.psu.edu>
date Wed, 11 Dec 2013 12:54:32 -0500
parents 30fdbaccb96b
children 05ca4081ca7e
files sam_to_bam.py sam_to_bam.xml tool-data/fasta_indexes.loc.sample tool-data/sam_fa_indices.loc.sample tool-data/tool_data_table_conf.xml.sample tool_data_table_conf.xml.sample
diffstat 6 files changed, 55 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/sam_to_bam.py	Mon Aug 26 14:22:00 2013 -0400
+++ b/sam_to_bam.py	Wed Dec 11 12:54:32 2013 -0500
@@ -3,43 +3,25 @@
 Converts SAM data to sorted BAM data.
 usage: sam_to_bam.py [options]
    --input1: SAM file to be converted
-   --dbkey: dbkey value
+   --index: path of the indexed reference genome
    --ref_file: Reference file if choosing from history
    --output1: output dataset in bam format
-   --index_dir: GALAXY_DATA_INDEX_DIR
 """
 
-import optparse, os, sys, subprocess, tempfile, shutil, gzip
-from galaxy import eggs
-import pkg_resources; pkg_resources.require( "bx-python" )
-from bx.cookbook import doc_optparse
-from galaxy import util
+import optparse, os, sys, subprocess, tempfile, shutil
 
 def stop_err( msg ):
     sys.stderr.write( '%s\n' % msg )
     sys.exit()
 
-def check_seq_file( dbkey, cached_seqs_pointer_file ):
-    seq_path = ''
-    for line in open( cached_seqs_pointer_file ):
-        line = line.rstrip( '\r\n' )
-        if line and not line.startswith( '#' ) and line.startswith( 'index' ):
-            fields = line.split( '\t' )
-            if len( fields ) < 3:
-                continue
-            if fields[1] == dbkey:
-                seq_path = fields[2].strip()
-                break
-    return seq_path
-
 def __main__():
     #Parse Command Line
     parser = optparse.OptionParser()
     parser.add_option( '', '--input1', dest='input1', help='The input SAM dataset' )
-    parser.add_option( '', '--dbkey', dest='dbkey', help='The build of the reference dataset' )
+    
+    parser.add_option( '', '--index', dest='index', help='The path of the indexed reference genome' )
     parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' )
     parser.add_option( '', '--output1', dest='output1', help='The output BAM dataset' )
-    parser.add_option( '', '--index_dir', dest='index_dir', help='GALAXY_DATA_INDEX_DIR' )
     ( options, args ) = parser.parse_args()
 
     # output version # of tool
@@ -61,24 +43,18 @@
     except:
         sys.stdout.write( 'Could not determine Samtools version\n' )
 
-    cached_seqs_pointer_file = '%s/sam_fa_indices.loc' % options.index_dir
-    if not os.path.exists( cached_seqs_pointer_file ):
-        stop_err( 'The required file (%s) does not exist.' % cached_seqs_pointer_file )
-    # If found for the dbkey, seq_path will look something like /galaxy/data/equCab2/sam_index/equCab2.fa,
-    # and the equCab2.fa file will contain fasta sequences.
-    seq_path = check_seq_file( options.dbkey, cached_seqs_pointer_file )
     tmp_dir = tempfile.mkdtemp( dir='.' )
     if not options.ref_file or options.ref_file == 'None':
         # We're using locally cached reference sequences( e.g., /galaxy/data/equCab2/sam_index/equCab2.fa ).
         # The indexes for /galaxy/data/equCab2/sam_index/equCab2.fa will be contained in
         # a file named /galaxy/data/equCab2/sam_index/equCab2.fa.fai
         fai_index_file_base = seq_path
-        fai_index_file_path = '%s.fai' % seq_path 
+        fai_index_file_path = '%s.fai' % options.index
         if not os.path.exists( fai_index_file_path ):
             #clean up temp files
             if os.path.exists( tmp_dir ):
                 shutil.rmtree( tmp_dir )
-            stop_err( 'No sequences are available for build (%s), request them by reporting this error.' % options.dbkey )
+            stop_err( 'Indexed genome %s not present, request it by reporting this error.' % options.index )
     else:
         try:
             # Create indexes for history reference ( e.g., ~/database/files/000/dataset_1.dat ) using samtools faidx, which will:
--- a/sam_to_bam.xml	Mon Aug 26 14:22:00 2013 -0400
+++ b/sam_to_bam.xml	Wed Dec 11 12:54:32 2013 -0500
@@ -1,4 +1,4 @@
-<tool id="sam_to_bam" name="SAM-to-BAM" version="1.1.2">
+<tool id="sam_to_bam" name="SAM-to-BAM" version="1.1.3">
   <description>converts SAM format to BAM format</description>
   <requirements>
     <requirement type="package" version="0.1.18">samtools</requirement>
@@ -7,13 +7,11 @@
     sam_to_bam.py
       --input1=$source.input1
       #if $source.index_source == "history":
-        --dbkey=${ref_file.metadata.dbkey} 
         --ref_file=$source.ref_file
       #else
-        --dbkey=${input1.metadata.dbkey} 
+        --index=${source.index.fields.path}
       #end if
       --output1=$output1
-      --index_dir=${GALAXY_DATA_INDEX_DIR}
   </command>
   <inputs>
     <conditional name="source">
@@ -22,13 +20,19 @@
         <option value="history">History</option>
       </param>
       <when value="cached">
-        <param name="input1" type="data" format="sam" metadata_name="dbkey" label="SAM File to Convert">
-           <validator type="unspecified_build" />
-           <validator type="dataset_metadata_in_file" filename="sam_fa_indices.loc" metadata_name="dbkey" metadata_column="1" message="Sequences are not currently available for the specified build." line_startswith="index" />
+        <param name="input1" type="data" format="sam" metadata_name="dbkey" label="SAM file to convert">
+          <validator type="unspecified_build" />
+          <validator type="dataset_metadata_in_data_table" table_name="fasta_indexes" metadata_name="dbkey" metadata_column="1" message="Sequences are not currently available for the specified build." />
+        </param>
+        <param name="index" type="select" label="Using reference genome">
+          <options from_data_table="fasta_indexes">
+            <filter type="data_meta" ref="input1" key="dbkey" column="1" />
+            <validator type="no_options" message="No reference genome is available for the build associated with the selected input dataset" />
+          </options>
         </param>
       </when>
       <when value="history">
-        <param name="input1" type="data" format="sam" label="Convert SAM file" />
+        <param name="input1" type="data" format="sam" label="SAM file to convert" />
         <param name="ref_file" type="data" format="fasta" metadata_name="dbkey" label="Using reference file" />
       </when>
     </conditional>
@@ -76,6 +80,7 @@
       -->
       <param name="index_source" value="cached" />
       <param name="input1" value="sam_to_bam_in1.sam" ftype="sam" dbkey="chrM" />
+      <param name="index" value="chr_m" />
       <output name="output1" file="sam_to_bam_out2.bam" ftype="bam" />
     </test>
   </tests>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool-data/fasta_indexes.loc.sample	Wed Dec 11 12:54:32 2013 -0500
@@ -0,0 +1,29 @@
+#This is a sample file distributed with Galaxy that enables tools
+#to use a directory of Samtools indexed sequences data files.  You will need
+#to create these data files and then create a sam_fa_new_indices.loc file
+#similar to this one (store it in this directory) that points to
+#the directories in which those files are stored. The sam_fa_new_indices.loc
+#file has this format (white space characters are TAB characters):
+#
+# <unique_build_id>	<dbkey>	<display_name>	<file_base_path>
+#
+#So, for example, if you had hg19 Canonical indexed stored in
+#
+# /depot/data2/galaxy/hg19/sam/,
+#
+#then the sam_fa_new_indices.loc entry would look like this:
+#
+#hg19canon	hg19	Human (Homo sapiens): hg19 Canonical	/depot/data2/galaxy/hg19/sam/hg19canon.fa
+#
+#and your /depot/data2/galaxy/hg19/sam/ directory
+#would contain hg19canon.fa and hg19canon.fa.fai files.
+#
+#Your sam_fa_new_indices.loc file should include an entry per line for
+#each index set you have stored.  The file in the path does actually
+#exist, but it should never be directly used. Instead, the name serves
+#as a prefix for the index file.  For example:
+#
+#hg18canon	hg18	Human (Homo sapiens): hg18 Canonical	/depot/data2/galaxy/hg18/sam/hg18canon.fa
+#hg18full	hg18	Human (Homo sapiens): hg18 Full	/depot/data2/galaxy/hg18/sam/hg18full.fa
+#hg19canon	hg19	Human (Homo sapiens): hg19 Canonical	/depot/data2/galaxy/hg19/sam/hg19canon.fa
+#hg19full	hg19	Human (Homo sapiens): hg19 Full	/depot/data2/galaxy/hg19/sam/hg19full.fa
--- a/tool-data/sam_fa_indices.loc.sample	Mon Aug 26 14:22:00 2013 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#This is a sample file distributed with Galaxy that enables tools
-#to use a directory of Samtools indexed sequences data files.  You will need
-#to create these data files and then create a sam_fa_indices.loc file 
-#similar to this one (store it in this directory) that points to 
-#the directories in which those files are stored. The sam_fa_indices.loc 
-#file has this format (white space characters are TAB characters):
-#
-#index	<seq>	<location>
-#
-#So, for example, if you had hg18 indexed stored in 
-#/depot/data2/galaxy/sam/, 
-#then the sam_fa_indices.loc entry would look like this:
-#
-#index	hg18	/depot/data2/galaxy/sam/hg18.fa
-#
-#and your /depot/data2/galaxy/sam/ directory
-#would contain hg18.fa and hg18.fa.fai files:
-#
-#-rw-r--r--  1 james    universe 830134 2005-09-13 10:12 hg18.fa
-#-rw-r--r--  1 james    universe 527388 2005-09-13 10:12 hg18.fa.fai
-#
-#Your sam_fa_indices.loc file should include an entry per line for 
-#each index set you have stored.  The file in the path does actually
-#exist, but it should never be directly used. Instead, the name serves
-#as a prefix for the index file.  For example:
-#
-#index	hg18	/depot/data2/galaxy/sam/hg18.fa
-#index	hg19	/depot/data2/galaxy/sam/hg19.fa
--- a/tool-data/tool_data_table_conf.xml.sample	Mon Aug 26 14:22:00 2013 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-<!-- Use the file tool_data_table_conf.xml.oldlocstyle if you don't want to update your loc files as changed in revision 4550:535d276c92bc-->
-<tables>
-    <!-- Location of SAMTools indexes and other files -->
-    <table name="sam_fa_indexes" comment_char="#">
-        <columns>line_type, value, path</columns>
-        <file path="tool-data/sam_fa_indices.loc" />
-    </table>
-</tables>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_data_table_conf.xml.sample	Wed Dec 11 12:54:32 2013 -0500
@@ -0,0 +1,7 @@
+<!-- Use the file tool_data_table_conf.xml.oldlocstyle if you don't want to update your loc files as changed in revision 4550:535d276c92bc-->
+<tables>
+    <table name="fasta_indexes" comment_char="#">
+        <columns>value, dbkey, name, path</columns>
+        <file path="tool-data/fasta_indexes.loc" />
+    </table>
+</tables>