From 67c858c8b55852bb68134ea5b9ed4a0cdfb7cd41 Mon Sep 17 00:00:00 2001
From: Stefan Bienert <stefan.bienert@unibas.ch>
Date: Tue, 18 Aug 2015 17:39:29 +0200
Subject: [PATCH] Simplification

---
 core/pymod/core/pm3argparse.py | 59 ++++++++++++++++++----------------
 core/tests/test_pm3argparse.py |  4 +--
 2 files changed, 33 insertions(+), 30 deletions(-)

diff --git a/core/pymod/core/pm3argparse.py b/core/pymod/core/pm3argparse.py
index 69601078..3765ecf1 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 948d20f1..8bd20a84 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)
-- 
GitLab