Example #1
0
def histogram(data, nbins, range=None):
    """
    Create a histogram.
    Comes from Konrad Hinsen: Scientific Python

    @param data: data list or array
    @type  data: [any]
    @param nbins: number of bins
    @type  nbins: int
    @param range: data range to create histogram from (min val, max val)
    @type  range: (float, float) OR None

    @return: array (2 x len(data) ) with start of bin and witdh of bin. 
    @rtype: array
    """
    data = N0.array(data, N0.Float)
    if range is None:
        min = N0.minimum.reduce(data)
        max = N0.maximum.reduce(data)
    else:
        min, max = range
        data = N0.repeat(
            data,
            N0.logical_and(N0.less_equal(data, max),
                           N0.greater_equal(data, min)))
    bin_width = (max - min) / nbins
    data = N0.floor((data - min) / bin_width).astype(N0.Int)
    histo = N0.add.reduce(N0.equal(N0.arange(nbins)[:, N0.NewAxis], data), -1)
    histo[-1] = histo[-1] + N0.add.reduce(N0.equal(nbins, data))
    bins = min + bin_width * (N0.arange(nbins) + 0.5)
    return N0.transpose(N0.array([bins, histo]))
Example #2
0
    def fractionNativeSurface(self, cont, contRef):
        """
        fraction of atoms/residues that are involved in B{any} contacts
        in both complexes.

        @param cont: contact matrix
        @type  cont: matrix
        @param contRef: reference contact matrix
        @type  contRef: matrix
        
        @return: (fractRec, fractLig), fraction of atoms/residues that
                  are involved in any contacts in both complexes
        @rtype: (float, float)
           
        """
        lig, ligRef = N0.clip(N0.sum(cont), 0,
                              1), N0.clip(N0.sum(contRef), 0, 1)
        rec = N0.clip(N0.sum(cont, 1), 0, 1)
        recRef = N0.clip(N0.sum(contRef, 1), 0, 1)

        fLig = N0.sum(N0.logical_and(lig, ligRef)) * 1. / N0.sum(ligRef)
        fRec = N0.sum(N0.logical_and(rec, recRef)) * 1. / N0.sum(recRef)

        return (fRec, fLig)
Example #3
0
 def contactsShared(self, reference, cutoff=None):
     """
     Number of equal B{residue-residue} contacts in this and
     reference complex.
     
     @param reference: reference complex
     @type  reference: Complex
     @param cutoff: cutoff for atom-atom contact to be counted
     @type  cutoff: float
     @return: the number or residue-residue contacts that are common to
              both this and reference::
                abs( N0.sum( N0.sum( contactMatrix_a - contactMatrix_b )))
     @rtype: int
     """
     equality = N0.logical_and(self.resContacts(cutoff=cutoff),
                               reference.resContacts(cutoff=cutoff))
     return abs(N0.sum(N0.sum(equality)))
Example #4
0
    def contactsOverlap(self, ref, cutoff=None):
        """
        Fraction of overlapping B{residue-residue} contacts between this and
        reference complex.
        
        @param ref: reference complex
        @type  ref: Complex
        @param cutoff: maximal atom-atom distance, None .. previous setting
        @type  cutoff: float
        
        @return: fraction of contacts shared between this and ref
                 (normalized to number of all contacts)
        @rtype: float
        """
        equal = N0.logical_and(self.resContacts(cutoff=cutoff),
                               ref.resContacts(cutoff=cutoff))
        total = N0.logical_or(self.resContacts(cutoff),
                              ref.resContacts(cutoff))

        return N0.sum(N0.sum(equal)) * 1.0 / N0.sum(N0.sum(total))
Example #5
0
def match(x, y, n_iterations=1, z=2, eps_rmsd=0.5, eps_stdv=0.05):
    """
    Matches two arrays onto each other, while iteratively removing outliers.
    Superimposed array y would be C{ N0.dot(y, N0.transpose(r)) + t }.

    @param n_iterations: number of calculations::
                           1 .. no iteration 
                           0 .. until convergence
    @type  n_iterations: 1|0
    @param z: number of standard deviations for outlier definition (default: 2)
    @type  z: float
    @param eps_rmsd: tolerance in rmsd (default: 0.5)
    @type  eps_rmsd: float
    @param eps_stdv: tolerance in standard deviations (default: 0.05)
    @type  eps_stdv: float

    @return: (r,t), [ [percent_considered, rmsd_for_it, outliers] ]
    @rtype: (array, array), [float, float, int]
    """
    iter_trace = []

    rmsd_old = 0
    stdv_old = 0

    n = 0
    converged = 0

    mask = N0.ones(len(y), N0.Int32)

    while not converged:

        ## find transformation for best match
        r, t = findTransformation(N0.compress(mask, x, 0),
                                  N0.compress(mask, y, 0))

        ## transform coordinates
        xt = N0.dot(y, N0.transpose(r)) + t

        ## calculate row distances
        d = N0.sqrt(N0.sum(N0.power(x - xt, 2), 1)) * mask

        ## calculate rmsd and stdv
        rmsd = N0.sqrt(N0.average(N0.compress(mask, d)**2))
        stdv = MU.SD(N0.compress(mask, d))

        ## check conditions for convergence
        d_rmsd = abs(rmsd - rmsd_old)
        d_stdv = abs(1 - stdv_old / stdv)

        if d_rmsd < eps_rmsd and d_stdv < eps_stdv:
            converged = 1
        else:
            rmsd_old = rmsd
            stdv_old = stdv

        ## store result
        perc = round(float(N0.sum(mask)) / float(len(mask)), 2)

        ## throw out non-matching rows
        mask = N0.logical_and(mask, N0.less(d, rmsd + z * stdv))
        outliers = N0.nonzero(N0.logical_not(mask))
        iter_trace.append([perc, round(rmsd, 3), outliers])

        n += 1

        if n_iterations and n >= n_iterations:
            break

    return (r, t), iter_trace