Skip to content

Commit 094de4c

Browse files
committed
minor edits
1 parent 811a110 commit 094de4c

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

PCA/PCAsubs.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def write_pdb(self,n:int=0,A:float=None,PCamp:list=None,from_traj:bool=True,sele
953953
it exists or in the same folder as the original topology.
954954
PCamp : list-like, optional
955955
List of amplitudes for the first len(PCamp) principal components.
956-
If this is provided, then 'n' and 'std' are ignored.
956+
If this is provided, then 'n' and 'A' are ignored.
957957
The default is None.
958958
from_traj : bool, optional
959959
Extracts a frame from the trajectory that is closest given PCamps
@@ -973,11 +973,14 @@ def write_pdb(self,n:int=0,A:float=None,PCamp:list=None,from_traj:bool=True,sele
973973
folder=self.project.directory if self.project is not None and self.project.directory is not None \
974974
else os.path.split(self.pca.select.molsys.topo)[0]
975975
filename=os.path.join(folder,'pca.pdb')
976-
atoms=self.pca.uni.atoms.select_atoms(select_str) if from_traj else self.atoms
976+
atoms=self.pca.uni.atoms.select_atoms(select_str) if (from_traj and PCamp is not None) else self.pca.atoms
977977
if PCamp is not None:
978978
if from_traj:
979979
i=self.PC2index(PCamp)
980980
self.pca.traj[i]
981+
if hasattr(self.pca.traj,'traj_index'):
982+
q=self.pca.traj.traj_index
983+
print(f'Extracting frame {self.pca.traj.trajs[q].frame} from trajectory {q}')
981984
atoms.positions=self.pca.align(self.pca.ref_pos,
982985
self.pca.uni.select_atoms(self.pca.align_ref),
983986
atoms)
@@ -992,7 +995,7 @@ def write_pdb(self,n:int=0,A:float=None,PCamp:list=None,from_traj:bool=True,sele
992995
self.pca.traj[frame0]
993996
return filename
994997

995-
def chimera(self,n:int=0,std:float=1,PCamp:list=None,from_traj:bool=True,select_str:str='protein'):
998+
def chimera(self,n:int=0,std:float=1,PCamp:list=None,from_traj:bool=True,select_str:str='protein',cmap_ch='tab10'):
996999
"""
9971000
Plots the change in a structure for a given principal component. That is,
9981001
if n=0, then we plot the mean structure with +/-sigma for the n=0
@@ -1028,12 +1031,13 @@ def chimera(self,n:int=0,std:float=1,PCamp:list=None,from_traj:bool=True,select_
10281031
self
10291032
10301033
"""
1034+
if isinstance(cmap_ch,str):cmap_ch=plt.get_cmap(cmap_ch)
10311035
if PCamp is not None:
10321036
filename=self.write_pdb(n=n,PCamp=PCamp,from_traj=from_traj,select_str=select_str)
10331037
if self.project.chimera.current is None:self.project.chimera.current=0
10341038
self.project.chimera.command_line('open "{0}"'.format(filename))
10351039
mdls=self.project.chimera.CMX.how_many_models(self.project.chimera.CMXid)
1036-
clr=[int(c*100) for c in plt.get_cmap('tab10')(mdls-1)[:-1]]
1040+
clr=[int(c*100) for c in cmap_ch(mdls-1)[:-1]]
10371041
if from_traj:
10381042
self.project.chimera.command_line(['ribbon #{0}','~show #{0}','color #{0} {1},{2},{3}'.format(mdls,clr[0],clr[1],clr[2])])
10391043
else:
@@ -1055,13 +1059,13 @@ def chimera(self,n:int=0,std:float=1,PCamp:list=None,from_traj:bool=True,select_
10551059
if self.project.chimera.current is None:self.project.chimera.current=0
10561060
self.project.chimera.command_line('open "{0}"'.format(filename))
10571061
mdls=self.project.chimera.CMX.how_many_models(self.project.chimera.CMXid)
1058-
clr=[int(c*100) for c in plt.get_cmap('tab10')(mdls-1)[:-1]]
1062+
clr=[int(c*100) for c in cmap_ch(mdls-1)[:-1]]
10591063
self.project.chimera.command_line(['~ribbon','show','color #{0} {1},{2},{3}'.format(mdls,clr[0],clr[1],clr[2])])
10601064
self.project.chimera.command_line(self.project.chimera.saved_commands)
10611065

10621066
return self
10631067

1064-
def hist2struct(self,nmax:int=4,from_traj:bool=True,select_str:str='protein',ref_struct:bool=False,ax=None,**kwargs):
1068+
def hist2struct(self,nmax:int=4,from_traj:bool=True,select_str:str='protein',ref_struct:bool=False,ax=None,cmap_ch='tab10',n_colors=10,**kwargs):
10651069
"""
10661070
Interactively view structures corresponding to positions on the
10671071
histogram plots. Specify the maximum principle component to display. Then,
@@ -1091,6 +1095,10 @@ def hist2struct(self,nmax:int=4,from_traj:bool=True,select_str:str='protein',ref
10911095
None.
10921096
10931097
"""
1098+
1099+
1100+
if isinstance('cmap_ch',str):cmap_ch=plt.get_cmap(cmap_ch).resampled(n_colors)
1101+
10941102
x,y=int(np.ceil(np.sqrt(nmax))),int(np.ceil(np.sqrt(nmax)))
10951103
if (x-1)*y>=nmax:x-=1
10961104

@@ -1109,12 +1117,13 @@ def hist2struct(self,nmax:int=4,from_traj:bool=True,select_str:str='protein',ref
11091117
if ref_struct:
11101118
self.chimera(PCamp=[0])
11111119
mdls=self.project.chimera.CMX.how_many_models(self.project.chimera.CMXid)
1112-
clr=plt.get_cmap('tab10')(mdls-1)
1120+
clr=cmap_ch((mdls-1)%n_colors)
11131121
for k,a in enumerate(ax):
11141122
a.scatter(0,0,marker='x',color=clr)
11151123

11161124
PCamp=[None for _ in range(nmax+1)]
11171125
markers=['x','o','+','v','>','s','1','*']
1126+
mkr_hdls=[]
11181127
def onclick(event):
11191128
if event.inaxes:
11201129
ax0=event.inaxes
@@ -1145,12 +1154,15 @@ def onclick(event):
11451154
PCamp0=self.pca.PCamp[:len(PCamp)][:,i] #Set PCamp to the nearest value in trajectory
11461155
else:
11471156
PCamp0=PCamp
1148-
self.chimera(PCamp=PCamp0,from_traj=from_traj)
1157+
self.chimera(PCamp=PCamp0,from_traj=from_traj,cmap_ch=cmap_ch)
11491158
for k,a in enumerate(ax):
11501159
mdls=self.project.chimera.CMX.how_many_models(self.project.chimera.CMXid)
1151-
clr=plt.get_cmap('tab10')((mdls-1)%10)
1160+
clr=cmap_ch((mdls-1)%n_colors)
11521161

1153-
a.scatter(PCamp0[k],PCamp0[k+1],100,marker=markers[(mdls-1)%len(markers)],linewidth=3,color=clr)
1162+
if mdls==0:
1163+
while mkr_hdls:mkr_hdls.pop().remove()
1164+
1165+
mkr_hdls.append(a.scatter(PCamp0[k],PCamp0[k+1],100,marker=markers[(mdls-1)%len(markers)],linewidth=3,color=clr))
11541166

11551167
#Clear the positions in the plot
11561168
for k in range(len(PCamp)):PCamp[k]=None
@@ -1159,6 +1171,10 @@ def onclick(event):
11591171
h0.set_visible(False)
11601172
plt.pause(0.01)
11611173
else: #Clicking outside the axes clears out the positions
1174+
1175+
if self.project.chimera.CMX.how_many_models(self.project.chimera.CMXid)==0:
1176+
while mkr_hdls:mkr_hdls.pop().remove()
1177+
11621178
for k in range(len(PCamp)):PCamp[k]=None
11631179
for h in hdls:
11641180
for h0 in h:
@@ -1308,6 +1324,19 @@ def plot(self,ax=None,skip:int=10,maxbin:float=None,nbins:int=None,cmap='binary'
13081324

13091325
return ax
13101326

1327+
def plot_t_depend(self,ax=None):
1328+
if ax is None:ax=plt.subplots()[1]
1329+
ax.scatter(self.pca.t,self.state,3,marker='o',color='black')
1330+
ax.set_ylim([-.5,self.n_clusters-.5])
1331+
if len(self.pca.traj.lengths)>1:
1332+
lengths=self.pca.traj.lengths
1333+
for k in range(len(lengths)-1):
1334+
ax.plot(np.sum(lengths[:k+1])*np.ones(2),ax.get_ylim(),color='grey',linestyle=':')
1335+
1336+
return ax
1337+
1338+
1339+
13111340
@property
13121341
def PCavg(self):
13131342
"""

Selection/MolSys.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ def time(self):
422422
def frame(self):
423423
return (self.mda_traj.frame-self.t0)//self.step
424424

425+
@property
426+
def lengths(self):
427+
l=(self.mda_traj.total_times/self.mda_traj.dt).astype(int)
428+
l[0]-=self.t0
429+
l[-1]-=self.__tf-self.tf
430+
return l
431+
432+
425433
@property
426434
def details(self):
427435
out=['Trajectory:'+', '.join(self.files)]
@@ -435,7 +443,7 @@ def __setattr__(self,name,value):
435443
super().__setattr__(name, value)
436444
return
437445

438-
if name in ['t0','tf','step']:
446+
if name in ['t0','tf','step'] and value is not None:
439447
value=int(value)
440448
if name=='tf':
441449
if value is None:value=self.__tf

0 commit comments

Comments
 (0)