pKaTool/pKaIO.py
def read_matrix(self,filename=None):
        """
        # This subroutine read a MATRIX file
        """
        if not filename:
            if self.matrix_file:
                filename=self.matrix_file
            else:
                raise Exception('No matrix filename given')
        #
        import os, string
        if not os.path.isfile(filename):
            raise Exception("File not found: "+filename)
        fd=open(filename)
        lines=fd.readlines()
        fd.close()
        #
        # Initialise dictionary
        #
        self.matrix={}
        #
        # Read title lines
        #
        if string.lower(string.strip(lines[0]))==string.lower('WHAT IF Interaction Matrix File'):
            format='WHAT IF'
        elif string.lower(string.strip(lines[0]))==string.lower('pdb2pka Interaction Matrix File'):
            format='WHAT IF'
        elif lines[2].strip()=='#pKa initialization':
            return self.read_MEADfile(lines,mode='matrix')
        else:
            raise Exception('Unknown format')
        if not string.strip(lines[1])=='Format 1.0':
            raise Exception('Wrong format'+str(lines[1]))
        x=1
        done=None
        partners=None
        Nterm=0
        while not done:
            x=x+1
            #
            # Read first line for this partner residue
            #
            if format=='WHAT IF':
                term=None
                if string.strip(lines[x])=='TERMINAL GROUP:':
                    term=1
                    Nterm=abs(Nterm-1)
                    x=x+1
                nline=string.replace(lines[x],'(','')
                nline=string.replace(nline,')','')
                resid=pKaUT.getWI_resid2(lines[x])
                if term:
                    #
                    # Replace the residue name by the terminal name
                    #
                    resname=resid.split(':')[-1]
                    d={1:'NTERM',0:'CTERM'}
                    resid=resid.replace(resname,d[Nterm])
                #
                # Get the number of interaction partners
                #
                np=int(float(nline.split()[-1]))
                if not partners:
                    partners=np
                else:
                    if partners!=np:
                        raise Exception('Number of partners changes: %d' %np)
                self.matrix[resid]={}
                #
                # Now read all the interactions with the partners
                #
                Nterm_partner=0
                for count in range(partners):
                    x=x+1
                    term2=None
                    if string.strip(lines[x])=='TERMINAL GROUP:':
                        Nterm_partner=abs(Nterm_partner-1)
                        term2=1
                        x=x+1
                    nline=string.replace(lines[x],'(','')
                    nline=string.replace(nline,')','')
                    list=string.split(nline)
                    partid=pKaUT.getWI_resid2(lines[x])
                    if term2:
                        #
                        # Replace the residue name by the terminal name
                        #
                        resname=partid.split(':')[-1]
                        d={1:'NTERM',0:'CTERM'}
                        partid=partid.replace(resname,d[Nterm_partner])
                    #
                    # New name?
                    #
                    chacha=string.atof(string.strip(list[-1]))
                    x=x+1
                    i2=float(lines[x])
                    x=x+1
                    i3=float(lines[x])
                    x=x+1
                    i4=float(lines[x])
                    energies=[chacha,i2,i3,i4]
                    self.matrix[resid][partid]=energies
                    term2=None
            elif format=='pdb2pka':
                #
                # pseudo-pdb2pka format
                #
                list=lines[x].split()
                resid=string.strip(list[0])
                self.matrix[resid]={}
                partners=int(float(list[-1]))
                #
                # Now read all the interactions with the partners
                #
                for count in range(partners):
                    x=x+1
                    list=lines[x].split()
                    partid=list[0]
                    chacha=string.atof(string.strip(list[-1]))
                    x=x+1
                    i2=string.atof(lines[x])
                    x=x+1
                    i3=string.atof(lines[x])
                    x=x+1
                    i4=string.atof(lines[x])
                    energies=[chacha,i2,i3,i4]
                    self.matrix[resid][partid]=energies
            x=x+1
            if string.strip(lines[x+1])=='End of file':
                done=1
        return self.matrix

        # This subroutine read a MATRIX file
        
This subroutine read a MATRIX file
This subroutine read a MATRIX file
def read_matrix(self,filename=None):
        if not filename:
            if self.matrix_file:
                filename=self.matrix_file
            else:
                raise Exception('No matrix filename given')
        import os, string
        if not os.path.isfile(filename):
            raise Exception("File not found: "+filename)
        fd=open(filename)
        lines=fd.readlines()
        fd.close()
         Initialise dictionary
        self.matrix={}
         Read title lines
        if string.lower(string.strip(lines[0]))==string.lower('WHAT IF Interaction Matrix File'):
            format='WHAT IF'
        elif string.lower(string.strip(lines[0]))==string.lower('pdb2pka Interaction Matrix File'):
            format='WHAT IF'
        elif lines[2].strip()=='pKa initialization':
            return self.read_MEADfile(lines,mode='matrix')
        else:
            raise Exception('Unknown format')
        if not string.strip(lines[1])=='Format 1.0':
            raise Exception('Wrong format'+str(lines[1]))
        x=1
        done=None
        partners=None
        Nterm=0
        while not done:
            x=x+1
             Read first line for this partner residue
            if format=='WHAT IF':
                term=None
                if string.strip(lines[x])=='TERMINAL GROUP:':
                    term=1
                    Nterm=abs(Nterm-1)
                    x=x+1
                nline=string.replace(lines[x],'(','')
                nline=string.replace(nline,')','')
                resid=pKaUT.getWI_resid2(lines[x])
                if term:
                     Replace the residue name by the terminal name
                    resname=resid.split(':')[-1]
                    d={1:'NTERM',0:'CTERM'}
                    resid=resid.replace(resname,d[Nterm])
                 Get the number of interaction partners
                np=int(float(nline.split()[-1]))
                if not partners:
                    partners=np
                else:
                    if partners!=np:
                        raise Exception('Number of partners changes: %d' %np)
                self.matrix[resid]={}
                 Now read all the interactions with the partners
                Nterm_partner=0
                for count in range(partners):
                    x=x+1
                    term2=None
                    if string.strip(lines[x])=='TERMINAL GROUP:':
                        Nterm_partner=abs(Nterm_partner-1)
                        term2=1
                        x=x+1
                    nline=string.replace(lines[x],'(','')
                    nline=string.replace(nline,')','')
                    list=string.split(nline)
                    partid=pKaUT.getWI_resid2(lines[x])
                    if term2:
                         Replace the residue name by the terminal name
                        resname=partid.split(':')[-1]
                        d={1:'NTERM',0:'CTERM'}
                        partid=partid.replace(resname,d[Nterm_partner])
                     New name?
                    chacha=string.atof(string.strip(list[-1]))
                    x=x+1
                    i2=float(lines[x])
                    x=x+1
                    i3=float(lines[x])
                    x=x+1
                    i4=float(lines[x])
                    energies=[chacha,i2,i3,i4]
                    self.matrix[resid][partid]=energies
                    term2=None
            elif format=='pdb2pka':
                 pseudo-pdb2pka format
                list=lines[x].split()
                resid=string.strip(list[0])
                self.matrix[resid]={}
                partners=int(float(list[-1]))
                 Now read all the interactions with the partners
                for count in range(partners):
                    x=x+1
                    list=lines[x].split()
                    partid=list[0]
                    chacha=string.atof(string.strip(list[-1]))
                    x=x+1
                    i2=string.atof(lines[x])
                    x=x+1
                    i3=string.atof(lines[x])
                    x=x+1
                    i4=string.atof(lines[x])
                    energies=[chacha,i2,i3,i4]
                    self.matrix[resid][partid]=energies
            x=x+1
            if string.strip(lines[x+1])=='End of file':
                done=1
        return self.matrix
def read_matrix ( self , filename = None ) : if not filename : if self . matrix_file : filename = self . matrix_file else : raise Exception ( 'No matrix filename given' ) import os , string if not os . path . isfile ( filename ) : raise Exception ( "File not found: " + filename ) fd = open ( filename ) lines = fd . readlines ( ) fd . close ( ) self . matrix = { } if string . lower ( string . strip ( lines [ 0 ] ) ) == string . lower ( 'WHAT IF Interaction Matrix File' ) : format = 'WHAT IF' elif string . lower ( string . strip ( lines [ 0 ] ) ) == string . lower ( 'pdb2pka Interaction Matrix File' ) : format = 'WHAT IF' elif lines [ 2 ] . strip ( ) == '#pKa initialization' : return self . read_MEADfile ( lines , mode = 'matrix' ) else : raise Exception ( 'Unknown format' ) if not string . strip ( lines [ 1 ] ) == 'Format 1.0' : raise Exception ( 'Wrong format' + str ( lines [ 1 ] ) ) x = 1 done = None partners = None Nterm = 0 while not done : x = x + 1 if format == 'WHAT IF' : term = None if string . strip ( lines [ x ] ) == 'TERMINAL GROUP:' : term = 1 Nterm = abs ( Nterm - 1 ) x = x + 1 nline = string . replace ( lines [ x ] , '(' , '' ) nline = string . replace ( nline , ')' , '' ) resid = pKaUT . getWI_resid2 ( lines [ x ] ) if term : resname = resid . split ( ':' ) [ - 1 ] d = { 1 : 'NTERM' , 0 : 'CTERM' } resid = resid . replace ( resname , d [ Nterm ] ) np = int ( float ( nline . split ( ) [ - 1 ] ) ) if not partners : partners = np else : if partners != np : raise Exception ( 'Number of partners changes: %d' % np ) self . matrix [ resid ] = { } Nterm_partner = 0 for count in range ( partners ) : x = x + 1 term2 = None if string . strip ( lines [ x ] ) == 'TERMINAL GROUP:' : Nterm_partner = abs ( Nterm_partner - 1 ) term2 = 1 x = x + 1 nline = string . replace ( lines [ x ] , '(' , '' ) nline = string . replace ( nline , ')' , '' ) list = string . split ( nline ) partid = pKaUT . getWI_resid2 ( lines [ x ] ) if term2 : resname = partid . split ( ':' ) [ - 1 ] d = { 1 : 'NTERM' , 0 : 'CTERM' } partid = partid . replace ( resname , d [ Nterm_partner ] ) chacha = string . atof ( string . strip ( list [ - 1 ] ) ) x = x + 1 i2 = float ( lines [ x ] ) x = x + 1 i3 = float ( lines [ x ] ) x = x + 1 i4 = float ( lines [ x ] ) energies = [ chacha , i2 , i3 , i4 ] self . matrix [ resid ] [ partid ] = energies term2 = None elif format == 'pdb2pka' : list = lines [ x ] . split ( ) resid = string . strip ( list [ 0 ] ) self . matrix [ resid ] = { } partners = int ( float ( list [ - 1 ] ) ) for count in range ( partners ) : x = x + 1 list = lines [ x ] . split ( ) partid = list [ 0 ] chacha = string . atof ( string . strip ( list [ - 1 ] ) ) x = x + 1 i2 = string . atof ( lines [ x ] ) x = x + 1 i3 = string . atof ( lines [ x ] ) x = x + 1 i4 = string . atof ( lines [ x ] ) energies = [ chacha , i2 , i3 , i4 ] self . matrix [ resid ] [ partid ] = energies x = x + 1 if string . strip ( lines [ x + 1 ] ) == 'End of file' : done = 1 return self . matrix
This subroutine read a MATRIX file
This subroutine read a MATRIX file
"""
        # This subroutine read a MATRIX file
        """ # # # Initialise dictionary # # # Read title lines # # # Read first line for this partner residue # # # Replace the residue name by the terminal name # # # Get the number of interaction partners # # # Now read all the interactions with the partners # # # Replace the residue name by the terminal name # # # New name? # # # pseudo-pdb2pka format # # # Now read all the interactions with the partners #
self
filename