allpy
changeset 196:a179d48a9caf
Converted geometrical_core.py to a proper UNIX script.
That is:
* renamed geometrical_core.py -> geometrical-core,
* added #!/usr/bin/python header,
* added executable permission.
author | Daniil Alexeyevsky <me.dendik@gmail.com> |
---|---|
date | Mon, 22 Nov 2010 13:36:48 +0300 |
parents | 596bdc5897bf |
children | 72db5aa461c0 |
files | geometrical_core/geometrical-core geometrical_core/geometrical_core.py |
diffstat | 2 files changed, 181 insertions(+), 180 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/geometrical_core/geometrical-core Mon Nov 22 13:36:48 2010 +0300 1.3 @@ -0,0 +1,181 @@ 1.4 +#!/usr/bin/python 1.5 +""" 1.6 +Geometrical Core building tool 1.7 +version 2.0 1.8 +""" 1.9 + 1.10 +from allpy import config, alignment, block 1.11 +Block = block.Block 1.12 +Alignment = alignment.Alignment 1.13 +import argparse 1.14 +import os 1.15 +from tempfile import NamedTemporaryFile 1.16 + 1.17 +r = argparse.FileType('r') 1.18 +w = argparse.FileType('w') 1.19 +c = config 1.20 + 1.21 +def f_nng(string): 1.22 + """ Validates nonnegative (>=0) float """ 1.23 + try: 1.24 + value = float(string) 1.25 + except: 1.26 + msg = "%r is wrong float" % string 1.27 + raise argparse.ArgumentTypeError(msg) 1.28 + if value < 0: 1.29 + msg = "%r is negative" % string 1.30 + raise argparse.ArgumentTypeError(msg) 1.31 + return value 1.32 + 1.33 +def part(string): 1.34 + """ Validates 0.0 <= float <= 1.0 """ 1.35 + try: 1.36 + value = float(string) 1.37 + except: 1.38 + msg = "%r is wrong float" % string 1.39 + raise argparse.ArgumentTypeError(msg) 1.40 + if not (0.0 <= value <= 1.0): 1.41 + msg = "%r is not float, representing part, ie in [0, 1]" % string 1.42 + raise argparse.ArgumentTypeError(msg) 1.43 + return value 1.44 + 1.45 +def timeout(string): 1.46 + """ Validates int >= -1 """ 1.47 + try: 1.48 + value = int(string) 1.49 + except: 1.50 + msg = "%r is wrong integer" % string 1.51 + raise argparse.ArgumentTypeError(msg) 1.52 + if value < -1: 1.53 + msg = "integer %r is less than -1" % string 1.54 + raise argparse.ArgumentTypeError(msg) 1.55 + return value 1.56 + 1.57 +def pos(string): 1.58 + """ Validates positive integer """ 1.59 + try: 1.60 + value = int(string) 1.61 + except: 1.62 + msg = "%r is wrong integer" % string 1.63 + raise argparse.ArgumentTypeError(msg) 1.64 + if value < 1: 1.65 + msg = "%r is not positive integer" % string 1.66 + raise argparse.ArgumentTypeError(msg) 1.67 + return value 1.68 + 1.69 +def i_nng(string): 1.70 + """ Validates int >= 0 """ 1.71 + try: 1.72 + value = int(string) 1.73 + except: 1.74 + msg = "%r is wrong integer" % string 1.75 + raise argparse.ArgumentTypeError(msg) 1.76 + if value < 0: 1.77 + msg = "integer %r is less than 0" % string 1.78 + raise argparse.ArgumentTypeError(msg) 1.79 + return value 1.80 + 1.81 +p = argparse.ArgumentParser( 1.82 +description='Geometrical Core building tool.', 1.83 +epilog='''1) Distance spreading [angstrom] 1.84 +2) -1 timeout means running Bron-Kerbosh algorithm without timeout 1.85 +3) Alternative core new aa part: read documentation for more information 1.86 +4) Superposition core identifier: main core is 0, first alternative is 1 etc. ''', 1.87 +formatter_class=argparse.ArgumentDefaultsHelpFormatter, 1.88 +#~ argument_default=argparse.SUPPRESS, 1.89 +) 1.90 + 1.91 +p.add_argument('-v','--version',action='version',version='%(prog)s 2.0') 1.92 +p.add_argument('-i',help='Input alignment file',metavar='FILE',type=r,required=True) 1.93 +p.add_argument('-c',help='PDB names conformity file',metavar='FILE',type=r) 1.94 +p.add_argument('-l',help='Output list file',metavar='FILE',type=w) 1.95 +p.add_argument('-f',help='Output fasta file',metavar='FILE',type=w) 1.96 +p.add_argument('-g',help='Output msf file',metavar='FILE',type=w) 1.97 +p.add_argument('-p',help='Output pdb file',metavar='FILE',type=w) 1.98 +p.add_argument('-s',help='Output spt file',metavar='FILE',type=w) 1.99 +p.add_argument('-d',help='Distance spreading',metavar='DELTA',type=f_nng,default=c.delta) 1.100 +p.add_argument('-m',help='Min core size',metavar='MIN_SIZE',type=pos,default=c.minsize) 1.101 +p.add_argument('-t',help='Bron-Kerbosh algorithm timeout',type=timeout,default=c.timeout) 1.102 +p.add_argument('-n',help='Alternative core new aa part',type=part,default=c.ac_new_atoms) 1.103 +p.add_argument('-a',help='Cores count',metavar='NEW_ATOMS',type=pos,default=c.ac_count) 1.104 +p.add_argument('-x',help='Superposition core identifier',type=i_nng,default=0) 1.105 + 1.106 +tmp_file = None 1.107 + 1.108 +try: 1.109 + args = p.parse_args() 1.110 + 1.111 + if not args.l and not args.f and not args.g and not args.p and not args.s: 1.112 + print 'Error: no output file provided' 1.113 + exit() 1.114 + if not (args.p and args.s) and not (not args.p and not args.s): 1.115 + print 'Error: provide both pdb and spt file or none of them' 1.116 + exit() 1.117 + 1.118 + try: 1.119 + alignment = Alignment(args.i) 1.120 + except: 1.121 + args.i.close() 1.122 + tmp_file = NamedTemporaryFile(delete=False) 1.123 + tmp_file.close() 1.124 + os.system('seqret %(msf)s %(fasta)s' % \ 1.125 + {'msf': args.i.name, 'fasta': tmp_file.name}) 1.126 + args.i = open(tmp_file.name) 1.127 + alignment = Alignment(args.i) 1.128 + 1.129 + block = Block(alignment) 1.130 + GCs = block.geometrical_cores(max_delta=args.d, timeout=args.t, 1.131 + minsize=args.t, ac_new_atoms=args.n, ac_count=args.a) 1.132 + 1.133 + if not GCs: 1.134 + print 'No cores! Try to change parameters' 1.135 + exit() 1.136 + 1.137 + if args.l: 1.138 + l = args.l 1.139 + 1.140 + l.write('Geometrical core positions for alignment %s' % args.i.name) 1.141 + l.write('\n\n') 1.142 + l.write('First alignment position is 0') 1.143 + 1.144 + for i, GC in enumerate(GCs): 1.145 + l.write('\n\n') 1.146 + if i == 0: 1.147 + l.write('Geometrical core:') 1.148 + else: 1.149 + l.write('Alternative geometrical core %i:' % i) 1.150 + l.write('\n') 1.151 + l.write(', '.join(str(n) for n in GC.positions)) 1.152 + l.close() 1.153 + 1.154 + if args.g and not args.f: 1.155 + args.f = args.g 1.156 + 1.157 + if args.f: 1.158 + args.i.seek(0) 1.159 + f = args.f 1.160 + f.write(args.i.read()) # write sequences 1.161 + 1.162 + # write GCs 1.163 + for i, GC in enumerate(GCs): 1.164 + f.write('\n\n') 1.165 + if i == 0: 1.166 + GC.save_xstring(f, 'GC', 'Main geometrical core') 1.167 + else: 1.168 + GC.save_xstring(f, 'AGC_%i' % i, 'Alternative geometrical core %i' % i) 1.169 + f.close() 1.170 + 1.171 + 1.172 + if args.g: 1.173 + args.g.close() 1.174 + os.system('seqret %(fasta)s msf::%(msf)s' % \ 1.175 + {'fasta': args.f.name, 'msf': args.g.name}) 1.176 + 1.177 + 1.178 +except Exception, t: 1.179 + print t 1.180 + exit() 1.181 + 1.182 +if tmp_file: 1.183 + os.unlink(tmp_file) 1.184 +
2.1 --- a/geometrical_core/geometrical_core.py Fri Nov 19 00:17:11 2010 +0300 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,180 +0,0 @@ 2.4 -""" 2.5 -Geometrical Core building tool 2.6 -version 2.0 2.7 -""" 2.8 - 2.9 -from allpy import config, alignment, block 2.10 -Block = block.Block 2.11 -Alignment = alignment.Alignment 2.12 -import argparse 2.13 -import os 2.14 -from tempfile import NamedTemporaryFile 2.15 - 2.16 -r = argparse.FileType('r') 2.17 -w = argparse.FileType('w') 2.18 -c = config 2.19 - 2.20 -def f_nng(string): 2.21 - """ Validates nonnegative (>=0) float """ 2.22 - try: 2.23 - value = float(string) 2.24 - except: 2.25 - msg = "%r is wrong float" % string 2.26 - raise argparse.ArgumentTypeError(msg) 2.27 - if value < 0: 2.28 - msg = "%r is negative" % string 2.29 - raise argparse.ArgumentTypeError(msg) 2.30 - return value 2.31 - 2.32 -def part(string): 2.33 - """ Validates 0.0 <= float <= 1.0 """ 2.34 - try: 2.35 - value = float(string) 2.36 - except: 2.37 - msg = "%r is wrong float" % string 2.38 - raise argparse.ArgumentTypeError(msg) 2.39 - if not (0.0 <= value <= 1.0): 2.40 - msg = "%r is not float, representing part, ie in [0, 1]" % string 2.41 - raise argparse.ArgumentTypeError(msg) 2.42 - return value 2.43 - 2.44 -def timeout(string): 2.45 - """ Validates int >= -1 """ 2.46 - try: 2.47 - value = int(string) 2.48 - except: 2.49 - msg = "%r is wrong integer" % string 2.50 - raise argparse.ArgumentTypeError(msg) 2.51 - if value < -1: 2.52 - msg = "integer %r is less than -1" % string 2.53 - raise argparse.ArgumentTypeError(msg) 2.54 - return value 2.55 - 2.56 -def pos(string): 2.57 - """ Validates positive integer """ 2.58 - try: 2.59 - value = int(string) 2.60 - except: 2.61 - msg = "%r is wrong integer" % string 2.62 - raise argparse.ArgumentTypeError(msg) 2.63 - if value < 1: 2.64 - msg = "%r is not positive integer" % string 2.65 - raise argparse.ArgumentTypeError(msg) 2.66 - return value 2.67 - 2.68 -def i_nng(string): 2.69 - """ Validates int >= 0 """ 2.70 - try: 2.71 - value = int(string) 2.72 - except: 2.73 - msg = "%r is wrong integer" % string 2.74 - raise argparse.ArgumentTypeError(msg) 2.75 - if value < 0: 2.76 - msg = "integer %r is less than 0" % string 2.77 - raise argparse.ArgumentTypeError(msg) 2.78 - return value 2.79 - 2.80 -p = argparse.ArgumentParser( 2.81 -description='Geometrical Core building tool.', 2.82 -epilog='''1) Distance spreading [angstrom] 2.83 -2) -1 timeout means running Bron-Kerbosh algorithm without timeout 2.84 -3) Alternative core new aa part: read documentation for more information 2.85 -4) Superposition core identifier: main core is 0, first alternative is 1 etc. ''', 2.86 -formatter_class=argparse.ArgumentDefaultsHelpFormatter, 2.87 -#~ argument_default=argparse.SUPPRESS, 2.88 -) 2.89 - 2.90 -p.add_argument('-v','--version',action='version',version='%(prog)s 2.0') 2.91 -p.add_argument('-i',help='Input alignment file',metavar='FILE',type=r,required=True) 2.92 -p.add_argument('-c',help='PDB names conformity file',metavar='FILE',type=r) 2.93 -p.add_argument('-l',help='Output list file',metavar='FILE',type=w) 2.94 -p.add_argument('-f',help='Output fasta file',metavar='FILE',type=w) 2.95 -p.add_argument('-g',help='Output msf file',metavar='FILE',type=w) 2.96 -p.add_argument('-p',help='Output pdb file',metavar='FILE',type=w) 2.97 -p.add_argument('-s',help='Output spt file',metavar='FILE',type=w) 2.98 -p.add_argument('-d',help='Distance spreading',metavar='DELTA',type=f_nng,default=c.delta) 2.99 -p.add_argument('-m',help='Min core size',metavar='MIN_SIZE',type=pos,default=c.minsize) 2.100 -p.add_argument('-t',help='Bron-Kerbosh algorithm timeout',type=timeout,default=c.timeout) 2.101 -p.add_argument('-n',help='Alternative core new aa part',type=part,default=c.ac_new_atoms) 2.102 -p.add_argument('-a',help='Cores count',metavar='NEW_ATOMS',type=pos,default=c.ac_count) 2.103 -p.add_argument('-x',help='Superposition core identifier',type=i_nng,default=0) 2.104 - 2.105 -tmp_file = None 2.106 - 2.107 -try: 2.108 - args = p.parse_args() 2.109 - 2.110 - if not args.l and not args.f and not args.g and not args.p and not args.s: 2.111 - print 'Error: no output file provided' 2.112 - exit() 2.113 - if not (args.p and args.s) and not (not args.p and not args.s): 2.114 - print 'Error: provide both pdb and spt file or none of them' 2.115 - exit() 2.116 - 2.117 - try: 2.118 - alignment = Alignment(args.i) 2.119 - except: 2.120 - args.i.close() 2.121 - tmp_file = NamedTemporaryFile(delete=False) 2.122 - tmp_file.close() 2.123 - os.system('seqret %(msf)s %(fasta)s' % \ 2.124 - {'msf': args.i.name, 'fasta': tmp_file.name}) 2.125 - args.i = open(tmp_file.name) 2.126 - alignment = Alignment(args.i) 2.127 - 2.128 - block = Block(alignment) 2.129 - GCs = block.geometrical_cores(max_delta=args.d, timeout=args.t, 2.130 - minsize=args.t, ac_new_atoms=args.n, ac_count=args.a) 2.131 - 2.132 - if not GCs: 2.133 - print 'No cores! Try to change parameters' 2.134 - exit() 2.135 - 2.136 - if args.l: 2.137 - l = args.l 2.138 - 2.139 - l.write('Geometrical core positions for alignment %s' % args.i.name) 2.140 - l.write('\n\n') 2.141 - l.write('First alignment position is 0') 2.142 - 2.143 - for i, GC in enumerate(GCs): 2.144 - l.write('\n\n') 2.145 - if i == 0: 2.146 - l.write('Geometrical core:') 2.147 - else: 2.148 - l.write('Alternative geometrical core %i:' % i) 2.149 - l.write('\n') 2.150 - l.write(', '.join(str(n) for n in GC.positions)) 2.151 - l.close() 2.152 - 2.153 - if args.g and not args.f: 2.154 - args.f = args.g 2.155 - 2.156 - if args.f: 2.157 - args.i.seek(0) 2.158 - f = args.f 2.159 - f.write(args.i.read()) # write sequences 2.160 - 2.161 - # write GCs 2.162 - for i, GC in enumerate(GCs): 2.163 - f.write('\n\n') 2.164 - if i == 0: 2.165 - GC.save_xstring(f, 'GC', 'Main geometrical core') 2.166 - else: 2.167 - GC.save_xstring(f, 'AGC_%i' % i, 'Alternative geometrical core %i' % i) 2.168 - f.close() 2.169 - 2.170 - 2.171 - if args.g: 2.172 - args.g.close() 2.173 - os.system('seqret %(fasta)s msf::%(msf)s' % \ 2.174 - {'fasta': args.f.name, 'msf': args.g.name}) 2.175 - 2.176 - 2.177 -except Exception, t: 2.178 - print t 2.179 - exit() 2.180 - 2.181 -if tmp_file: 2.182 - os.unlink(tmp_file) 2.183 -