diff --git a/core/pymod/core/pm3argparse.py b/core/pymod/core/pm3argparse.py index 69601078cb9fe2c22e27fe4795228fe17fbe1bb6..3765ecf168536244fb00e11689c482842b39d6e5 100644 --- a/core/pymod/core/pm3argparse.py +++ b/core/pymod/core/pm3argparse.py @@ -235,9 +235,38 @@ class PM3OptionsNamespace(object): "argument 'trg:' defining the "+ "target sequence name, empty one "+ "found: '%s'" % ' '.join(argstr), 14) + # checking that alignment file exists helper.FileExists("Alignment", 12, seqfile) + # checking if alignment file has 'gz' extension is_gz = helper.FileGzip("Alignment", 13, seqfile) - return trgname, seqfile, is_gz + # loading the alignment, switch for gzip + readfile = seqfile + if is_gz: + zip_fh = gzip.open(seqfile) + unzip_str = zip_fh.read() + zip_fh.close() + unzip_file = tempfile.NamedTemporaryFile(mode='w', + suffix='.fas') + unzip_file.write(unzip_str) + unzip_file.flush() + readfile = unzip_file.name + try: + aln = io.LoadAlignment(readfile, format="fasta") + except Exception, exc: #pylint: disable=broad-except + if exc.message == 'Bad FASTA file: File is empty': + helper.MsgErrorAndExit("'--fasta %s' " % ' '.join(argstr)+ + "refers to an empty file or its in the "+ + "wrong format.", 15) + elif exc.message == 'sequences have different lengths': + helper.MsgErrorAndExit("'--fasta %s': " % ' '.join(argstr)+ + "sequences in the alignment "+ + "have different length.", 18) + else: + raise + finally: + if is_gz: + unzip_file.close() + return trgname, seqfile, is_gz, aln def _PostProcessAlignment(self): #pylint: disable=no-member @@ -249,33 +278,7 @@ class PM3OptionsNamespace(object): self.alignments = seq.AlignmentList() if self.fasta: for src in self.fasta: - trgname, seqfile, is_gz = self._FetchAlnFromFastaOpt(src) - readfile = seqfile - if is_gz: - zip_fh = gzip.open(seqfile) - unzip_str = zip_fh.read() - zip_fh.close() - unzip_file = tempfile.NamedTemporaryFile(mode='w', - suffix='.fas') - unzip_file.write(unzip_str) - unzip_file.flush() - readfile = unzip_file.name - try: - aln = io.LoadAlignment(readfile, format="fasta") - except Exception, exc: #pylint: disable=broad-except - if exc.message == 'Bad FASTA file: File is empty': - helper.MsgErrorAndExit("'--fasta' refers to an empty "+\ - "file or its in the wrong "+ - "format: %s" % seqfile, 15) - elif exc.message == 'sequences have different lengths': - helper.MsgErrorAndExit("'--fasta %s': " % ' '.join(src)+ - "sequences in the alignment "+ - "have different length.", 18) - else: - raise - finally: - if is_gz: - unzip_file.close() + trgname, seqfile, is_gz, aln = self._FetchAlnFromFastaOpt(src) # check alignment nos = aln.GetCount() if nos > 2: diff --git a/core/tests/test_pm3argparse.py b/core/tests/test_pm3argparse.py index 948d20f116f10c8689aac474f1541167907ba1ca..8bd20a84b36636fc702c63119a7d5d0e7537e686 100644 --- a/core/tests/test_pm3argparse.py +++ b/core/tests/test_pm3argparse.py @@ -163,8 +163,8 @@ class PM3ArgParseTests(unittest.TestCase): parser.Parse(['--fasta', 'trg:foo', 'data/fasta/alignment.fas']) self.assertEqual(ecd.exception.code, 15) self.assertEqual(self.log.messages['ERROR'][0], - "'--fasta' refers to an empty file or its in the "+ - "wrong format: data/fasta/alignment.fas") + "'--fasta trg:foo data/fasta/alignment.fas' refers "+ + "to an empty file or its in the wrong format.") def testAddAlignmentToMany(self): parser = pm3argparse.PM3ArgumentParser(__doc__, action=False)