def writeHeatmap(filename, heatmap, **kwargs): """Returns *filename* that contains *heatmap* in Heat Mapper :file:`.hm` file (extension is automatically added when not found). *filename* may also be an output stream. :arg title: title of the heatmap :type title: str :arg xlabel: x-axis lab, default is ``'unknown'`` :type xlabel: str :arg ylabel: y-axis lab, default is ``'unknown'`` :type ylabel: str :arg xorigin: x-axis origin, default is 0 :type xorigin: float :arg xstep: x-axis step, default is 1 :type xstep: float :arg min: minimum value, default is minimum in *heatmap* :type min: float :arg max: maximum value, default is maximum in *heatmap* :type max: float :arg format: number format, default is ``'%f'`` :type format: str Other keyword arguments that are arrays with length equal to the y-axis (second dimension of heatmap) will be considered as *numbering*.""" try: ndim, shape = heatmap.ndim, heatmap.shape except: raise TypeError('heatmap must be an array object') if ndim != 2: raise TypeError('heatmap must be a 2D array') try: write, close, stream = filename.write, lambda: None, filename except AttributeError: out = openFile(addext(filename, '.hm'), 'w') write, close, stream = out.write, out.close, out format = kwargs.pop('format', '%f') write('-min "{0}"\n'.format(kwargs.pop('min', heatmap.min()))) write('-max "{0}"\n'.format(kwargs.pop('max', heatmap.max()))) for label, default in [ ('title', 'unknown'), ('xlabel', 'unknown'), ('xorigin', 0), ('xstep', 1), ('ylabel', 'unknown'), ]: write('-{0} "{1}"\n'.format(label, kwargs.pop(label, default))) numbering = [] numlabels = [] for key, val in kwargs.items(): try: length = len(val) except TypeError: LOGGER.warn('Keyword argument {0} is not used.'.format(key)) else: if length == shape[0]: numlabels.append(key) numbering.append(val) if not numbering: numlabels.append('unknown') numbering.append(arange(1, shape[0] + 1)) write('-numbering "{0}"\n'.format(':'.join(numlabels))) for i, row in enumerate(heatmap): write(':'.join(str(nums[i]) for nums in numbering) + ':') row.tofile(stream, sep=';', format=format) write(';\n') close() return filename
def writeNMD(filename, modes, atoms): """Returns *filename* that contains *modes* and *atoms* data in NMD format described in :ref:`nmd-format`. :file:`.nmd` extension is appended to filename, if it does not have an extension. .. note:: #. This function skips modes with zero eigenvalues. #. If a :class:`.Vector` instance is given, it will be normalized before it is written. It's length before normalization will be written as the scaling factor of the vector.""" if not isinstance(modes, (NMA, ModeSet, Mode, Vector)): raise TypeError('modes must be NMA, ModeSet, Mode, or Vector, ' 'not {0}'.format(type(modes))) if modes.numAtoms() != atoms.numAtoms(): raise Exception('number of atoms do not match') out = openFile(addext(filename, '.nmd'), 'w') #out.write('#!{0} -e\n'.format(VMDPATH)) out.write('nmwiz_load {0}\n'.format(abspath(filename))) name = modes.getTitle() name = name.replace(' ', '_').replace('.', '_') if not name.replace('_', '').isalnum() or len(name) > 30: name = str(atoms) name = name.replace(' ', '_').replace('.', '_') if not name.replace('_', '').isalnum() or len(name) > 30: name = splitext(split(filename)[1])[0] out.write('name {0}\n'.format(name)) try: coords = atoms.getCoords() except: raise ValueError('coordinates could not be retrieved from atoms') if coords is None: raise ValueError('atom coordinates are not set') try: data = atoms.getNames() if data is not None: out.write('atomnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getResnames() if data is not None: out.write('resnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getResnums() if data is not None: out.write('resids ') data.tofile(out, ' ') out.write('\n') except: pass try: data = atoms.getChids() if data is not None: out.write('chainids {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getSegnames() if data is not None: out.write('segnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getBetas() if data is not None: out.write('bfactors ') data.tofile(out, ' ', '%.2f') out.write('\n') except: pass format = '{0:.3f}'.format out.write('coordinates ') coords.tofile(out, ' ', '%.3f') out.write('\n') count = 0 if isinstance(modes, Vector): out.write('mode 1 {0:.2f} '.format(abs(modes))) modes.getNormed()._getArray().tofile(out, ' ', '%.3f') out.write('\n') count += 1 else: if isinstance(modes, Mode): modes = [modes] for mode in modes: if mode.getEigval() < ZERO: continue out.write('mode {0} {1:.2f} '.format(mode.getIndex() + 1, mode.getVariance()**0.5)) arr = mode._getArray().tofile(out, ' ', '%.3f') out.write('\n') count += 1 if count == 0: LOGGER.warning('No normal mode data was written. ' 'Given modes might have 0 eigenvalues.') out.close() return filename
def writeVMDstiffness(model, pdb, indices, k_range, filename='vmd_out', \ selstr='protein and name CA', loadToVMD=True): """Returns three *filename* files: (1) PDB file with coordinates. (2) TCL file containing vmd commands for loading PDB file with accurate vmd representation. Pair of residues with selected *k_range* of effective spring constant are shown in VMD respresentation with solid line between them. If more than one residue will be selected in *indices*, different pair for each residue will be colored in the different colors. (3) TXT file contains pair of residues with effective spring constant in selected range *k_range*. The effective spring constant calculation using ``buildSM`` method from :class:`.ANM`. .. note:: #. This function skips modes with zero eigenvalues. #. If a :class:`.Vector` instance is given, it will be normalized before it is written. It's length before normalization will be written as the scaling factor of the vector. :arg model: this is an 3-dimensional NMA instance from a :class:`.ANM calculations :type model: :class:`.ANM` :arg pdb: a coordinate set or an object with ``getCoords`` method :type pdb: :class:`numpy.ndarray`. :arg indices: amino acid number. :type indices: ``[int, int]`` or ``[int]`` for one amino acid :arg k_range: effective force constant value. :type k_range: int or float, ``[int, int]`` By default files are saved as *filename* and loaded to VMD program and *selstr* is a selection from :class:`.Select` """ try: coords_sel = pdb.select(selstr) resnum_list = coords_sel.getResnums() coords = (coords_sel._getCoords() if hasattr(coords_sel, '_getCoords') else coords_sel.getCoords()) except AttributeError: try: checkCoords(coords_sel) except TypeError: raise TypeError('pdb must be a Numpy array or an object ' 'with `getCoords` method') if not isinstance(model, NMA): raise TypeError('model must be an NMA instance') elif not model.is3d(): raise TypeError('model must be a 3-dimensional NMA instance') elif len(model) == 0: raise ValueError('model must have normal modes calculated') elif model.getStiffness() is None: raise ValueError('model must have stiffness matrix calculated') elif len(indices) == 0: raise ValueError('indices cannot be an empty array') if len(indices) == 1: indices0 = indices[0] - resnum_list[0] indices1 = indices[0] - resnum_list[0] elif len(indices) == 2: indices0 = indices[0] - resnum_list[0] indices1 = indices[1] - resnum_list[0] out = openFile(addext(filename, '.tcl'), 'w') out_txt = openFile(addext(filename, '.txt'), 'w') writePDB(filename + '.pdb', pdb) LOGGER.info('Creating VMD file.') out.write('display rendermode GLSL \n') out.write('display projection orthographic\n') out.write('color Display Background white\n') out.write('display shadows on\n') out.write('display depthcue off\n') out.write('axes location off\n') out.write('stage location off\n') out.write('light 0 on\n') out.write('light 1 on\n') out.write('light 2 off\n') out.write('light 3 on\n') out.write('mol addrep 0\n') out.write('display resetview\n') out.write('mol new {./' + str(filename) + '.pdb} type {pdb} first 0 last -1 step 1 waitfor 1\n') out.write('mol modselect 0 0 protein\n') out.write('mol modstyle 0 0 NewCartoon 0.300000 10.000000 4.100000 0\n') out.write('mol modcolor 0 0 Structure\n') out.write('mol color Structure\n') out.write('mol representation NewCartoon 0.300000 10.000000 4.100000 0\n') out.write('mol selection protein\n') out.write('mol material Opaque\n') colors = ['blue', 'red', 'gray', 'orange','yellow', 'tan','silver', 'green', \ 'white', 'pink', 'cyan', 'purple', 'lime', 'mauve', 'ochre', 'iceblue', 'black', \ 'yellow2','yellow3','green2','green3','cyan2','cyan3','blue2','blue3','violet', \ 'violet2','magenta','magenta2','red2','red3','orange2','orange3']*50 color_nr = 1 # starting from red color in VMD ResCounter = [] for r in xrange(indices0, indices1 + 1): baza_col = [] # Value of Kij is here for each residue nr_baza_col = [] # Resid of aa are here out.write("draw color " + str(colors[color_nr]) + "\n") for nr_i, i in enumerate(model.getStiffness()[r]): if k_range[0] < float(i) < k_range[1]: baza_col.append(i) nr_baza_col.append(nr_i + resnum_list[0]) resid_r = str( coords_sel.getResnames()[r]) + str(r + resnum_list[0]) resid_r2 = str( coords_sel.getResnames()[nr_i]) + str(nr_i + resnum_list[0]) if len( baza_col ) == 0: # if base is empty then it will not change the color color_nr = 0 else: out.write("draw line "+'{'+str(coords[r])[1:-1]+'} {'+\ str(coords[nr_i])[1:-1]+'} width 3 style solid \n') out_txt.write( str(resid_r) + '\t' + resid_r2 + '\t' + str(i) + '\n') ResCounter.append(len(baza_col)) else: pass if len(baza_col) != 0: out.write('mol addrep 0\n') out.write('mol modselect '+str(color_nr+1)+' 0 protein and name CA and resid '+ \ str(r+resnum_list[0])+' '+str(nr_baza_col)[1:-1].replace(',','')+'\n') out.write('mol modcolor ' + str(color_nr + 1) + ' 0 ColorID ' + str(color_nr) + '\n') out.write('mol modstyle ' + str(color_nr + 1) + ' 0 VDW 0.600000 12.000000\n') out.write('mol color ColorID ' + str(color_nr) + '\n') out.write('mol representation VDW 1.000000 12.000000 \n') out.write('mol selection protein and name CA and resid '+ \ str(r+resnum_list[0])+' '+str(nr_baza_col)[1:-1].replace(',','')+'\n') out.write('mol material Opaque \n') color_nr = color_nr + 1 out.write('mol addrep 0\n') out.close() out_txt.close() if (loadToVMD == True): from prody import pathVMD LOGGER.info('File will be loaded to VMD program.') os.system(pathVMD() + " -e " + str(filename) + ".tcl") if len(ResCounter) > 0: return out elif len(ResCounter) == 0: LOGGER.info('There is no residue pair in this Kij range.') return 'None'
def writeVMDstiffness(model, pdb, indices, k_range, filename='vmd_out', \ selstr='protein and name CA', loadToVMD=True): """Returns three *filename* files: (1) PDB file with coordinates. (2) TCL file containing vmd commands for loading PDB file with accurate vmd representation. Pair of residues with selected *k_range* of effective spring constant are shown in VMD respresentation with solid line between them. If more than one residue will be selected in *indices*, different pair for each residue will be colored in the different colors. (3) TXT file contains pair of residues with effective spring constant in selected range *k_range*. The effective spring constant calculation using ``buildSM`` method from :class:`.ANM`. .. note:: #. This function skips modes with zero eigenvalues. #. If a :class:`.Vector` instance is given, it will be normalized before it is written. It's length before normalization will be written as the scaling factor of the vector. :arg model: this is an 3-dimensional NMA instance from a :class:`.ANM calculations :type model: :class:`.ANM` :arg pdb: a coordinate set or an object with ``getCoords`` method :type pdb: :class:`numpy.ndarray`. :arg indices: amino acid number. :type indices: ``[int, int]`` or ``[int]`` for one amino acid :arg k_range: effective force constant value. :type k_range: int or float, ``[int, int]`` By default files are saved as *filename* and loaded to VMD program and *selstr* is a selection from :class:`.Select` """ try: coords_sel = pdb.select(selstr) resnum_list = coords_sel.getResnums() coords = (coords_sel._getCoords() if hasattr(coords_sel, '_getCoords') else coords_sel.getCoords()) except AttributeError: try: checkCoords(coords_sel) except TypeError: raise TypeError('pdb must be a Numpy array or an object ' 'with `getCoords` method') if not isinstance(model, NMA): raise TypeError('model must be an NMA instance') elif not model.is3d(): raise TypeError('model must be a 3-dimensional NMA instance') elif len(model) == 0: raise ValueError('model must have normal modes calculated') elif model.getStiffness() is None: raise ValueError('model must have stiffness matrix calculated') elif len(indices)==0: raise ValueError('indices cannot be an empty array') if len(indices)==1: indices0=indices[0]-resnum_list[0] indices1=indices[0]-resnum_list[0] elif len(indices)==2: indices0=indices[0]-resnum_list[0] indices1=indices[1]-resnum_list[0] out = openFile(addext(filename, '.tcl'), 'w') out_txt = openFile(addext(filename,'.txt'), 'w') writePDB(filename + '.pdb', pdb) LOGGER.info('Creating VMD file.') out.write('display rendermode GLSL \n') out.write('display projection orthographic\n') out.write('color Display Background white\n') out.write('display shadows on\n') out.write('display depthcue off\n') out.write('axes location off\n') out.write('stage location off\n') out.write('light 0 on\n') out.write('light 1 on\n') out.write('light 2 off\n') out.write('light 3 on\n') out.write('mol addrep 0\n') out.write('display resetview\n') out.write('mol new {./'+str(filename)+'.pdb} type {pdb} first 0 last -1 step 1 waitfor 1\n') out.write('mol modselect 0 0 protein\n') out.write('mol modstyle 0 0 NewCartoon 0.300000 10.000000 4.100000 0\n') out.write('mol modcolor 0 0 Structure\n') out.write('mol color Structure\n') out.write('mol representation NewCartoon 0.300000 10.000000 4.100000 0\n') out.write('mol selection protein\n') out.write('mol material Opaque\n') colors = ['blue', 'red', 'gray', 'orange','yellow', 'tan','silver', 'green', \ 'white', 'pink', 'cyan', 'purple', 'lime', 'mauve', 'ochre', 'iceblue', 'black', \ 'yellow2','yellow3','green2','green3','cyan2','cyan3','blue2','blue3','violet', \ 'violet2','magenta','magenta2','red2','red3','orange2','orange3']*50 color_nr = 1 # starting from red color in VMD ResCounter = [] for r in xrange(indices0, indices1+1): baza_col = [] # Value of Kij is here for each residue nr_baza_col = [] # Resid of aa are here out.write("draw color "+str(colors[color_nr])+"\n") for nr_i, i in enumerate(model.getStiffness()[r]): if k_range[0] < float(i) < k_range[1]: baza_col.append(i) nr_baza_col.append(nr_i+resnum_list[0]) resid_r = str(coords_sel.getResnames()[r])+str(r+resnum_list[0]) resid_r2 = str(coords_sel.getResnames()[nr_i])+str(nr_i+resnum_list[0]) if len(baza_col) == 0: # if base is empty then it will not change the color color_nr = 0 else: out.write("draw line "+'{'+str(coords[r])[1:-1]+'} {'+\ str(coords[nr_i])[1:-1]+'} width 3 style solid \n') out_txt.write(str(resid_r)+'\t'+resid_r2+'\t'+str(i)+'\n') ResCounter.append(len(baza_col)) else: pass if len(baza_col) != 0: out.write('mol addrep 0\n') out.write('mol modselect '+str(color_nr+1)+' 0 protein and name CA and resid '+ \ str(r+resnum_list[0])+' '+str(nr_baza_col)[1:-1].replace(',','')+'\n') out.write('mol modcolor '+str(color_nr+1)+' 0 ColorID '+str(color_nr)+'\n') out.write('mol modstyle '+str(color_nr+1)+' 0 VDW 0.600000 12.000000\n') out.write('mol color ColorID '+str(color_nr)+'\n') out.write('mol representation VDW 1.000000 12.000000 \n') out.write('mol selection protein and name CA and resid '+ \ str(r+resnum_list[0])+' '+str(nr_baza_col)[1:-1].replace(',','')+'\n') out.write('mol material Opaque \n') color_nr = color_nr + 1 out.write('mol addrep 0\n') out.close() out_txt.close() if (loadToVMD == True): from prody import pathVMD LOGGER.info('File will be loaded to VMD program.') os.system(pathVMD()+" -e "+str(filename)+".tcl") if len(ResCounter) > 0: return out elif len(ResCounter) == 0: LOGGER.info('There is no residue pair in this Kij range.') return 'None'
def writeHeatmap(filename, heatmap, **kwargs): """Returns *filename* that contains *heatmap* in Heat Mapper :file:`.hm` file (extension is automatically added when not found). *filename* may also be an output stream. :arg title: title of the heatmap :type title: str :arg xlabel: x-axis lab, default is ``'unknown'`` :type xlabel: str :arg ylabel: y-axis lab, default is ``'unknown'`` :type ylabel: str :arg xorigin: x-axis origin, default is 0 :type xorigin: float :arg xstep: x-axis step, default is 1 :type xstep: float :arg min: minimum value, default is minimum in *heatmap* :type min: float :arg max: maximum value, default is maximum in *heatmap* :type max: float :arg format: number format, default is ``'%f'`` :type format: str Other keyword arguments that are arrays with length equal to the y-axis (second dimension of heatmap) will be considered as *numbering*.""" try: ndim, shape = heatmap.ndim, heatmap.shape except: raise TypeError('heatmap must be an array object') if ndim!= 2: raise TypeError('heatmap must be a 2D array') try: write, close, stream = filename.write, lambda: None, filename except AttributeError: out = openFile(addext(filename, '.hm'), 'wb') write, close, stream = out.write, out.close, out format = kwargs.pop('format', '%f') write('-min "{0}"\n'.format(kwargs.pop('min', heatmap.min()))) write('-max "{0}"\n'.format(kwargs.pop('max', heatmap.max()))) for label, default in [ ('title', 'unknown'), ('xlabel', 'unknown'), ('xorigin', 0), ('xstep', 1), ('ylabel', 'unknown'), ]: write('-{0} "{1}"\n'.format(label, kwargs.pop(label, default))) numbering = [] numlabels = [] for key, val in kwargs.items(): try: length = len(val) except TypeError: LOGGER.warn('Keyword argument {0} is not used.'.format(key)) else: if length == shape[0]: numlabels.append(key) numbering.append(val) if not numbering: numlabels.append('unknown') numbering.append(arange(1, shape[0] + 1)) write('-numbering "{0}"\n'.format(':'.join(numlabels))) for i, row in enumerate(heatmap): write(':'.join(str(nums[i]) for nums in numbering) + ':') row.tofile(stream, sep=';', format=format) write(';\n') close() return filename
def writeNMD(filename, modes, atoms): """Return *filename* that contains *modes* and *atoms* data in NMD format described in :ref:`nmd-format`. :file:`.nmd` extension is appended to filename, if it does not have an extension. .. note:: #. This function skips modes with zero eigenvalues. #. If a :class:`.Vector` instance is given, it will be normalized before it is written. It's length before normalization will be written as the scaling factor of the vector.""" if not isinstance(modes, (NMA, ModeSet, Mode, Vector)): raise TypeError('modes must be NMA, ModeSet, Mode, or Vector, ' 'not {0}'.format(type(modes))) if modes.numAtoms() != atoms.numAtoms(): raise Exception('number of atoms do not match') out = openFile(addext(filename, '.nmd'), 'w') #out.write('#!{0} -e\n'.format(VMDPATH)) out.write('nmwiz_load {0}\n'.format(abspath(filename))) name = modes.getTitle() name = name.replace(' ', '_').replace('.', '_') if not name.replace('_', '').isalnum() or len(name) > 30: name = str(atoms) name = name.replace(' ', '_').replace('.', '_') if not name.replace('_', '').isalnum() or len(name) > 30: name = splitext(split(filename)[1])[0] out.write('name {0}\n'.format(name)) try: coords = atoms.getCoords() except: raise ValueError('coordinates could not be retrieved from atoms') if coords is None: raise ValueError('atom coordinates are not set') try: data = atoms.getNames() if data is not None: out.write('atomnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getResnames() if data is not None: out.write('resnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getResnums() if data is not None: out.write('resids ') data.tofile(out, ' ') out.write('\n') except: pass try: data = atoms.getChids() if data is not None: out.write('chainids {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getSegnames() if data is not None: out.write('segnames {0}\n'.format(' '.join(data))) except: pass try: data = atoms.getBetas() if data is not None: out.write('bfactors ') data.tofile(out, ' ', '%.2f') out.write('\n') except: pass format = '{0:.3f}'.format out.write('coordinates ') coords.tofile(out, ' ', '%.3f') out.write('\n') count = 0 if isinstance(modes, Vector): out.write('mode 1 {0:.2f} '.format(abs(modes))) modes.getNormed()._getArray().tofile(out, ' ', '%.3f') out.write('\n') count += 1 else: if isinstance(modes, Mode): modes = [modes] for mode in modes: if mode.getEigval() < ZERO: continue out.write('mode {0} {1:.2f} '.format( mode.getIndex()+1, mode.getVariance()**0.5)) arr = mode._getArray().tofile(out, ' ', '%.3f') out.write('\n') count += 1 if count == 0: LOGGER.warning('No normal mode data was written. ' 'Given modes might have 0 eigenvalues.') out.close() return filename