def bootstrap(self, mapping_data, nboot, nsamples=-1, algorithm='rnastructure', bonus2d=False, replacement=False): print 'Starting bootstrap...' print 'Folding RNA with complete data' if bonus2d: mappind_data = array(mapping_data) if nsamples < 0: nsamples = len(self.sequence) full_bps = secondary_structure.fold(self.sequence, algorithm=algorithm, mapping_data=mapping_data, bonus2d=bonus2d)[0].base_pairs() bpdict = dict([(bp, 0) for bp in full_bps]) for i in range(nboot): print 'Doing bootstrap iteration %s' % i if bonus2d: grid = indices(mapping_data.shape) all_indices = zip(grid[0].ravel(), grid[1].ravel()) sampled_indices = [choice(all_indices) for x in xrange(len(all_indices))] md = zeros(mapping_data.shape) for j,k in sampled_indices: md[j,k] += mapping_data[j,k] else: md = mapping_data.sample(nsamples, replacement=replacement) bps = secondary_structure.fold(self.sequence, algorithm=algorithm, mapping_data=md, bonus2d=bonus2d)[0].base_pairs() for bp in bps: if bp in bpdict: bpdict[bp] += 1 else: bpdict[bp] = 1 for bp in bpdict: bpdict[bp] *= 100./nboot return bpdict
import rna import settings import secondary_structure import view import mapping s = 'UGCGCUUUUUUUUGCGCUUUUUUUUGCGCU' molecule = rna.RNA(s) data = mapping.MappingData(data=[0,0], seqpos=[2,26]) data2 = mapping.MappingData(data=[0,0], seqpos=[3,26]) structures = secondary_structure.fold(molecule.sequence, mapping_data=data,algorithm='rnastructure') ba = molecule.bootstrap(data, 5, nsamples=1, replacement=True) for b in ba: ba[b] = str(ba[b]) +'%' applet = view.VARNA(sequences=[molecule.sequence], structures=structures, mapping_data=[data]) html = applet.render(base_annotations=[ba], annotation_by_helix=True, \ helix_function=(lambda x, y: max(x,y))) f = open('test.html', 'w') f.write(html) f.close()
import rna import settings import secondary_structure import view import mapping s = 'UGCGCUUUUUUUUGCGCUUUUUUUUGCGCU' molecule = rna.RNA(s) data = mapping.MappingData(data=[0, 0], seqpos=[2, 26]) data2 = mapping.MappingData(data=[0, 0], seqpos=[3, 26]) structures = secondary_structure.fold(molecule.sequence, mapping_data=data, algorithm='rnastructure') ba = molecule.bootstrap(data, 5, nsamples=1, replacement=True) for b in ba: ba[b] = str(ba[b]) + '%' applet = view.VARNA(sequences=[molecule.sequence], structures=structures, mapping_data=[data]) html = applet.render(base_annotations=[ba], annotation_by_helix=True, \ helix_function=(lambda x, y: max(x,y))) f = open('test.html', 'w') f.write(html) f.close()