allpy
changeset 562:aef51fc30dcd
move generation of html to method of structure.BlockMixin
blocks3d program now only calls methods (and parse arguments)
author | boris (kodomo) <bnagaev@gmail.com> |
---|---|
date | Wed, 16 Mar 2011 22:26:42 +0300 |
parents | 66c4c0d9ec55 |
children | d6d06ac996e7 |
files | allpy/structure.py blocks3d/blocks3d.py |
diffstat | 2 files changed, 38 insertions(+), 49 deletions(-) [+] |
line diff
1.1 --- a/allpy/structure.py Wed Mar 16 22:15:19 2011 +0300 1.2 +++ b/allpy/structure.py Wed Mar 16 22:26:42 2011 +0300 1.3 @@ -11,6 +11,7 @@ 1.4 import os.path 1.5 import urllib2 1.6 from copy import copy 1.7 +import json 1.8 1.9 from Bio.PDB import PDBParser 1.10 from Bio.PDB import Select 1.11 @@ -217,6 +218,41 @@ 1.12 blocks[i].sequences.append(sequence) 1.13 return [block for i, block in sorted(blocks.items())] 1.14 1.15 + def blocks_to_html(self, file, blocks, template): 1.16 + """ Write convenient html-file for viewing blocks 1.17 + 1.18 + template -- str with template of html 1.19 + this template should contain substring 'self_js_text' 1.20 + to be replaced with information about blocks 1.21 + """ 1.22 + column2pos = self.column2pos() 1.23 + js_blocks = [] 1.24 + js_fasta_dict = {} 1.25 + for block in blocks: 1.26 + p_from = column2pos[block.columns[0]] 1.27 + p_to = column2pos[block.columns[-1]] 1.28 + js_block = {} 1.29 + js_block['start'] = p_from 1.30 + js_block['end'] = p_to 1.31 + js_block['IDs'] = [] 1.32 + for sequence in block.sequences: 1.33 + js_block['IDs'].append(sequence.name) 1.34 + js_block['cores'] = [] 1.35 + js_blocks.append(js_block) 1.36 + def char(monomer): 1.37 + if monomer: 1.38 + return monomer.code1 1.39 + return '-' 1.40 + for row in self.rows_as_lists(): 1.41 + sequence = row.sequence 1.42 + line = "".join(map(char, row)) 1.43 + js_fasta_dict[sequence.name] = line 1.44 + self_js_test = ("blocks = json('%(blocks)s');" +\ 1.45 + "fasta_dict=json('%(fasta_dict)s');") %\ 1.46 + {'blocks': json.dumps(js_blocks), 1.47 + 'fasta_dict': json.dumps(js_fasta_dict)} 1.48 + file.write(template.replace('self_js_text', self_js_test)) 1.49 + 1.50 class BlockMixin(base.Block, base.AlignmentMixin): 1.51 """Mixin to add 3D properties to blocks. 1.52
2.1 --- a/blocks3d/blocks3d.py Wed Mar 16 22:15:19 2011 +0300 2.2 +++ b/blocks3d/blocks3d.py Wed Mar 16 22:26:42 2011 +0300 2.3 @@ -6,7 +6,6 @@ 2.4 2.5 import argparse 2.6 import os 2.7 -import json 2.8 2.9 from allpy import config 2.10 from allpy import markup 2.11 @@ -56,55 +55,9 @@ 2.12 timeout=args.t, timeout_2=args.T, 2.13 min_width=args.m)) 2.14 2.15 -column2pos = alignment.column2pos() 2.16 - 2.17 if args.H: 2.18 - # html-file 2.19 - js_blocks = [] 2.20 - js_fasta_dict = {} 2.21 - for b in blocks: 2.22 - p_from = column2pos[b.columns[0]] 2.23 - p_to = column2pos[b.columns[-1]] 2.24 - js_block = {} 2.25 - js_block['start'] = p_from 2.26 - js_block['end'] = p_to 2.27 - js_block['IDs'] = [] 2.28 - for sequence in b.sequences: 2.29 - js_block['IDs'].append(sequence.name) 2.30 - js_block['cores'] = [] 2.31 - js_blocks.append(js_block) 2.32 - def char(monomer): 2.33 - if monomer: 2.34 - return monomer.code1 2.35 - return '-' 2.36 - for row in block.rows_as_lists(): 2.37 - sequence = row.sequence 2.38 - line = "".join(map(char, row)) 2.39 - js_fasta_dict[sequence.name] = line 2.40 - self_js_test = ("blocks = json('%(blocks)s');" +\ 2.41 - "fasta_dict=json('%(fasta_dict)s');") %\ 2.42 - {'blocks': json.dumps(js_blocks), 2.43 - 'fasta_dict': json.dumps(js_fasta_dict)} 2.44 - t = open(html_template).read() 2.45 - t = t.replace('self_js_text', self_js_test) 2.46 - args.H.write(t) 2.47 + alignment.blocks_to_html(args.H, blocks, open(html_template).read()) 2.48 2.49 if args.o: 2.50 - t = args.o 2.51 - # text output 2.52 - t.write("Block|from|to(exclusive)|gaps\n") 2.53 - for i, b in enumerate(blocks): 2.54 - p_from = column2pos[b.columns[0]] 2.55 - p_to = column2pos[b.columns[-1]] + 1 2.56 - width = p_to - p_from 2.57 - gaps = width - len(b.columns) 2.58 - t.write("%(i)i|%(from)i|%(to)i|%(gaps)i\n" %\ 2.59 - {'i': i, 'from': p_from, 2.60 - 'to': p_to, 'gaps': gaps}) 2.61 - t.write('#\n') 2.62 - t.write("Block|Sequence\n") 2.63 - for i, b in enumerate(blocks): 2.64 - for sequence in b.sequences: 2.65 - t.write("%(i)i|%(sequence)s\n" %\ 2.66 - {'i': i, 'sequence': sequence.name}) 2.67 + alignment.blocks_to_file(args.o, blocks) 2.68