# HG changeset patch
# User devteam
# Date 1396363683 14400
# Node ID 619dd9e9c09c63b498f245c1d6079fd7923b8b23
Imported from capsule None
diff -r 000000000000 -r 619dd9e9c09c data_manager/data_manager_gatk_picard_index_builder.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager/data_manager_gatk_picard_index_builder.py Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,179 @@
+#!/usr/bin/env python
+# Dave B.
+# Uses fasta sorting functions written by Dan Blankenberg.
+
+import json
+import optparse
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+CHUNK_SIZE = 2**20
+
+DEFAULT_DATA_TABLE_NAME = "fasta_indexes"
+
+def get_id_name( params, dbkey, fasta_description=None):
+ #TODO: ensure sequence_id is unique and does not already appear in location file
+ sequence_id = params['param_dict']['sequence_id']
+ if not sequence_id:
+ sequence_id = dbkey
+
+ sequence_name = params['param_dict']['sequence_name']
+ if not sequence_name:
+ sequence_name = fasta_description
+ if not sequence_name:
+ sequence_name = dbkey
+ return sequence_id, sequence_name
+
+def build_picard_index( data_manager_dict, fasta_filename, target_directory, dbkey, sequence_id, sequence_name, jar, data_table_name=DEFAULT_DATA_TABLE_NAME ):
+ fasta_base_name = os.path.split( fasta_filename )[-1]
+ gatk_sorted_fasta_filename = os.path.join( target_directory, fasta_base_name )
+ shutil.copy( fasta_filename, gatk_sorted_fasta_filename )
+ _sort_fasta_gatk( gatk_sorted_fasta_filename )
+ sam_index_filename = '%s.fai' % gatk_sorted_fasta_filename
+ if not os.path.exists( sam_index_filename ):
+ sam_command = [ 'samtools', 'faidx', gatk_sorted_fasta_filename ]
+ _run_command( sam_command, target_directory )
+ args = [ 'java', '-jar', jar, 'R=%s' % gatk_sorted_fasta_filename, 'O=%s.dict' % sequence_id ]
+ _run_command( args, target_directory )
+ data_table_entry = dict( value=sequence_id, dbkey=dbkey, name=sequence_name, path=fasta_base_name )
+ _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry )
+
+def _run_command( command, target_directory ):
+ tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-gatk_picard_index_builder-stderr" )
+ proc = subprocess.Popen( args=command, shell=False, cwd=target_directory, stderr=tmp_stderr.fileno() )
+ return_code = proc.wait()
+ if return_code:
+ tmp_stderr.flush()
+ tmp_stderr.seek( 0 )
+ sys.stderr.write( "Error building index:\n" )
+ while True:
+ chunk = tmp_stderr.read( CHUNK_SIZE )
+ if not chunk:
+ break
+ sys.stderr.write( chunk )
+ sys.exit( return_code )
+ tmp_stderr.close()
+
+def _add_data_table_entry( data_manager_dict, data_table_name, data_table_entry ):
+ data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
+ data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] )
+ data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry )
+ return data_manager_dict
+
+def _move_and_index_fasta_for_sorting( fasta_filename ):
+ unsorted_filename = tempfile.NamedTemporaryFile().name
+ shutil.move( fasta_filename, unsorted_filename )
+ fasta_offsets = {}
+ unsorted_fh = open( unsorted_filename )
+ while True:
+ offset = unsorted_fh.tell()
+ line = unsorted_fh.readline()
+ if not line:
+ break
+ if line.startswith( ">" ):
+ line = line.split( None, 1 )[0][1:]
+ fasta_offsets[ line ] = offset
+ unsorted_fh.close()
+ current_order = map( lambda x: x[1], sorted( map( lambda x: ( x[1], x[0] ), fasta_offsets.items() ) ) )
+ return ( unsorted_filename, fasta_offsets, current_order )
+
+def _write_sorted_fasta( sorted_names, fasta_offsets, sorted_fasta_filename, unsorted_fasta_filename ):
+ unsorted_fh = open( unsorted_fasta_filename )
+ sorted_fh = open( sorted_fasta_filename, 'wb+' )
+
+ for name in sorted_names:
+ offset = fasta_offsets[ name ]
+ unsorted_fh.seek( offset )
+ sorted_fh.write( unsorted_fh.readline() )
+ while True:
+ line = unsorted_fh.readline()
+ if not line or line.startswith( ">" ):
+ break
+ sorted_fh.write( line )
+ unsorted_fh.close()
+ sorted_fh.close()
+
+def _int_to_roman( integer ):
+ if not isinstance( integer, int ):
+ raise TypeError, "expected integer, got %s" % type( integer )
+ if not 0 < integer < 4000:
+ raise ValueError, "Argument must be between 1 and 3999, got %s" % str( integer )
+ ints = ( 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 )
+ nums = ( 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' )
+ result = ""
+ for i in range( len( ints ) ):
+ count = int( integer / ints[ i ] )
+ result += nums[ i ] * count
+ integer -= ints[ i ] * count
+ return result
+
+def _sort_fasta_gatk( fasta_filename ):
+ ( unsorted_filename, fasta_offsets, current_order ) = _move_and_index_fasta_for_sorting( fasta_filename )
+ sorted_names = map( str, range( 1, 100 ) ) + map( _int_to_roman, range( 1, 100 ) ) + [ 'X', 'Y', 'M' ]
+ #detect if we have chrN, or just N
+ has_chr = False
+ for chrom in sorted_names:
+ if "chr%s" % chrom in current_order:
+ has_chr = True
+ break
+
+ if has_chr:
+ sorted_names = map( lambda x: "chr%s" % x, sorted_names)
+ else:
+ sorted_names.insert( 0, "MT" )
+ sorted_names.extend( map( lambda x: "%s_random" % x, sorted_names ) )
+
+ existing_sorted_names = []
+ for name in sorted_names:
+ # Append each chromosome only once.
+ if name in current_order and name not in existing_sorted_names:
+ existing_sorted_names.append( name )
+ for name in current_order:
+ #TODO: confirm that non-canonical names do not need to be sorted specially
+ if name not in existing_sorted_names:
+ existing_sorted_names.append( name )
+
+ if existing_sorted_names == current_order:
+ shutil.move( unsorted_filename, fasta_filename )
+ else:
+ _write_sorted_fasta( existing_sorted_names, fasta_offsets, fasta_filename, unsorted_filename )
+
+def main():
+ #Parse Command Line
+ parser = optparse.OptionParser()
+ parser.add_option( '-f', '--fasta_filename', dest='fasta_filename', action='store', type="string", default=None, help='fasta_filename' )
+ parser.add_option( '-d', '--fasta_dbkey', dest='fasta_dbkey', action='store', type="string", default=None, help='fasta_dbkey' )
+ parser.add_option( '-t', '--fasta_description', dest='fasta_description', action='store', type="string", default=None, help='fasta_description' )
+ parser.add_option( '-n', '--data_table_name', dest='data_table_name', action='store', type="string", default=None, help='data_table_name' )
+ parser.add_option( '-j', '--jar', dest='jar', action='store', type="string", default=None, help='GATK .jar file' )
+ (options, args) = parser.parse_args()
+
+ filename = args[0]
+
+ params = json.loads( open( filename ).read() )
+ target_directory = params[ 'output_data' ][0]['extra_files_path']
+ os.mkdir( target_directory )
+ data_manager_dict = {}
+
+ if options.fasta_dbkey in [ None, '', '?' ]:
+ raise Exception( '"%s" is not a valid dbkey. You must specify a valid dbkey.' % ( options.fasta_dbkey ) )
+
+ sequence_id, sequence_name = get_id_name( params, dbkey=options.fasta_dbkey, fasta_description=options.fasta_description )
+
+ #build the index
+ build_picard_index( data_manager_dict,
+ options.fasta_filename,
+ target_directory,
+ options.fasta_dbkey,
+ sequence_id,
+ sequence_name,
+ options.jar,
+ data_table_name=options.data_table_name or DEFAULT_DATA_TABLE_NAME )
+
+ #save info to json file
+ open( filename, 'wb' ).write( json.dumps( data_manager_dict ) )
+
+if __name__ == "__main__": main()
diff -r 000000000000 -r 619dd9e9c09c data_manager/data_manager_gatk_picard_index_builder.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager/data_manager_gatk_picard_index_builder.xml Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,33 @@
+
+ builder
+
+ samtools
+ picard
+
+
+ data_manager_gatk_picard_index_builder.py "${out_file}" \
+ --jar "\$JAVA_JAR_PATH/CreateSequenceDictionary.jar" \
+ --fasta_filename "${all_fasta_source.fields.path}" \
+ --fasta_dbkey "${all_fasta_source.fields.dbkey}" \
+ --fasta_description "${all_fasta_source.fields.name}" \
+ --data_table_name "gatk_picard_indexes"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+.. class:: infomark
+
+**Notice:** If you leave name, description, or id blank, it will be generated automatically.
+
+
+
diff -r 000000000000 -r 619dd9e9c09c data_manager_conf.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager_conf.xml Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
diff -r 000000000000 -r 619dd9e9c09c tool-data/all_fasta.loc.sample
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tool-data/all_fasta.loc.sample Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,18 @@
+#This file lists the locations and dbkeys of all the fasta files
+#under the "genome" directory (a directory that contains a directory
+#for each build). The script extract_fasta.py will generate the file
+#all_fasta.loc. This file has the format (white space characters are
+#TAB characters):
+#
+#
+#
+#So, all_fasta.loc could look something like this:
+#
+#apiMel3 apiMel3 Honeybee (Apis mellifera): apiMel3 /path/to/genome/apiMel3/apiMel3.fa
+#hg19canon hg19 Human (Homo sapiens): hg19 Canonical /path/to/genome/hg19/hg19canon.fa
+#hg19full hg19 Human (Homo sapiens): hg19 Full /path/to/genome/hg19/hg19full.fa
+#
+#Your all_fasta.loc file should contain an entry for each individual
+#fasta file. So there will be multiple fasta files for each build,
+#such as with hg19 above.
+#
diff -r 000000000000 -r 619dd9e9c09c tool-data/gatk_sorted_picard_index.loc.sample
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tool-data/gatk_sorted_picard_index.loc.sample Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,26 @@
+#This is a sample file distributed with Galaxy that enables tools
+#to use a directory of Picard dict and associated files. You will need
+#to create these data files and then create a picard_index.loc file
+#similar to this one (store it in this directory) that points to
+#the directories in which those files are stored. The picard_index.loc
+#file has this format (longer white space is the TAB character):
+#
+#
+#
+#So, for example, if you had hg18 indexed and stored in
+#/depot/data2/galaxy/srma/hg18/,
+#then the srma_index.loc entry would look like this:
+#
+#hg18 hg18 hg18 Pretty /depot/data2/galaxy/picard/hg18/hg18.fa
+#
+#and your /depot/data2/galaxy/srma/hg18/ directory
+#would contain the following three files:
+#hg18.fa
+#hg18.dict
+#hg18.fa.fai
+#
+#The dictionary file for each reference (ex. hg18.dict) must be
+#created via Picard (http://picard.sourceforge.net). Note that
+#the dict file does not have the .fa extension although the
+#path list in the loc file does include it.
+#
diff -r 000000000000 -r 619dd9e9c09c tool_data_table_conf.xml.sample
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_data_table_conf.xml.sample Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,13 @@
+
+
+
+
+ value, dbkey, name, path
+
+
+
+
+ value, dbkey, name, path
+
+
+
diff -r 000000000000 -r 619dd9e9c09c tool_dependencies.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_dependencies.xml Tue Apr 01 10:48:03 2014 -0400
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+