allpy
changeset 1107:bc1a7595d9d1
Clean rewrite or markup_to_file.py util; --markup is now optional and allows specifying multiple markups as a comma-separated list
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Thu, 14 Jun 2012 19:05:28 +0400 |
parents | 2b3cad50c2b1 |
children | 0736e1bbd186 |
files | allpy/util.py utils/markup_to_file.py |
diffstat | 2 files changed, 76 insertions(+), 39 deletions(-) [+] |
line diff
1.1 --- a/allpy/util.py Sun Jun 10 16:08:47 2012 +0400 1.2 +++ b/allpy/util.py Thu Jun 14 19:05:28 2012 +0400 1.3 @@ -3,6 +3,8 @@ 1.4 import sys 1.5 import warnings 1.6 import os 1.7 +import inspect 1.8 +import functools 1.9 from tempfile import mkstemp 1.10 from StringIO import StringIO 1.11 1.12 @@ -43,10 +45,43 @@ 1.13 """Clone of str that user may add attributes to.""" 1.14 pass 1.15 1.16 -def deprecated(message): 1.17 +def deprecated(message=None, what=None, in_favor=None, removed_in=None): 1.18 """Warn about function being deprecated.""" 1.19 + if not message: 1.20 + if what is None: 1.21 + frame = inspect.currentframe().f_back 1.22 + caller = [] 1.23 + if inspect.getmodule(frame): 1.24 + caller.append(inspect.getmodule(frame).__name__) 1.25 + if 'self' in frame.f_locals: 1.26 + caller.append(frame.f_locals['self'].__class__.__name__) 1.27 + elif 'cls' in frame.f_locals: 1.28 + caller.append(frame.f_locals['cls'].__name__) 1.29 + caller.append(frame.f_code.co_name) 1.30 + what = ".".join(caller) + "(...)" 1.31 + message = "{0} is deprecated".format(what) 1.32 + if in_favor: 1.33 + message = "{0} in favor of {1}".format(message, in_favor) 1.34 + if removed_in: 1.35 + message = "{0}; will be removed in allpy version {1}".format(message, removed_in) 1.36 warnings.warn(message, DeprecationWarning, stacklevel=2) 1.37 1.38 +def Deprecated(message=None, in_favor=None, removed_in=None): 1.39 + def decorator(function): 1.40 + @functools.wraps(function) 1.41 + def decorated(*args, **kws): 1.42 + text = "{0} is deprecated".format(function) 1.43 + if in_favor: 1.44 + text = "{0} in favor of {1}".format(text, in_favor) 1.45 + if removed_in: 1.46 + text = "{0}\n\nWill be removed in allpy version {1}.".format(text, removed_in) 1.47 + if message: 1.48 + text = "{0}\n\n{1}".format(text, message) 1.49 + warnings.warn(text, DeprecationWarning, stacklevel=2) 1.50 + return function(*args, **kws) 1.51 + return decorated 1.52 + return decorator 1.53 + 1.54 class Silence(object): 1.55 """Context manager for use with `with`. 1.56
2.1 --- a/utils/markup_to_file.py Sun Jun 10 16:08:47 2012 +0400 2.2 +++ b/utils/markup_to_file.py Thu Jun 14 19:05:28 2012 +0400 2.3 @@ -3,51 +3,53 @@ 2.4 import sys 2.5 import optparse 2.6 import copy 2.7 -from allpy import protein, markups 2.8 +from allpy import protein, markups, util 2.9 2.10 -parser = optparse.OptionParser() 2.11 -parser.add_option("--input-format", default="markup", 2.12 - help="Format of the input file") 2.13 -parser.add_option("-f", "--output-format", default="fasta", 2.14 - help="Format of the output file") 2.15 -parser.add_option("-i", "--input-file", 2.16 - help="Input file (defaults to stdin)") 2.17 -parser.add_option("-o", "--output-file", 2.18 - help="Output file (defaults to stdout)") 2.19 -parser.add_option("-m", "--markup", 2.20 - help="Markup to save as a sequence") 2.21 -options, args = parser.parse_args() 2.22 +def add_markup(alignment, sequence, markup): 2.23 + new = copy.deepcopy(sequence) 2.24 + new.name = "markup_{0}|{1}".format(markup.name, sequence.name) 2.25 + for new_monomer, monomer in zip(new, sequence): 2.26 + new_monomer.code1 = str(markup.get(monomer, '-')) 2.27 + assert len(new_monomer.code1) == 1 2.28 + assert new_monomer.code1 not in (markup.separator, markup.quotes) 2.29 2.30 -if not options.markup: 2.31 - parser.error("You must specify -m argument") 2.32 + sequence.add_markup('index') 2.33 + for column in alignment.columns: 2.34 + if sequence in column: 2.35 + column[new] = new[column[sequence].index] 2.36 + sequence.remove_markup('index') 2.37 2.38 -infile, outfile = sys.stdin, sys.stdout 2.39 -if options.input_file is not None: 2.40 - infile = open(options.input_file) 2.41 -if options.output_file is not None: 2.42 - outfile = open(options.output_file, "w") 2.43 + alignment.sequences.insert(alignment.sequences.index(sequence)+1, new) 2.44 2.45 -aln = protein.Alignment().append_file(infile, format=options.input_format) 2.46 +def main(): 2.47 + aln = protein.Alignment().append_file( 2.48 + util.open(options.input_file), format=options.input_format) 2.49 2.50 -mseqs = copy.deepcopy(aln.sequences) 2.51 -for mseq in mseqs: 2.52 - mseq.name = "markup_%s|%s" % (options.markup, mseq.name) 2.53 - for monomer in mseq: 2.54 - monomer.code1 = str(getattr(monomer, options.markup, '-')) 2.55 -seqs = aln.sequences 2.56 + for seq in list(aln.sequences): 2.57 + for markup in seq.markups.values(): 2.58 + if not options.markups or markup.name in options.markups: 2.59 + add_markup(aln, seq, markup) 2.60 2.61 -for seq, mseq in zip(seqs, mseqs): 2.62 - seq.add_markup('index') 2.63 - for column in aln.columns: 2.64 - if seq in column: 2.65 - column[mseq] = mseq[column[seq].index] 2.66 - seq.remove_markup('index') 2.67 + aln.to_file(util.open(options.output_file, "w"), format=options.output_format) 2.68 2.69 -aln.sequences = [] 2.70 -for seq, mseq in zip(seqs, mseqs): 2.71 - aln.sequences.append(seq) 2.72 - aln.sequences.append(mseq) 2.73 +if __name__ == "__main__": 2.74 2.75 -aln.to_file(outfile) 2.76 + parser = optparse.OptionParser() 2.77 + parser.add_option("--input-format", default="markup", 2.78 + help="Format of the input file") 2.79 + parser.add_option("-f", "--output-format", default="fasta", 2.80 + help="Format of the output file") 2.81 + parser.add_option("-i", "--input-file", default="-", 2.82 + help="Input file (defaults to stdin)") 2.83 + parser.add_option("-o", "--output-file", default="-", 2.84 + help="Output file (defaults to stdout)") 2.85 + parser.add_option("-m", "--markups", "--markup", 2.86 + help="Optional comma (,) separated list of sequence markups to convert") 2.87 + options, args = parser.parse_args() 2.88 + 2.89 + if options.markups: 2.90 + options.markups = options.markups.split(",") 2.91 + 2.92 + main() 2.93 2.94 # vim: set et ts=4 sts=4 sw=4: