Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use pysam instead of samtools binary
This will make it possible to remove the requirement of having `samtools`
installed when testing tools, e.g. in the tools-iuc TravisCI setup:
https://github.com/galaxyproject/tools-iuc/blob/master/.travis.yml#L59
  • Loading branch information
nsoranzo committed Aug 22, 2017
1 parent 63d1d42 commit d3ae735
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/galaxy/tools/verify/__init__.py
Expand Up @@ -7,9 +7,13 @@
import os
import re
import shutil
import subprocess
import tempfile

try:
import pysam
except ImportError:
pysam = None

from galaxy.util.compression_utils import get_fileobj

from .asserts import verify_assertions
Expand Down Expand Up @@ -132,10 +136,14 @@ def _bam_to_sam(local_name, temp_name):
temp_local = tempfile.NamedTemporaryFile(suffix='.sam', prefix='local_bam_converted_to_sam_')
fd, temp_temp = tempfile.mkstemp(suffix='.sam', prefix='history_bam_converted_to_sam_')
os.close(fd)
command = 'samtools view -h -o "%s" "%s"' % (temp_local.name, local_name)
check_command(command, 'Converting local (test-data) bam to sam')
command = 'samtools view -h -o "%s" "%s"' % (temp_temp, temp_name)
check_command(command, 'Converting history bam to sam ')
try:
pysam.view('-h', '-o%s' % temp_local.name, local_name)
except Exception as e:
raise Exception("Converting local (test-data) BAM to SAM failed: %s" % e)
try:
pysam.view('-h', '-o%s' % temp_temp, temp_name)
except Exception as e:
raise Exception("Converting history BAM to SAM failed: %s" % e)
os.remove(temp_name)
return temp_local, temp_temp

Expand All @@ -153,18 +161,6 @@ def _verify_checksum(data, checksum_type, expected_checksum_value):
raise AssertionError(message)


def check_command(command, description):
"""Verify a command runs with an exit code of 0."""
# TODO: also collect ``which samtools`` and ``samtools --version``
p = subprocess.Popen(args=command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(stdout, stderr) = p.communicate()
if p.returncode:
template = description
template += " failed: (cmd=[%s], stdout=[%s], stderr=[%s])"
message = template % (command, stdout, stderr)
raise AssertionError(message)


def files_diff(file1, file2, attributes=None):
"""Check the contents of 2 files for differences."""
def get_lines_diff(diff):
Expand Down

0 comments on commit d3ae735

Please sign in to comment.