fasta.validator

Written by Lucas Sinclair. MIT Licensed. Contact at www.sinclair.bio

  1#!/usr/bin/env python3
  2# -*- coding: utf-8 -*-
  3
  4"""
  5Written by Lucas Sinclair.
  6MIT Licensed.
  7Contact at www.sinclair.bio
  8"""
  9
 10# Built-in modules #
 11import sys, platform
 12
 13# Internal modules #
 14from fasta.exceptions import ValidationError
 15
 16# First party modules #
 17from autopaths.tmp_path import new_temp_dir
 18from plumbing.apt_pkg   import get_apt_packages, check_apt_exists
 19from plumbing.scraping  import download_from_url
 20from plumbing.check_cmd_found import check_cmd
 21
 22# Third party modules #
 23if platform.system() == 'Windows': import pbs3 as sh
 24else: import sh
 25
 26###############################################################################
 27class Validator:
 28    """
 29    Determine if a FASTQ file is valid by calling
 30    https://github.com/statgen/fastQValidator
 31    on the file.
 32    Documentation is at:
 33    https://genome.sph.umich.edu/wiki/FastQValidator
 34    """
 35
 36    def __repr__(self):
 37        msg = '<%s object on "%s">'
 38        return msg % (self.__class__.__name__, self.path)
 39
 40    def __init__(self, path):
 41        self.path = path
 42
 43    #---------------------------- Installing ---------------------------------#
 44    apt_packages = ['g++', 'libssl-dev', 'zlib1g-dev']
 45
 46    @classmethod
 47    def check_installed(cls, exception=True):
 48        """
 49        Try to determine if the fastQValidator software is installed and
 50        accessible on this machine.
 51        """
 52        return check_cmd('fastQValidator', exception, cls.install.__doc__)
 53
 54    @classmethod
 55    def install(cls, prefix="~/programs/fastQValidator/"):
 56        """
 57        To automatically download and install the fastQValidator software
 58        on this computer and for the current user, type these commands in
 59        python:
 60
 61            >>> from fasta.validator import Validator
 62            >>> Validator.install()
 63        """
 64        # Check we are on an OS with aptitude #
 65        check_apt_exists()
 66        # Start with the required apt packages #
 67        get_apt_packages(cls.apt_packages, verbose=True)
 68        # Download tarball 1 #
 69        tmp_dir_1 = new_temp_dir()
 70        tgz_url_1 = 'https://github.com/statgen/libStatGen/archive/master.tar.gz'
 71        tgz_loc_1 = download_from_url(tgz_url_1, tmp_dir_1, stream=True, progress=True)
 72        src_dir_1 = tgz_loc_1.untargz_to()
 73        # Download tarball 2 #
 74        tmp_dir_2 = new_temp_dir()
 75        tgz_url_2 = 'https://github.com/statgen/fastQValidator/archive/master.tar.gz'
 76        tgz_loc_2 = download_from_url(tgz_url_2, tmp_dir_2, stream=True, progress=True)
 77        src_dir_2 = tgz_loc_2.untargz_to()
 78        # Uncompressed 1 #
 79        src_dir_1 = src_dir_1.sub_directory
 80        # Uncompressed 2 #
 81        src_dir_2 = src_dir_2.sub_directory
 82        # Make 1 #
 83        sh.make('-C', src_dir_1, _out=sys.stdout, _err=sys.stderr)
 84        # Make 2 #
 85        sh.make('-C', src_dir_2, 'LIB_PATH_FASTQ_VALIDATOR=%s' % src_dir_1,
 86                  _out=sys.stdout, _err=sys.stderr,)
 87        # Move the executable #
 88        binary = src_dir_2 + 'bin/fastQValidator'
 89        path = binary.move_to(prefix, overwrite=True)
 90        # The directory that contains the executable #
 91        bin_dir = path.directory.with_tilda[:-1].replace('~', '$HOME')
 92        # Suggest adding to the $PATH #
 93        print("\n fastQValidator was installed successfully. You should now "
 94              "add this line to your .bash_profile: \n\n    "
 95              "export PATH=%s:$PATH\n" % bin_dir)
 96
 97    #---------------------------- Running ------------------------------------#
 98    def __call__(self, exception=True):
 99        # Default message #
100        msg = "The fastq file '%s' failed to validate." % self.path
101        # Check it is installed #
102        self.check_installed()
103        # Run software #
104        try:
105            result = sh.fastQValidator('--file', self.path)
106        except sh.ErrorReturnCode as error_msg:
107            if exception: raise ValidationError(msg) from error_msg
108            return False
109        # Check result #
110        if "FASTQ_SUCCESS" in result: return True
111        # Default case #
112        msg += "The result:\n\n    %s\n" % result
113        if exception: raise ValidationError(msg)
114        return False
class Validator:
 28class Validator:
 29    """
 30    Determine if a FASTQ file is valid by calling
 31    https://github.com/statgen/fastQValidator
 32    on the file.
 33    Documentation is at:
 34    https://genome.sph.umich.edu/wiki/FastQValidator
 35    """
 36
 37    def __repr__(self):
 38        msg = '<%s object on "%s">'
 39        return msg % (self.__class__.__name__, self.path)
 40
 41    def __init__(self, path):
 42        self.path = path
 43
 44    #---------------------------- Installing ---------------------------------#
 45    apt_packages = ['g++', 'libssl-dev', 'zlib1g-dev']
 46
 47    @classmethod
 48    def check_installed(cls, exception=True):
 49        """
 50        Try to determine if the fastQValidator software is installed and
 51        accessible on this machine.
 52        """
 53        return check_cmd('fastQValidator', exception, cls.install.__doc__)
 54
 55    @classmethod
 56    def install(cls, prefix="~/programs/fastQValidator/"):
 57        """
 58        To automatically download and install the fastQValidator software
 59        on this computer and for the current user, type these commands in
 60        python:
 61
 62            >>> from fasta.validator import Validator
 63            >>> Validator.install()
 64        """
 65        # Check we are on an OS with aptitude #
 66        check_apt_exists()
 67        # Start with the required apt packages #
 68        get_apt_packages(cls.apt_packages, verbose=True)
 69        # Download tarball 1 #
 70        tmp_dir_1 = new_temp_dir()
 71        tgz_url_1 = 'https://github.com/statgen/libStatGen/archive/master.tar.gz'
 72        tgz_loc_1 = download_from_url(tgz_url_1, tmp_dir_1, stream=True, progress=True)
 73        src_dir_1 = tgz_loc_1.untargz_to()
 74        # Download tarball 2 #
 75        tmp_dir_2 = new_temp_dir()
 76        tgz_url_2 = 'https://github.com/statgen/fastQValidator/archive/master.tar.gz'
 77        tgz_loc_2 = download_from_url(tgz_url_2, tmp_dir_2, stream=True, progress=True)
 78        src_dir_2 = tgz_loc_2.untargz_to()
 79        # Uncompressed 1 #
 80        src_dir_1 = src_dir_1.sub_directory
 81        # Uncompressed 2 #
 82        src_dir_2 = src_dir_2.sub_directory
 83        # Make 1 #
 84        sh.make('-C', src_dir_1, _out=sys.stdout, _err=sys.stderr)
 85        # Make 2 #
 86        sh.make('-C', src_dir_2, 'LIB_PATH_FASTQ_VALIDATOR=%s' % src_dir_1,
 87                  _out=sys.stdout, _err=sys.stderr,)
 88        # Move the executable #
 89        binary = src_dir_2 + 'bin/fastQValidator'
 90        path = binary.move_to(prefix, overwrite=True)
 91        # The directory that contains the executable #
 92        bin_dir = path.directory.with_tilda[:-1].replace('~', '$HOME')
 93        # Suggest adding to the $PATH #
 94        print("\n fastQValidator was installed successfully. You should now "
 95              "add this line to your .bash_profile: \n\n    "
 96              "export PATH=%s:$PATH\n" % bin_dir)
 97
 98    #---------------------------- Running ------------------------------------#
 99    def __call__(self, exception=True):
100        # Default message #
101        msg = "The fastq file '%s' failed to validate." % self.path
102        # Check it is installed #
103        self.check_installed()
104        # Run software #
105        try:
106            result = sh.fastQValidator('--file', self.path)
107        except sh.ErrorReturnCode as error_msg:
108            if exception: raise ValidationError(msg) from error_msg
109            return False
110        # Check result #
111        if "FASTQ_SUCCESS" in result: return True
112        # Default case #
113        msg += "The result:\n\n    %s\n" % result
114        if exception: raise ValidationError(msg)
115        return False

Determine if a FASTQ file is valid by calling https://github.com/statgen/fastQValidator on the file. Documentation is at: https://genome.sph.umich.edu/wiki/FastQValidator

Validator(path)
41    def __init__(self, path):
42        self.path = path
path
apt_packages = ['g++', 'libssl-dev', 'zlib1g-dev']
@classmethod
def check_installed(cls, exception=True):
47    @classmethod
48    def check_installed(cls, exception=True):
49        """
50        Try to determine if the fastQValidator software is installed and
51        accessible on this machine.
52        """
53        return check_cmd('fastQValidator', exception, cls.install.__doc__)

Try to determine if the fastQValidator software is installed and accessible on this machine.

@classmethod
def install(cls, prefix='~/programs/fastQValidator/'):
55    @classmethod
56    def install(cls, prefix="~/programs/fastQValidator/"):
57        """
58        To automatically download and install the fastQValidator software
59        on this computer and for the current user, type these commands in
60        python:
61
62            >>> from fasta.validator import Validator
63            >>> Validator.install()
64        """
65        # Check we are on an OS with aptitude #
66        check_apt_exists()
67        # Start with the required apt packages #
68        get_apt_packages(cls.apt_packages, verbose=True)
69        # Download tarball 1 #
70        tmp_dir_1 = new_temp_dir()
71        tgz_url_1 = 'https://github.com/statgen/libStatGen/archive/master.tar.gz'
72        tgz_loc_1 = download_from_url(tgz_url_1, tmp_dir_1, stream=True, progress=True)
73        src_dir_1 = tgz_loc_1.untargz_to()
74        # Download tarball 2 #
75        tmp_dir_2 = new_temp_dir()
76        tgz_url_2 = 'https://github.com/statgen/fastQValidator/archive/master.tar.gz'
77        tgz_loc_2 = download_from_url(tgz_url_2, tmp_dir_2, stream=True, progress=True)
78        src_dir_2 = tgz_loc_2.untargz_to()
79        # Uncompressed 1 #
80        src_dir_1 = src_dir_1.sub_directory
81        # Uncompressed 2 #
82        src_dir_2 = src_dir_2.sub_directory
83        # Make 1 #
84        sh.make('-C', src_dir_1, _out=sys.stdout, _err=sys.stderr)
85        # Make 2 #
86        sh.make('-C', src_dir_2, 'LIB_PATH_FASTQ_VALIDATOR=%s' % src_dir_1,
87                  _out=sys.stdout, _err=sys.stderr,)
88        # Move the executable #
89        binary = src_dir_2 + 'bin/fastQValidator'
90        path = binary.move_to(prefix, overwrite=True)
91        # The directory that contains the executable #
92        bin_dir = path.directory.with_tilda[:-1].replace('~', '$HOME')
93        # Suggest adding to the $PATH #
94        print("\n fastQValidator was installed successfully. You should now "
95              "add this line to your .bash_profile: \n\n    "
96              "export PATH=%s:$PATH\n" % bin_dir)

To automatically download and install the fastQValidator software on this computer and for the current user, type these commands in python:

>>> from fasta.validator import Validator
>>> Validator.install()