From 46805eb07d386e59ab89955c2e9c573ef34f6adb Mon Sep 17 00:00:00 2001 From: Stefan Bienert <stefan.bienert@unibas.ch> Date: Tue, 18 Aug 2015 16:35:12 +0200 Subject: [PATCH] Simplifying handling fasta --- core/pymod/core/pm3argparse.py | 29 +++++++++++++++++++---------- core/tests/test_pm3argparse.py | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/core/pymod/core/pm3argparse.py b/core/pymod/core/pm3argparse.py index 8233e289..a606c81f 100644 --- a/core/pymod/core/pm3argparse.py +++ b/core/pymod/core/pm3argparse.py @@ -213,6 +213,24 @@ class PM3OptionsNamespace(object): if 'ALIGNMENT' in activated: self._PostProcessAlignment() + def _FetchAlnFromFastaOpt(self, argstr): + """ + Dissasemble an argument to '--fasta' into an alignment and return. + """ + # get which element of the argument list is the target identifier and + # which one is the filename + if argstr[0].startswith('trg:'): + trgname = argstr[0][4:] + seqfile = argstr[1] + elif argstr[1].startswith('trg:'): + trgname = argstr[1][4:] + seqfile = argstr[0] + else: + helper.MsgErrorAndExit("'--fasta %s' requires " % ' '.join(argstr)+ + "one argument prefixed with 'trg:' marking "+ + "the target sequence name", 11) + return trgname, seqfile + def _PostProcessAlignment(self): #pylint: disable=no-member #pylint: disable=attribute-defined-outside-init @@ -223,16 +241,7 @@ class PM3OptionsNamespace(object): self.alignments = seq.AlignmentList() if self.fasta: for src in self.fasta: - if src[0].startswith('trg:'): - trgname = src[0][4:] - seqfile = src[1] - elif src[1].startswith('trg:'): - trgname = src[1][4:] - seqfile = src[0] - else: - helper.MsgErrorAndExit("'--fasta' requires one argument "+ - "prefixed with 'trg:' marking the "+ - "target sequence name", 11) + trgname, seqfile = self._FetchAlnFromFastaOpt(src) if not len(trgname): helper.MsgErrorAndExit("'--fasta' requires argument "+ "'trg:' defining the "+ diff --git a/core/tests/test_pm3argparse.py b/core/tests/test_pm3argparse.py index cc2f4aed..10ad2777 100644 --- a/core/tests/test_pm3argparse.py +++ b/core/tests/test_pm3argparse.py @@ -87,7 +87,7 @@ class PM3ArgParseTests(unittest.TestCase): parser.Parse(['--fasta', 'foo', 'bar']) self.assertEqual(ecd.exception.code, 11) self.assertEqual(self.log.messages['ERROR'][0], - "'--fasta' requires one "+ + "'--fasta foo bar' requires one "+ "argument prefixed with 'trg:' marking the target "+ "sequence name") -- GitLab