def analyze(self, **kwargs): """Collect output xvg files as :class:`gromacs.formats.XVG` objects. :Returns: a dictionary of the results and also sets ``self.results``. """ from gromacs.formats import XVG logger.info("Preparing RMSF graphs as XVG objects.") results = AttributeDict(RMSF=XVG(self.parameters.filenames['RMSF']), RMSD=XVG(self.parameters.filenames['RMSD'])) self.results = results return results
def analyze(self, **kwargs): """Make data files available as numpy arrays.""" results = AttributeDict() for name, f in self.parameters.filenames.items(): results[name] = XVG(f) self.results = results return results
def analyze(self, **kwargs): """Analyze hydrogen bond output. * hydrogen bond existence (existence) * total number of hydrogen bonds (num) * (others can be added easily) :Returns: a dictionary of the results and also sets ``self.results``. """ from gromacs.formats import XPM, XVG results = AttributeDict() results['num'] = XVG(self.parameters.filenames['num']) results['matrix'] = hbm = XPM(self.parameters.filenames['hbm'], reverse=True) hb_fraction = hbm.array.mean(axis=0) desc = [ line.strip() for line in open(self.parameters.filenames['log']) if not line.startswith('#') ] results['existence'] = zip(desc, hb_fraction) with open(self.parameters.filenames['existence'], "w") as out: logger.info( "Hydrogen bond existence analysis (results['existence'] and %(existence)r)", self.parameters.filenames) for name, frac in results['existence']: logger.info("hb_existence: %-40s %4.1f%%", name, 100 * frac) out.write("{0:<40!s} {1:4.1f}%\n".format(name, 100 * frac)) self.results = results return results
def test_correl(self, correldata): xvg = XVG(array=correldata, names="t,y1,y2") # FIXME # force=True : TypeError: tcorrel() got an unexpected keyword argument 'force' xvg.set_correlparameters(nstep=None, ncorrel=25000) sigma = xvg.error tc = xvg.tc assert_equal(sigma.shape, (2, )) assert_equal(tc.shape, (2, ))
def analyze(self,**kwargs): """Collect output xvg files as :class:`gromacs.formats.XVG` objects. :Returns: a dictionary of the results and also sets ``self.results``. """ from gromacs.formats import XVG logger.info("Preparing HelixBundle graphs as XVG objects.") results = AttributeDict( (k, XVG(fn)) for k,fn in self.parameters.filenames.items() ) self.results = results return results
def store_xvg(self, name, a, **kwargs): """Store array *a* as :class:`~gromacs.formats.XVG` in result *name*. kwargs are passed to :class:`gromacs.formats.XVG`. This is a helper method that simplifies the task of storing results in the form of a numpy array as a data file on disk in the xmgrace format and also as a :class:`~gromacs.formats.XVG` instance in the :attr:`gromacs.analysis.core.Worker.results` dictionary. """ from gromacs.formats import XVG kwargs.pop('filename',None) # ignore filename filename = self.plugindir(name+'.xvg') xvg = XVG(**kwargs) xvg.set(a) xvg.write(filename) self.results[name] = xvg self.parameters.filenames[name] = filename return filename
def store_xvg(self, name, a, **kwargs): """Store array *a* as :class:`~gromacs.formats.XVG` in result *name*. kwargs are passed to :class:`gromacs.formats.XVG`. This is a helper method that simplifies the task of storing results in the form of a numpy array as a data file on disk in the xmgrace format and also as a :class:`~gromacs.formats.XVG` instance in the :attr:`gromacs.analysis.core.Worker.results` dictionary. """ from gromacs.formats import XVG kwargs.pop('filename', None) # ignore filename filename = self.plugindir(name + '.xvg') xvg = XVG(**kwargs) xvg.set(a) xvg.write(filename) self.results[name] = xvg self.parameters.filenames[name] = filename return filename
# Loop through all the density files in the main directory. In this case "b" stands for the OH-percentage of the SAM, # and "c" stands for the number of molecules (2 for 2000, 3 for 3000, etc) # We save each time the data of the density file in the variable "input" and # the radial coordinates in the variable "x" peakcount = 0 for b in SAMs: for c in Waters: print >> myfile, ' ' peakcount = systems[(b, c)] shift = peak[peakcount] print >> myfile2, '{0} {1} {2}'.format('#File:', b, c) for d in frange(start_time, end_time, 0.5): l = d + 0.5 l = str(l) d = str(d) xvg = XVG() filename = 'g_rad_dmap_%dpc_w%d_%sns_%sns.xvg'%(b,c,d,l, ) if not os.path.isfile(filename): print >> myfile, '{0} {1} {2}'.format(filename, 'nan', 'nan') print >> myfile2, '{0} {1}'.format('nan', 'nan') continue xvg.read(filename) input = xvg.array x = input[0] # Here we create the array "zcoord" with the z-coordinates of the slices subtracting the shift value. slicestotal = len(input) - 1 S = ZLENGTH/ slicestotal zcoord = np.zeros(len(input)-1) for i in range(1,len(input)): zcoord[i-1] = (S *(i-1))-shift
def test_decimate(self, correldata, method, maxpoints=100): xvg = XVG(array=correldata) data = xvg.array reduced = xvg.decimate(method, data, maxpoints=maxpoints) assert_equal(reduced.shape, (len(data), maxpoints))
def test_write_read(self, xvg, tmpdir): fname = "random.xvg" with tmpdir.as_cwd(): xvg.write(fname) newxvg = XVG(filename=fname) assert_almost_equal(newxvg.array, xvg.array)
def xvg(self, data): return XVG(array=data.copy(), names="t,a,b,c,d,e")
def analyze(self, **kwargs): """Load results from disk into :attr:`_Dihedrals.results` and compute PMF. The PMF W(phi) in kT is computed from each dihedral probability distribution P(phi) as W(phi) = -kT ln P(phi) It is stored in :attr:`_Dihedrals.results` with the key *PMF*. :Keywords: *bins* bins for histograms (passed to numpy.histogram(new=True)) :Returns: a dictionary of the results and also sets :attr:`_Dihedrals.results`. """ bins = kwargs.pop("bins", 361) results = AttributeDict() # get graphs that were produced by g_angle for name, f in self.parameters.filenames.items(): try: results[name] = XVG(f) except IOError: pass # either not computed (yet) or some failure # compute individual distributions ts = results["timeseries"].array # ts[0] = time, ts[1] = avg dih = ts[2:] phi_range = (-180.0, 180.0) Ndih = len(dih) p = Ndih * [None] # histograms (prob. distributions), one for each dihedral i for i in xrange(Ndih): phis = dih[i] p[i], e = numpy.histogram(phis, bins=bins, range=phi_range, normed=True, new=True) P = numpy.array(p) phi = 0.5 * (e[:-1] + e[1:]) # midpoints of bin edges distributions = numpy.concatenate((phi[numpy.newaxis, :], P)) # phi, P[0], P[1], ... xvg = XVG() xvg.set(distributions) xvg.write(self.parameters.filenames["distributions"]) results["distributions"] = xvg del xvg # compute PMF (from individual distributions) W = -numpy.log(P) # W(phi)/kT = -ln P W -= W.min(axis=1)[:, numpy.newaxis] # minimum at 0 kT pmf = numpy.concatenate((phi[numpy.newaxis, :], W), axis=0) xvg = XVG() xvg.set(pmf) xvg.write(self.parameters.filenames["PMF"]) results["PMF"] = xvg self.results = results return results
def analyze(self, **kwargs): """Load results from disk into :attr:`_Dihedrals.results` and compute PMF. The PMF W(phi) in kT is computed from each dihedral probability distribution P(phi) as W(phi) = -kT ln P(phi) It is stored in :attr:`_Dihedrals.results` with the key *PMF*. :Keywords: *bins* bins for histograms (passed to numpy.histogram(new=True)) :Returns: a dictionary of the results and also sets :attr:`_Dihedrals.results`. """ bins = kwargs.pop('bins', 361) results = AttributeDict() # get graphs that were produced by g_angle for name, f in self.parameters.filenames.items(): try: results[name] = XVG(f) except IOError: pass # either not computed (yet) or some failure # compute individual distributions ts = results['timeseries'].array # ts[0] = time, ts[1] = avg dih = ts[2:] phi_range = (-180., 180.) Ndih = len(dih) p = Ndih * [None] # histograms (prob. distributions), one for each dihedral i for i in xrange(Ndih): phis = dih[i] p[i],e = numpy.histogram(phis, bins=bins, range=phi_range, normed=True, new=True) P = numpy.array(p) phi = 0.5*(e[:-1]+e[1:]) # midpoints of bin edges distributions = numpy.concatenate((phi[numpy.newaxis, :], P)) # phi, P[0], P[1], ... xvg = XVG() xvg.set(distributions) xvg.write(self.parameters.filenames['distributions']) results['distributions'] = xvg del xvg # compute PMF (from individual distributions) W = -numpy.log(P) # W(phi)/kT = -ln P W -= W.min(axis=1)[:, numpy.newaxis] # minimum at 0 kT pmf = numpy.concatenate((phi[numpy.newaxis, :], W), axis=0) xvg = XVG() xvg.set(pmf) xvg.write(self.parameters.filenames['PMF']) results['PMF'] = xvg self.results = results return results
def analyze(self, **kwargs): """Collect output xvg files as :class:`gromacs.formats.XVG` objects. - Make COM as a function of time available as XVG files and objects. - Compute RMSD of the COM of each group (from average position, "rmsd"). - Compute distance whic encompasses 50% of observations ("median") - Compute drift of COM, i.e. length of the vector between initial and final position. Initial and final position are computed as averages over *nframesavg* frames ("drift"). RMSD, median, and drift are columns in an xvg file. The rows correspond to the groups in :attr:`gromacs.analysis.plugins.com.Worker.results.group_names`. :Keywords: *nframesavg* number of initial and final frames that are averaged in order to compute the drift of the COM of each group [5000] *refgroup* group name whose com is taken as the reference and subtracted from all other coms for the distance calculations. If supplied, additional result 'com_relative_*refgroup*' is created. :Returns: a dictionary of the results and also sets :attr:`gromacs.analysis.plugins.com.Worker.results`. """ from gromacs.formats import XVG logger.info("Preparing COM graphs as XVG objects.") self.results = AttributeDict( (k, XVG(fn)) for k, fn in self.parameters.filenames.items()) # compute RMSD of COM and shift of COM (drift) between avg pos # over first/last 5,000 frames nframesavg = kwargs.pop('nframesavg', 5000) ngroups = len(self.parameters.group_names) xcom = self.results['com'].array refgroup = kwargs.pop('refgroup', None) if refgroup is not None: if not refgroup in self.parameters.group_names: errmsg = "refgroup={0!s} must be one of {1!r}".format( refgroup, self.parameters.group_names) logger.error(errmsg) raise ValueError(errmsg) nreference = 1 + 3 * self.parameters.group_names.index( refgroup) # 1-based !! reference_com = xcom[nreference:nreference + 3] xcom[1:] -= numpy.vstack(ngroups * [reference_com]) # can't use broadcast logger.debug("distances computed with refgroup %r", refgroup) self.store_xvg('com_relative_{0!s}'.format(refgroup), xcom, names=['time'] + self.parameters.group_names) def vlength(v): return numpy.sqrt(numpy.sum(v**2, axis=0)) # distances over time step logger.debug( "drift calculated between %d-frame averages at beginning and end", nframesavg) records = [] for i in xrange(1, 3 * ngroups + 1, 3): x = xcom[i:i + 3] r = vlength( x - x.mean(axis=1)[:, numpy.newaxis]) # distances over time step #r0 = vlength(r - r[:,0][:,numpy.newaxis]) # distances over time step from r(t=0) #h,edges = numpy.histogram(r, bins=kwargs.get('bins', 100), normed=True) #m = 0.5*(edges[1:]+edges[:-1]) #c = h.cumsum(dtype=float) # integral #c /= c[-1] # normalized (0 to 1) #median = m[c < 0.5][-1] #g = h/(4*numpy.pi*m**2) #import scipy.integrate #radint = lambda y: 4*numpy.pi*scipy.integrate.simps(m**2*y, x=m) #g /= radint(g) # properly normalized radial distribution function rmsd = numpy.sqrt(numpy.mean( r**2)) # radial spread sqrt(radint(m**2 * g)) median = numpy.median( r) # radius that contains 50% of the observations dx = x[:, :nframesavg].mean(axis=1) - x[:, -nframesavg:].mean(axis=1) drift = vlength(dx) records.append((rmsd, median, drift)) self.store_xvg('distance', numpy.transpose(records), names="rmsd,median,drift") return self.results