File: BCPairwiseAlignment.m

package info (click to toggle)
biococoa 2.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,184 kB
  • sloc: objc: 21,348; makefile: 17; ansic: 4
file content (192 lines) | stat: -rw-r--r-- 6,259 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
//
//  BCPairwiseAlignment.m
//  BioCocoa
//
//  Created by Philipp Seibel on 10.03.05.
//  Copyright (c) 2003-2009 The BioCocoa Project.
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//  1. Redistributions of source code must retain the above copyright
//  notice, this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright
//  notice, this list of conditions and the following disclaimer in the
//  documentation and/or other materials provided with the distribution.
//  3. The name of the author may not be used to endorse or promote products
//  derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
//  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
//  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
//  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
//  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import "BCSequenceAlignment.h"
#import "BCStringDefinitions.h"

#define DIAG (idxB - 1) * lenA + (idxA - 1)
#define LEFT idxB * lenA + (idxA - 1)
#define UP  (idxB - 1) * lenA + idxA


//Pointer for backtracking matrix
typedef enum {
    kNone = 0,
	kDiagonal,
    kLeft,
    kUp
} Pointers;

@interface BCSequenceAlignment ( PairwiseAlignment_Private )



@end

@implementation BCSequenceAlignment ( PairwiseAlignment )

+ (BCSequenceAlignment *)needlemanWunschAlignmentWithSequences:(NSArray *)theSequences properties:(NSDictionary *)properties {
	
	
	// obtaining substitution matrix
	BCScoreMatrix *substitutionMatrix = [properties objectForKey: BCSubstitutionMatrixProperty];
	
	// sequences 
	BCSequence *sequenceA = [theSequences objectAtIndex:0];
	BCSequence *sequenceB = [theSequences objectAtIndex:1];
	
	// converting sequences to c char pointer
	const char * seqA = [[sequenceA sequenceString] UTF8String];
	const char * seqB = [[sequenceB sequenceString] UTF8String];
	
	// currently only default gap penalties possible
	int gapCosts = [(NSNumber *)[properties objectForKey: BCDefaultGapPenaltyProperty] intValue];
	
	
	unsigned int lenA = [sequenceA length];
	unsigned int lenB = [sequenceB length];
	
	// initialize matrices
	int *backtracking = (int *)malloc( sizeof( int ) * ( lenA + 1 ) * ( lenB + 1 ) );
	int *dynMatrix    = (int *)malloc( sizeof( int ) * ( lenA + 1 ) * ( lenB + 1 ) );
	
	unsigned int idxA;
	unsigned int idxB;
	
	// upper left corner 
	dynMatrix[ 0 ] = [substitutionMatrix substituteChar:seqA[0] forChar:seqB[0]];
	backtracking[ 0 ] = kNone;
	
	// initialize first column & row
	for ( idxA = 1; idxA <= lenA; idxA++ ) {
	    backtracking[ idxA ] = kLeft; 
		dynMatrix[ idxA ] = idxA * gapCosts;
	}
	
	for ( idxB = 1; idxB <= lenB; idxB++ ) { 
		backtracking[ idxB * lenA ] = kUp;
		dynMatrix[ idxB * lenA ] = idxB * gapCosts;
	}
	
	
	// dynamic programming -- filling up the matrix
	for ( idxA = 1; idxA <= lenA; idxA++ ) {
		for ( idxB = 1; idxB <= lenB; idxB++ ) {
			unsigned int currPos = idxB * lenA + idxA;
		
			int substitutionScore = [substitutionMatrix substituteChar:seqA[idxA - 1] forChar:seqB[idxB - 1]];
			int diagScore = dynMatrix[ DIAG ] + substitutionScore;
			int rightScore = dynMatrix[ LEFT ] + gapCosts;
			int downScore = dynMatrix[ UP ] + gapCosts;
			
			// looking for the direction with the highest score
			// storing direction in backtracking matrix
			if ( diagScore >= rightScore ) {
				if ( diagScore > downScore ) {
				    backtracking[ currPos ] = kDiagonal;
					dynMatrix[ currPos ] = diagScore;
				}
				else {
					backtracking[ currPos ] = kUp;
					dynMatrix[ currPos ] = downScore;
				}
			}
			else {
				if ( rightScore > downScore ) {
					backtracking[ currPos ] = kLeft;
					dynMatrix[ currPos ] = rightScore;
				}
				else {
					backtracking[ currPos ] = kUp;
					dynMatrix[ currPos ] = downScore;
				}
			}
		}
	}
	
	// preparing for the walk back through the backtracking matrix
	int i = lenA;
	int j = lenB;
	int k = 0;
	char *a = ( char * ) malloc( (lenA + lenB) * sizeof(char));
	char *b = ( char * ) malloc( (lenA + lenB) * sizeof(char));
	
	// walk back and build up the alignment
	while ( backtracking[j * lenA + i ] != kNone ) {
		switch(backtracking[j * lenA + i ]){
			case kDiagonal :
				a[k] = seqA[i - 1];
				b[k] = seqB[j - 1];
				i--;
				j--;
				k++;
				break;
				
			case kLeft :
				a[k] = seqA[i - 1];
				b[k] = '-';
				i--;
				k++;
				break;
				
			case kUp :
				a[k] = '-';
				b[k] = seqB[j - 1];
				j--;
				k++;
				break;
		}
	}
	
	// get the reverse versions ( form tail > head to head > tail )
	char *an = (char *)malloc( sizeof(char) * k );
	char *bn = (char *)malloc( sizeof(char) * k );
	
	for(i=k-1;i>=0;i--) an[k-i-1] = a[i];
	for(j=k-1;j>=0;j--) bn[k-j-1] = b[j];

	// converting back to objc class
	// to prevent a compiler warning, the usingType argument has been removed - this needs to be reviewed
	//	BCSequence *alnA = [BCSequence sequenceWithString:[NSString stringWithCString:an length:k] usingType:BCSequenceTypeOther];
	//	BCSequence *alnB = [BCSequence sequenceWithString:[NSString stringWithCString:bn length:k] usingType:BCSequenceTypeOther];
	BCSequence *alnA = [BCSequence sequenceWithString:[NSString stringWithCString:an length:k]];
	BCSequence *alnB = [BCSequence sequenceWithString:[NSString stringWithCString:bn length:k]];
	
	//building up the alignment 
	return [[[BCSequenceAlignment alloc] initWithSequenceArray:[NSArray arrayWithObjects:alnA,alnB,nil]] autorelease];
}


+ (BCSequenceAlignment *)smithWatermanAlignmentWithSequences:(NSArray *)theSequences properties:(NSDictionary *)properties {
	return nil;
}


@end