def GenerateContactMap(TimeStep,protocol):
    """Read a frame and compute the contactMap between two groups of atom given a ContactMapProtocol
    NOTE: if protocol.byres=True, then we report the number of atomic contacts for a given
          residue pair in contact
    Numbering of atoms and residues is the one given by the PSF file"""
    rowNS = AtomNeighborSearch(protocol.rowGroup) #KDE-tree object for rowGroup
    colNS = AtomNeighborSearch(protocol.colGroup) #KDE-tree object for colGroup
    rowClose = rowNS.search_list(protocol.colGroup,protocol.cutOff) #atoms of rowGroup in contact with colGroup
    if not rowClose: #no contacts
        shape=(TimeStep.numatoms,TimeStep.numatoms) #all atoms in the system
        if protocol.byres: shape=(protocol.numberOfResidues,protocol.numberOfResidues)
        csr=csr_matrix( ([],([],[])), shape, dtype='int32')  #empty map
        cma=CMA.csr_ContactMap(csr)
        cma.setDistanceCutOff(protocol.cutOff)
        return cma
    colClose = colNS.search_list(rowClose,protocol.cutOff) #atoms of colGroup in contact with rowClose/rowGroup
    dd=distance_array(rowClose.coordinates(),colClose.coordinates(),TimeStep.dimensions[0:3])
    (rowIndices,colIndices) = numpy.where(dd<protocol.cutOff)
    rowIndices=rowClose.indices()[rowIndices]
    colIndices=colClose.indices()[colIndices]  
    if protocol.byres:
        #switch from atomic indices to residue numbers
        rowIndices=protocol.atom2res[rowIndices]
        colIndices=protocol.atom2res[colIndices]
        shape=(protocol.numberOfResidues,protocol.numberOfResidues)
    else:
        #Take into account if the first atomic index was zero
        rowIndices += protocol.shift
        colIndices += protocol.shift
        shape=(TimeStep.numatoms,TimeStep.numatoms) #all atoms in the system
    #create sparse matrix for the contact map. 
    data=numpy.ones( len(rowIndices) ) #just signaling a contact map
    csr=csr_matrix( (data, (rowIndices,colIndices)), shape, dtype='int32')
    if protocol.byres: csr.data[:]=1 #overwrite number of atomic contacts per residue pair with 1 only
    #trace()
    if protocol.filter: csr=protocol.filter(csr) #Filtering
    cma=CMA.csr_ContactMap(csr)
    cma.setDistanceCutOff(protocol.cutOff)
    return cma
Ejemplo n.º 2
0
import sys
sys.path.append('/home/jbq/repositories/ContactMapAnalysis')
import ContactMapAnalysis as CMA
import argparse
from pdb import set_trace as tr
        
if __name__=='__main__':
    parser = argparse.ArgumentParser(description='save all residence times as a single column file')
    parser.add_argument('hdf5',help='HDF5 file containing the residence times')
    parser.add_argument('--outfile',help='save to file. Otherwise print to standard output"')
    args = parser.parse_args()

    rtl = CMA.loadResidenceTimesListFromFile(args.hdf5)
    buf=''
    for ires in range(1,rtl.n):
        if rtl.data[ires]:
            L=rtl.data[ires]  #list of isopropanol residence times to this particular residue
            buf+='\n'.join([str(x) for x in L])+'\n'
    if args.outfile:
        open(args.outfile,'w').write(buf)
    else:
        print buf