-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerneedle.c
648 lines (539 loc) · 21.7 KB
/
powerneedle.c
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* @source powerneedle application
**
** Application for large number of pairwise alignments without
** excessive filesystem traffic.
** @author Copyright (C) Mayo Röttger ([email protected])
**
** This application is based on:
**
** True Needleman-Wunsch global alignment
** @author Copyright (C) Alan Bleasby ([email protected])
** @@
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
#include "emboss.h"
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* The folowing HEAPSORT code is derived from software contributed to
* Berkeley by Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* $DragonFly: src/lib/libc/stdlib/heapsort.c,v 1.5 2005/11/20 12:37:48 swildner Exp $
*
* @(#)heapsort.c 8.1 (Berkeley) 6/4/93
*/
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
/*
* Swap two areas of size number of bytes. Although qsort(3) permits random
* blocks of memory to be sorted, sorting pointers is almost certainly the
* common case (and, were it not, could easily be made so). Regardless, it
* isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
* arithmetic gets lost in the time required for comparison function calls.
*/
#define SWAP(a, b, count, size, tmp) { \
count = size; \
do { \
tmp = *a; \
*a++ = *b; \
*b++ = tmp; \
} while (--count); \
}
/* Copy one block of size size to another. */
#define COPY(a, b, count, size, tmp1, tmp2) { \
count = size; \
tmp1 = a; \
tmp2 = b; \
do { \
*tmp1++ = *tmp2++; \
} while (--count); \
}
/*
* Build the list into a heap, where a heap is defined such that for
* the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
*
* There two cases. If j == nmemb, select largest of Ki and Kj. If
* j < nmemb, select largest of Ki, Kj and Kj+1.
*/
#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
par_i = child_i) { \
child = base + child_i * size; \
if (child_i < nmemb && compar(child, child + size) < 0) { \
child += size; \
++child_i; \
} \
par = base + par_i * size; \
if (compar(child, par) <= 0) \
break; \
SWAP(par, child, count, size, tmp); \
} \
}
/*
* Select the top of the heap and 'heapify'. Since by far the most expensive
* action is the call to the compar function, a considerable optimization
* in the average case can be achieved due to the fact that k, the displaced
* elememt, is ususally quite small, so it would be preferable to first
* heapify, always maintaining the invariant that the larger child is copied
* over its parent's record.
*
* Then, starting from the *bottom* of the heap, finding k's correct place,
* again maintianing the invariant. As a result of the invariant no element
* is 'lost' when k is assigned its correct place in the heap.
*
* The time savings from this optimization are on the order of 15-20% for the
* average case. See Knuth, Vol. 3, page 158, problem 18.
*
* XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
*/
#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
child = base + child_i * size; \
if (child_i < nmemb && compar(child, child + size) < 0) { \
child += size; \
++child_i; \
} \
par = base + par_i * size; \
COPY(par, child, count, size, tmp1, tmp2); \
} \
for (;;) { \
child_i = par_i; \
par_i = child_i / 2; \
child = base + child_i * size; \
par = base + par_i * size; \
if (child_i == 1 || compar(k, par) < 0) { \
COPY(child, k, count, size, tmp1, tmp2); \
break; \
} \
COPY(child, par, count, size, tmp1, tmp2); \
} \
}
/*
* Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
* and worst. While heapsort is faster than the worst case of quicksort,
* the BSD quicksort does median selection so that the chance of finding
* a data set that will trigger the worst case is nonexistent. Heapsort's
* only advantage over quicksort is that it requires little additional memory.
*/
int
heapsort(void *vbase, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
int cnt, i, j, l;
char tmp, *tmp1, *tmp2;
char *base, *k, *p, *t;
if (nmemb <= 1)
return (0);
if (!size) {
errno = EINVAL;
return (-1);
}
if ((k = malloc(size)) == NULL)
return (-1);
/*
* Items are numbered from 1 to nmemb, so offset from size bytes
* below the starting address.
*/
base = (char *)vbase - size;
for (l = nmemb / 2 + 1; --l;)
CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
/*
* For each element of the heap, save the largest element into its
* final slot, save the displaced element (k), then recreate the
* heap.
*/
while (nmemb > 1) {
COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
--nmemb;
SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
}
free(k);
return (0);
}
/* @func compare *************************************************************
**
** Compares two sequence names.
**
** @param [r] el1 [AjPSeq *] Pointer to sequence1
** @param [r] el2 [AjPSeq *] Pointer to sequence2
** @return [int] < 0, > 0 or 0 if name of sequence1 comes alphabetically before
** name of sequence2, after sequence2 or if the two sequences have identical
** names respectively.
** @@
******************************************************************************/
int compare(AjPSeq *el1, AjPSeq *el2)
{
/* compare the two sequence names */
return ajStrCmpS(ajSeqGetNameS(*el1),ajSeqGetNameS(*el2));
}
/* @func findRec *************************************************************
**
** Recursively finds a sequence by name in an alphabetically sorted sequence
** database and returns it.
** Because this is a pointer to the real internal sequence
** the caller must take care not to change the data in any way.
**
** @param [r] database [AjPSeqset] Sequence database
** @param [r] name [char *] Sequence name to search for
** @param [r] li [ajint] left boundary in database
** @param [r] re [ajint] right boundary in database
** @return [const AjPSeq] Pointer to the sequence in the database or NULL if
** sequence was not found
** @@
******************************************************************************/
const AjPSeq findRec(AjPSeqset database,char *name,ajint li,ajint re)
{
const AjPSeq seq;
ajint mid;
/* get sequence in the middle of boundaries */
mid=(li+re)/2;
seq=ajSeqsetGetseqSeq(database,mid);
if (li<=re)
{
if (ajStrCmpC(ajSeqGetNameS(seq),name)>0) return findRec(database,name,li,mid-1); /* search left */
else if (ajStrCmpC(ajSeqGetNameS(seq),name)<0) return findRec(database,name,mid+1,re); /* search right */
else return seq; /* sequence found */
}
else return NULL; /* sequence not found */
}
/* @func find ****************************************************************
**
** Finds a sequence by name in a alphabetically sorted sequence database and
** returns it.
** Because this is a pointer to the real internal sequence
** the caller must take care not to change the data in any way.
**
** @param [r] database [AjPSeqset] Sequence database
** @param [r] name [char *] Sequence name to search for
** @return [const AjPSeq] Pointer to the sequence in the database or NULL if
** sequence was not found
** @@
*****************************************************************************/
const AjPSeq find(AjPSeqset database,char *name)
{
return findRec(database,name,0,ajSeqsetGetSize(database)-1);
}
/* @prog powerneedle **********************************************************
**
** Program for large number of pairwise Needleman-Wunsch global alignments
** without excessive filesystem traffic.
**
******************************************************************************/
int main(int argc, char **argv)
{
AjPAlign align = NULL;
AjPSeqset database =NULL;
const AjPSeq finda = NULL;
const AjPSeq findb = NULL;
AjPSeq a = NULL;
AjPSeq b = NULL;
AjPStr m = NULL;
AjPStr n = NULL;
AjPStr ss = NULL;
AjPStr fm = NULL;
AjPStr fn = NULL;
AjPFile identities = NULL;
AjPFile pairs = NULL;
AjBool error = ajFalse;
AjBool dobrief = ajTrue;
AjBool lastseq = ajFalse;
const char *p = NULL;
const char *q = NULL;
char line[1024] = "";
char seq1[512] = "";
char seq2[512] = "";
char c;
ajint alignmentLength = 0;
ajint start1 = 0;
ajint start2 = 0;
ajint end1 = 0;
ajint end2 = 0;
ajint k = 0;
ajint *compass;
ajuint mgaps = 0;
ajuint ngaps = 0;
ajuint olen;
ajuint lena;
ajuint lenb;
ajuint i = 0;
ajuint j = 1;
ajuint linecount = 0;
ajulong maxarr = 1000; /* arbitrary. realloc'd if needed */
ajulong len;
float *path;
float **sub;
float gapopen;
float gapextend;
float score;
float id = 0.;
float sim = 0.;
float idx = 0.;
float simx = 0.;
AjPMatrixf matrix;
AjPSeqCvt cvt = 0;
size_t stlen;
/* Init everything */
embInit("powerneedle", argc, argv);
ajFmtPrint("Database has been read.\n");
matrix = ajAcdGetMatrixf("datafile");
database = ajAcdGetSeqset("database");
gapopen = ajAcdGetFloat("gapopen");
gapextend = ajAcdGetFloat("gapextend");
dobrief = ajAcdGetBoolean("brief");
align = ajAcdGetAlign("alignment");
identities = ajAcdGetOutfile("identities");
pairs = ajAcdGetInfile("pairs");
gapopen = ajRoundFloat(gapopen, 8);
gapextend = ajRoundFloat(gapextend, 8);
AJCNEW(path, maxarr);
AJCNEW(compass, maxarr);
m = ajStrNew();
n = ajStrNew();
ss = ajStrNew();
sub = ajMatrixfGetMatrix(matrix);
cvt = ajMatrixfGetCvt(matrix);
/* Output Header for pairwise identity file */
ajFmtPrintF(identities,"Sequence1 Sequence2 Identity Similarity Score Length #id #sim\n");
if (pairs!=NULL) /* if pairs file present */
{
/* Alphabetically sort database. This changes the sequence pointers of the database sequence set! */
ajFmtPrint("Sorting database...\n");
heapsort(database->Seq,ajSeqsetGetSize(database),sizeof(AjPSeq),(int(*)(const void*, const void*)) compare);
}
ajFmtPrint("Doing pairwise alignments...\n");
while (!lastseq) {
error=ajFalse;
/* Get next sequence pair */
if (pairs!=NULL) /* Align pairs in pairsfile */
{
/* read next line from file */
line[0]='\0';
i=0;
while ( (c = fgetc(ajFileGetFileptr(pairs)) ) != EOF && c != '\n') line[i++]=c;
line[i]='\0';
linecount++;
if (feof(ajFileGetFileptr(pairs))) lastseq=ajTrue;
if (strcmp(line,"")==0) error=ajTrue; /* not process empty line then */
else
{
/* load first string */
i=0;
j=0;
while ((c=line[i])!='\0' && line[i]!='\t' && line[i]!=' ')
{
seq1[j++]=c;
i++;
}
seq1[j]='\0';
/* load second string */
i++;
j=0;
if (c!='\0') {
while ((c=line[i])!='\0' && line[i]!='\t' && line[i]!=' ')
{
seq2[j++]=c;
i++;
}
}
seq2[j]='\0';
if (strcmp(seq1,"")==0)
{
ajFmtPrint("Error! Could not read sequence 1 from pairs file line %d! Sequence 2 is '%s'. Skipping this pair.\n",linecount,seq2);
error=ajTrue;
}
if (strcmp(seq2,"")==0)
{
ajFmtPrint("Error! Could not read sequence 2 from pairs file line %d! Sequence 1 is '%s'. Skipping this pair.\n",linecount,seq1);
error=ajTrue;
}
if (!error)
{
/* find sequences in alphabetically sorted sequence set */
if ((finda=find(database,seq1))==NULL)
{
ajFmtPrint("Error! Could not find '%s' in database! Skipping pair '%s'-'%s' from pairs file line %d.\n",seq1,seq1,seq2,linecount);
error=ajTrue;
}
if ((findb=find(database,seq2))==NULL)
{
ajFmtPrint("Error! Could not find '%s' in database! Skipping pair '%s'-'%s' from pairs file line %d.\n",seq2,seq1,seq2,linecount);
error=ajTrue;
}
if (!error)
{
a=ajSeqNewSeq(finda);
b=ajSeqNewSeq(findb);
}
}
}
}
else /* Align all sequence pairs */
{
/* take current sequence pair i,j */
a=ajSeqNewSeq(ajSeqsetGetseqSeq(database,i));
b=ajSeqNewSeq(ajSeqsetGetseqSeq(database,j++));
/* update indices */
if (j==ajSeqsetGetSize(database))
{
if (i==j-2) lastseq=ajTrue;
else j=(++i)+1;
}
}
/* Do Needleman & Wunsch pairwise alignment */
if (!error)
{
ajSeqTrim(a);
ajSeqTrim(b);
lena = ajSeqGetLen(a);
lenb = ajSeqGetLen(b);
mgaps=0;
ngaps=0;
if(lenb > (ULONG_MAX/(ajulong)(lena+1)))
{
/* ajFatal("Sequences too big. Try 'stretcher' or 'supermatcher'"); */
ajFmtPrint("Error in aligning of '%s'-'%s', pairs file line %d: Sequences too big. Try 'stretcher' or 'supermatcher' for this sequence pair!\n",ajSeqGetNameC(a),ajSeqGetNameC(b),linecount);
error=ajTrue;
}
else
{
len = lena*lenb;
if(len>maxarr)
{
stlen = (size_t) len;
AJCRESIZETRY(path,stlen);
if(!path)
{
/* ajDie("Sequences too big. Try 'stretcher'"); */
ajFmtPrint("Error in aligning '%s'-'%s', pairs file line %d: Sequences too big. Try 'stretcher' for this sequence pair!\n",ajSeqGetNameC(a),ajSeqGetNameC(b),linecount);
error=ajTrue;
}
AJCRESIZETRY(compass,stlen);
if(!compass)
{
/* ajDie("Sequences too big. Try 'stretcher'"); */
ajFmtPrint("Error in aligning '%s'-'%s': Sequences too big. Try 'stretcher' for this sequence pair!\n",ajSeqGetNameC(a),ajSeqGetNameC(b),linecount);
error=ajTrue;
}
maxarr=len;
}
}
if (!error)
{
p = ajSeqGetSeqC(a);
q = ajSeqGetSeqC(b);
ajStrAssignC(&m,"");
ajStrAssignC(&n,"");
score=embAlignPathCalc(p,q,lena,lenb,gapopen,gapextend,path,sub,cvt,
compass,ajFalse);
/* score = embAlignScoreNWMatrix(path,a,b,sub,cvt,lena,lenb,
gapopen,compass,
gapextend,&start1,&start2); */
embAlignWalkNWMatrix(path,a,b,&m,&n,lena,lenb,&start1,&start2,gapopen,
gapextend,compass);
if (!dobrief) /* only write alignment reports if brief option is not set */
{
embAlignReportGlobal(align, a, b ,m, n,
start1, start2,
gapopen, gapextend,
score, matrix,
ajSeqGetOffset(a), ajSeqGetOffset(b));
ajAlignWrite(align);
ajAlignReset(align);
}
/* Calculate identity and similarity */
embAlignCalcSimilarity(m,n,sub,cvt,lena,lenb,&id,&sim,&idx,
&simx);
/* Count gaps in alignment string */
ajStrAssignS(&fm,m);
ajStrAssignS(&fn,n);
ajStrFmtUpper(&fm);
ajStrFmtUpper(&fn);
p = ajStrGetPtr(fm);
q = ajStrGetPtr(fn);
olen = (ajint) strlen(p);
for(k=0;k<olen;++k)
{
if(p[k] =='.') ++mgaps;
if(q[k] =='.') ++ngaps;
}
/* Calculate alignment length */
end1=start2+ajSeqGetLen(a)+mgaps;
end2=start1+ajSeqGetLen(b)+ngaps;
alignmentLength=end1>end2?end1:end2;
/* Adjust percent identity and similarity to the basis of alignment length */
id=idx*(ajSeqGetLen(a)>ajSeqGetLen(b)?ajSeqGetLen(a):ajSeqGetLen(b))/alignmentLength;
sim=simx*(ajSeqGetLen(a)>ajSeqGetLen(b)?ajSeqGetLen(a):ajSeqGetLen(b))/alignmentLength;
/* Output pairwise identities */
ajFmtPrintF(identities,"%s %s %.1f %.1f %.1f %d %.0f %.0f\n",ajSeqGetNameC(a),ajSeqGetNameC(b),id,sim,score,alignmentLength,id*alignmentLength/100,sim*alignmentLength/100);
}
else
{
/* Output pairwise '-' for this sequence pair */
ajFmtPrintF(identities,"%s %s - - - - - -\n",ajSeqGetNameC(a),ajSeqGetNameC(b));
}
}
/* destroy sequence copies */
ajSeqDel(&a);
ajSeqDel(&b);
}
/* Destroy everything */
ajSeqsetDel(&database);
ajAlignClose(align);
ajAlignDel(&align);
ajFileClose(&identities);
if (pairs!=NULL) ajFileClose(&pairs);
AJFREE(compass);
AJFREE(path);
ajStrDel(&n);
ajStrDel(&m);
ajStrDel(&ss);
ajStrDel(&fm);
ajStrDel(&fn);
ajFmtPrint("Done.\n");
embExit();
return 0;
}