예제 #1
0
    def check_statement(self, statement):

        parts = statement.split(" ")
        r_samtools = {"out": self.executable}
        r_pysam = {"out": "pysam"}

        command = self.get_command(statement)

        # self.assertTrue(command in pysam.SAMTOOLS_DISPATCH)

        targets = [x for x in parts if "%(out)s" in x]
        samtools_targets = [x % r_samtools for x in targets]
        pysam_targets = [x % r_pysam for x in targets]

        pysam_method = getattr(self.module, command)

        # run samtools
        full_statement = re.sub("%\(out\)s", self.executable, statement)
        run_command(" ".join((self.executable, full_statement)))
        # sys.stdout.write("%s %s ok" % (command, self.executable))

        # run pysam
        if ">" in statement:
            assert parts[-2] == ">"
            parts = parts[:-2]

        # avoid interpolation to preserve string quoting, tab chars, etc.
        pysam_parts = [re.sub("%\(out\)s", "pysam", x) for x in parts[1:]]
        output = pysam_method(*pysam_parts, raw=True, catch_stdout=True)
        # sys.stdout.write(" pysam ok\n")
        if ">" in statement:
            with open(pysam_targets[-1], "wb") as outfile:
                if output is not None:
                    outfile.write(force_bytes(output))
        for samtools_target, pysam_target in zip(samtools_targets,
                                                 pysam_targets):
            if os.path.isdir(samtools_target):
                samtools_files = glob.glob(os.path.join(samtools_target, "*"))
                pysam_files = glob.glob(os.path.join(pysam_target, "*"))
                self.assertEqual(len(samtools_files), len(pysam_files))
                # need to be able to exclude files like README, etc.
                continue
            else:
                samtools_files = [samtools_target]
                pysam_files = [pysam_target]

            for s, p in zip(samtools_files, pysam_files):
                binary_equal = checkBinaryEqual(s, p)
                error_msg = "%s failed: files %s and %s are not the same" % (
                    command, s, p)
                if binary_equal:
                    continue
                elif s.endswith(".bam"):
                    self.assertTrue(
                        check_samtools_view_equal(s, p, without_header=True),
                        error_msg)
                else:
                    check_lines_equal(self,
                                      s,
                                      p,
                                      filter_f=lambda x: x.startswith("#"),
                                      msg=error_msg)
예제 #2
0
    def check_statement(self, statement):

        parts = statement.split(" ")
        r_samtools = {"out": self.executable}
        r_pysam = {"out": "pysam"}

        command = parts[0]
        command = self.map_command.get(command, command)
        # self.assertTrue(command in pysam.SAMTOOLS_DISPATCH)

        targets = [x for x in parts if "%(out)s" in x]
        samtools_targets = [x % r_samtools for x in targets]
        pysam_targets = [x % r_pysam for x in targets]

        pysam_method = getattr(self.module, command)
        # run samtools
        full_statement = re.sub("%\(out\)s", self.executable, statement)
        run_command(" ".join((self.executable, full_statement)))
        # sys.stdout.write("%s %s ok" % (command, self.executable))

        # run pysam
        if ">" in statement:
            assert parts[-2] == ">"
            parts = parts[:-2]

        # avoid interpolation to preserve string quoting, tab chars, etc.
        pysam_parts = [re.sub("%\(out\)s", "pysam", x) for x in parts[1:]]
        output = pysam_method(*pysam_parts,
                              raw=True,
                              catch_stdout=True)
        # sys.stdout.write(" pysam ok\n")
        if ">" in statement:
            with open(pysam_targets[-1], "wb") as outfile:
                if output is not None:
                    outfile.write(force_bytes(output))

        for samtools_target, pysam_target in zip(samtools_targets,
                                                 pysam_targets):
            if os.path.isdir(samtools_target):
                samtools_files = glob.glob(os.path.join(
                    samtools_target, "*"))
                pysam_files = glob.glob(os.path.join(pysam_target, "*"))
                self.assertEqual(len(samtools_files), len(pysam_files))
                # need to be able to exclude files like README, etc.
                continue
            else:
                samtools_files = [samtools_target]
                pysam_files = [pysam_target]

            for s, p in zip(samtools_files, pysam_files):
                binary_equal = checkBinaryEqual(s, p)
                error_msg = "%s failed: files %s and %s are not the same" % (command, s, p)
                if binary_equal:
                    continue
                if s.endswith(".bam"):
                    self.assertTrue(
                        check_samtools_view_equal(
                            s, p, without_header=True),
                        error_msg)
                check_lines_equal(
                    self, s, p,
                    filter_f=lambda x: x.startswith("#"),
                    msg=error_msg)