Package Biskit :: Package Mod :: Module modUtils
[hide private]
[frames] | no frames]

Source Code for Module Biskit.Mod.modUtils

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2005 Raik Gruenberg & Johan Leckner 
  4  ## 
  5  ## This program is free software; you can redistribute it and/or 
  6  ## modify it under the terms of the GNU General Public License as 
  7  ## published by the Free Software Foundation; either version 2 of the 
  8  ## License, or any later version. 
  9  ## 
 10  ## This program is distributed in the hope that it will be useful, 
 11  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 13  ## General Public License for more details. 
 14  ## 
 15  ## You find a copy of the GNU General Public License in the file 
 16  ## license.txt along with this program; if not, write to the Free 
 17  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 18  ## 
 19  ## 
 20  ## last $Author: leckner $ 
 21  ## last $Date: 2006/12/21 15:52:04 $ 
 22  ## $Revision: 2.7 $ 
 23  """ 
 24  utility funtions for Mod package 
 25  """ 
 26   
 27  import os.path 
 28  import types 
 29  import Biskit.molUtils as MU 
 30  import Biskit.tools as T 
 31   
32 -def parse_tabbed_file( fname ):
33 """ 34 Parse the chaim index file written by TemplateSearcher 35 L{TemplateSearcher.F_CHAIN_INDEX}. 36 37 @param fname: name of file to parse 38 @type fname: str 39 40 @return: key : value mapping 41 @rtype: {key:value} 42 """ 43 f = open( fname ) 44 45 result = {} 46 for l in f: 47 if not l[0] == '#': 48 49 try: 50 fname, chain_id = l.split() 51 52 if not os.path.exists(fname): 53 fname = '%s/%s'%(T.testRoot(), fname) 54 55 if not len(fname) == 0: 56 result[ fname ] = chain_id 57 58 except: 59 fname = l.strip() 60 61 if not os.path.exists(fname): 62 fname = '%s/%s'%(T.testRoot(), fname) 63 64 result[ fname ] = '' 65 66 f.close() 67 68 return result
69 70
71 -def format_fasta(seq, width=60):
72 """ 73 Transform a given sequence to fasta format 74 75 @param seq: sequence 76 @type seq: str 77 @param width: length of a line in characters (default: 60) 78 @type width: int 79 80 @return: string sequence in fasta format 81 @rtype: str 82 """ 83 fasta_sequence = "" 84 85 for i in xrange(0,len(seq),width): 86 fasta_sequence += seq[i:i+width] 87 88 if(i+width>=len(seq)): 89 pass 90 else: 91 fasta_sequence += "\n" 92 93 return fasta_sequence
94 95
96 -def verify_fasta( target ):
97 """ 98 Verify that a given file or string is in Fasta format. 99 The definition used for a fasta file here is that: 100 - first line starts with '>' 101 - the following sequence lines are not longer that 80 characters 102 - the characters has to belong to the standard amino acid codes 103 104 @param target: name of fasta file OR file contents as list of strings 105 @type target: str OR [str] 106 107 @return: conforms to the fsata format 108 @rtype: True/False 109 """ 110 if not type(target) == types.ListType: 111 if os.path.exists( target ): 112 f = open( target, 'r' ) 113 target = f.readlines() 114 115 if not target[0][0] == '>': 116 print 'Fasta format does not contain description line.' 117 return False 118 119 for i in range( 1, len(target) ): 120 if len( target[i] ) >= 80: 121 print 'Fasta sequence lines longer that 80 characters' 122 return False 123 124 for j in target[i]: 125 aa_codes = MU.aaDicStandard.values() + [ '\n' ] 126 if not j.upper() in aa_codes: 127 print 'Invalid amino acid code: %s'%j.upper() 128 return False 129 130 return True
131 132 133 134 135 ############# 136 ## TESTING 137 ############# 138
139 -class Test:
140 """ 141 Test class 142 """ 143
144 - def run( self, local=0 ):
145 """ 146 run function test 147 148 @param local: transfer local variables to global and perform 149 other tasks only when run locally 150 @type local: 1|0 151 152 @return: 1 153 @rtype: int 154 """ 155 vf = verify_fasta( T.testRoot() + '/Mod/project/target.fasta') 156 157 if local: 158 globals().update( locals() ) 159 160 return vf
161 162
163 - def expected_result( self ):
164 """ 165 Precalculated result to check for consistent performance. 166 167 @return: 1 168 @rtype: int 169 """ 170 return 1
171 172 173 if __name__ == '__main__': 174 175 test = Test() 176 177 assert test.run( local=1 ) == test.expected_result() 178