Skip to content

Commit

Permalink
Misc. updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alsinmr committed Aug 7, 2024
1 parent 1c53ee1 commit 811a110
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 88 deletions.
92 changes: 72 additions & 20 deletions Entropy/Entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def CC(self):
i=top==0
bottom[i]=1
self._CC=top/bottom
return self._CC
return copy(self._CC)

@property
def Smax(self):
Expand Down Expand Up @@ -659,7 +659,42 @@ def CCpca(self):

return self._CCpca

def plotCCpca(self,index=None,ax=None,**kwargs):
def CCpca_states(self,states:list):
"""
Evaluates the pca CC for specific states in the PCA
Parameters
----------
states : list
DESCRIPTION.
Returns
-------
None.
"""

PCAstate=self.PCA.Cluster.state
i=np.sum([PCAstate==state for state in states],axis=0).astype(bool)

p=np.unique(PCAstate[i],return_counts=True)[1]/i.sum()

Spca=-(p*np.log(p)).sum() #PCA entropy

CC=[]
for state in self.state:
p=np.unique(state[i],return_counts=True)[1]/i.sum()
Ssc=-(p*np.log(p)).sum()

Tstate=state*(PCAstate.max()+1)+PCAstate
p=np.unique(Tstate[i],return_counts=True)[1]/i.sum()
Stotal=-(p*np.log(p)).sum()

CC.append(2*(Ssc+Spca-Stotal)/(Ssc+Spca))

return np.array(CC)

def plotCCpca(self,index=None,states:list=None,ax=None,**kwargs):
if ax is None:ax=plt.subplots()[1]
if index is None:index=np.ones(len(self.CCpca),dtype=bool)

Expand Down Expand Up @@ -849,19 +884,20 @@ def chimera(self,index=None,scaling:float=None,norm:bool=True,color=[1,0,0,1]):
CMXRemote.send_command(ID,f'show #{mn}&~@H*&~:GLY,ALA,PRO')


def CCchimera(self,index=None,indexCC:int=None,scaling:float=None,norm:bool=True) -> None:
def CCchimera(self,index=None,indexCC:int=None,states:list=None,scaling:float=None,norm:bool=True) -> None:
"""
Plots the cross correlation of motion for a given detector window in
chimera.
Plots the cross correlation of motion.
Parameters
----------
index : list-like, optional
Select which residues to plot. The default is None.
Select which residues to plot. The default is None (all residues).
indexCC : int,optional
Select which row of the CC matrix to show. Must be used in combination
with rho_index. Note that if index is also used, indexCC is applied
AFTER index.
Set indexCC to 'PCA' to correlate with the backbone
scaling : float, optional
Scale the display size of the detectors. If not provided, a scaling
will be automatically selected based on the size of the detectors.
Expand All @@ -879,16 +915,24 @@ def CCchimera(self,index=None,indexCC:int=None,scaling:float=None,norm:bool=True

CMXRemote=clsDict['CMXRemote']

index=np.arange(self.CC.shape[0]) if index is None else np.array(index)
index=np.arange(self.resids.size) if index is None else np.array(index)


x=self.CC[index][:,index]
x[np.isnan(x)]=0
x[x<0]=0
if isinstance(indexCC,str) and indexCC.lower()=='pca':
if states is not None:
x=self.CCpca_states(states)[index]
else:
x=self.CCpca[index]
x[np.isnan(x)]=0
x *= 1/x.max() if scaling is None else scaling
else:
x=self.CC[index][:,index]
x[np.isnan(x)]=0
x[x<0]=0
x -=np.eye(x.shape[0])
x *= 1/x.max() if scaling is None else scaling
x+=np.eye(x.shape[0])


x -=np.eye(x.shape[0])
x *= 1/(x-np.eye(x.shape[0])).max() if scaling is None else scaling
x+=np.eye(x.shape[0])

if self.project is not None:
ID=self.project.chimera.CMXid
Expand All @@ -897,7 +941,6 @@ def CCchimera(self,index=None,indexCC:int=None,scaling:float=None,norm:bool=True
ID=self.project.chimera.CMXid
else:
ID=CMXRemote.launch()
cmds=[]


ids=np.array([s.atoms.indices for s in self.resi[index]],dtype=object)
Expand All @@ -909,16 +952,25 @@ def CCchimera(self,index=None,indexCC:int=None,scaling:float=None,norm:bool=True


if indexCC is not None:
x=x[indexCC].squeeze()
if not(isinstance(indexCC,str) and indexCC.lower()=='pca'):
x=x[indexCC].squeeze()
else:
indexCC=None
x[np.isnan(x)]=0
self.select.chimera(x=x,index=index)
sel0=self.select.repr_sel[index][indexCC]



mn=CMXRemote.valid_models(ID)[-1]
CMXRemote.send_command(ID,'color '+'|'.join([f'#{mn}/{s.segid}:{s.resid}@{s.name}' for s in sel0])+' black')
if indexCC is not None:
sel0=self.select.repr_sel[index][indexCC]
if hasattr(sel0,'size'):sel0=sel0[0] #Depending on indexing type, we may still have a numpy array here
CMXRemote.send_command(ID,'color '+'|'.join([f'#{mn}/{s.segid}:{s.resid}@{s.name}' for s in sel0])+' black')

CMXRemote.send_command(ID,f'~show #{mn}')
CMXRemote.send_command(ID,f'ribbon #{mn}')
CMXRemote.send_command(ID,f'show #{mn}&~@H*&~:GLY,ALA,PRO')
CMXRemote.send_command(ID,f'set bgColor gray')
CMXRemote.send_command(ID,'set bgColor gray')
# print('color '+'|'.join(['#{0}/{1}:{2}@{3}'.format(mn,s.segid,s.resid,s.name) for s in sel0])+' black')
else:

Expand All @@ -928,7 +980,7 @@ def CCchimera(self,index=None,indexCC:int=None,scaling:float=None,norm:bool=True
CMXRemote.send_command(ID,f'~show #{mn}')
CMXRemote.send_command(ID,f'ribbon #{mn}')
CMXRemote.send_command(ID,f'show #{mn}&~@H*&~:GLY,ALA,PRO')
CMXRemote.send_command(ID,f'set bgColor gray')
CMXRemote.send_command(ID,'set bgColor gray')

out=dict(R=x,ids=ids)
CMXRemote.add_event(ID,'CC',out)
Expand Down
14 changes: 13 additions & 1 deletion Frames/eval_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ def frames2iRED(self, rank:int=2, include: list = None) -> list:
A_0m_PASinf.append(vft.D2vec(vZ_inf).mean(axis=-1))

source.details.append('Direct analysis of the correlation function')
source.select.molsys=copy(source.select.molsys)
source.select.molsys.make_pdb(ti=0)


out=[iRED({'v':vZ,'t':v['t'],'index':index,'source':source,'sampling_info':self.sampling_info},rank=rank)]
for k,fn in zip(range(nf+1),self.frame_names(include)):
if k==0:
Expand All @@ -661,7 +665,8 @@ def frames2iRED(self, rank:int=2, include: list = None) -> list:
source.details.append('Analyzed with iRED')
source.additional_info=fn
out.append(iRED({'v':v0,'t':v['t'],'index':index,'source':source,'sampling_info':self.sampling_info},rank=rank))
out.md2data=self.md2data
out[-1].md2data=self.md2data

return out

def md2iRED(self,rank:int=2):
Expand All @@ -680,6 +685,8 @@ def md2iRED(self,rank:int=2):
include=[False for _ in range(len(self.vf))]
out=self.frames2iRED(rank=rank,include=include)[0]
out.source.Type='iREDmode'
out.source.select.molsys=copy(out.source.select.molsys)
out.source.select.molsys.make_pdb(ti=0)
out.md2data=self.md2data
return out

Expand Down Expand Up @@ -832,6 +839,11 @@ def ct2data(ct_out,mol=None):
data.detect=out[0].detect
out.append(data)

ms=copy(out[0].select.molsys)
ms.make_pdb(ti=0)
for d in out:
d.select.molsys=ms

return out


Expand Down
Loading

0 comments on commit 811a110

Please sign in to comment.