def changeLoop(filename, loopname): posted=request.get_json(force=True) if posted["action"]=="change" and posted["method"]=="random": sm=get_sm(filename) sm.load_sampled_elems() if loopname not in sm.bg.coords: abort(404) original_cg=copy.deepcopy(sm.bg) new_filename=get_new_filename() smCache.renameSM(filename, new_filename) change_elem(sm, loopname) cg=sm.bg centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(original_cg)) centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(original_cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1])) filename=get_new_filename() cg.to_cg_file("user_files/"+filename) return jsonify({"url": url_for("structure_main", filename=filename)}) else: abort(403)
def changeLoop(filename, loopname): posted = request.get_json(force=True) if posted["action"] == "change" and posted["method"] == "random": sm = get_sm(filename) sm.load_sampled_elems() if loopname not in sm.bg.coords: abort(404) original_cg = copy.deepcopy(sm.bg) new_filename = get_new_filename() smCache.renameSM(filename, new_filename) change_elem(sm, loopname) cg = sm.bg centroid0 = ftuv.get_vector_centroid( ftug.bg_virtual_residues(original_cg)) centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(original_cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1])) filename = get_new_filename() cg.to_cg_file("user_files/" + filename) return jsonify({"url": url_for("structure_main", filename=filename)}) else: abort(403)
def align_rnas(rnas): crds0 = rnas[0].get_ordered_virtual_residue_poss() centroid0 = ftuv.get_vector_centroid(crds0) print(centroid0) rnas[0].rotate_translate(centroid0, ftuv.identity_matrix) crds0 -= centroid0 assert ftuv.magnitude( ftuv.get_vector_centroid(crds0)) < 10**-5, ftuv.magnitude( ftuv.get_vector_centroid(crds0)) assert ftuv.magnitude( ftuv.get_vector_centroid(rnas[0].get_ordered_virtual_residue_poss()) ) < 10**-5, ftuv.get_vector_centroid( rnas[0].get_ordered_virtual_residue_poss()) for rna in rnas[1:]: crds1 = rna.get_ordered_virtual_residue_poss() centroid1 = ftuv.get_vector_centroid(crds1) crds1 -= centroid1 rot_mat = ftms.optimal_superposition(crds0, crds1) rna.rotate_translate(centroid1, rot_mat) assert ftuv.magnitude( ftuv.get_vector_centroid(crds1)) < 10**-5, ftuv.magnitude( ftuv.get_vector_centroid(crds1)) assert ftuv.magnitude( ftuv.get_vector_centroid(rna.get_ordered_virtual_residue_poss()) ) < 10**-5, ftuv.magnitude( ftuv.get_vector_centroid(rna.get_ordered_virtual_residue_poss()))
def replace_base(res_dir, res_ref): ''' Orient res_ref so that it points in the same direction as res_dir. @param res_dir: The residue indicating the direction @param res_ref: The reference residue to be rotated @return res: A residue with the atoms of res_ref pointing in the direction of res_dir ''' #av = { 'U': ['N1', 'C6', 'C2'], 'C': ['N1', 'C6', 'C2'], 'A': ['N9', 'C4', 'C8'], 'G': ['N9', 'C4', 'C8'] } av = { 'U': ['N1', "C1'", "C2'"], 'C': ['N1', "C1'", "C2'"], 'A': ['N9', "C1'", "C2'"], 'G': ['N9', "C1'", "C2'"], 'rU': ['N1', "C1'", "C2'"], 'rC': ['N1', "C1'", "C2'"], 'rA': ['N9', "C1'", "C2'"], 'rG': ['N9', "C1'", "C2'"] } dv = av[res_dir.resname.strip()] rv = av[res_ref.resname.strip()] dir_points = np.array([res_dir[v].get_vector().get_array() for v in dv]) ref_points = np.array([res_ref[v].get_vector().get_array() for v in rv]) dir_centroid = cuv.get_vector_centroid(dir_points) ref_centroid = cuv.get_vector_centroid(ref_points) #sup = brmsd.optimal_superposition(dir_points - dir_centroid, ref_points - ref_centroid) sup = brmsd.optimal_superposition(ref_points - ref_centroid, dir_points - dir_centroid) new_res = res_ref.copy() for atom in new_res: atom.transform(np.eye(3,3), -ref_centroid) atom.transform(sup, dir_centroid) return new_res
def align_cgs(cgs): ''' Align each coarse grain RNA to the first one. The points representing each coarse grain RNA molecule will be the virtual residues. @param cgs: A list of CoarseGrainRNA structures. @return: Nothing, the cgs are modified in place ''' centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cgs[0])) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cgs[0])) for cg in cgs: centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1]))
def align_starts(chain_stems, chain_loop, handles, end=0, reverse=False): ''' Align the sugar rings of one part of the stem structure to one part of the loop structure. @param chain_stems: The chain containing the stems @param chain_loop: The chain containing the sampled loop @param handles: The indexes into the stem and loop for the overlapping residues. ''' v1 = [] v2 = [] for handle in handles: if end == 0: v1 += get_alignment_vectors(chain_stems, handle[0], handle[1]) v2 += get_alignment_vectors(chain_loop, handle[2], handle[3]) elif end == 2: if reverse: # used for aligning the 5' region, the back of which needs # to be aligned to the beginning of the stem v1 = (chain_stems[handle[1]]["C4'"].get_vector().get_array(), chain_stems[handle[1]]["C3'"].get_vector().get_array(), chain_stems[handle[1]]["O3'"].get_vector().get_array()) v2 = (chain_loop[handle[3]]["C4'"].get_vector().get_array(), chain_loop[handle[3]]["C3'"].get_vector().get_array(), chain_loop[handle[3]]["O3'"].get_vector().get_array()) else: v1 = (chain_stems[handle[0]]["C4'"].get_vector().get_array(), chain_stems[handle[0]]["C3'"].get_vector().get_array(), chain_stems[handle[0]]["O3'"].get_vector().get_array()) v2 = (chain_loop[handle[2]]["C4'"].get_vector().get_array(), chain_loop[handle[2]]["C3'"].get_vector().get_array(), chain_loop[handle[2]]["O3'"].get_vector().get_array()) else: v1 += get_measurement_vectors(chain_stems, handle[0], handle[1]) v2 += get_measurement_vectors(chain_loop, handle[2], handle[3]) #v1_m = get_measurement_vectors_rosetta(chain_stems, handle[0], handle[1]) #v2_m = get_measurement_vectors_barnacle(chain_loop, handle[2], handle[3]) v1_centroid = cuv.get_vector_centroid(v1) v2_centroid = cuv.get_vector_centroid(v2) v1_t = v1 - v1_centroid v2_t = v2 - v2_centroid sup = brmsd.optimal_superposition(v2_t, v1_t) #v2_mt = v2_m - v2_centroid #v2_rmt = np.dot(sup.transpose(), v2_mt.transpose()).transpose() #v1_rmt = v1_m - v1_centroid #rmsd = cuv.vector_set_rmsd(v1_rmt, v2_rmt) for atom in bpdb.Selection.unfold_entities(chain_loop, 'A'): atom.transform(np.eye(3,3), -v2_centroid) atom.transform(sup, v1_centroid)
def test_center_on_centroid(self): coords = [[0., 1., 1.], [1, 1, 1], [-1, 2, 3], [3, 0, 0], [-3, 1, 0]] nptest.assert_almost_equal( ftuv.center_on_centroid(np.array(coords)), [[0, 0., 0], [1, 0, 0], [-1, 1, 2], [3, -1, -1], [-3, 0, -1]]) nptest.assert_equal( ftuv.get_vector_centroid(ftuv.center_on_centroid(coords)), [0, 0., 0])
def align_residues(res_dir, res_ref, on=None): ''' Orient res_ref so that it points in the same direction as res_dir. :param res_dir: The residue indicating the direction :param res_ref: The reference residue to be rotated :return res: A residue with the atoms of res_ref pointing in the direction of res_dir ''' #TODO: BT: In the future we might align based on ALL non-sidechain atoms # using optimal_superposition. # If we stick to 3 reference atoms, we could use # ftuv.get_double_alignment_matrix instead. if on is None: av = { 'U': ['N1', "C1'", "C2'"], 'C': ['N1', "C1'", "C2'"], 'A': ['N9', "C1'", "C2'"], 'G': ['N9', "C1'", "C2'"], 'rU': ['N1', "C1'", "C2'"], 'rC': ['N1', "C1'", "C2'"], 'rA': ['N9', "C1'", "C2'"], 'rG': ['N9', "C1'", "C2'"] } else: av = defaultdict(lambda: on) dv = av[res_dir.resname.strip()] rv = av[res_ref.resname.strip()] dir_points = np.array([res_dir[v].coord for v in dv]) ref_points = np.array([res_ref[v].coord for v in rv]) dir_centroid = cuv.get_vector_centroid(dir_points) ref_centroid = cuv.get_vector_centroid(ref_points) sup = brmsd.optimal_superposition(ref_points - ref_centroid, dir_points - dir_centroid) new_res = res_ref.copy() for atom in new_res: atom.transform(np.eye(3, 3), -ref_centroid) atom.transform(sup, dir_centroid) return new_res
def align_source_to_target_fragment(target_chain, source_chain, sm, angle_def, ld): ''' Align a PDB chain to the position where it is supposed to bridge the gap between two stems. @param target_chain: The chain the fragment is to be inserted into @param source_chain: The chain from which the fragment comes @param sm: The SpatialModel being reconstructed @param angle_def: The define containing the residue numbers in source_chain @param ld: The name of the fragment. ''' connections = sm.bg.connections(ld) (s1b, s1e) = sm.bg.get_sides(connections[0], ld) #(s2b, s2e) = sm.bg.get_sides(connections[1], ld) (sd, bd) = sm.bg.get_sides_plus(connections[0], ld) t_v = (target_chain[sm.bg.defines[connections[0]][sd]]["C3'"].get_vector().get_array(), target_chain[sm.bg.defines[connections[0]][sd]]["C4'"].get_vector().get_array(), target_chain[sm.bg.defines[connections[0]][sd]]["O4'"].get_vector().get_array()) s_v = (source_chain[angle_def.define[bd]]["C3'"].get_vector().get_array(), source_chain[angle_def.define[bd]]["C4'"].get_vector().get_array(), source_chain[angle_def.define[bd]]["O4'"].get_vector().get_array()) t_centroid = cuv.get_vector_centroid(t_v) s_centroid = cuv.get_vector_centroid(s_v) t_v1 = t_v - t_centroid s_v1 = s_v - s_centroid sup = brmsd.optimal_superposition(s_v1, t_v1) for atom in bpdb.Selection.unfold_entities(source_chain, 'A'): atom.transform(np.eye(3,3), -s_centroid) atom.transform(sup, t_centroid) pass
def align_rnas(rnas): crds0 = rnas[0].get_ordered_virtual_residue_poss() centroid0 = ftuv.get_vector_centroid(crds0) print(centroid0) rnas[0].rotate_translate(centroid0, ftuv.identity_matrix) crds0-=centroid0 assert ftuv.magnitude(ftuv.get_vector_centroid(crds0))<10**-5, ftuv.magnitude(ftuv.get_vector_centroid(crds0)) assert ftuv.magnitude(ftuv.get_vector_centroid(rnas[0].get_ordered_virtual_residue_poss()))<10**-5, ftuv.get_vector_centroid(rnas[0].get_ordered_virtual_residue_poss()) for rna in rnas[1:]: crds1 = rna.get_ordered_virtual_residue_poss() centroid1 = ftuv.get_vector_centroid(crds1) crds1-=centroid1 rot_mat = ftms.optimal_superposition(crds0, crds1) rna.rotate_translate(centroid1, rot_mat) assert ftuv.magnitude(ftuv.get_vector_centroid(crds1))<10**-5, ftuv.magnitude(ftuv.get_vector_centroid(crds1)) assert ftuv.magnitude(ftuv.get_vector_centroid(rna.get_ordered_virtual_residue_poss()))<10**-5, ftuv.magnitude(ftuv.get_vector_centroid(rna.get_ordered_virtual_residue_poss()))
#! /usr/bin/python import forgi.threedee.model.coarse_grain as ftmc import fess.builder.models as fbm import forgi.threedee.utilities.vector as ftuv import forgi.threedee.utilities.graph_pdb as ftug import sys if __name__=="__main__": filename=sys.argv[1] print ("BUILDSTRUCTURE: Filename is ", filename) cg=ftmc.CoarseGrainRNA(filename) print ("BUILDSTRUCTURE: cg is ", cg) sm=fbm.SpatialModel(cg) print ("BUILDSTRUCTURE: sampling ") sm.sample_stats() print ("BUILDSTRUCTURE: traverse and build ") sm.traverse_and_build() print("BUILDSTRUCTURE: Writing File") centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(sm.bg)) for k in cg.coords.keys(): cg.coords[k] = ((cg.coords[k][0] - centroid0), (cg.coords[k][1] - centroid0)) sm.bg.to_cg_file(filename) print ("BUILDSTRUCTURE: DONE")
def test_center_on_centroid(self): coords = [[0., 1., 1.], [1, 1, 1], [-1, 2, 3], [3, 0, 0], [-3, 1, 0]] nptest.assert_almost_equal(ftuv.center_on_centroid(np.array(coords)), [[0, 0., 0], [1, 0, 0], [-1, 1, 2], [3, -1, -1], [-3, 0, -1]]) nptest.assert_equal(ftuv.get_vector_centroid( ftuv.center_on_centroid(coords)), [0, 0., 0])