Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/raw-rev/bc1a7595d9d1
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 07:50:34 2012
Кодировка:

# HG changeset patch
# User Daniil Alexeyevsky
# Date 1339686328 -14400
# Node ID bc1a7595d9d18574a4ace53d76bdd5e680b79de3
# Parent 2b3cad50c2b138a4ee5de5aae1d304a642eb27c8
Clean rewrite or markup_to_file.py util; --markup is now optional and allows specifying multiple markups as a comma-separated list

diff -r 2b3cad50c2b1 -r bc1a7595d9d1 allpy/util.py
--- a/allpy/util.py Sun Jun 10 16:08:47 2012 +0400
+++ b/allpy/util.py Thu Jun 14 19:05:28 2012 +0400
@@ -3,6 +3,8 @@
import sys
import warnings
import os
+import inspect
+import functools
from tempfile import mkstemp
from StringIO import StringIO

@@ -43,10 +45,43 @@
"""Clone of str that user may add attributes to."""
pass

-def deprecated(message):
+def deprecated(message=None, what=None, in_favor=None, removed_in=None):
"""Warn about function being deprecated."""
+ if not message:
+ if what is None:
+ frame = inspect.currentframe().f_back
+ caller = []
+ if inspect.getmodule(frame):
+ caller.append(inspect.getmodule(frame).__name__)
+ if 'self' in frame.f_locals:
+ caller.append(frame.f_locals['self'].__class__.__name__)
+ elif 'cls' in frame.f_locals:
+ caller.append(frame.f_locals['cls'].__name__)
+ caller.append(frame.f_code.co_name)
+ what = ".".join(caller) + "(...)"
+ message = "{0} is deprecated".format(what)
+ if in_favor:
+ message = "{0} in favor of {1}".format(message, in_favor)
+ if removed_in:
+ message = "{0}; will be removed in allpy version {1}".format(message, removed_in)
warnings.warn(message, DeprecationWarning, stacklevel=2)

+def Deprecated(message=None, in_favor=None, removed_in=None):
+ def decorator(function):
+ @functools.wraps(function)
+ def decorated(*args, **kws):
+ text = "{0} is deprecated".format(function)
+ if in_favor:
+ text = "{0} in favor of {1}".format(text, in_favor)
+ if removed_in:
+ text = "{0}\n\nWill be removed in allpy version {1}.".format(text, removed_in)
+ if message:
+ text = "{0}\n\n{1}".format(text, message)
+ warnings.warn(text, DeprecationWarning, stacklevel=2)
+ return function(*args, **kws)
+ return decorated
+ return decorator
+
class Silence(object):
"""Context manager for use with `with`.

diff -r 2b3cad50c2b1 -r bc1a7595d9d1 utils/markup_to_file.py
--- a/utils/markup_to_file.py Sun Jun 10 16:08:47 2012 +0400
+++ b/utils/markup_to_file.py Thu Jun 14 19:05:28 2012 +0400
@@ -3,51 +3,53 @@
import sys
import optparse
import copy
-from allpy import protein, markups
+from allpy import protein, markups, util

-parser = optparse.OptionParser()
-parser.add_option("--input-format", default="markup",
- help="Format of the input file")
-parser.add_option("-f", "--output-format", default="fasta",
- help="Format of the output file")
-parser.add_option("-i", "--input-file",
- help="Input file (defaults to stdin)")
-parser.add_option("-o", "--output-file",
- help="Output file (defaults to stdout)")
-parser.add_option("-m", "--markup",
- help="Markup to save as a sequence")
-options, args = parser.parse_args()
+def add_markup(alignment, sequence, markup):
+ new = copy.deepcopy(sequence)
+ new.name = "markup_{0}|{1}".format(markup.name, sequence.name)
+ for new_monomer, monomer in zip(new, sequence):
+ new_monomer.code1 = str(markup.get(monomer, '-'))
+ assert len(new_monomer.code1) == 1
+ assert new_monomer.code1 not in (markup.separator, markup.quotes)

-if not options.markup:
- parser.error("You must specify -m argument")
+ sequence.add_markup('index')
+ for column in alignment.columns:
+ if sequence in column:
+ column[new] = new[column[sequence].index]
+ sequence.remove_markup('index')

-infile, outfile = sys.stdin, sys.stdout
-if options.input_file is not None:
- infile = open(options.input_file)
-if options.output_file is not None:
- outfile = open(options.output_file, "w")
+ alignment.sequences.insert(alignment.sequences.index(sequence)+1, new)

-aln = protein.Alignment().append_file(infile, format=options.input_format)
+def main():
+ aln = protein.Alignment().append_file(
+ util.open(options.input_file), format=options.input_format)

-mseqs = copy.deepcopy(aln.sequences)
-for mseq in mseqs:
- mseq.name = "markup_%s|%s" % (options.markup, mseq.name)
- for monomer in mseq:
- monomer.code1 = str(getattr(monomer, options.markup, '-'))
-seqs = aln.sequences
+ for seq in list(aln.sequences):
+ for markup in seq.markups.values():
+ if not options.markups or markup.name in options.markups:
+ add_markup(aln, seq, markup)

-for seq, mseq in zip(seqs, mseqs):
- seq.add_markup('index')
- for column in aln.columns:
- if seq in column:
- column[mseq] = mseq[column[seq].index]
- seq.remove_markup('index')
+ aln.to_file(util.open(options.output_file, "w"), format=options.output_format)

-aln.sequences = []
-for seq, mseq in zip(seqs, mseqs):
- aln.sequences.append(seq)
- aln.sequences.append(mseq)
+if __name__ == "__main__":

-aln.to_file(outfile)
+ parser = optparse.OptionParser()
+ parser.add_option("--input-format", default="markup",
+ help="Format of the input file")
+ parser.add_option("-f", "--output-format", default="fasta",
+ help="Format of the output file")
+ parser.add_option("-i", "--input-file", default="-",
+ help="Input file (defaults to stdin)")
+ parser.add_option("-o", "--output-file", default="-",
+ help="Output file (defaults to stdout)")
+ parser.add_option("-m", "--markups", "--markup",
+ help="Optional comma (,) separated list of sequence markups to convert")
+ options, args = parser.parse_args()
+
+ if options.markups:
+ options.markups = options.markups.split(",")
+
+ main()

# vim: set et ts=4 sts=4 sw=4: