File: tracealign.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 (445 lines) | stat: -rw-r--r-- 18,509 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * Copyright (c) Medical Research Council 2002. 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 <cassert>
#include <cstring>                    // For strcpy(), memset()
#include <cstdio>                     // For std::printf(), std::sprintf()
#include <new>                        // For std::bad_alloc
#include <algorithm>                  // For std::max()
#include <list.hpp>                   // For list template
#include <align.hpp>                  // For Alignment object
#include <trace.hpp>                  // For Trace object
#include <muttag.hpp>                 // For MutTag object
#include <peakcall.hpp>               // For PeakCall object
#include <tagarray.hpp>               // For TagArray object
#include <tracealign.hpp>             // For helpers
#include <tracealign_cache.hpp>       // For TraceAlignCache object
#include <tracealign_preprocess.hpp>  // For TraceAlignPreprocessor object



// #define VERBOSE_DEBUG 



/**
   Initialises an empty tracealign_t structure.
*/
void TraceAlignInit( tracealign_t* ta )
{
   assert(ta != NULL);
   std::memset( ta, 0, sizeof(tracealign_t) );
   ta->ResultString    = new char[ 512 ];
   ta->ResultString[0] = 0;
   ta->Initialised     = 1;
}



/**
   Frees up all memory allocated by the trace alignment algorithm
   in the tracealign_t structure.
*/
void TraceAlignDestroy( tracealign_t* ta )
{
   assert(ta != NULL);
   assert(ta->Initialised);
   try
   {
      // Delete all data
      TraceAlignDestroyCache( ta );
      TraceAlignDestroyResults( ta );
      delete [] ta->ResultString;
   }
   catch(...)
   {
      // Shouldn't happen, but we musn't throw exceptions outside dll boundary
      assert(0);
   }
}



/**
   Sets the specified reference trace. The clip points are in base units.
   The caller retains ownership of the Read structure.
*/
void TraceAlignSetReference( tracealign_t* ta, mutlib_strand_t d, Read* r, int ql, int qr )
{
   assert(r != NULL);
   assert(ta != NULL);
   assert(ta->Initialised);
   ta->Reference[d].ClipL  = ql;
   ta->Reference[d].ClipR  = qr;
   ta->Reference[d].Trace  = r;
   ta->Reference[d].Strand = d;
   ta->Reference[d].New    = 1;
}



/**
   Sets the input traces. The clip points are in base units. The caller retains
   ownership of the Read structure.
*/
void TraceAlignSetInput( tracealign_t* ta, mutlib_strand_t d, Read* r, int ql, int qr )
{
   assert(r != NULL);
   assert(ta != NULL);
   assert(ta->Initialised);
   ta->Input.ClipL  = ql;
   ta->Input.ClipR  = qr;
   ta->Input.Trace  = r;
   ta->Input.Strand = d;
   ta->Input.New    = 1;
}



/**
   Returns the result code generated by the Execute() function. This can be
   used to query the result later without having to save the value returned
   by Execute().
*/
mutlib_result_t TraceAlignGetResultCode( tracealign_t* ta )
{
   assert(ta != NULL);
   assert(ta->Initialised);
   return ta->ResultCode;
}



/**
   Returns a read-only user friendly error message corresponding to the
   last result code. It may contain more useful information such as a
   filename or the particular intput that was invalid. This can be displayed
   to the user if required.
*/
const char* TraceAlignGetResultString( tracealign_t* ta )
{
   assert(ta != NULL);
   assert(ta->Initialised);
   return ta->ResultString;
}



/**
   Returns the aligned traces as Read structures. Ownership of the Read
   structures are retained by the trace alignment algorithm. If 'l' or 'r'
   is not null, the left and right clip points in base numbers are returned
   as well.
*/
Read* TraceAlignGetAlignment( tracealign_t* ta, mutlib_input_t i, int* l, int* r )
{
   assert(ta != NULL);
   assert(ta->Initialised);
   if( l != 0 )
      *l = ta->Alignment[i].ClipL;
   if( r != 0 )
      *r = ta->Alignment[i].ClipR;
   return ta->Alignment[i].Trace;
}



/**
   Executes the trace alignment algorithm.
*/
mutlib_result_t TraceAlignExecute( tracealign_t* ta )
{
    enum { STATE_INITIALISE, STATE_VALIDATE_INPUT, STATE_UPDATE_CACHE,
           STATE_PREPROCESS_INPUT, STATE_SEQUENCE_ALIGN, STATE_SEQUENCE_OVERLAP,
           STATE_SEQUENCE_MAP, STATE_QUANTISE_ENVELOPE, STATE_ESTIMATE_BANDSIZE,
           STATE_ENVELOPE_ALIGN, STATE_TRACE_ALIGN, STATE_EXIT };
    mutlib_strand_t         Strand = MUTLIB_STRAND_FORWARD; // silence warning
    Alignment               Aligner;
    int                     AlignedSeqOverlap[2];
    DNAArray<char>          RefSeq;
    TraceAlignPreprocessor* RefData = NULL;
    Trace                   RefTrace;
    SimpleArray<char>       RefEnvelope;
    SimpleArray<char>       RefEnvelopeAligned;
    int                     RefOverlapBases[2];
    int                     RefOverlapSamples[2];
    DNAArray<char>          InputSeq;
    TraceAlignPreprocessor  InputData;
    Trace                   InputTrace;
    SimpleArray<char>       InputEnvelope;
    SimpleArray<char>       InputEnvelopeAligned;
    int                     InputOverlapBases[2];
    int                     InputOverlapSamples[2];
    const int               L           = 0;
    const int               R           = 1;
    const int               QLOWER      = 1;
    const int               QLEVELS     = 126;
    const char              QPAD_SYMBOL = QLEVELS + QLOWER;
    const int               MIN_OVERLAP = 10;
    bool                    Done        = false;
    int                     State       = STATE_INITIALISE;
    TraceAlignCache*        Cache       = 0;
    int                     BandSize    = 0;
    assert(ta != NULL);
    assert(ta->Initialised);
    try
    {
        while(!Done)
        {
            switch(State)
            {
                case STATE_INITIALISE:
                    // Destroy old results
                    TraceAlignDestroyResults( ta );
                    Strand = ta->Input.Strand;
                    // Initialise trace objects
                    RefTrace.Wrap( ta->Reference[Strand].Trace, false );
                    InputTrace.Wrap( ta->Input.Trace, false );
                    State = STATE_VALIDATE_INPUT;
                    break;



                case STATE_VALIDATE_INPUT:
                    // Check input values
                    if( TraceAlignValidateInput(ta) )
                        State = STATE_EXIT;
                    else
                        State = STATE_UPDATE_CACHE;
                    break;



                case STATE_UPDATE_CACHE:
                    // Initialise cache if not done
                    if( !ta->Cache )
                        ta->Cache = static_cast<void*>( new TraceAlignCache );
                    Cache = static_cast<TraceAlignCache*>(ta->Cache);
                    if( Cache->AlignmentMatrix.Rows() <= 0 )
                        Cache->CreateAlignmentMatrix( QLEVELS+QLOWER+1, QLEVELS, QLOWER );
                    // If new wildtype has been supplied
                    if( ta->Reference[Strand].New )
                    {
                        // Preprocess wildtype and cache it
                        Cache->RefData[Strand].Flush();
                        Cache->RefData[Strand].PreprocessTrace( RefTrace, true );
                        ta->Reference[Strand].New = 0;

                    }
                    RefData = &Cache->RefData[Strand];
                    State    = STATE_PREPROCESS_INPUT;
                    break;



                case STATE_PREPROCESS_INPUT:
                    // Preprocess input trace
                    InputData.PreprocessTrace( InputTrace );
                    State = STATE_SEQUENCE_ALIGN;
                    break;



                case STATE_SEQUENCE_ALIGN: {
                    // Align sequences first so we can determine which parts of the
                    // trace to align - trace alignment takes a long time using DP!
                    char* wseq = &(RefTrace.Raw()->base[ ta->Reference[Strand].ClipL ]);
                    char* iseq = &(InputTrace.Raw()->base[ ta->Input.ClipL ]);
                    int   wlen = ta->Reference[Strand].ClipR - ta->Reference[Strand].ClipL - 1;
                    int   ilen = ta->Input.ClipR - ta->Input.ClipL - 1;
                    Aligner.InputSequence( 0, wseq, wlen );
                    Aligner.InputSequence( 1, iseq, ilen );
                    Aligner.EdgeScore( Alignment::EDGE_SCORE_RIGHT );
                    Aligner.Execute( Alignment::ALGORITHM_NORMAL );
                    #ifdef VERBOSE_DEBUG
                    Aligner.DumpToFile( "sequence.align.txt" );
                    #endif
                    RefSeq.Create( Aligner.OutputSequence(0), Aligner.OutputSequenceLength(0) );
                    InputSeq.Create( Aligner.OutputSequence(1), Aligner.OutputSequenceLength(1) );
                    State = STATE_SEQUENCE_OVERLAP;
                    break; }



                case STATE_SEQUENCE_OVERLAP:
                    // Checks that there is sufficient sequence overlap for an alignment
                    // operation to make any logical sense!
                    AlignedSeqOverlap[L] = Aligner.OutputSequenceLeftOverlap(0);
                    AlignedSeqOverlap[R] = Aligner.OutputSequenceRightOverlap(0);
                    if( AlignedSeqOverlap[R] - AlignedSeqOverlap[L] <= MIN_OVERLAP )
                    {
                        std::sprintf( ta->ResultString, "Insufficient sequence overlap to compute "
                                     "trace alignment for %s.\n", ta->Input.Trace->trace_name );
                        ta->ResultCode = MUTLIB_RESULT_INSUFFICIENT_OVERLAP;
                        State = STATE_EXIT;
                        break;

                    }
                    State = STATE_SEQUENCE_MAP;
                    break;



                case STATE_SEQUENCE_MAP:
                    // Maps the sequence alignment start/end points back onto the original
                    // trace. Two mappings are made, one for bases, the other for samples.
                    RefOverlapBases[L]   = RefSeq.GetOriginalPosition( AlignedSeqOverlap[L] );
                    RefOverlapBases[R]   = RefSeq.GetOriginalPosition( AlignedSeqOverlap[R] );
                    InputOverlapBases[L]  = InputSeq.GetOriginalPosition( AlignedSeqOverlap[L] );
                    InputOverlapBases[R]  = InputSeq.GetOriginalPosition( AlignedSeqOverlap[R] );
                    RefOverlapBases[L]  += ta->Reference[Strand].ClipL;
                    RefOverlapBases[R]  += ta->Reference[Strand].ClipL;
                    InputOverlapBases[L] += ta->Input.ClipL;
                    InputOverlapBases[R] += ta->Input.ClipL;
                    // At this point we have the original sequence zero-based base numbers
                    // for the overlapping regions, now we get the corresponding sample points
                    RefOverlapSamples[L]  = RefTrace.BasePosition( RefOverlapBases[L] );
                    RefOverlapSamples[R]  = RefTrace.BasePosition( RefOverlapBases[R] );
                    InputOverlapSamples[L] = InputTrace.BasePosition( InputOverlapBases[L] );
                    InputOverlapSamples[R] = InputTrace.BasePosition( InputOverlapBases[R] );
                    State = STATE_QUANTISE_ENVELOPE;
                    break;



                case STATE_QUANTISE_ENVELOPE: {
                    // Quantise the trace envelopes of the overlapping regions. Note
                    // that the quantised envelope indices will not be relative to the
                    // orignal trace unless you add xxxOverlapSamples[L] to them.
                    int QUPPER[2];
                    RefData->Envelope().Range( RefOverlapSamples[L], RefOverlapSamples[R] );
                    InputData.Envelope().Range( InputOverlapSamples[L], InputOverlapSamples[R] );
                    #ifdef VERBOSE_DEBUG
                    RefData->Envelope().SaveAs( "qenvelope.txt" );
                    InputData.Envelope().SaveAs( "qenvelope.txt", true );
                    #endif
                    QUPPER[0] = RefData->Envelope().Max();
                    QUPPER[1] = InputData.Envelope().Max();
                    QUPPER[0] = std::max( QUPPER[0], QUPPER[1] );
                    TraceAlignQuantiseEnvelope( RefData->Envelope(), RefEnvelope, QLEVELS, QLOWER, QUPPER[0] );
                    TraceAlignQuantiseEnvelope( InputData.Envelope(), InputEnvelope, QLEVELS, QLOWER, QUPPER[0] );
                    State = STATE_ESTIMATE_BANDSIZE;
                    break; }



                case STATE_ESTIMATE_BANDSIZE: {
                    // Estimate an optimal bandsize for the alignment routine to ensure
                    // it runs as fast as possible. We do this by calculating the maximum
                    // absolute difference between the sample vectors, stepping through
                    // one base at a time in the aligned sequence and keeping track of
                    // the maximum difference in samples. Note that the bandsize specified
                    // here is half the width of actual band used in the DP algorithm.
                    int diff;
                    int w, i;
                    int max_diff = 0;
                    const int MIN_BANDSIZE = 10;
                    for( int n=AlignedSeqOverlap[L]; n<=AlignedSeqOverlap[R]; n++ )
                    {
                       w = RefSeq.GetOriginalPosition(n) + ta->Reference[Strand].ClipL;
                       i = InputSeq.GetOriginalPosition(n) + ta->Input.ClipL;
                       w = RefTrace.BasePosition(w) - RefOverlapSamples[L];
                       i = InputTrace.BasePosition(i) - InputOverlapSamples[L];
                       diff = (w >= i) ? w-i : i-w;
                       if( diff > max_diff )
                           max_diff = diff;
                    }
                    BandSize = max_diff + MIN_BANDSIZE;
                    #ifdef VERBOSE_DEBUG
                    std::printf("Alignment Bandsize = %d+%d\n", max_diff, MIN_BANDSIZE );
                    #endif
                    State = STATE_ENVELOPE_ALIGN;
                    break; }



                case STATE_ENVELOPE_ALIGN: {
                    // Align the quantised envelopes of the overlapping region.
                    Aligner.PadSymbol( QPAD_SYMBOL );
                    Aligner.GapPenalty( 0, QLEVELS );
                    Aligner.EdgeScore( Alignment::EDGE_SCORE_RIGHT );
                    Aligner.Matrix( Cache->AlignmentMatrix.Raw(), Cache->AlignmentMatrix.Rows(), false );
                    Aligner.BandSize( BandSize );
                    Aligner.InputSequence( 0, RefEnvelope.Raw(), RefEnvelope.Length() );
                    Aligner.InputSequence( 1, InputEnvelope.Raw(), InputEnvelope.Length() );
                    Aligner.Execute( Alignment::ALGORITHM_NORMAL );
                    RefEnvelopeAligned.Wrap( Aligner.OutputSequence(0), Aligner.OutputSequenceLength(0) );
                    InputEnvelopeAligned.Wrap( Aligner.OutputSequence(1), Aligner.OutputSequenceLength(1) );
                    #ifdef VERBOSE_DEBUG
                    char FileName[256];
                    std::printf("Alignment Score = %f\n", Aligner.OutputScore() );
                    std::sprintf( FileName, "qenvelope.align.txt" );
                    Aligner.DumpToFile( FileName, true );
                    #endif
                    State = STATE_TRACE_ALIGN;
                    break; }



                case STATE_TRACE_ALIGN: {
                    // Align the original traces using the envelope alignment algorithm
                    // output, then insert the bases.
                    Trace RefInterpolated;
                    Trace InputInterpolated;
                    RefInterpolated.Create( RefEnvelopeAligned.Length(), RefOverlapBases[R]-RefOverlapBases[L]+1, RefTrace.Name() );
                    InputInterpolated.Create( InputEnvelopeAligned.Length(), InputOverlapBases[R]-InputOverlapBases[L]+1, InputTrace.Name() );
                    TraceAlignInterpolate( QPAD_SYMBOL, RefEnvelopeAligned, RefTrace, RefOverlapSamples[L], RefInterpolated );
                    TraceAlignInterpolate( QPAD_SYMBOL, InputEnvelopeAligned, InputTrace, InputOverlapSamples[L], InputInterpolated );
                    TraceAlignInsertBases( QPAD_SYMBOL, RefEnvelopeAligned, RefTrace, RefInterpolated, RefOverlapBases );
                    TraceAlignInsertBases( QPAD_SYMBOL, InputEnvelopeAligned, InputTrace, InputInterpolated, InputOverlapBases );
                    ta->Alignment[MUTLIB_INPUT].Trace           = InputInterpolated.Raw();
                    ta->Alignment[MUTLIB_INPUT].ClipL           = InputOverlapBases[L];
                    ta->Alignment[MUTLIB_INPUT].ClipR           = InputOverlapBases[R];
                    ta->Alignment[MUTLIB_INPUT_REFERENCE].Trace = RefInterpolated.Raw();
                    ta->Alignment[MUTLIB_INPUT_REFERENCE].ClipL = RefOverlapBases[L];
                    ta->Alignment[MUTLIB_INPUT_REFERENCE].ClipR = RefOverlapBases[R];
                    RefInterpolated.AutoDestroy( false );
                    InputInterpolated.AutoDestroy( false );
                    #ifdef VERBOSE_DEBUG
                    RefInterpolated.SaveAs( "alignment1.ztr" );
                    InputInterpolated.SaveAs( "alignment2.ztr" );
                    #endif
                    State = STATE_EXIT;
                    break; }



                case STATE_EXIT:
                    // Quit
                    Done = true;
                    break;
            }
        }
    }
    catch( std::bad_alloc& )
    {
        // Memory allocation error
        ta->ResultCode = MUTLIB_RESULT_OUT_OF_MEMORY;
        std::strcpy( ta->ResultString, "Not enough memory available to complete the operation.\n" );
    }
    catch(...)
    {
        // Unknown exceptions
        ta->ResultCode = MUTLIB_RESULT_UNEXPECTED_EXCEPTION;
        std::strcpy( ta->ResultString, "An unexpected fatal exception has occurred, please "
                     "report the details to staden-package@mrc-lmb.cam.ac.uk.\n" );
    }



    // Exit
    return ta->ResultCode;
}