Skip to content
Snippets Groups Projects
Commit 67c858c8 authored by Bienchen's avatar Bienchen
Browse files

Simplification

parent 7b44a3d6
Branches
Tags
No related merge requests found
...@@ -235,9 +235,38 @@ class PM3OptionsNamespace(object): ...@@ -235,9 +235,38 @@ class PM3OptionsNamespace(object):
"argument 'trg:' defining the "+ "argument 'trg:' defining the "+
"target sequence name, empty one "+ "target sequence name, empty one "+
"found: '%s'" % ' '.join(argstr), 14) "found: '%s'" % ' '.join(argstr), 14)
# checking that alignment file exists
helper.FileExists("Alignment", 12, seqfile) helper.FileExists("Alignment", 12, seqfile)
# checking if alignment file has 'gz' extension
is_gz = helper.FileGzip("Alignment", 13, seqfile) 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): def _PostProcessAlignment(self):
#pylint: disable=no-member #pylint: disable=no-member
...@@ -249,33 +278,7 @@ class PM3OptionsNamespace(object): ...@@ -249,33 +278,7 @@ class PM3OptionsNamespace(object):
self.alignments = seq.AlignmentList() self.alignments = seq.AlignmentList()
if self.fasta: if self.fasta:
for src in self.fasta: for src in self.fasta:
trgname, seqfile, is_gz = self._FetchAlnFromFastaOpt(src) trgname, seqfile, is_gz, aln = 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()
# check alignment # check alignment
nos = aln.GetCount() nos = aln.GetCount()
if nos > 2: if nos > 2:
......
...@@ -163,8 +163,8 @@ class PM3ArgParseTests(unittest.TestCase): ...@@ -163,8 +163,8 @@ class PM3ArgParseTests(unittest.TestCase):
parser.Parse(['--fasta', 'trg:foo', 'data/fasta/alignment.fas']) parser.Parse(['--fasta', 'trg:foo', 'data/fasta/alignment.fas'])
self.assertEqual(ecd.exception.code, 15) self.assertEqual(ecd.exception.code, 15)
self.assertEqual(self.log.messages['ERROR'][0], self.assertEqual(self.log.messages['ERROR'][0],
"'--fasta' refers to an empty file or its in the "+ "'--fasta trg:foo data/fasta/alignment.fas' refers "+
"wrong format: data/fasta/alignment.fas") "to an empty file or its in the wrong format.")
def testAddAlignmentToMany(self): def testAddAlignmentToMany(self):
parser = pm3argparse.PM3ArgumentParser(__doc__, action=False) parser = pm3argparse.PM3ArgumentParser(__doc__, action=False)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment