From 13e8b3e7dedd6736155854f61d36ffdf9bcb631a Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Wed, 28 Jan 2015 10:38:22 -0500 Subject: [PATCH 1/3] Allow BAM's set_meta() to use samtools 1 to generate the index, if the samtools found on $PATH is samtools 1. --- lib/galaxy/datatypes/binary.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/datatypes/binary.py b/lib/galaxy/datatypes/binary.py index 8622f88aa09..ae86ae549e4 100644 --- a/lib/galaxy/datatypes/binary.py +++ b/lib/galaxy/datatypes/binary.py @@ -264,10 +264,19 @@ class Bam( Binary ): ##$ samtools index ##Usage: samtools index [] stderr_name = tempfile.NamedTemporaryFile( prefix = "bam_index_stderr" ).name - command = 'samtools index %s %s' % ( dataset.file_name, index_file.file_name ) - proc = subprocess.Popen( args=command, shell=True, stderr=open( stderr_name, 'wb' ) ) + command = [ 'samtools', 'index', dataset.file_name, index_file.file_name ] + proc = subprocess.Popen( args=command, stderr=open( stderr_name, 'wb' ) ) exit_code = proc.wait() #Did index succeed? + if exit_code == -6: + # SIGABRT, most likely samtools 1.0+ which does not accept the index name parameter. + command = [ 'samtools', 'index', dataset.file_name ] + proc = subprocess.Popen( args=command, stderr=open( stderr_name, 'wb' ) ) + exit_code = proc.wait() + if os.path.exists( os.path.join( dataset.file_name, '.bai' ) ): + shutil.move( os.path.join( dataset.file_name, '.bai' ), index_file.file_name ) + else: + open( stderr_name, 'ab+' ).write( 'Galaxy attempted to build the BAM index with samtools 1.0+ but failed\n') stderr = open( stderr_name ).read().strip() if stderr: if exit_code != 0: From 11ae48a70f9999e44894136716284b78900fdac2 Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Wed, 28 Jan 2015 11:58:23 -0500 Subject: [PATCH 2/3] Use a symlink to the dataset in the same directory as the MetadataTempFile as the input to samtools index so there's no clobber risk. Thanks Dan. --- lib/galaxy/datatypes/binary.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/galaxy/datatypes/binary.py b/lib/galaxy/datatypes/binary.py index ae86ae549e4..9d4129c464b 100644 --- a/lib/galaxy/datatypes/binary.py +++ b/lib/galaxy/datatypes/binary.py @@ -270,13 +270,18 @@ class Bam( Binary ): #Did index succeed? if exit_code == -6: # SIGABRT, most likely samtools 1.0+ which does not accept the index name parameter. - command = [ 'samtools', 'index', dataset.file_name ] - proc = subprocess.Popen( args=command, stderr=open( stderr_name, 'wb' ) ) - exit_code = proc.wait() - if os.path.exists( os.path.join( dataset.file_name, '.bai' ) ): - shutil.move( os.path.join( dataset.file_name, '.bai' ), index_file.file_name ) - else: - open( stderr_name, 'ab+' ).write( 'Galaxy attempted to build the BAM index with samtools 1.0+ but failed\n') + dataset_symlink = os.path.join( os.path.dirname( index_file.file_name ), + '__dataset_%d_%s' % ( dataset.id, os.path.basename( index_file.file_name ) ) ) + os.symlink( dataset.file_name, dataset_symlink ) + try: + command = [ 'samtools', 'index', dataset_symlink ] + proc = subprocess.Popen( args=command, stderr=open( stderr_name, 'wb' ) ) + exit_code = proc.wait() + shutil.move( dataset_symlink + '.bai', index_file.file_name ) + except Exception, e: + open( stderr_name, 'ab+' ).write( 'Galaxy attempted to build the BAM index with samtools 1.0+ but failed: %s\n' % e) + finally: + os.unlink( dataset_symlink ) stderr = open( stderr_name ).read().strip() if stderr: if exit_code != 0: From 43159db9fef7d2d14586d5bf8946f4a79e5f2062 Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Wed, 28 Jan 2015 13:25:11 -0500 Subject: [PATCH 3/3] Use subprocess.call() rather than Popen(), thanks Nicola. --- lib/galaxy/datatypes/binary.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/galaxy/datatypes/binary.py b/lib/galaxy/datatypes/binary.py index 9d4129c464b..4fe54baabff 100644 --- a/lib/galaxy/datatypes/binary.py +++ b/lib/galaxy/datatypes/binary.py @@ -275,8 +275,7 @@ class Bam( Binary ): os.symlink( dataset.file_name, dataset_symlink ) try: command = [ 'samtools', 'index', dataset_symlink ] - proc = subprocess.Popen( args=command, stderr=open( stderr_name, 'wb' ) ) - exit_code = proc.wait() + exit_code = subprocess.call( args=command, stderr=open( stderr_name, 'wb' ) ) shutil.move( dataset_symlink + '.bai', index_file.file_name ) except Exception, e: open( stderr_name, 'ab+' ).write( 'Galaxy attempted to build the BAM index with samtools 1.0+ but failed: %s\n' % e)