File: RestrictionEnzymes.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 12,264 kB
  • ctags: 5,235
  • sloc: cpp: 41,571; ansic: 3,978; sh: 1,420; makefile: 291
file content (235 lines) | stat: -rwxr-xr-x 7,461 bytes parent folder | download | duplicates (6)
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
/** \file
	\brief Contains the TRestrictionEnzyme, TRestrictionCut, TProtease, and TProteaseCut classes
*/
#include "RestrictionEnzymes.h"

bool operator < ( const TRestrictionCut &c1 , const TRestrictionCut &c2 )
    {
    return ( ((TRestrictionCut)c1).getPos() > ((TRestrictionCut)c2).getPos() ) || 
		( ((TRestrictionCut)c1).getPos() == ((TRestrictionCut)c2).getPos() && c1.e->getName() < c2.e->getName() ) ;
    }
    
bool operator == ( const TRestrictionCut &c1 , const TRestrictionCut &c2 )
    {
    return ((TRestrictionCut)c1).getPos() == ((TRestrictionCut)c2).getPos() && c1.e->getName() == c2.e->getName() ;
    }

bool operator < ( const TFragment &f1 , const TFragment &f2 )
    {
	 if ( f1.from < f2.from ) return true ;
	 if ( f1.from > f2.from ) return false ;
	 if ( f1.to < f2.to ) return true ;
	 return false ;
    }
    
bool operator == ( const TFragment &f1 , const TFragment &f2 )
    {
    return ( f1.from == f2.from ) && ( f1.to == f2.to ) ;
    }

/** \brief Compares two restriction enzymes
	\param e The enzyme to compare this one to
	
	If any of name, sequence, note, location, cut, or overlap do
	not match, FALSE is returned
*/
bool TRestrictionEnzyme::differ ( TRestrictionEnzyme &e )
    {
    if ( name != e.name ) return true ;
    if ( note != e.note ) return true ;
    if ( sequence != e.sequence ) return true ;
    if ( location != e.location ) return true ;
    if ( cut != e.cut ) return true ;
    if ( overlap != e.overlap ) return true ;
    return false ;
    }
    
wxString TRestrictionEnzyme::getEndUpperLeft ( bool first_strand )
    {
    wxString r ;
    for ( int a = 0 ; a < getCut(first_strand) ; a++ )
        r += sequence.GetChar(a) ;
    return r ;
    }
    
wxString TRestrictionEnzyme::getEndLowerLeft ( bool first_strand )
    {
    wxString r , s = invertSequence () ;
    for ( int a = 0 ; a < getCut(first_strand)+getOverlap(first_strand) ; a++ )
        r += s.GetChar(a) ;
    return r ;
    }
    
wxString TRestrictionEnzyme::getEndUpperRight ( bool first_strand )
    {
    wxString r ;
    for ( int a = getCut(first_strand) ; a < sequence.length() ; a++ )
        r += sequence.GetChar(a) ;
    return r ;
    }
    
wxString TRestrictionEnzyme::getEndLowerRight ( bool first_strand )
    {
    wxString r , s = invertSequence () ;
    for ( int a = getCut(first_strand)+getOverlap(first_strand) ; a < s.length() ; a++ )
        r += s.GetChar(a) ;
    return r ;
    }
    
wxString TRestrictionEnzyme::invertSequence ()
    {
    TVector v ;
    v.setSequence ( sequence ) ;
    return v.transformSequence ( true , false ) . c_str() ;
    }
    
int TRestrictionEnzyme::getCut ( bool first_strand )
	{
	return first_strand ? cut : sequence.length() - cut - overlap ;
	}

int TRestrictionEnzyme::getOverlap ( bool first_strand )
	{
	return overlap ;
	}

void TRestrictionEnzyme::setCut ( int c ) { cut = c ; }
void TRestrictionEnzyme::setOverlap ( int o ) { overlap = o ; }
wxString TRestrictionEnzyme::getName () { return name ; }
void TRestrictionEnzyme::setName ( wxString _name ) { name = _name ; }
wxString TRestrictionEnzyme::getSequence () { return sequence ; }
bool TRestrictionEnzyme::isPalindromic () { return palindromic ; }

void TRestrictionEnzyme::setSequence ( wxString _seq )
	{
	sequence = _seq ;
    TVector v ;
    v.setSequence ( sequence ) ;
    _seq = v.transformSequence ( true , true ) . c_str() ;
	palindromic = _seq == sequence ;
	}


//------------------------------------------------------------------------------

/** \brief updates the cut position in linear display
	\param w Screen width
	\param h Screen height
*/
void TRestrictionCut::linearUpdate ( int w , int h )
    {
    p.x = lp.x * w / STANDARDRADIUS + 2 ;
    p.y = lp.y * h / STANDARDRADIUS ;
    lastrect = wxRect ( p.x - lastrect.GetWidth() ,
                        p.y - lastrect.GetHeight() ,
                        lastrect.GetWidth() ,
                        lastrect.GetHeight() ) ;
    }
    
wxString TRestrictionCut::getNameAndPosition ()
    {
	 if ( !myapp()->frame->showEnzymePos ) return getDisplayName() ;
    return wxString::Format ( _T("%s %d") , getDisplayName().c_str() , pos ) ;
    }

bool TRestrictionCut::isHidden ( TVector *v )
    {
    return v->isEnzymeHidden ( e->getName() ) ;
    }

wxString TRestrictionCut::getDisplayName ()
	{
	if ( display_name.IsEmpty() ) return e->getName() ;
	return display_name ;
	}    

/** \brief Merges a cut with an isoenzyme for simplified display
	\param c The restriction cut to merge
*/
bool TRestrictionCut::join ( TRestrictionCut *c )
	{
	if ( pos != c->pos ) return false ;
	if ( e->getSequence() != c->e->getSequence() ) return false ;
	if ( e->getCut() != c->e->getCut() ) return false ;
	if ( e->getOverlap() != c->e->getOverlap() ) return false ;
	if ( display_name.IsEmpty() ) display_name = e->getName() ;
	display_name += _T(", ") + c->e->getName() ;
	return true ;
	}    
        
wxString TRestrictionCut::getEndUpperLeft () { return e->getEndUpperLeft ( first_strand ) ; }
wxString TRestrictionCut::getEndLowerLeft () { return e->getEndLowerLeft ( first_strand ) ; }
wxString TRestrictionCut::getEndUpperRight () { return e->getEndUpperRight ( first_strand ) ; }
wxString TRestrictionCut::getEndLowerRight () { return e->getEndLowerRight ( first_strand ) ; }
int TRestrictionCut::getCut () { return e->getCut ( first_strand ) ; }
int TRestrictionCut::getOverlap () { return e->getOverlap ( first_strand ) ; }
void TRestrictionCut::setPos ( int p ) { pos = p ; }
wxString TRestrictionCut::getSequence () { return e->getSequence () ; }

int TRestrictionCut::getPos ()
	{
	if ( first_strand ) return pos ;
	int a = pos ;
	a -= e->getCut() ;
	a += getCut() ;
//	a += e->getSequence().length() ;
//	a -= e->getCut(first_strand) ;
	return a ;
	}

int TRestrictionCut::getFrom ()
	{
	return pos - e->getCut() ;
	}

int TRestrictionCut::getTo ()
	{
	return pos - e->getCut() + e->getSequence().length() - 1 ;
	}


//------------------------------------------------------------------------------

TProtease::TProtease ( wxString _name , wxString m , wxString _note )
    {
    name = _name ;
    str_match = m ;
    wxString s ;
    int a ;
    for ( a = 0 ; a < m.length() ; a++ )
        {
        char ma = m.GetChar(a) ;
        if ( ma == ',' || ma == ' ' || ma == '|' )
           {
           if ( !s.IsEmpty() ) match.Add ( s ) ;
           s = _T("") ;
           if ( ma == '|' ) cut = match.GetCount() - 1 ;
           }
        else s += m.GetChar(a) ;
        }
    if ( !s.IsEmpty() ) match.Add ( s ) ;
    use = true ;
    note = _note ;
    }



bool TProtease::does_match ( wxString s )
    {
    if ( s.length() != len() ) return false ;
    int a , b ;
    for ( a = 0 ; a < len() ; a++ )
        {
        bool yes = true , found = false ;
        wxString m = match[a] ;
        for ( b = 0 ; b < m.length() && !found ; b++ )
           {
           if ( m.GetChar(b) == '!' ) yes = !yes ;
           else if ( !yes && s.GetChar(a) == m.GetChar(b) ) return false ;
           else if ( yes && ( s.GetChar(a) == m.GetChar(b) || m.GetChar(b) == '*' ) ) found = true ;
           }
        if ( !found && yes ) return false ;
        }
    return true ;
    }