Package Biskit :: Module ChainWriter
[hide private]
[frames] | no frames]

Source Code for Module Biskit.ChainWriter

  1  ## ChainWriter: 
  2  ## 
  3  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  4  ## Copyright (C) 2004-2006 Raik Gruenberg & Johan Leckner 
  5  ## 
  6  ## This program is free software; you can redistribute it and/or 
  7  ## modify it under the terms of the GNU General Public License as 
  8  ## published by the Free Software Foundation; either version 2 of the 
  9  ## License, or any later version. 
 10  ## 
 11  ## This program is distributed in the hope that it will be useful, 
 12  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 14  ## General Public License for more details. 
 15  ## 
 16  ## You find a copy of the GNU General Public License in the file 
 17  ## license.txt along with this program; if not, write to the Free 
 18  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 19  ## 
 20  ## $Version: $ 
 21  ## last $Date: 2007/03/05 10:28:21 $ 
 22  ## last $Author: graik $ 
 23   
 24  """ 
 25  Write cleaned peptide_chains as PDB for import into XPlor 
 26   
 27  This is vintage code. See L{Biskit.PDBCleaner} for a more recent 
 28  version. The writing of single chains could easily be done with PDBModel. 
 29   
 30  @todo: re-implement pdb2xplor with PDBCleaner / PDBModel 
 31  """ 
 32  from Scientific.IO.PDB import * 
 33  import commands 
 34  import os.path 
 35   
 36  import tools as T 
 37   
38 -class ChainWriter:
39 """ 40 Take chain from chainCleaner; write single PDB with 41 unique segementID and no chainID 42 """ 43
44 - def __init__(self, path):
45 """ 46 Take chains from ChainCleaner and write pdb files. 47 File names are created from segid of each chain + '_seg.pdb' 48 49 @param path: output path for PDB files 50 @type path: string 51 """ 52 self.path = T.absfile( path )
53 54
55 - def _startPDB(self, chain, fname):
56 """ 57 Create pdb file and write header. 58 59 @param chain: Scientific.IO.PDB.PeptideChain object 60 @type chain: chain object 61 @param fname: file name 62 @type fname: string 63 64 @return: handle of open file 65 @rtype: PDBFile 66 """ 67 f = PDBFile(fname, 'w') 68 try: 69 for c in chain.comments: # write comments, if any 70 f.writeComment(c) 71 except: 72 pass 73 return f
74 75
76 - def removeTER(self, fname):
77 """ 78 Remove TER record from PDB. 79 80 @param fname: name of existing file. 81 @type fname: string 82 """ 83 try: 84 #command = 'egrep -v "^TER " ' + fname + '> temp.pdb' 85 path = os.path.dirname( fname ) 86 command = 'egrep -v "^TER " %s > %s/temp.pdb'%( fname, path ) 87 commands.getstatusoutput(command) 88 os.rename('%s/temp.pdb'%path, fname) 89 except (OSError): 90 T.errWriteln("Error removing 'TER' statement from %s: ") 91 T.errWriteln( T.lastError() )
92 93
94 - def writeChain(self, chain):
95 """ 96 Write single chain as PDB. File name will be segid + '_seg.pdb'. 97 98 @param chain: Scientific.IO.PDB.PeptideChain object 99 @type chain: chain object 100 """ 101 if (chain <> None): 102 try: 103 104 fname = self.path + '/' + chain.segment_id + "_seg.PDB" 105 file = self._startPDB(chain, fname) # include comments 106 chain.writeToFile(file) # create PDB 107 file.close() # make sure file is complete! 108 self.removeTER(file.file.file.name) #remove TER record from PDB 109 return 1 # write a chain 110 111 except (IOError): 112 T.errWriteln("Error writing chain to file %s:" % fname) 113 T.errWriteln( T.lastError() ) 114 115 return 0 # false, no more chains to write
116 117 118 ############# 119 ## TESTING 120 ############# 121 import Biskit.test as BT 122
123 -class Test(BT.BiskitTest):
124 """Test ChainWriter""" 125
126 - def prepare(self):
127 self.fname = T.testRoot() + '/rec/1A2P_rec_original.pdb' 128 self.outPath = T.tempDir()
129
130 - def test_ChainWriter( self ):
131 """ChainWriter test""" 132 133 from ChainCleaner import ChainCleaner 134 from ChainSeparator import ChainSeparator 135 136 self.cleaner = ChainCleaner( ChainSeparator( self.fname, 137 self.outPath ) ) 138 139 self.writer = ChainWriter( self.outPath ) 140 141 all_msg = [] 142 for i in range(3): 143 msg = self.writer.writeChain( self.cleaner.next() ) 144 if self.local: 145 print 'Writing separated, cleaned chain to disk...%i'%msg 146 all_msg += [ msg ] 147 148 self.assertEquals( all_msg, [1, 0, 0] )
149
150 - def cleanUp(self):
151 T.tryRemove( self.outPath + '/1A2P_waters.pdb' ) 152 T.tryRemove( self.outPath + '/1A2A_seg.PDB' )
153 154 155 156 if __name__ == '__main__': 157 158 BT.localTest() 159