Example #1
0
def run_command(args):
    if DotNetSystem is None:
        # cpython
        stdout = NamedTemporaryFile()
        stderr = NamedTemporaryFile()
        res = subprocess.call(args, stdin=PIPE, stdout=stdout, stderr=stderr)
        return stdout.read(), stderr.read(), res        
    else:
        # IronPython
        stdout = StringIO()
        stderr = StringIO()
        res = subprocess.call(args, stdin=PIPE, stdout=stdout, stderr=stderr)
        return stdout.getvalue(), stderr.getvalue(), res
Example #2
0
    def test_cons_recomb(self):
        vcf_fpath = os.path.join(TEST_DATA_DIR, 'scaff000025.vcf.gz')
        snvs = VCFReader(open(vcf_fpath)).parse_snvs()
        snv_filter = WeirdRecombFilter(pop_type='ril_self')
        flt_snvs = snv_filter.filter_snvs(snvs)
        assert len(list(flt_snvs)) == 258
        assert snv_filter.not_fitted_counter['no close region left'] == 10
        fhand = NamedTemporaryFile(suffix='.png')
        flt_snvs = snv_filter.plot_recomb_at_0_dist_hist(fhand)
        assert len(snv_filter.recomb_rates['ok']) == 245
        assert len(snv_filter.recomb_rates['ok_conf_is_None']) == 13
        assert len(snv_filter.recomb_rates['not_ok']) == 14

        snvs = VCFReader(open(vcf_fpath)).parse_snvs()
        snv_filter = WeirdRecombFilter(pop_type='ril_self',
                                       max_zero_dist_recomb=0.07,
                                       alpha_recomb_0=None)
        flt_snvs = snv_filter.filter_snvs(snvs)
        assert len(list(flt_snvs)) == 266
        assert snv_filter.not_fitted_counter['no close region left'] == 10
        fhand = NamedTemporaryFile(suffix='.png')
        flt_snvs = snv_filter.plot_recomb_at_0_dist_hist(fhand)
        assert len(snv_filter.recomb_rates['ok']) == 0
        assert len(snv_filter.recomb_rates['ok_conf_is_None']) == 266
        assert len(snv_filter.recomb_rates['not_ok']) == 6

        fhand = StringIO()
        snv_filter.write_log(fhand)
        assert 'SNVs processed: 282' in fhand.getvalue()
Example #3
0
    def test_cons_recomb(self):
        vcf_fpath = os.path.join(TEST_DATA_DIR, "scaff000025.vcf.gz")
        snvs = VCFReader(open(vcf_fpath)).parse_snvs()
        snv_filter = WeirdRecombFilter(pop_type="ril_self")
        flt_snvs = snv_filter.filter_snvs(snvs)
        assert len(list(flt_snvs)) == 258
        assert snv_filter.not_fitted_counter["no close region left"] == 10
        fhand = NamedTemporaryFile(suffix=".png")
        flt_snvs = snv_filter.plot_recomb_at_0_dist_hist(fhand)
        assert len(snv_filter.recomb_rates["ok"]) == 245
        assert len(snv_filter.recomb_rates["ok_conf_is_None"]) == 13
        assert len(snv_filter.recomb_rates["not_ok"]) == 14

        snvs = VCFReader(open(vcf_fpath)).parse_snvs()
        snv_filter = WeirdRecombFilter(pop_type="ril_self", max_zero_dist_recomb=0.07, alpha_recomb_0=None)
        flt_snvs = snv_filter.filter_snvs(snvs)
        assert len(list(flt_snvs)) == 266
        assert snv_filter.not_fitted_counter["no close region left"] == 10
        fhand = NamedTemporaryFile(suffix=".png")
        flt_snvs = snv_filter.plot_recomb_at_0_dist_hist(fhand)
        assert len(snv_filter.recomb_rates["ok"]) == 0
        assert len(snv_filter.recomb_rates["ok_conf_is_None"]) == 266
        assert len(snv_filter.recomb_rates["not_ok"]) == 6

        fhand = StringIO()
        snv_filter.write_log(fhand)
        assert "SNVs processed: 282" in fhand.getvalue()
Example #4
0
    def _retrieve_file(self, url: str, use_tempfile=True) -> str:
        """
        Retrieve a file (currently only from FTP).

        If use_tempfile is False, the file is read, stripped and then the
        contents are returned. If use_tempfile is True, the data is written
        to a temporary file, and the path of this file is returned.
        It is the responsibility of the caller to unlink thi spath later.

        If the URL ends in .gz, the file is gunzipped before being processed.
        """
        url_parsed = urlparse(url)

        if not url_parsed.scheme == 'ftp':
            raise ValueError(
                f'Invalid URL: {url} - scheme {url_parsed.scheme} is not supported'
            )

        if use_tempfile:
            destination = NamedTemporaryFile(delete=False)
        else:
            destination = BytesIO()

        ftp = FTP(url_parsed.netloc)
        ftp.login()
        ftp.retrbinary(f'RETR {url_parsed.path}', destination.write)
        ftp.quit()

        if use_tempfile:
            if url.endswith('.gz'):
                zipped_file = destination
                zipped_file.close()
                destination = NamedTemporaryFile(delete=False)
                logger.debug(
                    f'Downloaded file is expected to be gzipped, gunzipping from {zipped_file.name}'
                )
                with gzip.open(zipped_file.name, 'rb') as f_in:
                    shutil.copyfileobj(f_in, destination)
                os.unlink(zipped_file.name)

            destination.close()

            logger.info(f'Downloaded {url} to {destination.name}')
            return destination.name
        else:
            value = destination.getvalue().decode(
                'ascii').strip()  # type: ignore
            logger.info(f'Downloaded {url}, contained {value}')
            return value