IMP logo
RMF examples

adding data

This example shows how to add some arbitrary data to an RMF file. In this case, a per-frame score.

import IMP.atom
import RMF
import IMP.rmf
# Create a new IMP.atom.Hierarchy from the contents of the pdb file
# find the name for a temporary file to use to for writing the hdf5 file
tfn=IMP.create_temporary_file_name("pdb", ".rmf")
print "File name is", tfn
# open the temporary file, clearing any existing contents
my_kc= rh.add_category("my data");
# add the hierarchy to the file
# change a coordinate
# add the new configuration to the file as frame 1
# create my key
my_key= RMF.get_float_key_always(rh, my_kc, "my score", True)
# make up scores
rh.get_root_node().set_value(my_key, 3, 0)
rh.get_root_node().set_value(my_key, 5, 1)

alias

Show using aliases to provide two different organization schemes for manipulating a hierarchy.

import RMF
import IMP.base
# find the name for a temporary file to use to for writing the hdf5 file
print "File is", tfn
# open the temporary file, clearing any existing contents
rh= fh.get_root_node()
# add a bunch of "molecules" with colors
mrh= rh.add_child("molecules", RMF.REPRESENTATION)
red=[]
green=[]
blue=[]
for i in range(0,10):
curph= mrh.add_child("mol"+str(i), RMF.REPRESENTATION)
for j in range(0,10):
curh= curph.add_child("atom"+str(j), RMF.REPRESENTATION)
p= pf.get(curh)
p.set_coordinates([3*i,3*j,0])
p.set_mass(1)
p.set_radius(1)
c= cf.get(curh)
if j%3==0:
c.set_rgb_color([1,0,0])
red.append(curh)
elif j%3==1:
c.set_rgb_color([0,1,0])
green.append(curh)
else:
c.set_rgb_color([0,0,1])
blue.append(curh)
# add a second organization scheme by color
crh= rh.add_child("colors", RMF.REPRESENTATION)
redh= crh.add_child("red", RMF.REPRESENTATION)
greenh= crh.add_child("green", RMF.REPRESENTATION)
blueh= crh.add_child("blue", RMF.REPRESENTATION)
for r in red:
for g in green:
for b in blue:

create

This example creates an RMF file that tries to exploit as many capabilities of RMF as possible.

import RMF
rmf= RMF.create_rmf_file("example.rmf")
nframes=10
colored_factory= RMF.ColoredFactory(rmf)
particle_factory= RMF.ParticleFactory(rmf)
intermediate_particle_factory= RMF.IntermediateParticleFactory(rmf)
rigid_particle_factory = RMF.RigidParticleFactory(rmf)
ball_factory = RMF.BallFactory(rmf)
cylinder_factory = RMF.CylinderFactory(rmf)
segment_factory = RMF.SegmentFactory(rmf)
journal_article_factory= RMF.JournalArticleFactory(rmf)
residue_factory= RMF.ResidueFactory(rmf)
atom_factory= RMF.AtomFactory(rmf)
chain_factory= RMF.ChainFactory(rmf)
domain_factory= RMF.DomainFactory(rmf)
copy_factory= RMF.CopyFactory(rmf)
diffuser_factory= RMF.DiffuserFactory(rmf)
typed_factory= RMF.TypedFactory(rmf)
score_factory= RMF.ScoreFactory(rmf)
# add a coase grained molecule
cgchain= rmf.get_root_node().add_child("1", RMF.REPRESENTATION)
cg_bond_scores=cgchain.add_child("bond scores", RMF.FEATURE)
for i in range(0,5):
r= cgchain.add_child(str(i), RMF.REPRESENTATION)
rd= residue_factory.get(r)
rd.set_type("LEU")
rd.set_index(i)
pd= particle_factory.get(r)
pd.set_mass(120)
pd.set_radius(4)
cd= colored_factory.get(r)
cd.set_rgb_color([1,0,0])
if i > 0:
bs= cg_bond_scores.add_child(str(i), RMF.FEATURE)
sd= score_factory.get(bs)
sd.set_representation([last,r])
for j in range(0,nframes):
pdpf= particle_factory.get(r, j)
pdpf.set_coordinates([0, j*i*4, j*2])
if i >0:
sdpf= score_factory.get(bs, j)
sdpf.set_score(j)
last=r
# create an atomic protein fragment
chain= rmf.get_root_node().add_child("chain", RMF.REPRESENTATION)
chain_factory.get(chain).set_chain_id(0)
domain= chain.add_child("n-terminus", RMF.REPRESENTATION)
dd= domain_factory.get(domain)
dd.set_indexes(0,2)
atoms=[]
res_data=[( "MET", 0 , [ ("N", 7 , 14.00 , 1.85 ,
[24.27, 8.37, -9.85] ),
("CA", 6 , 12.01 , 2.27 ,
[24.40, 9.85, -9.93]),
("C", 6 , 12.01 , 2.0 ,
[25.81, 10.24, -10.35] ),
("O", 8 , 15.99 , 1.70 ,
[26.74, 9.46, -10.19]),
("CB", 6 , 12.0107 , 2.17 ,
[24.06, 10.49, -8.59] ),
("CG", 6 , 12.01 , 2.17 ,
[24.87, 9.93, -7.44] ),
("SD", 16 , 32.06 , 2.0 ,
[24.26, 10.55, -5.87] ),
("CE", 6 , 12.01 , 2.05 ,
[24.82, 12.26, -5.96] ),
]),
( "VAL", 1 , [ ("N", 7 , 14.00 , 1.85 ,
[25.96, 11.45, -10.90] ),
("CA", 6 , 12.01 , 2.27 ,
[27.26, 11.92, -11.35] ),
("C", 6 , 12.01 , 2.0 ,
[27.39, 13.42, -11.11] ),
("O", 8 , 15.99 , 1.70 ,
[26.44, 14.18, -11.32] ),
("CB", 6 , 12.01 , 2.27 ,
[27.45, 11.63, -12.87] ),
("CG1", 6 , 12.01 , 2.05 ,
[28.75, 12.20, -13.38] ),
("CG2", 6 , 12.01 , 2.05 ,
[27.43, 10.13, -13.14] ),
]),
( "LEU", 2 , [ ("N", 7 , 14.00 , 1.85 ,
[28.55, 13.85, -10.63] ),
("CA", 6 , 12.01 , 2.27 ,
[28.79, 15.26, -10.39] ),
("C", 6 , 12.01 , 2.0 ,
[29.49, 15.90, -11.58] ),
("O", 8 , 15.99 , 1.70 ,
[30.25, 15.23, -12.30] ),
("CB", 6 , 12.01 , 2.17 ,
[29.68, 15.47, -9.15] ),
("CG", 6 , 12.01 , 2.27 ,
[29.08, 15.41, -7.75] ),
("CD1", 6 , 12.01 , 2.05 ,
[28.72, 13.98, -7.38] ),
("CD2", 6 , 12.01 , 2.05 ,
[30.08, 16.00, -6.77] )]),
]
for rdata in res_data:
r=domain.add_child(rdata[0], RMF.REPRESENTATION);
rd= residue_factory.get(r)
rd.set_type(rdata[0])
rd.set_index(rdata[1])
for adata in rdata[2]:
a= r.add_child(adata[0], RMF.REPRESENTATION)
atoms.append(a)
ad= atom_factory.get(a)
ad.set_element(adata[1])
ad.set_mass(adata[2])
ad.set_radius(adata[3])
for i in range(0,nframes):
adpf= atom_factory.get(a, i)
adpf.set_coordinates([adata[4][0]+i,
adata[4][1]+i,
adata[4][2]+i])
bond_data=[(1,4),(0,1),(1,2),(2,3),(4,5),(5,6),(6,7),(2,8),(9,12),(8,9),(9,10),
(10,11),(12,13),(12,14),(10,15),(16,19),(15,16),(16,17),(17,18),(19,20),
(20,21),(20,22)]
bond_scores=chain.add_child("bond scores", RMF.FEATURE)
for b in bond_data:
eps=[atoms[b[0]], atoms[b[1]]]
rmf.add_node_pair(eps, RMF.BOND)
bd= bond_scores.add_child(str(b), RMF.FEATURE)
sd= score_factory.get(bd)
sd.set_score(0)
sd.set_representation(eps)
# now add some geometry floating around
#group it for kicks
dg= rmf.get_root_node().add_child("dynamic geometry", RMF.GEOMETRY)
bn= dg.add_child("ball", RMF.GEOMETRY)
cn= dg.add_child("cylinder", RMF.GEOMETRY)
sn= dg.add_child("segment", RMF.GEOMETRY)
cd= colored_factory.get(bn)
cd.set_rgb_color([float(5)/float(nframes),1,0])
for i in range(1, nframes, 2):
bd= ball_factory.get(bn, i)
bd.set_radius(i)
bd.set_coordinates([10+i, i, i])
#cd= colored_factory.get(bn)
#cd.set_rgb_color([float(i)/float(nframes),1,0])
cd= cylinder_factory.get(cn, i)
cd.set_radius(i)
cd.set_coordinates([[10+i, 12+i], [i,i], [i,i]])
sd= segment_factory.get(sn, i)
sd.set_coordinates([[12+i, 14+i],[i, i], [i,i]])
sg= rmf.get_root_node().add_child("static geometry", RMF.GEOMETRY)
bn= sg.add_child("ball", RMF.GEOMETRY)
sn= sg.add_child("segment", RMF.GEOMETRY)
cn= sg.add_child("cylinder", RMF.GEOMETRY)
bd= ball_factory.get(bn)
bd.set_radius(3)
bd.set_coordinates([-10, 0, 0])
#cd= colored_factory.get(bn)
#cd.set_rgb_color([0,0,1])
sd= segment_factory.get(sn)
sd.set_coordinates([[-18, -20], [0,0], [0,0]])
cd= cylinder_factory.get(cn)
cd.set_radius(2)
cd.set_coordinates([[-12, -14], [0, 0], [0,0]])
print "done"

rmf decorator

The example shows how to use the decorators by printing the coordates and radius of every node that has them.

import RMF
def traverse(nh, pf, frame):
# check if the current node has coordinates and radius
if pf.get_is(nh, frame):
# create a decorator for the current node
d=pf.get(nh, frame)
print nh.get_name(), d.get_coordinates()
# recurse on the children
children= nh.get_children()
for c in children:
traverse(c, pf, frame)
traverse(fch.get_root_node(), pf, 0)

rmf xml

This example converts an RMF file to xml, in order to display it in an XML viewer such as firefox. Its functionality is largely identical to the rmf_xml program included in RMF Tools. The secondary purpose it to provide an example of extracting all data from an RMF file and converting it to some other source. The data extracted here is completely uninterpreted (eg, cartesian coordinates are treated just the same as other values). See the source code of rmf_xml for a similar example in C++.

import RMF
# don't bother with command line arguments, to keep in simple
file_name=RMF.get_example_path("simple.rmf")
frame=0
verbose=True
# show the data with the specified key category
def show_data_xml(nh, kc, arity=1):
rh= nh.get_file()
# get all the keys, we could pull this up in the call stack
keys= rh.get_keys(kc, arity)
opened=False
for k in keys:
if nh.get_has_value(k, frame):
if not opened:
print "<", rh.get_name(kc)
opened=True
name=rh.get_name(k)
name.replace(" ", "_")
print name,"=\""+str(nh.get_value(k, frame))+"\""
if opened:
print "/>"
def show_xml(nh, kcs):
name=nh.get_name()
name.replace(" ", "_")
print "<node name=\""+name+"\" id=\""+str(nh.get_id().get_index())\
+"\" type=\""+RMF.get_type_name(nh.get_type())+"\"/>";
if verbose:
for kc in kcs:
show_data_xml(nh, kc)
children= nh.get_children()
for c in children:
print "<child>"
show_xml(c, kcs);
print "</child>"
# open the file, and don't clear the contents
rh= RMF.open_rmf_file(file_name);
print "<?xml version=\"1.0\"?>"
print "<rmf>"
print "<path>"
print file_name
print "</path>"
print "<description>"
print rh.get_description()
print "</description>"
print "<path>"
print input
print "</path>"
kcs= rh.get_categories()
show_xml(rh.get_root_node(), kcs)
for i in range(2,5):
sets= rh.get_node_sets(i)
kcs= rh.get_set_categories(i)
if len(sets) >0:
print "<"+str(i)+"_sets>"
for t in sets:
print "<"+str(i)+"_set id=\""+str(t.get_id().get_index())+"\" type=\""\
+ RMF.get_set_type_name(t.get_type())+"\" members=\""\
+",".join([str(t.get_node(x).get_id().get_index()) for x in range(0, t.get_arity())])+"\"/>"
if verbose:
for kc in kcs:
show_data_xml(t, kc, i)
print "</"+str(i)+"_sets>"
print "</rmf>";


Generated on Tue May 22 2012 23:33:21 for IMP by doxygen 1.8.1