Commit b5d9248f authored by Samuel  HANOT's avatar Samuel HANOT

first commit

parents
This diff is collapsed.
import IMP
import RMF
import IMP.atom
import IMP.rmf
import IMP.pmi
import IMP.pmi.topology
import IMP.pmi.dof
import IMP.pmi.macros
import IMP.pmi.restraints
import IMP.pmi.restraints.stereochemistry
import IMP.bayesianem
import IMP.bayesianem.restraint
import tempfile,os
import sys
import glob
import itertools
import math
import numpy as np
import os
from collections import defaultdict
import IMP.pmi.output
identifier=sys.argv[1]
data_file=sys.argv[2]
num_models=int(sys.argv[3])
sts=sys.argv[4:]
mdl = IMP.Model()
are=IMP.pmi.macros.AnalysisReplicaExchange(mdl,sts)
are.stath0.data.sort(key=lambda d:d.score)
are.stath1.data.sort(key=lambda d:d.score)
are.save_data(data_file)
This diff is collapsed.
import IMP
import IMP.rmf
import RMF
import IMP.display
import scipy
from scipy import interpolate
import numpy as np
import IMP.container
import sys
rmf_file=sys.argv[1]
class MiniNetwork(object):
import itertools
def __init__(self):
self.edges={}
self.nodes=set()
self.networks={}
self.networks_status={}
def add_edge(self,pair):
ps=list(pair)
if pair[0] not in self.edges:
self.edges[pair[0]]=set([pair[1]])
else:
self.edges[pair[0]].add(pair[1])
if pair[1] not in self.edges:
self.edges[pair[1]]=set([pair[0]])
else:
self.edges[pair[1]].add(pair[0])
self.nodes.add(pair[0])
self.nodes.add(pair[1])
attribute_0=None
attribute_1=None
for k in self.networks:
if pair[0] in self.networks[k]: attribute_0=k
if pair[1] in self.networks[k]: attribute_1=k
if attribute_0 is None and attribute_1 is None:
nindex=self.get_number_of_networks()
self.networks[nindex]=set(pair)
elif attribute_0 is not None and attribute_1 is None:
self.networks[attribute_0]|=set(pair)
elif attribute_0 is None and attribute_1 is not None:
self.networks[attribute_1]|=set(pair)
elif attribute_0 is not None and attribute_1 is not None:
if attribute_0 != attribute_1:
self.networks[attribute_0]|=set(pair)
self.networks[attribute_0]|=self.networks[attribute_1]
self.networks.pop(attribute_1)
else:
self.networks[attribute_0]|=set(pair)
#self.networks_status[nindex]="ACTIVE"
#self.update_network(0)
#self.purge_network()
def get_number_of_networks(self):
return len(self.networks.keys())
def purge_network(self):
for k in self.networks_status.keys():
if self.networks_status[k] == "DELETE":
self.networks.pop(k)
self.networks_status.pop(k)
def update_network(self,iter):
iter+=1
for (k,j) in list(self.itertools.combinations(self.networks.keys(), 2)):
if self.networks_status[j]!="DELETE" and self.networks_status[k]!="DELETE":
for item in self.networks[j]:
if item in self.networks[k]:
self.networks[k]|=self.networks[j]
self.networks_status[j]="DELETE"
self.update_network(iter)
def get_networks(self):
networks=[]
for k in self.networks:
netws=list(self.networks[k])
networks.append(self.order_network(netws))
return networks
def order_network(self,inputlist):
ordered_list=[]
nstart=None
print "NEW SEQUENCE"
for n in inputlist:
print n,len(self.edges[n])
if len(self.edges[n])==1:
print "GOT IT"
nstart=n
break
if not nstart: return []
oldedges=list(self.edges[nstart])
oldedge=oldedges[0]
oldedgem1=nstart
nedges=len(self.edges[oldedge])
ordered_list.append(oldedgem1)
ordered_list.append(oldedge)
while nedges==2:
newedges=list(self.edges[oldedge])
nedges=len(newedges)
if nedges == 1: break
newedges.remove(oldedgem1)
oldedgem1=oldedge
oldedge=list(newedges)[0]
ordered_list.append(oldedge)
return ordered_list
#test mini network
mn=MiniNetwork()
"""
mn.add_edge((1,2))
print mn.get_networks()
mn.add_edge((4,3))
print mn.get_networks()
mn.add_edge((2,3))
print mn.get_networks()
exit()
"""
m=IMP.Model()
rh = RMF.open_rmf_file_read_only(rmf_file)
prot = IMP.rmf.create_hierarchies(rh, m)[0]
IMP.rmf.link_hierarchies(rh, [prot])
IMP.rmf.load_frame(rh, RMF.FrameID(0))
del rh
sel=IMP.atom.Selection(prot, resolution=1, state_index=0)
sel-=IMP.atom.Selection(prot, molecule='Rrp46_gfp', residue_indexes=range(247, 1000))
ps = [p for p in sel.get_selected_particles() if IMP.core.XYZR.get_is_setup(p)]
particle_pairs=[]
for n,p in enumerate(ps):
#if IMP.atom.Bond.get_is_setup(p): print p, "BOND"
print "atom",n
if IMP.atom.Bonded.get_is_setup(p):
for i in range(IMP.atom.Bonded(p).get_number_of_bonds()):
mn.add_edge((p.get_index(),IMP.atom.Bonded(p).get_bonded(i).get_particle().get_index()))
nets=mn.get_networks()
print nets
hbase=IMP.atom.Hierarchy(IMP.Particle(m))
rh = RMF.create_rmf_file("%s_rendered.rmf3"%(rmf_file))
for net in nets:
coords=[]
radii=[]
for n,index in enumerate(net):
coor=list(IMP.core.XYZ(m,index).get_coordinates())
rad=[IMP.core.XYZR(m,index).get_radius()]
color=IMP.display.Colored(m,index).get_color()
rgb=[color.get_red(),color.get_green(),color.get_blue()]
coords.append(coor+rad+rgb)
#radii.append([IMP.core.XYZR(m,index).get_radius(),float(n)])
data=np.array(coords)
#This is your data, but we're 'zooming' into just 5 data points
#because it'll provide a better visually illustration
#also we need to transpose to get the data in the right format
data = data.transpose()
#now we get all the knots and info about the interpolated spline
try:
tck, u= interpolate.splprep(data,s=1.0)
except:
continue
#here we generate the new interpolated dataset,
#increase the resolution by increasing the spacing, 500 in this example
new = interpolate.splev(np.linspace(0,1,len(coords)*2), tck)
#radiinp=np.array(radii)
#radiinp=radiinp.transpose()
#tck1, u1= interpolate.splprep(radiinp,s=0.1)
#newradii = interpolate.splev(np.linspace(0,1,len(radiinp)), tck1)
sgs=[]
for n in range(1,len(new[0])):
xn=new[0][n]
yn=new[1][n]
zn=new[2][n]
xnm1=new[0][n-1]
ynm1=new[1][n-1]
znm1=new[2][n-1]
radn=new[3][n]
radnm1=new[3][n-1]
avrad=(radn+radnm1)/2.0/2.0
redn=new[4][n]
rednm1=new[4][n-1]
greenn=new[5][n]
greennm1=new[5][n-1]
bluen=new[6][n]
bluenm1=new[6][n-1]
avred=(redn+rednm1)/2.0
avgreen=(greenn+greennm1)/2.0
avblue=(bluen+bluenm1)/2.0
c=IMP.display.Color(avred,avgreen,avblue)
#sgs.append(IMP.display.SegmentGeometry(IMP.algebra.Segment3D(IMP.algebra.Vector3D(xn,yn,zn),IMP.algebra.Vector3D(xnm1,ynm1,znm1))))
#cyl=IMP.algebra.Cylinder3D(IMP.algebra.Segment3D(IMP.algebra.Vector3D(xn,yn,zn),IMP.algebra.Vector3D(xnm1,ynm1,znm1)),avrad)
#sgs.append(IMP.display.CylinderGeometry(cyl,c))
seg=IMP.algebra.Segment3D(IMP.algebra.Vector3D(xn,yn,zn),IMP.algebra.Vector3D(xnm1,ynm1,znm1))
sgs.append(IMP.display.SegmentGeometry(seg,c))
'''
hroot=IMP.atom.Hierarchy(IMP.Particle(m))
hbase.add_child(hroot)
xn=new[0][1]
yn=new[1][1]
zn=new[2][1]
radn=new[3][1]
redn=new[4][1]
greenn=new[5][1]
bluen=new[6][1]
c=IMP.display.Color(avred,avgreen,avblue)
p=IMP.Particle(m)
xyzr=IMP.core.XYZR.setup_particle(p)
xyzr.set_coordinates((xn,yn,zn))
xyzr.set_radius(radn)
colored=IMP.display.Colored.setup_particle(p,c)
h=IMP.atom.Hierarchy.setup_particle(p)
hroot.add_child(h)
IMP.atom.Bonded.setup_particle(p)
particles=[p]
for n in range(1,len(new[0])):
xn=new[0][n]
yn=new[1][n]
zn=new[2][n]
radn=new[3][n]
redn=new[4][n]
greenn=new[5][n]
bluen=new[6][n]
c=IMP.display.Color(avred,avgreen,avblue)
p=IMP.Particle(m)
xyzr=IMP.core.XYZR.setup_particle(p)
xyzr.set_coordinates((xn,yn,zn))
xyzr.set_radius(radn)
colored=IMP.display.Colored.setup_particle(p,c)
h=IMP.atom.Hierarchy.setup_particle(p)
IMP.atom.Bonded.setup_particle(p)
hroot.add_child(h)
IMP.atom.Bonded.setup_particle(p)
particles.append(p)
IMP.atom.create_bond(
IMP.atom.Bonded(p),
IMP.atom.Bonded(particles[n-1]),
1)
'''
IMP.rmf.add_geometries(rh, sgs)
IMP.rmf.add_hierarchies(rh, [hbase])
IMP.rmf.save_frame(rh)
del rh
import IMP
import RMF
import IMP.atom
import IMP.rmf
import IMP.pmi
import IMP.pmi.topology
import IMP.pmi.dof
import IMP.pmi.macros
import IMP.pmi.restraints
import IMP.pmi.restraints.stereochemistry
import IMP.bayesianem
import IMP.bayesianem.restraint
import tempfile,os
import sys
import glob
import itertools
import math
import numpy as np
class Cluster(object):
def __init__(self,rmf_file, rmf_frame, score, rmsd):
self.members=[]
self.add_member(rmf_file, rmf_frame, score, rmsd)
def add_member(self,rmf_file, rmf_frame, score, rmsd):
self.members.append((rmf_file, rmf_frame, score, rmsd))
self.members.sort(key=lambda t: t[2])
def get_best_member(self):
return self.members[0]
class ClusterDict(object):
def __init__(self, cluster):
self.dict={0:cluster}
def add_cluster(self,cluster):
new_key=max(self.dict.keys())+1
self.dict[new_key]=cluster
def find_mol_by_name(hier,k):
molname, copy_num = k.split('.')
copy_num=int(copy_num)
for mol in hier.get_children()[0].get_children():
if molname == mol.get_name() and copy_num == IMP.atom.Copy(mol).get_copy_index():
return mol
return None
def get_num_residues(state):
N=0
for mol in state.get_children():
name=mol.get_name()
parts=True
i=1
while parts:
sel=IMP.atom.Selection(mol,residue_index=i,representation_type=IMP.atom.BALLS,resolution=1)
parts=sel.get_selected_particles()
if parts:
i=i+1
N+=i-1
return N
def get_moldict_xyzdict(state, dist_dict=None):
moldict={}
mol_xyzs={}
added=[]
for mol in state.get_children():
print mol.get_name(), mol
name=mol.get_name()
parts=True
mol_xyzs[mol]=[]
i=1
while parts:
sel=IMP.atom.Selection(mol,residue_index=i,representation_type=IMP.atom.BALLS,resolution=1)
parts=sel.get_selected_particles()
if parts:
mol_xyzs[mol].append(IMP.core.XYZ(parts[0]))
i=i+1
mols=[mol]
if dist_dict is not None:
for k1,k2 in dist_dict.keys():
if find_mol_by_name(state.get_parent(),k1)==mol:
mols.append(find_mol_by_name(state.get_parent(),k2))
if not mol in added:
if name not in moldict:
moldict[name]=[]
moldict[name]+=list(set(mols))
added+=list(set(mols))
print moldict
return moldict, mol_xyzs
def get_xyzs(mol):
i=1
mol_xyzs=[]
parts=True
while parts:
sel=IMP.atom.Selection(mol,residue_index=i,representation_type=IMP.atom.BALLS,resolution=1)
parts=sel.get_selected_particles()
if parts:
mol_xyzs.append(IMP.core.XYZ(parts[0]))
i=i+1
return mol_xyzs
def make_xyzs_pairs_dict(dist_dict, hier1, hier2):
xyzs_pairs_dict={}
if dist_dict is not None:
for (k1,k2), index_dict in dist_dict.iteritems():
m1 = find_mol_by_name(hier1,k1)
m2 = find_mol_by_name(hier2,k2)
parts_m1=IMP.atom.Selection(m1,residue_indexes=index_dict.keys(),resolution=1,representation_type=IMP.atom.BALLS).get_selected_particles()
parts_m2=IMP.atom.Selection(m2,residue_indexes=index_dict.values(),resolution=1,representation_type=IMP.atom.BALLS).get_selected_particles()
xyzs1 = [IMP.core.XYZ(p) for p in parts_m1]
xyzs2 = [IMP.core.XYZ(p) for p in parts_m2]
xyzs_pairs_dict[(m1,m2)]=(xyzs1, xyzs2)
return xyzs_pairs_dict
def get_permutation_with_lowest_rmsd(ref_mols, mols, xyzs_pairs_dict=None):
mols_list=[]
rmsd=[]
for rmf_mols in itertools.permutations(mols):
ref_xyzs=[]
rmf_xyzs=[]
for (m1,m2) in zip(ref_mols, rmf_mols):
if (m1,m2) in xyzs_pairs_dict:
ref_xyzs+=xyzs_pairs_dict[(m1,m2)][0]
rmf_xyzs+=xyzs_pairs_dict[(m1,m2)][1]
else:
ref_xyzs += get_xyzs(m1)
rmf_xyzs += get_xyzs(m2)
r=IMP.atom.get_rmsd(rmf_xyzs, ref_xyzs)
rmsd.append(r)
mols_list.append(rmf_mols)
m=min(rmsd)
return mols_list[rmsd.index(m)],m
output_objects=[]
pdb_id=sys.argv[1]
rmf_file=sys.argv[2]
frame_idx=int(sys.argv[3])
if len(sys.argv)>4:
dist_dict_file=sys.argv[4]
dist_dict=None
try:
with open(dist_dict_file, 'r') as f:
c=""
for line in f:
c=line
dist_dict=eval(c)
except:
pass
root_dir='/baycells/scratch/shanot/EM_benchmark/'
output_dir='%s/modeling/%s/'%(root_dir, pdb_id)
mdl = IMP.Model()
ref_fh = RMF.open_rmf_file_read_only("%s/ref.rmf"%(output_dir))
ref_h=IMP.rmf.create_hierarchies(ref_fh, mdl)
ref_state = ref_h[0].get_children()[0]
ref_gmm_sel=IMP.atom.Selection(ref_state, representation_type=IMP.atom.DENSITIES)
ref_gaussians=ref_gmm_sel.get_selected_particles()
ref_moldict, ref_xyzs = get_moldict_xyzdict(ref_state, dist_dict)
rmf_hh=None
distances={}
m=None
M=None
print(rmf_file, frame_idx)
rmf_fh = RMF.open_rmf_file_read_only(rmf_file)
num_frames = rmf_fh.get_number_of_frames()
rmf_state=None
rmf_hh=None
xyzs_pairs_dict={}
if num_frames>0:
if rmf_hh is None:
rmf_hh = IMP.rmf.create_hierarchies(rmf_fh, mdl)
rmf_state = rmf_hh[0].get_children()[0]
gmm_sel=IMP.atom.Selection(rmf_state, representation_type=IMP.atom.DENSITIES)
rmf_gaussians=gmm_sel.get_selected_particles()
rmf_moldict, rmf_xyzs = get_moldict_xyzdict(rmf_state, dist_dict)
xyzs_pairs_dict = make_xyzs_pairs_dict(dist_dict, ref_h[0], rmf_hh[0])
else:
IMP.rmf.link_hierarchies(rmf_fh, rmf_hh)
try:
IMP.rmf.load_frame(rmf_fh, RMF.FrameID(frame_idx))
except:
print("skipping frame %s:%d\n"%(rmf_file, frame_idx))
#compare to reference structure
rmsd_dict={}
position_dict={} #the postitional error of the center of mass of subunits
angular_dict={} #the angular error of the center of mass of subunits
perc_dict={} # the percentage of CAs withing 10A of the reference position
total_rmsd=0.0
total_perc=0.0
total_N=0
for molname, ref_mols in ref_moldict.iteritems():
print molname
rmf_mols_best_order, m = get_permutation_with_lowest_rmsd(ref_mols,rmf_moldict[molname], xyzs_pairs_dict)
for n, (ref_mol,rmf_mol) in enumerate(zip(ref_mols,rmf_mols_best_order)):
ref_xyzs=[]
rmf_xyzs=[]
if (ref_mol,rmf_mol) in xyzs_pairs_dict:
print "getting",ref_mol, "and", rmf_mol , "from moldict"
ref_xyzs=xyzs_pairs_dict[(ref_mol,rmf_mol)][0]
rmf_xyzs=xyzs_pairs_dict[(ref_mol,rmf_mol)][1]
else:
ref_xyzs = get_xyzs(ref_mol)
rmf_xyzs = get_xyzs(rmf_mol)
for i, (x1,x2) in enumerate(zip(ref_xyzs, rmf_xyzs)):
dx=IMP.core.get_distance(x1, x2)
distances[(rmf_mol,i)]=dx
if dx<m or m is None:
m=dx
if dx>M or M is None:
M=dx
print m,M
for (mol, res_idx), dx in distances.iteritems():
bead=IMP.atom.Selection(mol,residue_index=res_idx,representation_type=IMP.atom.BALLS).get_selected_particles()
#color = IMP.display.get_rgb_color((dx-m)/(M-m))
c=0
if dx<10:
c=0
elif dx<20:
c=0.5
else:
c=1
color = IMP.display.get_rgb_color(c)
for b in bead:
IMP.display.Colored(b).set_color(color)
outfile="%s/colored.rmf"%(output_dir)
if dist_dict is not None:
outfile="%s/colored_dist_dict.rmf"%(output_dir)
rh = RMF.create_rmf_file(outfile)
IMP.rmf.add_hierarchies(rh, rmf_hh)
IMP.rmf.save_frame(rh)
del rh
>1CS4:A
MHHHHHHAMEMKADINAKQEDMMFHKIYIQKHDNVSILFADIEGFTSLASQCTAQELVMTLNELFARFDKLAAENHCLRIKILGDCYYCVSGLPEARADHAHCCVEMGMDMIEAISLVREMTGVNVNMRVGIHSGRVHCGVLGLRKWQFDVWSNDVTLANHMEAGGKAGRIHITKATLSYLNGDYEVEPGCGGERNAYLKEHSIETFLILRCTQKRKEEKAMIAK
>1CS4:B
RSLKNEELYHQSYDCVCVMFASIPDFKEFYTESDVNKEGLECLRLLNEIIADFDDLLSKPKFSGVEKIKTIGSTYMAATGLSAIPSQEHAQEPERQYMHIGTMVEFAYALVGKLDAINKHSFNDFKLRVGINHGPVIAGVIGAQKPQYDIWGNTVNVASRMDSTGVLDKIQVTEETSLILQTLGYTCTCRGIINVKGKGDLKTYFVNTEMSR
>1CS4:C
MGCLGNSKTEDQRNEEKAQREANKKIEKQLQKDKQVYRATHRLLLLGAGESGKSTIVKQMRILHVNGFNGEGGEEDPQAARSNSDGEKATKVQDIKNNLKEAIETIVAAMSNLVPPVELANPENQFRVDYILSVMNVPDFDFPPEFYEHAKALWEDEGVRACYERSNEYQLIDCAQYFLDKIDVIKQDDYVPSDQDLLRCRVLTSGIFETKFQVDKVNFHMFDVGGQRDERRKWIQCFNDVTAIIFVVASSSYNMVIREDNQTNRLQEALNLFKSIWNNRWLRTISVILFLNKQDLLAEKVLAGKSKIEDYFPEFARYTTPEDATPEPGEDPRVTRAKYFIRDEFLRISTASGDGRHYCYPHFTCAVDTENIRRVFNDCRDIIQRMHLRQYELL
This diff is collapsed.
This diff is collapsed.
>1GPQ:A
QDDLTISSLAKGETTKAAFNQMVQGHKLPAWVMKGGTYTPAQTVTLGDETYQVMSACKPHDCGSQRIAVMWSEKSNQMTGLFSTIDEKTSQEKLTWLNVNDALSIDGKTVLFAALTGSLENHPDGFNFRHHHHHH
>1GPQ:B
QDDLTISSLAKGETTKAAFNQMVQGHKLPAWVMKGGTYTPAQTVTLGDETYQVMSACKPHDCGSQRIAVMWSEKSNQMTGLFSTIDEKTSQEKLTWLNVNDALSIDGKTVLFAALTGSLENHPDGFNFRHHHHHH
>1GPQ:C
KVFGRCELAAAMKRHGLDNYRGYSLGNWVCAAKFESNFNTQATNRNTDGSTDYGILQINSRWWCNDGRTPGSRNLCNIPCSALLSSDITASVNCAKKIVSDGNGMNAWVAWRNRCKGTDVQAWIRGCRL
>1GPQ:D
KVFGRCELAAAMKRHGLDNYRGYSLGNWVCAAKFESNFNTQATNRNTDGSTDYGILQINSRWWCNDGRTPGSRNLCNIPCSALLSSDITASVNCAKKIVSDGNGMNAWVAWRNRCKGTDVQAWIRGCRL
This diff is collapsed.
|0|0.0214421648|-18.546373 19.231449 -1.810386|14.6375862007 -2.1873650620 3.4463588503 -2.1873650620 8.1343153500 -3.1750163973 3.4463588503 -3.1750163973 15.9954056812|