File: __init__.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (258 lines) | stat: -rw-r--r-- 8,404 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2002 by Katharine Lindner.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.
"""
Immune system simulation based on ideas from Immunocomputing: a survey.
I.Antoniou, S.Gutnikov, V.Ivanov, Yu.Melnikov, A.Tarakanov
12. Forrest S., Perelson A. Aleen L. and Cherukuri R. 
Self-nonself disctimination in a computer. Proc. of IEEE symposium on reseqrch
in security and privacy. Oakland, USA, 1994, 202-212.


Immune system simulation.
Accepts an initial set of sequences to be protected.
Creates a set of randomly scrambled sequences and uses a lazy check to remove
those that trigger on members of the protected set.
The detector for a suspicious sequence checks for a close match to a scrambled sequence.
The detectors start out with equal weights.  When a detector finds a suspicious antigen,
its weight is incremented so its chances of being selected in the future increases.
Intended only for experimentation.
"""

import os
import sys
import string
import random
from urllib import FancyURLopener
from urllib import urlencode

from Bio.SGMLExtractor import SGMLExtractorHandle
from Bio.NetCatch import NetCatch
from Bio.NetCatch import ExtractUrls
from Bio.Seq import Seq
from Bio.Align.Generic import Alignment
from Bio.Align.AlignInfo import SummaryInfo
from Bio.Alphabet import DNAAlphabet
from Bio.Alphabet import Gapped
from Bio.SGMLExtractor import SGMLExtractorHandle
from Bio.HotRand import HotRandom





def match_sequence( first, second, threshold ):
    len_first = len( first )
    len_second = len( second )
    if( len_first > len_second ):
        len_min = len_second
    else:
        len_min = len_first
    if( threshold > len_min ):
        threshold = len_min
    max_match = 0
    match_count = 0
    for j in range( 0, len_min ):
        if( first[ j ] == second[ j ] ):
            match_count = match_count + 1
            if( match_count > max_match ):
                max_match = match_count
        else:
            match_count = 0
    if( max_match >= threshold ):
        return 1
    else:
        return 0

class Lymphocyte:

    def __init__( self, residues ):
        self.residues = residues
        self.may_be_autoimmune = 1
        self.weight = 1


class Immune:
    """
    friendly should be an instance of Align.  It should contain the set of
    protected sequences.
    """

    def __init__( self, friendly_seq, alphabet = 'acgt', tuner_dict = None,
hot_mode = 0 ):
        self.hot_mode = hot_mode
        if hot_mode:
            self.hot_random = HotRandom()
        self.set_defaults()

        try:
            self.tuner_dict = tuner_dict
        except:
            self.tuner_dict= self.default_tuner_dict
        self.tune()
        self.build_align( friendly_seq )
        self.friendly = friendly
        self.alphabet = alphabet[:]
        self.lymphocyte_factory()

    def set_defaults( self ):
        self.default_tuner_dict = { \
            'num_lymphocytes' : 20, \
            'num_tosses' : 5, \
            'threshold' : 5, \
            'segment_size' : 60, \
            'replicant_num' : 1 \
        }

    def tune( self ):
        for ( key, val ) in self.tuner_dict:
            key = key.strip()
            val = int( val.strip() )
            self.__dict__[ key ] = val

    def build_align( self, seq ):
        align = Alignment( Gapped( DNAAlphabet() ) )
        alphabet = self.alphabet
        len_seq = len( seq )
        step = self.segment_size
        for j in range( 0, len_seq, step ):
            segment = seq[j : j + step]
            align.add_sequence( name, segment )
        self.friendly = align

    def select_at_random( self, items ):
        max_select = len( items )
        if self.hot_mode:
            selector = self.hot_random.hot_rand( max_select )
        else:
            selector = random.randint( 0, max_select )
        return selector

    def guess_gaps( self, seq ):
        """
        Fill gaps with random selction from alphabet
        """
        seq = seq.lower()
        for dest_index in range( 0, len( seq ) ):
            if( seq[ dest_index ] not in self.alphabet ):
                source_index = self.select_at_random( self.alphabet )
                seq = seq[ :dest_index] + self.alphabet[ source_index ] + seq[ dest_index + 1: ]
        return seq

    def scramble( self, seq ):
        """
        Substitute residues in sequence at random.
        """
        num_tosses = self.num_tosses
        seq = seq[:].lower()
        for toss in range( 0, num_tosses ):
            dest_index = self.select_at_random( seq )
            source_index = self.select_at_random( self.alphabet )
            seq = seq[ :dest_index] + self.alphabet[ source_index ] + seq[ dest_index + 1: ]

        return seq


    def found_antigen( self, detector, mystery_sequence ):
        detector = detector.lower()
        mystery_sequence = mystery_sequence.lower()
        return( match_sequence( detector, mystery_sequence, self.threshold ) )

    def lazy_auto_immune_check( self, seq ):
        auto_immune = 0
        for candidate in self.friendly.get_all_seqs():
            if( self.found_antigen( seq, candidate.seq.data ) ):
                auto_immune = 1
                break
        return auto_immune


    def compute_accum_weight( self ):
        accum_weight = 0
        for index in range( 0, len( self.lymphocytes ) ):
            lymphocyte = self.lymphocytes[ index ]
            accum_weight = accum_weight + lymphocyte.weight
            lymphocyte.accum_weight = accum_weight
            self.lymphocytes[ index ] = lymphocyte
        self.accum_weight = accum_weight
        return self.accum_weight

    def search_accum_weight( self, t):
        last =  len( self.lymphocytes ) - 1
        min = 0; max = last
        while 1:
            if max < min:
                if( min <= last ):
                    return min
                else:
                    return last
            m = (min + max) / 2
            if self.lymphocytes[ m ].accum_weight < t:
                min = m + 1
            elif self.lymphocytes[ m ].accum_weight > t:
                max = m - 1
            else:
                return m

    def pick_a_lymphocyte( self ):
        """
        Random selection biased by weight
        """
        if self.hot_mode:
            weight = self.hot_random.hot_rand( self.accum_weight )
        else:
            weight = random.randint( self.accum_weight )
        index = self.search_accum_weight( weight )
        return index

    def random_test( self, mystery_sequence ):
        """
        A single test probably won't catch a corrupted sequence.
        Lots of tests are required
        """
        index = self.pick_a_lymphocyte()
        mystery_sequence = mystery_sequence.lower()
        lymphocyte = self.lymphocytes[ index ]
        detector = lymphocyte.residues
        suspicious = self.found_antigen( detector, mystery_sequence )
        if suspicious:
            auto_immune = 0
            if( lymphocyte.may_be_autoimmune ):
                auto_immune = self.lazy_auto_immune_check( detector )
            if( auto_immune ):
                del self.lymphocytes[ index ]
                self.create_lymphocyte()
                suspicious = 0
            else:
                lymphocyte.may_be_autoimmune = auto_immune
                lymphocyte.weight = lymphocyte.weight + 1
                self.lymphocytes[ index ] = lymphocyte
                self.compute_accum_weight()
        return suspicious


    def create_lymphocyte( self ):
        lymphocyte = self.guess_gaps( self.consensus.data )
        lymphocyte = self.scramble( lymphocyte )
        self.lymphocytes.append( Lymphocyte( lymphocyte ) )
        self.compute_accum_weight()



    def lymphocyte_factory( self ):
        num_lymphocytes = self.num_lymphocytes
        self.lymphocytes = []
        summary_info = SummaryInfo( self.friendly )
        consensus = summary_info.dumb_consensus()
        self.consensus = consensus
        for j in range( 0, num_lymphocytes ):
            lymphocyte = self.guess_gaps( consensus.data )
            lymphocyte = self.scramble( lymphocyte )
            self.lymphocytes.append( Lymphocyte( lymphocyte ) )
        self.compute_accum_weight()