sumatra.c 16.3 KB
Newer Older
Celine Mercier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * FileName:    sumatra.c
 * Authors:      Eric Coissac, Celine Mercier
 * Description: computation of pairwise similarities of DNA sequences
 * **/

#include "sumatra.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>

#include <sys/time.h>

Celine Mercier committed
17 18 19 20
#include "libfasta/sequence.h"
#include "liblcs/upperband.h"
#include "liblcs/sse_banded_LCS_alignment.h"
#include "libutils/utilities.h"
Celine Mercier committed
21 22
#include "mtcompare_sumatra.h"

23
#define VERSION "1.0.34"
Celine Mercier committed
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


/* ----------------------------------------------- */
/* printout help                                   */
/* ----------------------------------------------- */
#define PP fprintf(stdout,

static void PrintHelp()
{
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
        PP      " SUMATRA Version %s\n", VERSION);
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
        PP      " Synopsis : sumatra computes all the pairwise LCS (Longest Common Subsequence) scores\n");
        PP		" of one nucleotide dataset or between two nucleotide datasets.\n");
        PP      " Usage: sumatra [options] <dataset1> [dataset2]\n");
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
        PP      " Options:\n\n");
        PP      " -h       : [H]elp - print <this> help\n\n");
        PP      " -l       : Reference sequence length is the shortest. \n\n");
        PP      " -L       : Reference sequence length is the largest. \n\n");
        PP      " -a       : Reference sequence length is the alignment length (default). \n\n");
        PP      " -n       : Score is normalized by reference sequence length (default).\n\n");
        PP      " -r       : Raw score, not normalized. \n\n");
        PP      " -d       : Score is expressed in distance (default: score is expressed in similarity). \n\n");
        PP      " -t ##.## : Score threshold. If the score is normalized and expressed in similarity (default),\n");
        PP		"            it is an identity, e.g. 0.95 for an identity of 95%%. If the score is normalized\n");
        PP		"            and expressed in distance, it is (1.0 - identity), e.g. 0.05 for an identity of 95%%.\n");
        PP		"            If the score is not normalized and expressed in similarity, it is the length of the\n");
        PP		"            Longest Common Subsequence. If the score is not normalized and expressed in distance,\n");
        PP		"            it is (reference length - LCS length).\n");
        PP		"            Only sequence pairs with a similarity above ##.## are printed. Default: 0.00 \n");
        PP		"            (no threshold).\n\n");
        PP      " -p ##    : Number of threads used for computation (default=1).\n\n");
        PP      " -g       : n's are replaced with a's (default: sequences with n's are discarded).\n");
        PP      " -x       : Adds four extra columns with the count and length of both sequences.\n");
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
60 61 62
        PP      " First argument  : the nucleotide dataset to analyze (or nothing   \n");
        PP      "                   if there is only one dataset and the standard   \n");
        PP		"                   input should be used).                        \n\n");
Celine Mercier committed
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
        PP      " Second argument : optionally the second nucleotide dataset\n");
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
        PP      " Results table description : \n");
        PP      " column 1 : Identifier sequence 1\n");
        PP      " column 2 : Identifier sequence 2\n");
        PP      " column 3 : Score\n");
        PP      " column 4 : Count of sequence 1  (only with option -x)\n");
        PP      " column 5 : Count of sequence 2  (only with option -x)\n");
        PP      " column 6 : Length of sequence 1 (only with option -x)\n");
        PP      " column 7 : Length of sequence 2 (only with option -x)\n");
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n");
        PP		" http://metabarcoding.org/sumatra\n");
        PP      "-----------------------------------------------------------------------------------------------------------------------------\n\n");
}

#undef PP

/* ----------------------------------------------- */
/* printout usage and exit                         */
/* ----------------------------------------------- */

#define PP fprintf(stderr,

static void ExitUsage(stat)
        int stat;
{
89
        PP      "usage: sumatra [-l|L|a|n|r|d|g|x] [-t threshold_value] [-p number of threads] [dataset1] [dataset2]\n");
Celine Mercier committed
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
        PP      "type \"sumatra -h\" for help\n");

        if (stat)
            exit(stat);
}

#undef  PP


void printResults(fastaSeqPtr seq1,fastaSeqPtr seq2,
			      double score,
			      BOOL extradata,
			      int64_t pairs,
			      BOOL print)
{
	static struct timeval start;
	static struct timeval lastprint;
	static BOOL first=TRUE;
	static uint64_t aligned=0;

	struct timeval current;
	double  fraction;
	time_t fulltime;
	time_t remaintime;
	double elapsedtime;
	int32_t day;
	int32_t hour;
	int32_t minute;
	int32_t seconde;


	aligned++;

	if (first)
	{
		first=FALSE;
		gettimeofday(&start,NULL);
		lastprint=start;
	}

	gettimeofday(&current,NULL);

	if (current.tv_sec!=lastprint.tv_sec)
	{
		lastprint=current;
		fraction = (double)aligned/(double)pairs;
		elapsedtime = difftime(current.tv_sec,start.tv_sec);
		fulltime = elapsedtime / fraction;
		remaintime = (time_t)difftime(fulltime,(time_t)elapsedtime);


		fprintf(stderr,
				"Computed %lld / %lld -> %5.2lf%%",
				aligned, pairs, fraction*100.
				);
		seconde = fulltime % 60;
		minute  = fulltime / 60;
		hour    = minute / 60;
		minute  = minute % 60;
		day     = hour / 24;
		hour    = hour % 24;
		if (day)
		fprintf(stderr,
				", estimated computation time = %3d days %02d:%02d:%02d",
				day,
				hour,
				minute,
				seconde
				);
		else
			fprintf(stderr,
					", estimated computation time = %02d:%02d:%02d",
					hour,
					minute,
					seconde
					);

		seconde = remaintime % 60;
		minute  = remaintime / 60;
		hour    = minute / 60;
		minute  = minute % 60;
		day     = hour / 24;
		hour    = hour % 24;
		if (day)
		fprintf(stderr,
				", about %3d days %02d:%02d:%02d remaining                  \r",
				day,
				hour,
				minute,
				seconde
				);
		else
			fprintf(stderr,
					", about %02d:%02d:%02d remaining                  \r",

					hour,
					minute,
					seconde
					);

	}

	if (print)
	{

	if (extradata)
		printf("%s\t%s\t%lf\t%d\t%d\t%d\t%d\n", seq1->accession_id,
										seq2->accession_id,
										score,
										seq1->count,
										seq2->count,
										seq1->length,
										seq2->length
		      );
	else
		printf("%s\t%s\t%lf\n", seq1->accession_id,
				seq2->accession_id,
				score);
	}
}


int compare1(fastaSeqCount db1, double threshold, BOOL normalize, int reference, BOOL lcsmode, BOOL extradata)
{
	BOOL     always = TRUE;
	int64_t  pairs = (int64_t)(db1.count - 1) * (int64_t)db1.count /2;
	BOOL     print;
Celine Mercier committed
217
	double   score, scoreG;
Celine Mercier committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	int32_t  i,j;
	char* s1;
	char* s2;
	int l1;
	int l2;
	int16_t* iseq1;
	int16_t* iseq2;
	int16_t* address;
	int sizeForSeqs;
	int lmax, lmin;
	int LCSmin;

	fprintf(stderr,"Pairwise alignments of one dataset against itself\n");
	fprintf(stderr,"Count of alignments to do : %lld\n",pairs);

	if (threshold > 0)
	{
		fprintf(stderr,"Computing for scores > %lf\n",threshold);
		always = FALSE;
	}

	calculateMaxAndMinLenDB(db1, &lmax, &lmin);
	sizeForSeqs = prepareTablesForSumathings(lmax, lmin, threshold, normalize, reference, lcsmode, &address, &iseq1, &iseq2);

Celine Mercier committed
242
	for (i=0; i < db1.count; i++) // ...?? db1.count - 1 probably
Celine Mercier committed
243 244 245 246 247 248 249 250 251 252 253 254
		for (j=i+1; j < db1.count; j++)
		{
			print = FALSE;
			filtersSumatra(db1.fastaSeqs+i, db1.fastaSeqs+j, threshold, normalize, reference, lcsmode, &score, &LCSmin);
			if (score >= 0) // identical sequences
				print = TRUE;
			else if (always || (score == -1.0))	// filter passed or no threshold
			{
				s1 = (db1.fastaSeqs+i)->sequence;
				l1 = (db1.fastaSeqs+i)->length;
				s2 = (db1.fastaSeqs+j)->sequence;
				l2 = (db1.fastaSeqs+j)->length;
Celine Mercier committed
255 256 257 258 259 260 261 262 263 264

/*				fprintf(stderr, "\n%s", s1);
				fprintf(stderr, "\n%s", s2);
				fprintf(stderr, "\n%f", threshold);
				fprintf(stderr, "\n%d", normalize);
				fprintf(stderr, "\n%d", reference);
				fprintf(stderr, "\n%d\n", lcsmode);
*/
//				score = generic_sse_banded_lcs_align(s1, s2, threshold, normalize, reference, lcsmode);
//				fprintf(stderr, "\nscore generic = %f", scoreG);
Celine Mercier committed
265
				score = alignForSumathings(s1, iseq1, s2, iseq2, l1, l2, normalize, reference, lcsmode, address, sizeForSeqs, LCSmin);
Celine Mercier committed
266 267 268 269 270 271
//				fprintf(stderr, "\nscore = %f\n", score);
//				if (scoreG != score)
//				{
//					fprintf(stderr, "\nscores differents\n");
//					exit(1);
//				}
Celine Mercier committed
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
				print = always || (((normalize || lcsmode) && (score >= threshold)) || ((!lcsmode && !normalize) && (score <= threshold)));
				if (print && !lcsmode && normalize)
					score = 1.0 - score;
			}
			printResults(db1.fastaSeqs+i, db1.fastaSeqs+j, score, extradata, pairs, print);
		}

	free(iseq1-sizeForSeqs+lmax);
	free(iseq2-sizeForSeqs+lmax);
	return 0;
}


int compare2(fastaSeqCount db1, fastaSeqCount db2, double threshold, BOOL normalize, int reference, BOOL lcsmode, BOOL extradata)
{
	BOOL     always = TRUE;
	int64_t  pairs = (int64_t)(db1.count) * (int64_t)(db2.count);
	BOOL     print;
	double   score;
	int32_t  i,j;
	char* s1;
	char* s2;
	int l1;
	int l2;
	int16_t* iseq1;
	int16_t* iseq2;
	int16_t* address;
	int sizeForSeqs;
	int lmax;
	int lmax1;
	int lmin;
	int lmin1;
	int LCSmin;

	fprintf(stderr,"Pairwise alignments of two datasets\n");
	fprintf(stderr,"Count of alignments to do : %lld\n",pairs);

	if (threshold > 0)
	{
		fprintf(stderr,"Computing for scores > %lf\n",threshold);
		always = FALSE;
	}

	calculateMaxAndMinLenDB(db1, &lmax, &lmin);
	calculateMaxAndMinLenDB(db2, &lmax1, &lmin1);

	if (lmax1 > lmax)
		lmax = lmax1;
	if (lmin1 < lmin)
		lmin = lmin1;

	sizeForSeqs = prepareTablesForSumathings(lmax, lmin, threshold, normalize, reference, lcsmode, &address, &iseq1, &iseq2);

	for (i=0; i < db1.count; i++)
		for (j=0; j < db2.count; j++)
		{
			print = FALSE;
			filtersSumatra(db1.fastaSeqs+i, db2.fastaSeqs+j, threshold, normalize, reference, lcsmode, &score, &LCSmin);
			if (score >= 0) // identical sequences
				print = TRUE;
			else if (always || (score == -1.0))	// filter passed or no threshold
			{
				s1 = (db1.fastaSeqs+i)->sequence;
				l1 = (db1.fastaSeqs+i)->length;
				s2 = (db2.fastaSeqs+j)->sequence;
				l2 = (db2.fastaSeqs+j)->length;
				score = alignForSumathings(s1, iseq1, s2, iseq2, l1, l2, normalize, reference, lcsmode, address, sizeForSeqs, LCSmin);
				print = always || (((normalize || lcsmode) && (score >= threshold)) || ((!lcsmode && !normalize) && (score <= threshold)));
				if (print && !lcsmode && normalize)
Celine Mercier committed
341
					score = 1.0 - score;	// TODO isn't that already done?
Celine Mercier committed
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
			}
			printResults(db1.fastaSeqs+i, db2.fastaSeqs+j, score, extradata, pairs, print);
		}

	free(iseq1-sizeForSeqs+lmax);
	free(iseq2-sizeForSeqs+lmax);
	return 0;
}


int main(int argc, char **argv)
{

	int32_t     	carg		= 0;
	int32_t         errflag     = 0;
	BOOL            normalize   = TRUE;
	BOOL            lcsmode     = TRUE;
	int             reference   = ALILEN;
	BOOL            extradata   = FALSE;
	BOOL			onlyATGC	   = TRUE;
	double          threshold   = 0.0;
	int             ndb         = 0;
	int				nproc       = 1;
	fastaSeqCount   db1;
	fastaSeqCount   db2;

	while ((carg = getopt(argc, argv, "hlLanrdt:p:gx")) != -1) {
	    switch (carg) {
								   /* -------------------- */
	    case 'h':                  /* help                 */
								   /* -------------------- */
	    	PrintHelp();
	    	exit(0);
	    	break;

									  /* -------------------------------------------------- */
	    case 'l':               	  /* Normalize LCS/Error by the shortest sequence length*/
									  /* -------------------------------------------------- */
	    	reference=MINLEN;
	        break;

									  /* -------------------------------------------------- */
	    case 'L':               	  /* Normalize LCS/Error by the largest sequence length */
									  /* -------------------------------------------------- */
	    	reference=MAXLEN;
	        break;

	          	  	  	  	  	  	  /* -------------------------------------------------- */
	    case 'a':               	  /* Normalize LCS/Error by the alignment length        */
	    	   	   	   	   	   	   	  /* -------------------------------------------------- */
	    	reference=ALILEN;
	    	break;

									  /* -------------------------------------------------- */
	    case 'n':               	  /* Normalize LCS by the reference length              */
									  /* -------------------------------------------------- */
	    	normalize=TRUE;
	    	break;

	    								/* -------------------------------------------------- */
		case 'r':   					/* No normalization     				              */
					  	  	  	  	  	/* -------------------------------------------------- */
			normalize=FALSE;
			break;

									  /* -------------------------------------------------- */
	    case 'd':               	  /* Score is expressed in distance                  */
								      /* -------------------------------------------------- */
	    	lcsmode=FALSE;
	    	break;

						/* ------------------------------------------------------------------- */
		case 't':   	/* Prints only pairs with similarity higher than (threshold)         */
						/* ------------------------------------------------------------------- */
	    	sscanf(optarg,"%lf",&threshold);
	    	break;

							 /* -------------------------------------------------- */
	    case 'p':            /* number of processors to use                        */
							 /* -------------------------------------------------- */
	    	sscanf(optarg,"%d",&nproc);
			break;

								  /* -------------------------------------------------- */
	    case 'x':              	  /* Print extra data (node weight, lseq1, lseq2)       */
								  /* -------------------------------------------------- */
	    	extradata=TRUE;
			break;


									/* -------------------------------------------------- */
		case 'g':   				/* replace n's with a's in sequences                  */
									/* -------------------------------------------------- */
			onlyATGC=FALSE;
			break;


	    case '?':               	/* bad option   	        */
	    	errflag++;
	    	break;
	     }
	}

	ndb = argc - optind;

	if (errflag)
		ExitUsage(errflag);

    fprintf(stderr,"===============================================================\n");
	fprintf(stderr," SUMATRA version %s\n",VERSION);
#ifdef __SSE2__
	fprintf(stderr,"Alignment using SSE2 code\n");
#else
	fprintf(stderr,"Alignment using standard code, SSE2 unavailable\n");
#endif
	fprintf(stderr,"===============================================================\n");


	if (normalize && (threshold > 1.0))
	{
		fprintf(stderr, "\nERROR : Please specify a threshold included between 0.0 and 1.0 when normalizing scores.\n\n");
		exit(1);
	}

	fprintf(stderr,"Reading dataset 1...");
	db1 = seq_readAllSeq2(argv[optind], TRUE, onlyATGC);
	fprintf(stderr,"\n%d sequences\n",db1.count);

	if (!onlyATGC)
		(void)cleanDB(db1);

	addCounts(&db1);

	if (threshold>0)
		(void)hashDB(db1);

	optind++;

	if (ndb == 2)
	{
	    fprintf(stderr,"Reading dataset 2...");
		db2 = seq_readAllSeq2(argv[optind], TRUE, onlyATGC);
		fprintf(stderr,"\n%d sequences\n",db2.count);

		if (!onlyATGC)
			(void)cleanDB(db2);

		addCounts(&db2);

		if (threshold>0)
			(void)hashDB(db2);
	}

	if (!lcsmode && normalize && (threshold > 0))
		threshold = 1.0 - threshold;

	if (ndb==1)
	{
		if (nproc==1)
			compare1(db1, threshold, normalize, reference, lcsmode, extradata);
		else
		    mt_compare_sumatra(&db1, NULL, threshold, normalize, reference, lcsmode, extradata, nproc);
	}
	else
	{
		if (nproc==1)
			compare2(db1, db2, threshold, normalize, reference, lcsmode, extradata);
		else
		    mt_compare_sumatra(&db1, &db2, threshold, normalize, reference, lcsmode, extradata, nproc);
	}

	fprintf(stderr,"\nDone.\n\n");
	return 0;

}