File: align.cpp

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (398 lines) | stat: -rw-r--r-- 9,286 bytes parent folder | download | duplicates (5)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (c) Medical Research Council 2001. All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * this copyright and notice appears in all copies.
 *
 * This file was written as part of the Staden Package at the MRC Laboratory
 * of Molecular Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom.
 *
 * MRC disclaims all warranties with regard to this software.
 *
 */

#include <staden_config.h>

#include <new>                // For std::bad_alloc()
#include <cstdio>             // For fprintf(), fputc()
#include <cctype>             // For tolower()
#include <cstring>            // For strlen()
#include <align.hpp>
#include <locale>


//------------
// Local Data
//------------

bool Alignment::m_bDNALookupInitialised = false;
static const char* CharSet              = "ACGTURYMWSKDHVBN-*";
static const int   ScoreMatrix[18][18]  = {
{ 4,  -8,  -8,  -8,  -8,   2,  -3,   2,   2,  -3,  -3,   1,   1,   1,  -4,   1,   1,   0 },
{-8,   4,  -8,  -8,  -8,  -3,   2,   2,  -3,   2,  -3,  -4,   1,   1,   1,   1,   1,   0 },
{-8,  -8,   4,  -8,  -8,   2,  -3,  -3,  -3,   2,   2,   1,  -4,   1,   1,   1,   1,   0 },
{-8,  -8,  -8,   4,   4,  -3,   2,  -3,   2,  -3,   2,   1,   1,  -4,   1,   1,   1,   0 },
{-8,  -8,  -8,   4,   4,  -3,   2,  -3,   2,  -3,   2,   1,   1,  -4,   1,   1,   1,   0 },
{ 2,  -3,   2,  -3,  -3,   2,  -4,   0,   1,   1,   1,   1,   0,   1,   0,   0,   0,   0 },
{-3,   2,  -3,   2,   2,  -4,   2,   0,   1,   1,   1,   0,   1,   0,   1,   0,   0,   0 },
{ 2,   2,  -3,  -3,  -3,   0,   0,   2,   1,   1,  -3,   0,   1,   1,   0,   0,   0,   0 },
{ 2,  -3,  -3,   2,   2,   1,   1,   1,   2,  -3,   1,   1,   1,   0,   0,   0,   0,   0 },
{-3,   2,   2,  -3,  -3,   1,   1,   1,  -3,   2,   1,   0,   0,   1,   1,   0,   0,   0 },
{-3,  -3,   2,   2,   2,   1,   1,  -3,   1,   1,   2,   1,   0,   0,   1,   0,   0,   0 },
{ 1,  -4,   1,   1,   1,   1,   0,   0,   1,   0,   1,   1,   0,   0,   0,   0,   0,   0 },
{ 1,   1,  -4,   1,   1,   0,   1,   1,   1,   0,   0,   0,   1,   0,   0,   0,   0,   0 },
{ 1,   1,   1,  -4,  -4,   1,   0,   1,   0,   1,   0,   0,   0,   1,   0,   0,   0,   0 },
{-4,   1,   1,   1,   1,   0,   1,   0,   0,   1,   1,   0,   0,   0,   1,   0,   0,   0 },
{ 1,   1,   1,   1,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1 },
{ 1,   1,   1,   1,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1 },
{ 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   4 }};



//-------------
// Constructor
//-------------

Alignment::Alignment()
{
   // NOTE. The edge score left/right parameters are important for
   //       the envelope alignments to be performed correctly at the
   //       chunk ends. Do not alter them!
   m_nBand              = 0;
   m_pParams            = 0;
   m_pOverlap           = 0;
   m_nPadSymbol         = '*';
   m_nEdgeScore         = EDGE_SCORE_RIGHT;
   m_nGapPenaltyBegin   = 12;
   m_nGapPenaltyExtend  = 4;
   for( int n=0; n<MAX_INPUT_SEQUENCES; n++ )
   {
      m_pInputSequence[n]       = 0;
      m_nInputSequenceLength[n] = 0;
   }
}



//------------
// Destructor
//------------

Alignment::~Alignment()
{
   // Cleanup
   if( m_pOverlap )
   {
      sp::destroy_overlap(m_pOverlap);
      m_pOverlap = 0;
   }
   if( m_pParams )
   {
      sp::destroy_align_params(m_pParams);
      m_pParams = 0;
   }
}



//------------
// Edge Score
//------------

void Alignment::EdgeScore( edge_score_t es )
{
   m_nEdgeScore = es;
}



//----------------
// Input Sequence
//----------------

void Alignment::InputSequence( int n, const char* s, int l )
{
   assert(n>=0);
   assert(n<MAX_INPUT_SEQUENCES);
   assert(s != NULL);
   assert(*s);
   m_pInputSequence[n]       = s;
   m_nInputSequenceLength[n] = (l<0) ? std::strlen(s) : l;
}



//--------
// Matrix
//--------

void Alignment::Matrix( int** m, int n, bool AutoDestroy )
{
   assert(m != NULL);
   assert(m[0] != NULL);
   assert(n>0);
   m_oMatrix.Wrap( m, n, n, AutoDestroy );
}



//---------
// Execute
//---------

int Alignment::Execute( algorithm_t a )
{
   int n;



   // Check there are at least 2 input sequences
   for( n=0; n<2; n++ )
   {
      if( !m_pInputSequence[n] || !(*(m_pInputSequence[n])) )
         return -1;
   }



   // Initialisation
   if( m_bDNALookupInitialised == false )
   {
      sp::init_DNA_lookup();
      m_bDNALookupInitialised = true;
   }



   // Create a weight matrix if none has been supplied
   if( m_oMatrix.Rows() <= 0 )
      CreateDefaultMatrix();



   // Do memory allocation
   if( !m_pParams )
   {
      m_pParams = sp::create_align_params();
      if( !m_pParams )
         throw std::bad_alloc();
   }
   if( m_pOverlap )
   {
      sp::destroy_overlap(m_pOverlap);
      m_pOverlap = 0;
   }
   m_pOverlap = sp::create_overlap();
   if( !m_pOverlap )
      throw std::bad_alloc();



   // Adjust alignment parameters
   sp::set_align_params( m_pParams, m_nBand, m_nGapPenaltyBegin, m_nGapPenaltyExtend,
                        SP_ALIGNMENT_RETURN_SEQ, 0, 0, m_nPadSymbol, m_nPadSymbol,
                        0, 0, a, 8, 0, m_nEdgeScore, 0.0, m_oMatrix.Raw() );



   // Adjust overlap parameters
   sp::init_overlap( m_pOverlap, (char*)m_pInputSequence[0], (char*)m_pInputSequence[1],
                    m_nInputSequenceLength[0], m_nInputSequenceLength[1] );



   // Do alignment
   return sp::aligner( m_pParams, m_pOverlap );
}



//-----------------
// Output Sequence
//-----------------

char* Alignment::OutputSequence( int n ) const
{
   // This horrible hack is required due to poor Overlap structure design.
   assert(n>=0);
   assert(n<MAX_INPUT_SEQUENCES);
   assert(m_pOverlap != NULL);
   switch(n)
   {
      case 0: return m_pOverlap->seq1_out;
      case 1: return m_pOverlap->seq2_out;
   }
   assert(0);
   return 0;
}



//------------------------
// Output Sequence Length
//------------------------

int Alignment::OutputSequenceLength( int n ) const
{
    assert(n>=0);
    assert(n<MAX_INPUT_SEQUENCES);
    assert(m_pOverlap != NULL);
    return m_pOverlap->seq_out_len;
}



//------------------------------
// Output Sequence Left Overlap
//------------------------------

int Alignment::OutputSequenceLeftOverlap( int n ) const
{
    assert(n>=0);
    assert(n<MAX_INPUT_SEQUENCES);
    assert(m_pOverlap != NULL);
    return m_pOverlap->left;
}



//-------------------------------
// Output Sequence Right Overlap
//-------------------------------

int Alignment::OutputSequenceRightOverlap( int n ) const
{
    assert(n>=0);
    assert(n<MAX_INPUT_SEQUENCES);
    assert(m_pOverlap != NULL);
    return m_pOverlap->right;
}



//--------------
// Output Score
//--------------

double Alignment::OutputScore() const
{
    assert(m_pOverlap != NULL);
    if( m_pOverlap->seq_out_len > 0 )
        return m_pOverlap->score / m_pOverlap->seq_out_len;
    else
        return m_pOverlap->score;
}



//-----------
// Debugging
//-----------

void Alignment::DumpToFile( const char* s, bool AsInteger ) const
{
    int         n;
    int         k;
    int         nLen;
    const char* pSeq;



    // Create file to dump the output to
    std::FILE* pOut = std::fopen( s, "wb" );
    if( !pOut )
        return;


    // Dump the input sequences
    for( k=0; k<2; k++ )
    {
        pSeq = m_pInputSequence[k];
        nLen = m_nInputSequenceLength[k];
        if( AsInteger )
        {
            for( n=0; n<nLen; n++ )
                std::fprintf( pOut, "%3d ", int(pSeq[n]) );
        }
        else
        {
            for( n=0; n<nLen; n++ )
                std::fputc( pSeq[n], pOut );
        }
        std::fprintf( pOut, "\r\n" );
    }


    // Dump the output sequences
    for( k=0; k<2; k++ )
    {
        pSeq = OutputSequence(k);
        nLen = OutputSequenceLength(k);
        if( AsInteger )
        {
            for( n=0; n<nLen; n++ )
                std::fprintf( pOut, "%3d ", int(pSeq[n]) );
        }
        else
        {
            for( n=0; n<nLen; n++ )
                std::fputc( pSeq[n], pOut );
        }
        std::fprintf( pOut, "\r\n" );
    }
    std::fclose( pOut );
}




//---------
// Helpers
//---------

void Alignment::CreateDefaultMatrix()
{
   int  n;
   int  nMax;
   char cr, cc;
   int  r,  c;



   // Compute optimal matrix size for given character set
   n    = 0;
   nMax = 0;
   while( CharSet[n] )
   {
      c = std::tolower( CharSet[n] );
      if( c > nMax )
         nMax = c;
      n++;
   }
   nMax++;



   // Create weight matrix, background score is slightly negative.
   m_oMatrix.Create( nMax, nMax );
   m_oMatrix.Fill( -1 );



   // Initialise weight matrix from score matrix
   int e = std::strlen(CharSet);
   for( r=0; r<e; r++ )
   {
      cr = CharSet[r];
      for( c=0; c<e; c++ )
      {
          cc = CharSet[c];
          m_oMatrix[             cr ][             cc ] = ScoreMatrix[r][c];
          m_oMatrix[std::tolower(cr)][             cc ] = ScoreMatrix[r][c];
          m_oMatrix[             cr ][std::tolower(cc)] = ScoreMatrix[r][c];
          m_oMatrix[std::tolower(cr)][std::tolower(cc)] = ScoreMatrix[r][c];
      }
   }
}