Debian Codegraph @

NDist/NDist.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 August 2012 Code has been optimised for speed by Tessella, funded by Unilever.
3 See comments in code for details of optimisation changes.
4 Output has been verified to be unaffected by these changes.
5 */
6
7 /**System includes****/
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <float.h>
12 #include <assert.h>
13 #include <math.h>
14 #include <mpi.h>
15
16 #include "NDist.h"
17
18 /*global constants*/
19 static char *usage[] = {"NDist - pairwise Needleman-Wunsch sequence distance matrix from a fasta file\n",
20 "-in string fata file name\n",
21 "Options:\n",
22 "-i output identifiers\n"};
23
24 static int nLines = 4;
25
26 static char szSequence[] = "acgturynACGTURYN-";
27
28 void broadcastData(t_Data *ptData)
29 {
30 int i = 0;
31
32 MPI_Bcast((void *) &ptData->nSeq, 1, MPI_INT, 0, MPI_COMM_WORLD);
33
34 MPI_Bcast((void *) &ptData->nMaxLen, 1, MPI_INT, 0, MPI_COMM_WORLD);
35
36 MPI_Bcast((void *) ptData->anLen, ptData->nSeq, MPI_INT, 0, MPI_COMM_WORLD);
37
38 for(i = 0; i < ptData->nMaxLen;i++){
39 MPI_Bcast((void *) &ptData->acSequences[i*ptData->nSeq], ptData->nSeq, MPI_CHAR, 0, MPI_COMM_WORLD);
40 }
41 }
42
43 void receiveData(t_Data *ptData)
44 {
45 int i = 0;
46 MPI_Bcast((void *) &ptData->nSeq, 1, MPI_INT, 0, MPI_COMM_WORLD);
47
48 MPI_Bcast((void *) &ptData->nMaxLen, 1, MPI_INT, 0, MPI_COMM_WORLD);
49
50 ptData->anLen = (int *) malloc(ptData->nSeq*sizeof(int));
51 if(!ptData->anLen)
52 goto memoryError;
53
54 MPI_Bcast((void *) ptData->anLen, ptData->nSeq, MPI_INT, 0, MPI_COMM_WORLD);
55
56 ptData->acSequences = (char *) malloc(ptData->nSeq*ptData->nMaxLen*sizeof(char));
57 if(!ptData->acSequences)
58 goto memoryError;
59
60 for(i = 0; i < ptData->nMaxLen; i++){
61 MPI_Bcast((void *) &ptData->acSequences[i*ptData->nSeq], ptData->nSeq, MPI_CHAR, 0, MPI_COMM_WORLD);
62 }
63 return;
64 memoryError:
65 fprintf(stderr, "Failed allocating memory in receiveData\n");
66 fflush(stderr);
67 exit(EXIT_FAILURE);
68 }
69
70
71 int main(int argc, char* argv[]){
72 int a = 0, nN = 0, nM = 0;
73 unsigned long i = 0, j = 0;
74 t_Params tParams;
75 t_Data tData;
76 double *adDists = NULL;
77 int numtasks, rank, rc;
78 unsigned long ulNumTasks, ulOffset, ulA, ulA0, ulSize, ulCount, ulStart, ulFinish;
79 int nTag = 1;
80 unsigned long ulPackets = 0, ulPacketSize = 0, ulPacketCurr = 0;
81 double **aadFMatrix = NULL;
82 int **aanMoves = NULL;
83 MPI_Status status;
84
85 fflush(stdout);
86
87 rc = MPI_Init(&argc,&argv);
88 if (rc != MPI_SUCCESS) {
89 printf ("Error starting MPI program. Terminating.\n");
90 MPI_Abort(MPI_COMM_WORLD, rc);
91 }
92
93 MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
94 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
95
96 /*get command line params*/
97 getCommandLineParams(&tParams, argc, argv);
98
99 fflush(stdout);
100
101 if(rank == 0){ /*head node reads data*/
102
103 //fprintf(stdout, "%d read data\n",rank); fflush(stdout);
104
105 readData(&tData, &tParams);
106
107 //fprintf(stdout, "%d broadcast data\n",rank); fflush(stdout);
108
109 /*sends it out to other nodes*/
110 broadcastData(&tData);
111
112 //fprintf(stdout, "%d start calculations\n",rank); fflush(stdout);
113
114 /*do my bit*/
115 nN = tData.nSeq; nM = tData.nMaxLen;
116
117 /*allocate memory for whole dist matrix*/
118 ulSize = ((unsigned long) nN*( (unsigned long) nN - 1))/2;
119
120 ulNumTasks = (unsigned long) numtasks;
121 ulA = (unsigned long) (floor(ulSize / ulNumTasks));
122
123 ulA0 = ulA + (ulSize % ulNumTasks);
124
125 //fprintf(stderr, "%d allocate memory %lu %lu\n",rank, ulSize, ulA); fflush(stderr);
126
127 adDists = (double *) malloc(ulA*sizeof(double));
128 if(!adDists)
129 goto memoryError;
130
131 printf("%d\n",nN);
132 /*output identifiers here*/
133 if(tParams.bIdent == TRUE){
134 for(i = 0; i < tData.nSeq; i++){
135 printf("%s\n", tData.aszID[i]);
136 }
137 }
138
139 /* Start 1A August 2012
140 Speed optimisation 1: allocate memory here once for the whole set of comparisons
141 We use the max sequence length to size the matrix and rely on each run of the needlemanWunsch algorithm to
142 initialise the values in the sub-set of the matrix space it will use
143 The change is repeated in head and slave node sections */
144 aadFMatrix = (double **) malloc((nM + 1)*sizeof(double *));
145 aanMoves = (int **) malloc((nM + 1)*sizeof(int *));
146 if(!aadFMatrix || !aanMoves)
147 goto memoryError;
148 for(i = 0; i <= nM; i++){
149 aadFMatrix[i] = (double *) malloc((nM + 1)*sizeof(double));
150 aanMoves[i] = (int *) malloc((nM + 1)*sizeof(int));
151 if(!aadFMatrix[i] || !aanMoves[i])
152 goto memoryError;
153
154 /* August 2012
155 Speed optimisation 3A: leave matrix empty initially except for edges*/
156 aadFMatrix[i][0] = GAP_PENALTY*i;
157 aanMoves[i][0] = UP;
158 /* Because this master matrix is square, we can initialise the edges in 1 pass */
159 aadFMatrix[0][i] = GAP_PENALTY*i;
160 aanMoves[0][i] = LEFT;
161 }
162 /* End 1A */
163
164 ulCount = 0;
165 for(i = 0; i < nN; i++){
166 for(j = 0; j < i; j++){
167 t_Align tAlign;
168 double dDist = 0.0;
169
170 needlemanWunsch(&tAlign, &tData.acSequences[i*nM], &tData.acSequences[j*nM],
171 tData.anLen[i], tData.anLen[j], aadFMatrix, aanMoves);
172
173 dDist = tAlign.dDist;
174 printf("%.8e\n",dDist);
175
176 ulCount++;
177 if(ulCount == ulA0){
178 goto finish;
179 }
180 }
181 fflush(stdout);
182 }
183
184 finish:
185 ulPackets = ((ulA - 1)/MAX_PACKET_SIZE) + 1;
186
187 for (i = 1; i < numtasks; i++){
188
189 ulPacketCurr = 0;
190 for(j = 0; j < ulPackets; j++){
191
192 if(j < ulPackets - 1){
193 ulPacketSize = MAX_PACKET_SIZE;
194 }
195 else{
196 ulPacketSize = ulA % MAX_PACKET_SIZE;
197 }
198
199 MPI_Recv((void *) (&adDists[ulPacketCurr]), ulPacketSize,
200 MPI_DOUBLE, i, nTag, MPI_COMM_WORLD, &status);
201
202 ulPacketCurr += ulPacketSize;
203 }
204
205
206 for(j = 0; j < ulA; j++){
207 printf("%.8e\n",adDists[j]);
208 }
209 }
210
211 }
212 else{
213 /* receive data*/
214
215 //fprintf(stdout,"%d receive data\n",rank); fflush(stderr);
216
217 receiveData(&tData);
218
219 nN = tData.nSeq; nM = tData.nMaxLen;
220
221 ulSize = ((unsigned long) nN*( (unsigned long) nN - 1))/2;
222 ulNumTasks = (unsigned long) numtasks;
223 ulA = (unsigned long) floor(ulSize / ulNumTasks);
224
225 ulA0 = ulA + (ulSize % ulNumTasks);
226
227 //fprintf(stdout,"%d allocate memory %lu %lu\n",rank, ulSize, ulA); fflush(stdout);
228 adDists = (double *) malloc(ulA*sizeof(double));
229 if(!adDists)
230 goto memoryError;
231
232 /* Start 1B August 2012
233 Speed optimisation 1: allocate memory here once for the whole set of comparisons
234 We use the max sequence length to size the matrix and rely on each run of the needlemanWunsch algorithm to
235 initialise the values in the sub-set of the matrix space it will use
236 The change is repeated in head and slave node sections */
237 aadFMatrix = (double **) malloc((nM + 1)*sizeof(double *));
238 aanMoves = (int **) malloc((nM + 1)*sizeof(int *));
239 if(!aadFMatrix || !aanMoves)
240 goto memoryError;
241 for(i = 0; i <= nM; i++){
242 aadFMatrix[i] = (double *) malloc((nM + 1)*sizeof(double));
243 aanMoves[i] = (int *) malloc((nM + 1)*sizeof(int));
244 if(!aadFMatrix[i] || !aanMoves[i])
245 goto memoryError;
246
247 /* August 2012
248 Speed optimisation 3B: leave matrix empty initially except for edges*/
249 aadFMatrix[i][0] = GAP_PENALTY*i;
250 aanMoves[i][0] = UP;
251 /* Because this master matrix is square, we can initialise the edges in 1 pass */
252 aadFMatrix[0][i] = GAP_PENALTY*i;
253 aanMoves[0][i] = LEFT;
254 }
255 /* End 1B */
256
257 ulCount = 0; ulStart = ulA0 + (rank - 1)*ulA; ulFinish = ulA0 + rank*ulA;
258
259 //fprintf(stdout,"%d begin calculations\n",rank); fflush(stdout);
260 for(i = 0; i < nN; i++){
261 for(j = 0; j < i; j++){
262 t_Align tAlign;
263 double dDist = 0.0;
264
265 if(ulCount >= ulStart){
266
267 needlemanWunsch(&tAlign, &tData.acSequences[i*nM], &tData.acSequences[j*nM],
268 tData.anLen[i], tData.anLen[j], aadFMatrix, aanMoves);
269 dDist = tAlign.dDist;
270
271 adDists[ulCount - ulStart] = dDist;
272 }
273
274 ulCount++;
275 if(ulCount == ulFinish){
276 goto finish2;
277 }
278
279 }
280 }
281
282 finish2:
283
284 ulPackets = ((ulA - 1)/MAX_PACKET_SIZE) + 1;
285
286 ulPacketCurr = 0;
287 for(j = 0; j < ulPackets; j++){
288 if(j < ulPackets - 1){
289 ulPacketSize = MAX_PACKET_SIZE;
290 }
291 else{
292 ulPacketSize = ulA % MAX_PACKET_SIZE;
293 }
294
295 //printf("Send %d %d %d\n",rank, nPacketCurr, nPacketSize);
296 MPI_Send((void *) (&adDists[ulPacketCurr]), ulPacketSize,
297 MPI_DOUBLE, 0, nTag, MPI_COMM_WORLD);
298 ulPacketCurr += ulPacketSize;
299 }
300
301 if(ulPacketCurr != ulA){
302 fprintf(stderr, "Message error\n");
303 }
304 }
305
306 /* Start 1C August 2012
307 Speed optimisation 1: free allocated matrix*/
308 /*free allocated memory*/
309 free(adDists);
310 free(tData.acSequences);
311 free(tData.anLen);
312 for(i = 0; i <= nM; i++){
313 free(aadFMatrix[i]);
314 free(aanMoves[i]);
315 }
316 free(aadFMatrix);
317 free(aanMoves);
318 /* End1C August 2012 */
319
320 // Wait for everyone to stop
321 MPI_Barrier(MPI_COMM_WORLD);
322
323 // Always use MPI_Finalize as the last instruction of the program
324
325 MPI_Finalize();
326 exit(EXIT_SUCCESS);
327
328 memoryError:
329 fprintf(stderr, "Failed allocating memory in main\n");
330 fflush(stderr);
331 exit(EXIT_FAILURE);
332 }
333
334 void writeUsage(FILE* ofp)
335 {
336 int i = 0;
337 char *line;
338
339 for(i = 0; i < nLines; i++){
340 line = usage[i];
341 fputs(line,ofp);
342 }
343 }
344
345 char *extractParameter(int argc, char **argv, char *param,int when)
346 {
347 int i = 0;
348
349 while((i < argc) && (strcmp(param,argv[i]))){
350 i++;
351 }
352
353 if(i < argc - 1){
354 return(argv[i + 1]);
355 }
356
357 if((i == argc - 1) && (when == OPTION)){
358 return "";
359 }
360
361 if(when == ALWAYS){
362 fprintf(stdout,"Can't find asked option %s\n",param);
363 }
364
365 return (char *) NULL;
366 }
367
368 void getCommandLineParams(t_Params *ptParams,int argc,char *argv[])
369 {
370 char *szTemp = NULL;
371 char *cError = NULL;
372
373 /*get parameter file name*/
374 ptParams->szInputFile = extractParameter(argc,argv, INPUT_FILE,ALWAYS);
375 if(ptParams->szInputFile == NULL)
376 goto error;
377
378 /*identifiers*/
379 szTemp = extractParameter(argc, argv, IDENT, OPTION);
380 if(szTemp != NULL){
381 ptParams->bIdent = TRUE;
382 }
383 else{
384 ptParams->bIdent = FALSE;
385 }
386
387 szTemp = extractParameter(argc, argv, PHYLIP, OPTION);
388 if(szTemp != NULL){
389 ptParams->bPhylip = TRUE;
390 }
391 else{
392 ptParams->bPhylip = FALSE;
393 }
394
395 return;
396
397 error:
398 writeUsage(stdout);
399 exit(EXIT_FAILURE);
400 }
401
402 /* August 2012
403 Speed optimisation 5B: inline loop functions */
404
405 double calcDistance(char* acA, char* acB, int nLen)
406 {
407 int gap_alert1 = 0, gap_alert2 = 0;
408 int start_seq1 = 0;
409 int start_seq2 = 0;
410 int end_seq1 = nLen;
411 int end_seq2 = nLen;
412 double residuecount = 0.0, distance = 0.0;
413 int k = 0;
414 int start_seq = 0, end_seq = 0;
415
416 /* get left side gaps */
417 k = 0;
418 while(acA[k] == GAP && k < nLen) k++;
419 start_seq1 = k;
420
421 k = 0;
422 while(acB[k] == GAP && k < nLen) k++;
423 start_seq2 = k;
424
425 /* get right side gaps */
426 k = nLen - 1;
427 while(acA[k] == GAP && k >= 0) k--;
428 end_seq1 = k + 1; /* add one on for < not <= further on down */
429
430 k = nLen - 1;
431 while(acB[k] == GAP && k >= 0) k--;
432 end_seq2 = k + 1;
433
434 start_seq = start_seq1;
435 if(start_seq < start_seq2){
436 start_seq = start_seq2;
437 }
438
439 end_seq = end_seq1;
440 if(end_seq > end_seq2){
441 end_seq = end_seq2;
442 }
443
444 gap_alert1 = 0;
445 gap_alert2 = 0;
446
447 if(end_seq - start_seq <= 0){
448 return 1.0;
449 }
450
451 for(k = start_seq; k < end_seq; k++){
452
453 if (acA[k] == GAP && acB[k] == GAP){
454 continue;
455 }
456
457 if(acA[k] == GAP){
458 if(gap_alert1 == 0){
459 residuecount+=1.0;
460 distance+=1.0;
461 }
462 gap_alert1 = 1;
463 gap_alert2 = 0;
464
465 continue;
466 }
467
468 if(acB[k] == GAP){
469 if(gap_alert2 == 0){
470 residuecount+=1.0;
471 distance+=1.0;
472 }
473 gap_alert1 = 0;
474 gap_alert2 = 1;
475
476 continue;
477 }
478
479 /* ok no gaps , check for mismatch */
480 if(acA[k] != 'N' && acB[k] != 'N'){
481
482 if (acA[k] != acB[k]){
483 distance += 1.0;
484 residuecount+=1.0;
485 gap_alert1 = 0;
486 gap_alert2 = 0;
487
488 continue;
489 }
490
491 if(acA[k] == acB[k]){
492 residuecount+= 1.0;
493 gap_alert1 = 0;
494 gap_alert2 = 0;
495
496 continue;
497 }
498 }
499 else{
500 residuecount+= 1.0;
501
502 gap_alert1 = 0;
503 gap_alert2 = 0;
504
505 continue;
506 }
507
508 printf("FELL THROUGH \n");
509 }
510
511 if (residuecount > 0) {
512 distance = distance / residuecount;
513 }
514 else {
515 distance = 1.0;
516 }
517
518 return distance;
519 }
520
521 void needlemanWunsch(t_Align *ptAlign, const char* acA, const char* acB, int nLenA, int nLenB, double** aadFMatrix, int** aanMoves)
522 {
523 char *acAlignA = NULL, *acAlignB = NULL;
524 int nCount = 0;
525 int i = 0, j = 0;
526
527 /* August 2012
528 Speed optimisation 2: don't initialise the whole matrix space: once the top and left edge values have
529 been initialised the algorithm always refers to values that have just been set */
530
531 /* August 2012
532 Speed optimisation 3C: don't re-initialise the matrix edges as they dont change*/
533
534
535 /* August 2012
536 Speed optimisation 5A: streamline loop code */
537 double dChoice1, dChoice2, dChoice3, dPenaltyi, dPenaltyj;
538 for(i = 1; i <= nLenA; i++){
539 dPenaltyi = i != nLenA ? GAP_PENALTY : 0;
540
541 for(j = 1; j <= nLenB; j++){
542 dPenaltyj = j != nLenB ? GAP_PENALTY : 0;
543
544 dChoice1 = aadFMatrix[i-1][j-1] + dist(acA[i - 1], acB[j - 1]);
545 dChoice2 = aadFMatrix[i][j-1] + dPenaltyi;
546 dChoice3 = aadFMatrix[i-1][j] + dPenaltyj;
547
548 aanMoves[i][j] = getMove(dChoice1, dChoice2, dChoice3);
549 aadFMatrix[i][j] = dmin3(dChoice1, dChoice2, dChoice3);
550 }
551 }
552
553 ptAlign->dDist = aadFMatrix[nLenA][nLenB];
554
555 acAlignA = (char *) malloc((nLenA + nLenB)*sizeof(char));
556 if(!acAlignA)
557 goto memoryError;
558
559 acAlignB = (char *) malloc((nLenA + nLenB)*sizeof(char));
560 if(!acAlignB)
561 goto memoryError;
562
563 for(i = 0; i < nLenA + nLenB; i++){
564 acAlignA[i] = GAP; acAlignB[i] = GAP;
565 }
566
567 nCount = 0;
568 i = nLenA; j = nLenB;
569 while(i > 0 && j > 0){
570
571 switch(aanMoves[i][j]){
572 case DIAG:
573
574 acAlignA[nCount] = acA[i - 1];
575 acAlignB[nCount] = acB[j - 1];
576
577 i--;
578 j--;
579 break;
580 case UP:
581 acAlignA[nCount] = acA[i - 1];
582 acAlignB[nCount] = GAP;
583
584 i--;
585 break;
586 case LEFT:
587 acAlignA[nCount] = GAP;
588 acAlignB[nCount] = acB[j - 1];
589
590 j--;
591 break;
592 }
593
594 nCount++;
595 }
596
597 while(i > 0){
598 acAlignA[nCount] = acA[i - 1];
599 acAlignB[nCount] = GAP;
600 i--;
601 nCount++;
602 }
603
604 while(j > 0){
605 acAlignA[nCount] = GAP;
606 acAlignB[nCount] = acB[j - 1];
607 j--;
608 nCount++;
609 }
610
611 ptAlign->nLen = nCount;
612
613 //for(i = 0; i < ptAlign->nLen; i++){
614 //fprintf(stderr,"%c %c\n",acAlignA[i],acAlignB[i]);
615 //}
616 //fprintf(stderr,"\n\n");
617 ptAlign->dDist = calcDistance(acAlignA, acAlignB, ptAlign->nLen);
618
619
620 free(acAlignA);
621 free(acAlignB);
622 return;
623
624 memoryError:
625 fprintf(stderr, "Failed to allocate memory in needlemanWunsch\n");
626 fflush(stderr);
627 exit(EXIT_FAILURE);
628 }
629
630 static char szNoisy[] = "N";
631
632 void readData(t_Data *ptData, t_Params *ptParams)
633 {
634 FILE *ifp = NULL;
635 char szLine[MAX_LINE_LENGTH];
636 int nPos = 0, i = 0, j = 0, nM = 0, nSequences = 0;
637 char *szBrk;
638 char *szRet;
639
640 /*first count sequences and get length*/
641 ptData->nSeq = 0;
642 ptData->nMaxLen = 0;
643
644 ifp = fopen(ptParams->szInputFile, "r");
645
646 if(ifp){
647 while(fgets(szLine, MAX_LINE_LENGTH, ifp)){
648 if(szLine[0] == '>'){
649 if(nPos > ptData->nMaxLen){
650 ptData->nMaxLen = nPos;
651 }
652
653 ptData->nSeq++;
654 nPos = 0;
655 }
656 else{
657 i = 0;
658 while(strrchr(szSequence,szLine[i]) != NULL){
659 i++;
660 nPos++;
661 }
662 }
663 }
664
665 fclose(ifp);
666 }
667 else{
668 fprintf(stderr, "Can't open input file %s\n", ptParams->szInputFile);
669 exit(EXIT_FAILURE);
670 }
671
672 ptData->aszID = (char **) malloc(ptData->nSeq*sizeof(char *));
673
674 nM = ptData->nMaxLen;
675 ptData->acSequences = (char *) malloc(ptData->nSeq*nM*sizeof(char));
676 ptData->anLen = (int *) malloc(ptData->nSeq*sizeof(int));
677
678 ifp = fopen(ptParams->szInputFile, "r");
679
680 if(ifp){
681 while(szRet = fgets(szLine, MAX_LINE_LENGTH, ifp)){
682 if(szLine[0] == '>'){
683 if(nSequences > 0){
684 ptData->anLen[nSequences - 1] = nPos;
685 }
686
687 szBrk = strpbrk(szLine, " \n");
688 (*szBrk) = '\0';
689 ptData->aszID[nSequences] = strdup(szLine + 1);
690 nPos = 0;
691 nSequences++;
692 }
693
694 i = 0;
695 while(szLine[i] != '\0' && strrchr(szSequence,szLine[i]) != NULL){
696 ptData->acSequences[(nSequences - 1)*nM + nPos] = toupper(szLine[i]);
697
698 nPos++; i++;
699 }
700 }
701
702 ptData->anLen[nSequences - 1] = nPos;
703
704 fclose(ifp);
705 }
706 else{
707 fprintf(stderr, "Can't open input file %s\n", ptParams->szInputFile);
708 exit(EXIT_FAILURE);
709 }
710 }