Package Bio :: Package Ais
[hide private]
[frames] | no frames]

Source Code for Package Bio.Ais

  1  # Copyright 2002 by Katharine Lindner.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  # This module was deprecated, as it does not seem to have any users, 
  7  # no documentation, and the example script is broken. 
  8  """ 
  9  Immune system simulation based on ideas from Immunocomputing: a survey. 
 10  I.Antoniou, S.Gutnikov, V.Ivanov, Yu.Melnikov, A.Tarakanov 
 11  12. Forrest S., Perelson A. Aleen L. and Cherukuri R.  
 12  Self-nonself disctimination in a computer. Proc. of IEEE symposium on reseqrch 
 13  in security and privacy. Oakland, USA, 1994, 202-212. 
 14   
 15   
 16  Immune system simulation. 
 17  Accepts an initial set of sequences to be protected. 
 18  Creates a set of randomly scrambled sequences and uses a lazy check to remove 
 19  those that trigger on members of the protected set. 
 20  The detector for a suspicious sequence checks for a close match to a scrambled sequence. 
 21  The detectors start out with equal weights.  When a detector finds a suspicious antigen, 
 22  its weight is incremented so its chances of being selected in the future increases. 
 23  Intended only for experimentation. 
 24  """ 
 25   
 26   
 27   
 28  import warnings 
 29  warnings.warn("Bio.AIS was deprecated, as it does not seem to have any users. If you do use this module, please contact the Biopython developers at biopython-dev@biopython.org to avoid permanent removal of this module") 
 30   
 31   
 32   
 33   
 34  import os 
 35  import sys 
 36  import string 
 37  import random 
 38  from urllib import FancyURLopener 
 39  from urllib import urlencode 
 40   
 41  from Bio.SGMLExtractor import SGMLExtractorHandle 
 42  from Bio.NetCatch import NetCatch 
 43  from Bio.NetCatch import ExtractUrls 
 44  from Bio.Seq import Seq 
 45  from Bio.Align.Generic import Alignment 
 46  from Bio.Align.AlignInfo import SummaryInfo 
 47  from Bio.Alphabet import DNAAlphabet 
 48  from Bio.Alphabet import Gapped 
 49  from Bio.SGMLExtractor import SGMLExtractorHandle 
 50  from Bio.HotRand import HotRandom 
 51   
 52   
 53   
 54   
 55   
56 -def match_sequence( first, second, threshold ):
57 len_first = len( first ) 58 len_second = len( second ) 59 if( len_first > len_second ): 60 len_min = len_second 61 else: 62 len_min = len_first 63 if( threshold > len_min ): 64 threshold = len_min 65 max_match = 0 66 match_count = 0 67 for j in range( 0, len_min ): 68 if( first[ j ] == second[ j ] ): 69 match_count = match_count + 1 70 if( match_count > max_match ): 71 max_match = match_count 72 else: 73 match_count = 0 74 if( max_match >= threshold ): 75 return 1 76 else: 77 return 0
78
79 -class Lymphocyte:
80
81 - def __init__( self, residues ):
82 self.residues = residues 83 self.may_be_autoimmune = 1 84 self.weight = 1
85 86
87 -class Immune:
88 """ 89 friendly should be an instance of Align. It should contain the set of 90 protected sequences. 91 """ 92
93 - def __init__( self, friendly_seq, alphabet = 'acgt', tuner_dict = None, 94 hot_mode = 0 ):
95 self.hot_mode = hot_mode 96 if hot_mode: 97 self.hot_random = HotRandom() 98 self.set_defaults() 99 100 try: 101 self.tuner_dict = tuner_dict 102 except: 103 self.tuner_dict= self.default_tuner_dict 104 self.tune() 105 self.build_align( friendly_seq ) 106 self.friendly = friendly 107 self.alphabet = alphabet[:] 108 self.lymphocyte_factory()
109
110 - def set_defaults( self ):
111 self.default_tuner_dict = { \ 112 'num_lymphocytes' : 20, \ 113 'num_tosses' : 5, \ 114 'threshold' : 5, \ 115 'segment_size' : 60, \ 116 'replicant_num' : 1 \ 117 }
118
119 - def tune( self ):
120 for ( key, val ) in self.tuner_dict: 121 key = key.strip() 122 val = int( val.strip() ) 123 self.__dict__[ key ] = val
124
125 - def build_align( self, seq ):
126 align = Alignment( Gapped( DNAAlphabet() ) ) 127 alphabet = self.alphabet 128 len_seq = len( seq ) 129 step = self.segment_size 130 for j in range( 0, len_seq, step ): 131 segment = seq[j : j + step] 132 align.add_sequence( name, segment ) 133 self.friendly = align
134
135 - def select_at_random( self, items ):
136 max_select = len( items ) 137 if self.hot_mode: 138 selector = self.hot_random.hot_rand( max_select ) 139 else: 140 selector = random.randint( 0, max_select ) 141 return selector
142
143 - def guess_gaps( self, seq ):
144 """ 145 Fill gaps with random selction from alphabet 146 """ 147 seq = seq.lower() 148 for dest_index in range( 0, len( seq ) ): 149 if( seq[ dest_index ] not in self.alphabet ): 150 source_index = self.select_at_random( self.alphabet ) 151 seq = seq[ :dest_index] + self.alphabet[ source_index ] + seq[ dest_index + 1: ] 152 return seq
153
154 - def scramble( self, seq ):
155 """ 156 Substitute residues in sequence at random. 157 """ 158 num_tosses = self.num_tosses 159 seq = seq[:].lower() 160 for toss in range( 0, num_tosses ): 161 dest_index = self.select_at_random( seq ) 162 source_index = self.select_at_random( self.alphabet ) 163 seq = seq[ :dest_index] + self.alphabet[ source_index ] + seq[ dest_index + 1: ] 164 165 return seq
166 167
168 - def found_antigen( self, detector, mystery_sequence ):
169 detector = detector.lower() 170 mystery_sequence = mystery_sequence.lower() 171 return( match_sequence( detector, mystery_sequence, self.threshold ) )
172
173 - def lazy_auto_immune_check( self, seq ):
174 auto_immune = 0 175 for candidate in self.friendly.get_all_seqs(): 176 if( self.found_antigen( seq, candidate.seq.data ) ): 177 auto_immune = 1 178 break 179 return auto_immune
180 181
182 - def compute_accum_weight( self ):
183 accum_weight = 0 184 for index in range( 0, len( self.lymphocytes ) ): 185 lymphocyte = self.lymphocytes[ index ] 186 accum_weight = accum_weight + lymphocyte.weight 187 lymphocyte.accum_weight = accum_weight 188 self.lymphocytes[ index ] = lymphocyte 189 self.accum_weight = accum_weight 190 return self.accum_weight
191
192 - def search_accum_weight( self, t):
193 last = len( self.lymphocytes ) - 1 194 min = 0; max = last 195 while 1: 196 if max < min: 197 if( min <= last ): 198 return min 199 else: 200 return last 201 m = (min + max) / 2 202 if self.lymphocytes[ m ].accum_weight < t: 203 min = m + 1 204 elif self.lymphocytes[ m ].accum_weight > t: 205 max = m - 1 206 else: 207 return m
208
209 - def pick_a_lymphocyte( self ):
210 """ 211 Random selection biased by weight 212 """ 213 if self.hot_mode: 214 weight = self.hot_random.hot_rand( self.accum_weight ) 215 else: 216 weight = random.randint( self.accum_weight ) 217 index = self.search_accum_weight( weight ) 218 return index
219
220 - def random_test( self, mystery_sequence ):
221 """ 222 A single test probably won't catch a corrupted sequence. 223 Lots of tests are required 224 """ 225 index = self.pick_a_lymphocyte() 226 mystery_sequence = mystery_sequence.lower() 227 lymphocyte = self.lymphocytes[ index ] 228 detector = lymphocyte.residues 229 suspicious = self.found_antigen( detector, mystery_sequence ) 230 if suspicious: 231 auto_immune = 0 232 if( lymphocyte.may_be_autoimmune ): 233 auto_immune = self.lazy_auto_immune_check( detector ) 234 if( auto_immune ): 235 del self.lymphocytes[ index ] 236 self.create_lymphocyte() 237 suspicious = 0 238 else: 239 lymphocyte.may_be_autoimmune = auto_immune 240 lymphocyte.weight = lymphocyte.weight + 1 241 self.lymphocytes[ index ] = lymphocyte 242 self.compute_accum_weight() 243 return suspicious
244 245
246 - def create_lymphocyte( self ):
247 lymphocyte = self.guess_gaps( self.consensus.data ) 248 lymphocyte = self.scramble( lymphocyte ) 249 self.lymphocytes.append( Lymphocyte( lymphocyte ) ) 250 self.compute_accum_weight()
251 252 253
254 - def lymphocyte_factory( self ):
255 num_lymphocytes = self.num_lymphocytes 256 self.lymphocytes = [] 257 summary_info = SummaryInfo( self.friendly ) 258 consensus = summary_info.dumb_consensus() 259 self.consensus = consensus 260 for j in range( 0, num_lymphocytes ): 261 lymphocyte = self.guess_gaps( consensus.data ) 262 lymphocyte = self.scramble( lymphocyte ) 263 self.lymphocytes.append( Lymphocyte( lymphocyte ) ) 264 self.compute_accum_weight()
265