def test_building_does_not_change_structure(self): self.sm1.load_sampled_elems() self.sm2.load_sampled_elems() self.sm1.traverse_and_build() print("RMSD", ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg)) self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg), 0) assertModelsEqual(self.sm1, self.sm2, 12) #Note: Coords do change!!!
def test_cg_rmsd2(self): cg1 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A.cg') self.assertAlmostEqual(ftme.cg_rmsd(cg1, cg1), 0) cg2 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A_sampled.cg') self.assertAlmostEqual(ftme.cg_rmsd(cg1, cg2), 25.563376828137844)
def test_building_does_not_change_structure(self): self.sm1.load_sampled_elems() self.sm2.load_sampled_elems() self.sm1.new_traverse_and_build() print("RMSD", ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg)) assertModelsEqual(self.sm1, self.sm2, 12) #Note: Coords do change!!! self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg), 0, places=6)
def test_rotate_keeps_RMSD_zero(self): cg1_rot = copy.deepcopy(self.cg1) cg1_rot.rotate(30, unit="degrees") self.assertAlmostEqual( ftme.cg_rmsd(self.cg1, cg1_rot), 0 ) #This currently uses virtual atoms, thus takes twists into account. print(self.cg2.coords) cg2_rot = copy.deepcopy(self.cg2) cg2_rot.rotate(45, unit="degrees") self.assertAlmostEqual(ftme.cg_rmsd(self.cg2, cg2_rot), 0)
def test_HDE_energy(self): open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 200 --energy HDE --seed 1 --step-save 200 " "--ref-img test/fess/data/1GID_A-structure2.coord.dpi15.width120.4.png --scale 120") # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000200.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_new_traverse_and_build_steps_doesnt_build_after(self): self.sm.load_sampled_elems() #We need to traverse_and_build at least once from the start! self.sm.new_traverse_and_build() self.sm.elem_defs["i6"] = self.stat_source.sample_for(self.cg, "i6") self.sm.new_traverse_and_build(max_steps=2) self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0, places=6) self.sm.new_traverse_and_build() self.assertGreater(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0)
def test_FPP_energy(self): open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 200 --energy FPP --seed 1 --step-save 200 --scale 120 " "--fpp-landmarks 21,7,12:1,10,7:137,8,4:50,7,2 --ref-img test/fess/data/1GID_A-structure2.forFPP.png") # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000200.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_PRO_energy(self): # We use distances from a projection of 1GID_A_structure2.coord, # proj.dir (-0.147,-0.311,-0.876) open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 500 --energy PRO --seed 1 --step-save 500 --projected-dist h0,h1,31.64:" "h0,h2,13.56:h0,s3,58.44:h1,h2,30.30:h1,s3,54.27:h2,s3,44.95") # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000500.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_HDE_energy(self): open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 200 --energy HDE --seed 1 --step-save 200 " "--ref-img test/fess/data/1GID_A-structure2.coord.dpi15.width120.4.png --scale 120" ) # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA( "test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000200.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_PRO_energy(self): # We use distances from a projection of 1GID_A_structure2.coord, # proj.dir (-0.147,-0.311,-0.876) open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 500 --energy 50PRO --seed 1 --step-save 500 --projected-dist h0,h1,31.64:" "h0,h2,13.56:h0,s3,58.44:h1,h2,30.30:h1,s3,54.27:h2,s3,44.95") # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA( "test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000500.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_FPP_energy(self): open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord " "-i 200 --energy FPP --seed 1 --step-save 200 --scale 120 " "--fpp-landmarks 21,7,12:1,10,7:137,8,4:50,7,2 --ref-img test/fess/data/1GID_A-structure2.forFPP.png" ) # Although we start from structure1, our result should stronger resemble structure2, # Because we used distances from structure2 orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") target_cg = ftmc.CoarseGrainRNA( "test/fess/data/1GID_A-structure2.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000200.coord") self.assertLess(ftmsim.cg_rmsd(new_cg, target_cg), ftmsim.cg_rmsd(new_cg, orig_cg))
def test_start_not_from_scratch(self): open_main, open_stats, stdout, stderr = self.runErnwin( "python ernwin_new.py test/fess/data/1GID_A-structure1.coord -i 1 " "--step-save 1 --seed 1") orig_cg = ftmc.CoarseGrainRNA("test/fess/data/1GID_A-structure1.coord") new_cg = self.getSavedFile(open_stats, "1GID_A/step000001.coord") self.assertLess(ftmsim.cg_rmsd(orig_cg, new_cg), 5)
def main(): usage = """ python calculate_mcc.py struct1.cg struct2.cg """ num_args= 0 parser = OptionParser(usage=usage) #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str') #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option') (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1) cg1 = ftmc.CoarseGrainRNA(args[0]) cg2 = ftmc.CoarseGrainRNA(args[1]) confusion_matrix = ftme.confusion_matrix(cg1, cg2) print "confusion_matrix:", confusion_matrix print "mcc:", ftme.mcc(confusion_matrix) print "rmsd:", ftme.cg_rmsd(cg1, cg2)
def main(args): with fuc.hide_traceback(): cg1, cg2 = fuc.cgs_from_args( args, rna_type="3d", enable_logging=True) if not (args.acc or args.rmsd or args.pdb_rmsd): showall = True else: showall = False if showall or args.acc: if cg1.defines != cg2.defines: if args.acc: print( "Cannot compare two 3d structures that do not correspond to the same RNA.") sys.exit(1) else: adj = ftms.AdjacencyCorrelation(cg1) print("ACC:\t{:.3f}".format(ftms.mcc(adj.evaluate(cg2)))) if showall or args.rmsd: print("RMSD:\t{:.3f}".format(ftms.cg_rmsd(cg1, cg2))) if showall or args.pdb_rmsd: if not pdb_rmsd(cg1, cg2): # If --pdb-rmsd was not given, just don't print it. # If it was given, we exit with non-zero exit status. if args.pdb_rmsd: print( "Cannot calculate PDB-RMSD: The two files do not contain the same chains.") sys.exit(1)
def test_cg_rmsd3(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2.coords.rotate(ftuv.rotation_matrix([1.,2.,3.], 22)) cg2.twists.rotate(ftuv.rotation_matrix([1.,2.,3.], 22)) self.assertAlmostEqual(ftme.cg_rmsd(cg1,cg2), 0)
def test_cg_rmsd3(self): cg1 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A.cg') cg2.rotate( 0.75) self.assertLess(ftme.cg_rmsd(cg1, cg2), 10**-6)
def test_new_traverse_and_build_start_doesnt_build_before_start(self): self.sm.load_sampled_elems() #We need to traverse_and_build at least once from the start! self.sm.new_traverse_and_build() #Change structure at s0 self.sm.elem_defs["i8"] = self.stat_source.sample_for(self.cg, "i8") #Build only part that did not change self.sm.new_traverse_and_build(start="s1") self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0, places=6) #Now build everything including the changed s0 self.sm.new_traverse_and_build() self.assertGreater(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 9 * 10**-6)
def test_cg_rmsd(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A_sampled.cg') residues1 = ftug.bg_virtual_residues(cg1) residues2 = ftug.bg_virtual_residues(cg2) self.assertAlmostEqual(ftme.rmsd(residues1, residues2), ftme.cg_rmsd(cg1, cg2))
def main(args): with fuc.hide_traceback(): cg1, cg2 = fuc.cgs_from_args(args, rna_type="3d", enable_logging=True) if not (args.acc or args.rmsd or args.pdb_rmsd): showall = True else: showall = False if showall or args.acc: if cg1.defines != cg2.defines: if args.acc: print( "Cannot compare two 3d structures that do not correspond to the same RNA." ) sys.exit(1) else: adj = ftms.AdjacencyCorrelation(cg1) print("ACC:\t{:.3f}".format(ftms.mcc(adj.evaluate(cg2)))) if showall or args.rmsd: print("RMSD:\t{:.3f}".format(ftms.cg_rmsd(cg1, cg2))) if showall or args.pdb_rmsd: if not pdb_rmsd(cg1, cg2): # If --pdb-rmsd was not given, just don't print it. # If it was given, we exit with non-zero exit status. if args.pdb_rmsd: print( "Cannot calculate PDB-RMSD: The two files do not contain the same chains." ) sys.exit(1)
def test_new_traverse_and_build_steps_really_builds(self): self.sm.load_sampled_elems() #We need to traverse_and_build at least once from the start! self.sm.new_traverse_and_build() print(self.sm.bg.traverse_graph()) self.sm.elem_defs["i7"] = self.stat_source.sample_for(self.cg, "i7") self.sm.new_traverse_and_build(max_steps=5) self.assertGreater(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0)
def test_extracting_stats_from_sm_after_building(self): self.sm1.load_sampled_elems() self.sm1.new_traverse_and_build() self.sm2.elem_defs = {} for d in self.sm1.elem_defs: self.sm2.elem_defs[d] = self.sm1.elem_defs[d] self.sm2.new_traverse_and_build() self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg), 0)
def test_extracting_stats_from_sm_after_building(self): self.sm1.load_sampled_elems() self.sm1.traverse_and_build() self.sm2.elem_defs={} for d in self.sm1.elem_defs: self.sm2.elem_defs[d] = self.sm1.elem_defs[d] self.sm2.traverse_and_build() self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg), 0) assertModelsEqual(self.sm1, self.sm2, 12)
def test_new_traverse_and_build(self): self.sm.load_sampled_elems() self.sm.new_traverse_and_build() for k in self.cg_copy.defines.keys(): log.info("k: %s file: %s, built %s", k, ftuv.magnitude(self.cg_copy.coords.get_direction(k)), ftuv.magnitude(self.sm.bg.coords.get_direction(k))) self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0, places=6)
def test_rotate_keeps_RMSD_zero(self): cg1_rot = copy.deepcopy(self.cg1) cg1_rot.rotate(30, unit="degrees") # This currently uses virtual atoms, thus takes twists into account. self.assertLess(ftme.cg_rmsd(self.cg1, cg1_rot), 10**-6) cg2_rot = copy.deepcopy(self.cg2) cg2_rot.rotate(45, unit="degrees") a, b = self.cg2.get_ordered_virtual_residue_poss(True) log.warning("------------------------") c, d = cg2_rot.get_ordered_virtual_residue_poss(True) c2 = np.dot(c, ftuv.rotation_matrix("x", math.radians(-45)).T) log.warning("==================================") for i, coord in enumerate(a): if any(abs(coord - c2[i]) > 10**-4): log.warning("%s %s %s %s", coord, b[i], c2[i], d[i]) self.assertLess(ftme.cg_rmsd(self.cg2, cg2_rot), 10**-6)
def test_new_traverse_and_build_start_really_builds(self): self.sm.load_sampled_elems() #We need to traverse_and_build at least once from the start! self.sm.new_traverse_and_build() #Change structure at s6 self.sm.elem_defs["i6"] = self.stat_source.sample_for(self.cg, "i6") #Build part that did change self.sm.new_traverse_and_build(start="s2") self.assertGreater(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 0)
def test_rotate_keeps_RMSD_zero(self): cg1_rot = copy.deepcopy(self.cg1) cg1_rot.rotate(30, unit="degrees") # This currently uses virtual atoms, thus takes twists into account. self.assertLess(ftme.cg_rmsd(self.cg1, cg1_rot), 10**-6) cg2_rot = copy.deepcopy(self.cg2) cg2_rot.rotate(45, unit="degrees") a,b = self.cg2.get_ordered_virtual_residue_poss(True) log.warning("------------------------") c,d = cg2_rot.get_ordered_virtual_residue_poss(True) c2 = np.dot(c, ftuv.rotation_matrix("x", math.radians(-45)).T) log.warning("==================================") for i, coord in enumerate(a): if any(abs(coord-c2[i])>10**-4): log.warning("%s %s %s %s",coord, b[i], c2[i], d[i]) self.assertLess(ftme.cg_rmsd(self.cg2, cg2_rot), 10**-6)
def test_cg_rmsd(self): cg1 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/1GID_A_sampled.cg') cg1.add_all_virtual_residues() cg2.add_all_virtual_residues() residues1 = cg1.get_ordered_virtual_residue_poss() residues2 = cg2.get_ordered_virtual_residue_poss() self.assertAlmostEqual( ftme.rmsd(residues1, residues2), ftme.cg_rmsd(cg1, cg2))
def test_coords_from_direction_with_pseudoknot(self): #This tests the case where the link is inserted from reverse direction. cg = ftmc.CoarseGrainRNA('test/forgi/threedee/data/3D0U_A.cg') cg_old = copy.deepcopy(cg) coords = cg.get_coordinates_array() directions = cg.coords_to_directions() cg.coords.clear() cg.coords_from_directions(directions) self.assertAlmostEqual(ftme.cg_rmsd(cg, cg_old), 0) new_coords = cg.get_coordinates_array() offset = (coords - new_coords) assert np.allclose(offset, offset[0])
def test_save_and_load_does_not_change_coords(self): self.sm1.load_sampled_elems() self.sm2.load_sampled_elems() self.sm2.traverse_and_build() cg2_str = self.sm2.bg.to_cg_string() bg_loaded=ftmc.CoarseGrainRNA() bg_loaded.from_cg_string(cg2_str) sm2_reloaded = fbm.SpatialModel( bg_loaded ) sm2_reloaded.load_sampled_elems() self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm2.bg, sm2_reloaded.bg), 0) assertModelsEqual(self.sm2, sm2_reloaded, 12) assertModelsEqual(self.sm1, sm2_reloaded, 12)
def test_coords_from_direction(self): cg = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1I9V_noPK.cg') cg_old = copy.deepcopy(cg) coords = cg.get_coordinates_array() directions = coords[1::2] - coords[0::2] cg.coords.clear() cg.coords_from_directions(directions) self.assertAlmostEqual(ftme.cg_rmsd(cg, cg_old), 0) #This only looks at stems # The coordinates should be the same as before except for a constant offset new_coords = cg.get_coordinates_array() offset = (coords - new_coords) assert np.allclose(offset, offset[0])
def test_save_and_load_does_not_change_coords(self): self.sm1.load_sampled_elems() self.sm2.load_sampled_elems() self.sm2.new_traverse_and_build() cg2_str = self.sm2.bg.to_cg_string() bg_loaded = ftmc.CoarseGrainRNA.from_bg_file(cg2_str) sm2_reloaded = fbm.SpatialModel(bg_loaded) sm2_reloaded.load_sampled_elems() self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm2.bg, sm2_reloaded.bg), 0, places=6) #assertModelsEqual(self.sm2, sm2_reloaded, 12) assertModelsEqual(self.sm1, sm2_reloaded, 12)
def test_multiple_chain_to_cg(self): cg, = ftmc.CoarseGrainRNA.from_pdb( 'test/forgi/threedee/data/4GV9.pdb', load_chains=None) log.debug("======= FIRST IS LOADED =========") cg_str = cg.to_cg_string() log.debug("\n" + cg_str) print(cg_str) cg2 = ftmc.CoarseGrainRNA.from_bg_string(cg_str) self.assertEqual(cg.defines, cg2.defines) self.assertLess(ftme.cg_rmsd(cg, cg2), 10**-6) self.assertEqual(cg.backbone_breaks_after, cg2.backbone_breaks_after) cg, = ftmc.CoarseGrainRNA.from_pdb( 'test/forgi/threedee/data/3CQS.pdb', load_chains=None) cg.log(logging.WARNING) cg_str = cg.to_cg_string() cg2 = ftmc.CoarseGrainRNA.from_bg_string(cg_str) self.assertEqual(cg.defines, cg2.defines) # This only looks at stems self.assertLess(ftme.cg_rmsd(cg, cg2), 10**-6) self.assertEqual(cg.backbone_breaks_after, cg2.backbone_breaks_after)
def test_multiple_chain_to_cg(self): cg, = ftmc.CoarseGrainRNA.from_pdb('test/forgi/threedee/data/4GV9.pdb', load_chains=None) log.debug("======= FIRST IS LOADED =========") cg_str = cg.to_cg_string() log.debug("\n" + cg_str) print(cg_str) cg2 = ftmc.CoarseGrainRNA.from_bg_string(cg_str) self.assertEqual(cg.defines, cg2.defines) self.assertLess(ftme.cg_rmsd(cg, cg2), 10**-6) self.assertEqual(cg.backbone_breaks_after, cg2.backbone_breaks_after) cg, = ftmc.CoarseGrainRNA.from_pdb('test/forgi/threedee/data/3CQS.pdb', load_chains=None) cg.log(logging.WARNING) cg_str = cg.to_cg_string() cg2 = ftmc.CoarseGrainRNA.from_bg_string(cg_str) self.assertEqual(cg.defines, cg2.defines) # This only looks at stems self.assertLess(ftme.cg_rmsd(cg, cg2), 10**-6) self.assertEqual(cg.backbone_breaks_after, cg2.backbone_breaks_after)
def test_extracting_stats_from_cg_after_building(self): self.sm1.load_sampled_elems() self.sm1.new_traverse_and_build() self.sm2.elem_defs = {} cg1 = self.sm1.bg for d in cg1.defines: stats = cg1.get_stats(d) self.sm2.elem_defs[d] = stats[0] self.sm2.new_traverse_and_build() assertModelsEqual(self.sm1, self.sm2, 12, ignore_keys=["sampled", "elem_defs"]) self.assertAlmostEqual(ftmsim.cg_rmsd(self.sm1.bg, self.sm2.bg), 0)
def main(args): if len(args.compareTo)==1: cg1 = ftmc.CoarseGrainRNA(args.reference[0]) cg2 = ftmc.CoarseGrainRNA(args.compareTo[0]) print (ftms.cg_rmsd(cg1, cg2)) else: print ("{:15}\t{:6}\t{:6}\t{:6}".format("filename","RMSD", "dRMSD", "ACC")) ref_cg = ftmc.CoarseGrainRNA(args.reference[0]) reference = ref_cg.get_ordered_virtual_residue_poss() acc_calc = ftms.AdjacencyCorrelation(ref_cg) for filename in args.compareTo: cg=ftmc.CoarseGrainRNA(filename) curr_vress=cg.get_ordered_virtual_residue_poss() rmsd = ftur.rmsd(reference, curr_vress) drmsd = ftur.drmsd(reference, curr_vress) acc = ftms.mcc(acc_calc.evaluate(cg)) print ("{:15}\t{:6.3f}\t{:6.3f}\t{:6.3f}".format(filename[-15:], rmsd, drmsd, acc))
def test_coords_from_direction_with_pseudoknot(self): # This tests the case where the link is inserted from reverse direction. cg = ftmc.CoarseGrainRNA.from_bg_file( 'test/forgi/threedee/data/3D0U_A.cg') cg_old = copy.deepcopy(cg) coords = cg.get_coordinates_array() directions = cg.coords_to_directions() cg._init_coords() cg.twists = cg_old.twists log.info("len(coords):{}, len(directions):{}, len(defines):{}".format( len(coords), len(directions), len(cg.defines))) cg.coords_from_directions(directions) self.assertLess(ftme.cg_rmsd(cg, cg_old), 10**-6) new_coords = cg.get_coordinates_array() offset = (coords - new_coords) assert np.allclose(offset, offset[0])
def main(args): if len(args.compareTo) == 1: cg1 = ftmc.CoarseGrainRNA(args.reference[0]) cg2 = ftmc.CoarseGrainRNA(args.compareTo[0]) print(ftms.cg_rmsd(cg1, cg2)) else: print("{:15}\t{:6}\t{:6}\t{:6}".format("filename", "RMSD", "dRMSD", "ACC")) ref_cg = ftmc.CoarseGrainRNA(args.reference[0]) reference = ref_cg.get_ordered_virtual_residue_poss() acc_calc = ftms.AdjacencyCorrelation(ref_cg) for filename in args.compareTo: cg = ftmc.CoarseGrainRNA(filename) curr_vress = cg.get_ordered_virtual_residue_poss() rmsd = ftur.rmsd(reference, curr_vress) drmsd = ftur.drmsd(reference, curr_vress) acc = ftms.mcc(acc_calc.evaluate(cg)) print("{:15}\t{:6.3f}\t{:6.3f}\t{:6.3f}".format( filename[-15:], rmsd, drmsd, acc))
def main(): usage = """ python calculate_mcc.py struct1.cg struct2.cg """ num_args = 0 parser = OptionParser(usage=usage) #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str') #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option') (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1) cg1 = ftmc.CoarseGrainRNA(args[0]) cg2 = ftmc.CoarseGrainRNA(args[1]) confusion_matrix = ftme.confusion_matrix(cg1, cg2) print "confusion_matrix:", confusion_matrix print "mcc:", ftme.mcc(confusion_matrix) print "rmsd:", ftme.cg_rmsd(cg1, cg2)
def main(): usage = """ python cg_to_fornac_html.py file1.cg file2.cg Convert coarse grain files to html files using fornac to display a 2D version of the structure. """ num_args= 1 parser = OptionParser(usage=usage) parser.add_option('-d', '--distance', dest='distance', default=25, help="Draw links between elements that are within a certain distance from each other", type='float') parser.add_option('-b', '--bp-distance', dest='bp_distance', default=16, help="Draw links only between nucleotides which are so many nucleotides apart", type='int') parser.add_option('-s', '--sort-by', dest='sort_by', default='mcc', help="What to sort by (options: mcc, pca)", type='string') parser.add_option('-n', '--names', dest='names', default=False, action='store_true', help='Add the name of the structure to the display') (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1) structs = [] pair_bitmaps = [] cgs = [] all_links = [] mccs = [] cm=None for filename in args: cg = ftmc.CoarseGrainRNA(filename) cgs += [cg] if not cm: cm=ftme.AdjacencyCorrelation(cg) (links, pair_bitmap) = extract_extra_links(cg, options.distance, options.bp_distance, correct_links = None if len(all_links) == 0 else all_links[0]) all_links += [links] pair_bitmaps += [pair_bitmap] mcc = ftme.mcc(cm.evaluate(cg)) rmsd = ftme.cg_rmsd(cgs[0], cg) seq_struct = {"sequence": cg.seq, "structure": cg.to_dotbracket_string(), "extraLinks": links} fud.pv('options.names') fud.pv('mcc, rmsd') if options.names: seq_struct['name'] = op.basename(filename) + " ({:.2f},{:.1f})".format(mcc, rmsd) else: seq_struct['name'] = '' structs += [seq_struct] mccs += [mcc] if options.sort_by == 'pca': print >>sys.stderr, "Sorting by pca" ix = reorder_structs(pair_bitmaps) else: print >>sys.stderr, "Sorting by mcc" ix = np.argsort(-np.array(mccs)) new_array = [0 for i in range(len(ix))] for i,x in enumerate(ix): new_array[i] = structs[x] print output_template.format(json.dumps(new_array))
def test_rotate_keeps_RMSD_zero0(self): cg1_rot = copy.deepcopy(self.cg1) cg1_rot.rotate(30, unit="degrees") cg1_rot.rotate(-30, unit="degrees") self.assertLess(ftme.cg_rmsd(self.cg1, cg1_rot), 10**-6)
def main(args): cgs = [] projs = [] for file_ in args.files: cgs.append(ftmc.CoarseGrainRNA(file_)) try: projs.append(ftmp.Projection2D(cgs[-1])) except ValueError: projs.append(None) p_rmsds = OrderedDict() cg_rmsds = OrderedDict() p_rmsds[0] = 0.0 cg_rmsds[0] = 0.0 if "," in args.max_diff: diffs = map(int, args.max_diff.split(",")) else: diffs = range(1, int(args.max_diff) + 1) for diff in diffs: print ("diff {}".format(diff)) prmsd = 0 cgrmsd = 0 count = 0 pcount = 0 for i in range(0, len(cgs) - diff): count += 1 try: vrs1 = np.array([x for p in sorted(projs[i]._coords.keys()) for x in projs[i]._coords[p]]) vrs2 = np.array([x for p in sorted(projs[i + diff]._coords.keys()) for x in projs[i + diff]._coords[p]]) prmsd += ftms.rmsd(vrs1, vrs2) pcount += 1 except: pass cgrmsd += ftms.cg_rmsd(cgs[i], cgs[i + diff]) if count: cg_rmsds[diff] = cgrmsd / count if pcount: p_rmsds[diff] = prmsd / pcount print "projection RMSDs:", p_rmsds print "3D-RMSDs:", cg_rmsds import matplotlib.pyplot as plt if args.target_structure and args.hausdorff_reference: fig, (ax, axT, axH) = plt.subplots(3) elif args.target_structure: fig, (ax, axT) = plt.subplots(2) elif args.hausdorff_reference: fig, (ax, axH) = plt.subplots(2) else: fig, ax = plt.subplots() if args.target_structure: target = ftmc.CoarseGrainRNA(args.target_structure) target_rmsds = [] target_proj_rmsds = [] try: target_proj = ftmp.Projection2D(target) except ValueError: pass else: target_vrs = np.array([x for p in sorted(target_proj._coords.keys()) for x in target_proj._coords[p]]) for proj in projs: try: vrs1 = np.array([x for p in sorted(proj._coords.keys()) for x in proj._coords[p]]) prmsd = ftms.rmsd(vrs1, target_vrs) target_proj_rmsds.append(prmsd) except: target_proj_rmsds.append(float("nan")) target_vrs = ftug.bg_virtual_residues(target) for cg in cgs: vrs1 = ftug.bg_virtual_residues(cg) cgrmsd = ftms.rmsd(vrs1, target_vrs) target_rmsds.append(cgrmsd) xval = np.arange(len(cgs)) * args.step_size if target_proj_rmsds: axT.plot(xval, target_proj_rmsds, label="Projection", color="green") axT.plot(xval, target_rmsds, label="3D", color="blue") axT.set_title("RMSD to target structure") axT.set_xlabel("step") axT.set_ylabel("RMSD") leg = axT.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) if args.hausdorff_reference: ref_img = scipy.ndimage.imread(args.hausdorff_reference) ref_quarter = scipy.ndimage.zoom(ref_img, 0.3) > 150 degrees = get_refimg_longest_axis(ref_img) global_optima = [] local_optima = [] for cg in cgs: score, img, params = fph.try_parameters(ref_img, args.hausdorff_scale, cg, degrees) local_optima.append(score) s, i, params = fph.globally_minimal_distance( ref_quarter, args.hausdorff_scale, cg, start_points=40, starting_rotations=degrees, virtual_atoms=False ) score, img, params = fph.locally_minimal_distance( ref_img, args.hausdorff_scale, cg, params[1], params[2], params[0], maxiter=200 ) global_optima.append(score) axH.plot(xval, global_optima, label="Global Minimization", color="red") axH.plot(xval, local_optima, label="Local Minimization", color="lavender") axH.set_title("Hausdorff Distance to reference image") axH.set_xlabel("step") axH.set_ylabel("Hausdorff Distance") leg = axH.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) p_rmsds = np.array(p_rmsds.items()) cg_rmsds = np.array(cg_rmsds.items()) ax.plot(p_rmsds[:, 0] * args.step_size, p_rmsds[:, 1], label="Projection", color="green", marker="o") ax.plot(cg_rmsds[:, 0] * args.step_size, cg_rmsds[:, 1], label="3D", color="blue", marker="o") ax.set_title("Average RMSD between structures X steps apart.") ax.set_xlabel("steps apart") ax.set_ylabel("RMSD") leg = ax.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) fig.patch.set_facecolor("white") if args.save_fig: with open(args.save_fig, "w") as f: pickle.dump(fig, f) plt.show()
def view_2d_embedding(self, reference=None): # http://baoilleach.blogspot.co.at/2014/01/convert-distance-matrix-to-2d.html if reference is None: # First cluster all structures based on pairwise RMSD db = self._cluster_dbscan() labels = db.labels_ core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True unique_labels = set(labels) # Then calculate the 2D coordinates for our embedding mds = MDS(n_components=2, dissimilarity="precomputed", random_state=6) results = mds.fit(self._rmsd) coords = results.embedding_ # Now plot plt.plot(coords[:, 0], coords[:, 1], '-', color="blue") colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels))) for k, col in zip(unique_labels, colors): if k == -1: # Black used for noise. col = 'k' class_member_mask = (labels == k) plt.plot(coords[:, 0][class_member_mask & core_samples_mask], coords[:, 1][class_member_mask & core_samples_mask], 'o', markerfacecolor=col, markeredgecolor='k', markersize=6 ) plt.plot(coords[:, 0][class_member_mask & ~core_samples_mask], coords[:, 1][class_member_mask & ~core_samples_mask], 'o', markerfacecolor=col, markeredgecolor=col, markersize=1 ) plt.savefig("embedding_{}.svg".format(self._cgs[0].name)) plt.clf() plt.close() else: # Create a huge distance matrix alldists = np.zeros( ((len(self._cgs) + len(reference) + 1), (len(self._cgs) + len(reference) + 1))) for i, j in it.combinations(range(len(alldists)), 2): if i < len(self._cgs): cg1 = self._cgs[i] elif i < len(self._cgs) + len(reference): cg1 = reference[i - len(self._cgs)] else: assert i == len(self._cgs) + len(reference) cg1 = self._reference_cg if j < len(self._cgs): cg2 = self._cgs[j] elif j < len(self._cgs) + len(reference): cg2 = reference[j - len(self._cgs)] else: assert j == len(self._cgs) + len(reference) cg2 = self._reference_cg alldists[i, j] = alldists[j, i] = ftms.cg_rmsd(cg1, cg2) # Then calculate the 2D coordinates for our embedding mds = MDS(n_components=2, dissimilarity="precomputed", random_state=6) results = mds.fit(alldists) coords = results.embedding_ # Now plot plt.plot(coords[len(self._cgs):len(self._cgs) + len(reference), 0], coords[len(self._cgs):len(self._cgs) + len(reference), 1], 's', color="green") plt.plot(coords[:len(self._cgs), 0], coords[:len(self._cgs), 1], '-o', color="blue") plt.plot([coords[-1, 0]], [coords[-1, 1]], 's', color="red") plt.savefig("embedding1_{}.svg".format(self._cgs[0].name)) plt.clf() plt.close()
def test_cg_rmsd2(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A_sampled.cg') self.assertAlmostEqual(ftme.cg_rmsd(cg1, cg2), 25.170088934277373)
def test_cg_rmsd2(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') self.assertAlmostEqual(ftme.cg_rmsd(cg1,cg1), 0) cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A_sampled.cg') self.assertAlmostEqual(ftme.cg_rmsd(cg1,cg2), 7.684377397812648)
def main(args): cgs = [] projs = [] for file_ in args.files: cgs.append(ftmc.CoarseGrainRNA(file_)) try: projs.append(ftmp.Projection2D(cgs[-1])) except ValueError: projs.append(None) p_rmsds = OrderedDict() cg_rmsds = OrderedDict() p_rmsds[0] = 0. cg_rmsds[0] = 0. if "," in args.max_diff: diffs = map(int, args.max_diff.split(",")) else: diffs = range(1, int(args.max_diff) + 1) for diff in diffs: print("diff {}".format(diff)) prmsd = 0 cgrmsd = 0 count = 0 pcount = 0 for i in range(0, len(cgs) - diff): count += 1 try: vrs1 = np.array([ x for p in sorted(projs[i]._coords.keys()) for x in projs[i]._coords[p] ]) vrs2 = np.array([ x for p in sorted(projs[i + diff]._coords.keys()) for x in projs[i + diff]._coords[p] ]) prmsd += ftms.rmsd(vrs1, vrs2) pcount += 1 except: pass cgrmsd += ftms.cg_rmsd(cgs[i], cgs[i + diff]) if count: cg_rmsds[diff] = cgrmsd / count if pcount: p_rmsds[diff] = prmsd / pcount print("projection RMSDs:", p_rmsds) print("3D-RMSDs:", cg_rmsds) import matplotlib.pyplot as plt if args.target_structure and args.hausdorff_reference: fig, (ax, axT, axH) = plt.subplots(3) elif args.target_structure: fig, (ax, axT) = plt.subplots(2) elif args.hausdorff_reference: fig, (ax, axH) = plt.subplots(2) else: fig, ax = plt.subplots() if args.target_structure: target = ftmc.CoarseGrainRNA(args.target_structure) target_rmsds = [] target_proj_rmsds = [] try: target_proj = ftmp.Projection2D(target) except ValueError: pass else: target_vrs = np.array([ x for p in sorted(target_proj._coords.keys()) for x in target_proj._coords[p] ]) for proj in projs: try: vrs1 = np.array([ x for p in sorted(proj._coords.keys()) for x in proj._coords[p] ]) prmsd = ftms.rmsd(vrs1, target_vrs) target_proj_rmsds.append(prmsd) except: target_proj_rmsds.append(float("nan")) target_vrs = target.get_ordered_virtual_residue_poss() for cg in cgs: vrs1 = cg.get_ordered_virtual_residue_poss() cgrmsd = ftms.rmsd(vrs1, target_vrs) target_rmsds.append(cgrmsd) xval = np.arange(len(cgs)) * args.step_size if target_proj_rmsds: axT.plot(xval, target_proj_rmsds, label="Projection", color="green") axT.plot(xval, target_rmsds, label="3D", color="blue") axT.set_title("RMSD to target structure") axT.set_xlabel("step") axT.set_ylabel("RMSD") leg = axT.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) if args.hausdorff_reference: ref_img = scipy.ndimage.imread(args.hausdorff_reference) ref_quarter = (scipy.ndimage.zoom(ref_img, 0.3) > 150) degrees = get_refimg_longest_axis(ref_img) global_optima = [] local_optima = [] for cg in cgs: score, img, params = fph.try_parameters(ref_img, args.hausdorff_scale, cg, degrees) local_optima.append(score) s, i, params = fph.globally_minimal_distance( ref_quarter, args.hausdorff_scale, cg, start_points=40, starting_rotations=degrees, virtual_atoms=False) score, img, params = fph.locally_minimal_distance( ref_img, args.hausdorff_scale, cg, params[1], params[2], params[0], maxiter=200) global_optima.append(score) axH.plot(xval, global_optima, label="Global Minimization", color="red") axH.plot(xval, local_optima, label="Local Minimization", color="lavender") axH.set_title("Hausdorff Distance to reference image") axH.set_xlabel("step") axH.set_ylabel("Hausdorff Distance") leg = axH.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) p_rmsds = np.array(list(p_rmsds.items())) cg_rmsds = np.array(list(cg_rmsds.items())) ax.plot(p_rmsds[:, 0] * args.step_size, p_rmsds[:, 1], label="Projection", color="green", marker="o") ax.plot(cg_rmsds[:, 0] * args.step_size, cg_rmsds[:, 1], label="3D", color="blue", marker="o") ax.set_title("Average RMSD between structures X steps apart.") ax.set_xlabel("steps apart") ax.set_ylabel("RMSD") leg = ax.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) fig.patch.set_facecolor('white') if args.save_fig: with open(args.save_fig, "w") as f: pickle.dump(fig, f) plt.show()
def main(): usage = """ python cg_to_fornac_html.py file1.cg file2.cg Convert coarse grain files to html files using fornac to display a 2D version of the structure. """ num_args = 1 parser = OptionParser(usage=usage) parser.add_option( '-d', '--distance', dest='distance', default=25, help= "Draw links between elements that are within a certain distance from each other", type='float') parser.add_option( '-b', '--bp-distance', dest='bp_distance', default=16, help= "Draw links only between nucleotides which are so many nucleotides apart", type='int') parser.add_option('-s', '--sort-by', dest='sort_by', default='mcc', help="What to sort by (options: mcc, pca)", type='string') parser.add_option('-n', '--names', dest='names', default=False, action='store_true', help='Add the name of the structure to the display') (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1) structs = [] pair_bitmaps = [] cgs = [] all_links = [] mccs = [] cm = None for filename in args: cg = ftmc.CoarseGrainRNA(filename) cgs += [cg] if not cm: cm = ftme.AdjacencyCorrelation(cg) (links, pair_bitmap) = extract_extra_links( cg, options.distance, options.bp_distance, correct_links=None if len(all_links) == 0 else all_links[0]) all_links += [links] pair_bitmaps += [pair_bitmap] mcc = ftme.mcc(cm.evaluate(cg)) rmsd = ftme.cg_rmsd(cgs[0], cg) seq_struct = { "sequence": cg.seq, "structure": cg.to_dotbracket_string(), "extraLinks": links } fud.pv('options.names') fud.pv('mcc, rmsd') if options.names: seq_struct['name'] = op.basename( filename) + " ({:.2f},{:.1f})".format(mcc, rmsd) else: seq_struct['name'] = '' structs += [seq_struct] mccs += [mcc] if options.sort_by == 'pca': print("Sorting by pca", file=sys.stderr) ix = reorder_structs(pair_bitmaps) else: print("Sorting by mcc", file=sys.stderr) ix = np.argsort(-np.array(mccs)) new_array = [0 for i in range(len(ix))] for i, x in enumerate(ix): new_array[i] = structs[x] print(output_template.format(json.dumps(new_array)))
def rmsd_to_stru(cgs, reference_cg): rmsd = np_nans(len(cgs)) for i, cg in enumerate(cgs): rmsd[i] = ftms.cg_rmsd(cg, reference_cg) return rmsd
def test_building_samples_stats(self): for i in range(RAND_REPETITION): self.builder.build(self.sm) self.assertGreater(ftmsim.cg_rmsd(self.sm.bg, self.cg_copy), 1)