File: stsmatch_i.cpp

package info (click to toggle)
epcr 2.3.12-1-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 916 kB
  • sloc: cpp: 5,730; ansic: 231; makefile: 31; python: 26; sh: 12
file content (304 lines) | stat: -rw-r--r-- 10,278 bytes parent folder | download | duplicates (7)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* $Id: stsmatch_i.cpp,v 1.13 2005/06/14 16:46:44 rotmistr Exp $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * =========================================================================
 *
 * Author: Kirill Rotmistrovsky
 *
 * ========================================================================= */

#include <epcr/hashset.hpp>
#include <epcr/stsmatch_i.hpp>

#include <stdexcept>

USING_NCBI_SCOPE;
USING_SCOPE(EPCR_SCOPE);

#define DBG fprintf(stderr,__FILE__":%d\n",__LINE__)
#define INT(i) fprintf(stderr,__FILE__":%d - %s = %d\n",__LINE__,#i,i)
#define STR(s,i) fprintf(stderr,__FILE__":%d - %s = %.*s\n",__LINE__,#s,i,s)

bool CStsHash::sm_OneTimeRun(false);

void CStsHash::Reset()
{
    if(m_Table) {
        // NB: It DOES NOT delete STSes themselve!!!
        for(unsigned i=0; i<m_Hash.GetWordCount(); ++i)
            delete[] m_Table[i];
        
        delete[] m_Table;
    }
    m_Table=0;
    if(!sm_OneTimeRun) {
        for(TStsList::iterator i=m_All.begin(); i!=m_All.end(); ++i) {
            delete *i;
        }
        m_All.clear();
    }
}

void CStsHash::Clear()
{
    if(m_Table) {
        for(unsigned i=0; i<m_Hash.GetWordCount(); ++i)
            m_Table[i]->clear();
    }
    
    for(TStsList::iterator i=m_All.begin(); i!=m_All.end(); ++i) {
        delete *i;
    }
    m_All.clear();
}


void CStsHash::SetHash(const CHashSet& hs)
{
    Reset();
    
    m_Hash=hs;

	m_Table=new THashTable[m_Hash.GetWordCount()];
	for(size_t i=0; i<m_Hash.GetWordCount(); ++i) {
		m_Table[i]=new TStsList[m_Hash.GetTableSize(i)];
	}
}

bool CStsHash::AddStsEntry(ISts * sts) 
{
    CStrRef primer=sts->GetPrimer(ISts::eLeft);
    if(primer.length()<GetWordSize()) return false;

    bool ok=false;
    
    m_Hash.Begin(primer.data()+(primer.length()-GetWordSize()));
    for(unsigned it=0; it<GetWordCount(); ++it) {
        if(m_Hash.Good(it)) {
            m_Table[it][m_Hash[it]].push_back(sts);
            ok=true;
        }
    }    
    if(ok && !sm_OneTimeRun) m_All.push_back(sts);
    return ok;
}

////////////////////////////////////////////////////////////////////////
void CPcrMachine::ProcessSequence(const char * label,
                                  const char * seq_data,
                                  unsigned seq_len) 
{
    if(!m_Callback || !m_StsHash) 
        throw logic_error("CPcrMachine needs SetHash and SetCallback calls");
    if(m_StsHash->GetWordCount()==0)
        throw logic_error("Word count should be greater then zero");
    if(m_StsHash->m_Table==0)
        throw logic_error("Sts table should be initialized");
    if(seq_len==0) seq_len=strlen(seq_data);
    
    m_Callback->CbkSequence(label);
    m_Callback->CbkSequenceData(seq_data,seq_len);

    CHashSet& hash=m_HashL;

    if(m_Progress) 
        m_Progress->PgsSequenceStart(label,seq_data,seq_len,
                                     hash.GetWordSize());

    if(seq_len<hash.GetWordSize()) {
        m_Callback->CbkWarning("too short sequence");
        m_Callback->CbkSequenceEnd();
        if(m_Progress) m_Progress->PgsSequenceEnd();
        return;
    }
    
    if(hash.GetWordCount()==1) {
        CStsHash::THashTable tab=m_StsHash->m_Table[0];
        for(hash.Begin(seq_data); !hash.End(); hash.Next()) {
            if(m_Progress) m_Progress->PgsSequenceAt(hash.GetPosition());
            if(!hash.Good(0)) continue;
            if(tab[hash[0]].size()) Match(0, tab[hash[0]], seq_data, seq_len);
        }
    } else {
        CStsHash::TData tab=m_StsHash->m_Table;
    
        for(hash.Begin(seq_data); !hash.End(); hash.Next()) {
            if(m_Progress) m_Progress->PgsSequenceAt(hash.GetPosition());
            for(unsigned word=0; word<hash.GetWordCount(); ++word) {
                if(!hash.Good(word)) continue;
                if(tab[word][hash[word]].size())
                    Match(word, tab[word][hash[word]], seq_data, seq_len);
            }

        }
    }
    if(m_Progress) m_Progress->PgsSequenceEnd();
    m_Callback->CbkSequenceEnd();
}

void CPcrMachine::Match(unsigned word, const TStsList& lst,
                        const char * seq_data, int seq_len) 
{
    int seq_pos = m_HashL.GetPosition();
    const char * seq_cur = seq_data+seq_pos;//+pcr_p1_len;
    int cur_len = seq_len - seq_pos;

    for(TStsList::const_iterator ists=lst.begin(); ists!=lst.end(); ++ists) {

        const ISts * sts=*ists;
    
    
        int pcr_p1_len  = sts->GetPrimerLength(ISts::eLeft);
        
        if(m_HashL.GetPosition() < (unsigned)pcr_p1_len) continue;
        const char * lprimer = sts->GetPrimerData(ISts::eLeft);
                
        bool match=m_AlignL->Reverse(seq_data,seq_cur,lprimer,pcr_p1_len);
        
        if(match) {
            int pcr_p2_len = sts->GetPrimerLength(ISts::eRight);

            int pcr_len = pcr_p1_len + pcr_p2_len;
            
            const char * p = seq_cur + sts->GetSizeLo() - pcr_len - m_Margin;
            const char * P = seq_cur + sts->GetSizeHi() - pcr_len + m_Margin;
            
            if( p < seq_cur - pcr_p1_len ) p = seq_cur - pcr_p1_len;
            if( p < seq_data ) p = seq_data;
            if( P > seq_cur + cur_len - pcr_p2_len )
                P = seq_cur + cur_len - pcr_p2_len;

            if( p > P ) continue;

            const char * rprimer = sts->GetPrimerData(ISts::eRight);
                
#ifndef USE_HASH_FOR_RIGHT_PRIMER
#define USE_HASH_FOR_RIGHT_PRIMER 1
#endif

            int ovhg1 = sts->GetOverhangChars(ISts::eLeft);
            int ovhg2 = sts->GetOverhangChars(ISts::eRight);
            int ovhgall = ovhg1+ovhg2;

#if !USE_HASH_FOR_RIGHT_PRIMER
            for(; p<=P; --P) {

                if(m_AlignR->Forward(P,seq_data+seq_len,rprimer,pcr_p2_len)) {
                    
                    IPcrMachineCallback::SScore score(
                        P-p+pcr_len+ovhgall,
                        m_AlignL->GetMismatches(),
                        m_AlignR->GetMismatches(),
                        m_AlignL->GetGaps(),
                        m_AlignR->GetGaps());
                    
                    m_Callback->CbkMatch(
                        sts,
                        seq_pos-pcr_p1_len-ovhg1,
                        P-seq_data+pcr_p2_len+ovhg2,
                        &score);
                }
            }
#else
            CHashSet& rhash=m_HashR;
            CHashSet& mhash=m_HashS;
            rhash.Begin(sts->GetPrimerData(ISts::eRight));

            for (mhash.Begin(p); p<=P; p++, mhash.Next()) {
                for(unsigned wd=0; wd<rhash.GetWordSize(); ++wd) {
                    if(!rhash.Good(wd) || !mhash.Good(wd)) continue;
			
                    if(mhash.GetValue(wd)!=rhash.GetValue(wd)) continue;
                
                    if(m_AlignR->Forward(p,seq_data+seq_len,
                                         rprimer,pcr_p2_len)) {
                        
                        IPcrMachineCallback::SScore score(
                            P-p+pcr_len+ovhgall,
                            m_AlignL->GetMismatches(),
                            m_AlignR->GetMismatches(),
                            m_AlignL->GetGaps(),
                            m_AlignR->GetGaps());
                    
                        m_Callback->CbkMatch(sts,
                                             seq_pos-pcr_p1_len-ovhg1, 
                                             p-seq_data+pcr_p2_len+ovhg2,
                                             &score);
                        
                        break; // no other reports for this positions 
                    }
                }
            }
#endif
        }
	
    }
}

/*
 * $Log: stsmatch_i.cpp,v $
 * Revision 1.13  2005/06/14 16:46:44  rotmistr
 * Changed report format for floppy tails
 *
 * Revision 1.12  2004/10/26 17:16:35  rotmistr
 * Added 5'-end masking for primers
 *
 * Revision 1.11  2004/06/03 23:37:22  rotmistr
 * New aligner added.
 *
 * Revision 1.10  2004/05/27 21:12:46  rotmistr
 * Some warnings fixed.
 *
 * Revision 1.9  2004/05/27 20:35:49  rotmistr
 * Version 2.1.0 with appropriate changes (see Changes) is ready for tests.
 *
 * Revision 1.8  2004/03/31 05:04:00  rotmistr
 * Search range fix
 *
 * Revision 1.7  2004/03/25 19:36:52  rotmistr
 * API: separate left and right primers mism/gaps in forward API
 *
 * Revision 1.6  2004/03/07 06:35:59  rotmistr
 * Many bugfixes and optimisations -- cgi is to go to production
 *
 * Revision 1.5  2004/02/18 05:43:22  rotmistr
 * Bug fix with search range
 *
 * Revision 1.4  2004/02/11 04:34:57  rotmistr
 * Optimised lookup speed and memory usage
 * Fixed bug with end of sequence in stsmatch
 * Changing CGI look
 *
 * Revision 1.3  2004/01/28 23:27:02  rotmistr
 * "Best of overlapping" hit selection postprocessor added.
 *
 * Revision 1.2  2004/01/08 23:22:41  rotmistr
 * Fixed init error in faread,
 * Adjusted output to standard,
 * Added output format style and output file to parameters.
 *
 * Revision 1.1.1.1  2003/12/23 18:17:27  rotmistr
 * Package that includes e-PCR, reverse e-PCR, and sequence data preparation
 * program for reverse e-PCR looks ready
 *
 */