Commit 81878f97 authored by Sander Bollen's avatar Sander Bollen
Browse files

fetch samtools version with regex, so that it handles version

strings with suffixes. Warn in stead of fail when version cannot be
determined.
parent c2c6fee7
......@@ -14,7 +14,8 @@ from pysam import AlignmentFile
from .models import Primer, Region
from .primer3 import Primer3, parse_primer3_output
from .utils import NoPrimersException, calc_gc, NEW_VCF, generate_fastq_from_primers
from .utils import (NoPrimersException, calc_gc, NEW_VCF,
generate_fastq_from_primers, is_at_least_version_samtools)
PRIMER3_SCRIPT = os.path.join(os.path.join(os.path.dirname(__file__) ,"static"), 'getprimers.sh')
......@@ -67,8 +68,12 @@ def samtools_version_check(samtools_exe):
lines = handle.readlines()
version_line = lines[0]
version = tuple(map(int, version_line.strip().split(" ")[-1].split(".")))
return version >= (1, 3, 0)
try:
is_at_least_version_samtools(version_line, (1, 3))
except ValueError:
warnings.warn("Could not determine samtools version. "
"Assuming it is at least as recent as version 1.3")
return True
def aln_primers(primers, bwa_exe=None, samtools_exe=None, ref=None, output_bam=None):
......
......@@ -6,6 +6,7 @@ __author__ = 'ahbbollen'
from datetime import datetime
import hashlib
import sys
import re
import vcf
......@@ -32,6 +33,35 @@ def _is_vcf_version_at_least_0_6_8():
NEW_VCF = _is_vcf_version_at_least_0_6_8()
def is_at_least_version_samtools(version_str, version_tupl):
"""
Check whether a samtools version string is at least a certain version
:param version_str: the version string of emitted by samtools --version
This should be first line of that output. If it contains spaces,
only the last word is considered. May contain build and/or commit hashes.
E.g '1.9' or 'samtools 1.9'
:param version_tupl: 2-tuple of ints of major-minor version.
:return: Boolean
:raises ValueError: if unparsable string
:raises ValueError: if version_tupl is not a 2-tuple
:raises TypeError: if version_tupl contains non-integer
"""
regex = re.compile('^(\d+)\.(\d+).*$')
if "_" not in version_str:
match = regex.match(version_str)
else:
match = regex.match(version_str.split(" ")[-1])
if len(version_tupl) != 2:
raise ValueError
if not all([isinstance(x, int) for x in version_tupl]):
raise TypeError
if match is None:
raise ValueError("Unparsable version string")
return (int(match.group(1)), int(match.group(2))) >= version_tupl
def calc_gc(sequence):
"""
Calculates GC percentage from DNA sequence
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment