1
2
3
4
5
6
7
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
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
80
82 self.residues = residues
83 self.may_be_autoimmune = 1
84 self.weight = 1
85
86
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 ):
109
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
120 for ( key, val ) in self.tuner_dict:
121 key = key.strip()
122 val = int( val.strip() )
123 self.__dict__[ key ] = val
124
134
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
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
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
169 detector = detector.lower()
170 mystery_sequence = mystery_sequence.lower()
171 return( match_sequence( detector, mystery_sequence, self.threshold ) )
172
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
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
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
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
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
251
252
253
265