obi_clean.c 18.9 KB
Newer Older
Celine Mercier's avatar
Celine Mercier committed
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
/****************************************************************************
 * Tags a set of sequences for PCR/sequencing errors identification		    *
 ****************************************************************************/

/**
 * @file obi_clean.c
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 * @date April 9th 2018
 * @brief Functions tagging a set of sequences for PCR/sequencing errors identification.
 */

//#define OMP_SUPPORT // TODO
#ifdef OMP_SUPPORT
#include <omp.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <search.h>
#include <sys/time.h>

#include "obi_clean.h"
#include "obidms.h"
#include "obidebug.h"
#include "obierrno.h"
#include "obitypes.h"
#include "obiview.h"
#include "sse_banded_LCS_alignment.h"
#include "upperband.h"
#include "obiblob.h"



#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)


/**************************************************************************
 *
 * D E C L A R A T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 **************************************************************************/

44 45 46 47 48 49 50 51 52 53 54 55 56 57

/**
 * Internal function creating the output columns for the obiclean algorithm.
 *
 * @param o_view A pointer on the output view.
 * @param sample_column A pointer on the column where sample counts are kept.
 * @param sample_count The number of different samples.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
Celine Mercier's avatar
Celine Mercier committed
58 59 60 61 62
static int create_output_columns(Obiview_p o_view,
								 OBIDMS_column_p sample_column,
								 int sample_count);


63 64 65 66 67 68 69 70 71 72 73 74 75
/**
 * @brief Internal function comparing two indexes (int64_t).
 *
 * @param idx1 A pointer on the first index.
 * @param idx2 A pointer on the second index.
 *
 * @returns -1 if idx1 < idx2,
 * 			1 if idx1 > idx2,
 * 			and 0 if idx1 == idx2.
 *
 * @since April 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
Celine Mercier's avatar
Celine Mercier committed
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
static inline int idxcmp(const void* idx1, const void* idx2);


/************************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 ************************************************************************/

static int create_output_columns(Obiview_p o_view,
								 OBIDMS_column_p sample_column,
								 int sample_count)
{
	// Status column
	if (obi_view_add_column(o_view, CLEAN_STATUS_COLUMN_NAME, -1, NULL, OBI_CHAR, 0, sample_count, (sample_column->header)->elements_names, true, false, false, NULL, NULL, -1, CLEAN_STATUS_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_STATUS_COLUMN_NAME);
		return -1;
	}

	// Head column
	if (obi_view_add_column(o_view, CLEAN_HEAD_COLUMN_NAME, -1, NULL, OBI_BOOL, 0, 1, NULL, false, false, false, NULL, NULL, -1, CLEAN_HEAD_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_HEAD_COLUMN_NAME);
		return -1;
	}

	// Sample count column
	if (obi_view_add_column(o_view, CLEAN_SAMPLECOUNT_COLUMN_NAME, -1, NULL, OBI_INT, 0, 1, NULL, false, false, false, NULL, NULL, -1, CLEAN_SAMPLECOUNT_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_SAMPLECOUNT_COLUMN_NAME);
		return -1;
	}

	// Head count column
	if (obi_view_add_column(o_view, CLEAN_HEADCOUNT_COLUMN_NAME, -1, NULL, OBI_INT, 0, 1, NULL, false, false, false, NULL, NULL, -1, CLEAN_HEADCOUNT_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_HEADCOUNT_COLUMN_NAME);
		return -1;
	}

	// Internal count column
	if (obi_view_add_column(o_view, CLEAN_INTERNALCOUNT_COLUMN_NAME, -1, NULL, OBI_INT, 0, 1, NULL, false, false, false, NULL, NULL, -1, CLEAN_INTERNALCOUNT_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_INTERNALCOUNT_COLUMN_NAME);
		return -1;
	}

	// Singleton count column
	if (obi_view_add_column(o_view, CLEAN_SINGLETONCOUNT_COLUMN_NAME, -1, NULL, OBI_INT, 0, 1, NULL, false, false, false, NULL, NULL, -1, CLEAN_SINGLETONCOUNT_COLUMN_COMMENTS, true) < 0)
	{
		obidebug(1, "\nError creating the %s column", CLEAN_SINGLETONCOUNT_COLUMN_NAME);
		return -1;
	}

	return 0;
}


static inline int idxcmp(const void* idx1, const void* idx2)
{
    if (*(index_t*) idx1 < *(index_t*) idx2)
        return -1;
    if (*(index_t*) idx1 > *(index_t*) idx2)
        return 1;
    return 0;
}


/**********************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P U B L I C   F U N C T I O N S
 *
 **********************************************************************/


int obi_clean(const char* dms_name,
		      const char* i_view_name,
			  const char* sample_column_name,
			  const char* o_view_name,
			  const char* o_view_comments,
			  double threshold,
			  double max_ratio,
			  bool heads_only,
			  int thread_count)
{
162
	char* 			o_view_name_temp;
Celine Mercier's avatar
Celine Mercier committed
163
	float 			p;
164
	index_t         i, j, l;
Celine Mercier's avatar
Celine Mercier committed
165 166
	index_t         seq_count;
	index_t*  		index_array;
167
	index_t*  		line_selection;
Celine Mercier's avatar
Celine Mercier committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	double          score;
	bool            above_threshold;
	int             lcs_length;
	int             ali_length;
	Kmer_table_p    ktable;
	Obi_blob_p      blob1;
	Obi_blob_p   	blob2;
	int				lcs_min;
	int				sample_count;
	int 			sample;
	int             s1_count;
	int             s2_count;
	bool            head;
	int             head_count;
	int       	    internal_count;
	int 			singleton_count;
	int 			ind_sample_count;
	char  			status;

187 188 189 190 191 192
	void**          yes_trees = NULL;
	void**          no_trees = NULL;

	int* complete_sample_count_array = NULL;
	int* sample_count_array = NULL;
	Obi_blob_p* blob_array = NULL;
Celine Mercier's avatar
Celine Mercier committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	OBIDMS_p        dms = NULL;
	Obiview_p       i_view = NULL;
	Obiview_p       o_view = NULL;
	OBIDMS_column_p iseq_column = NULL;
	OBIDMS_column_p sample_column = NULL;
	OBIDMS_column_p status_column = NULL;
	OBIDMS_column_p head_column = NULL;
	OBIDMS_column_p internalcount_column = NULL;
	OBIDMS_column_p headcount_column = NULL;
	OBIDMS_column_p singletoncount_column = NULL;
	OBIDMS_column_p samplecount_column = NULL;

	void* no;
	void* yes;
	void* key_p;

	bool normalize = false;
	int reference = 0;
	bool similarity_mode = false;

	// Open DMS
	dms = obi_dms(dms_name);
216 217 218 219 220
	if (dms == NULL)
	{
		obidebug(1, "\nError opening the DMS");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
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

	// Open input view
	i_view = obi_open_view(dms, i_view_name);
	if (i_view == NULL)
	{
		obidebug(1, "\nError opening the input view to clean");
		return -1;
	}

	// Open the sequence column
	if (strcmp((i_view->infos)->view_type, VIEW_TYPE_NUC_SEQS) == 0)
		iseq_column = obi_view_get_column(i_view, NUC_SEQUENCE_COLUMN);
	else
	{
		obi_set_errno(OBI_CLEAN_ERROR);
		obidebug(1, "\nError: no sequence column");
		return -1;
	}
	if (iseq_column == NULL)
	{
		obidebug(1, "\nError getting the sequence column");
		return -1;
	}

	// Open the sample column if there is one
	if ((strcmp(sample_column_name, "") == 0) || (sample_column_name == NULL))
		sample_column = NULL;
	else
	{
		sample_column = obi_view_get_column(i_view, sample_column_name);
		if (sample_column == NULL)
		{
			obidebug(1, "\nError getting the sample column");
			return -1;
		}
	}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	// Create the output view, or a temporary one if heads only
	if (heads_only)
	{
		o_view_name_temp = calloc((strlen(o_view_name)+strlen("_temp")+1), sizeof(char));
		if (o_view_name_temp == NULL)
		{
			obidebug(1, "\nError allocating memory for the name of a temporary output view in obiclean");
			return -1;
		}
		o_view_name_temp = strcpy(o_view_name_temp, o_view_name);
		strcat(o_view_name_temp, "_temp");
		o_view = obi_clone_view(dms, i_view, o_view_name_temp, NULL, o_view_comments);
	}
	else
		o_view = obi_clone_view(dms, i_view, o_view_name, NULL, o_view_comments);

Celine Mercier's avatar
Celine Mercier committed
274 275
	if (o_view == NULL)
	{
276
		obidebug(1, "\nError creating the output view in obiclean");
Celine Mercier's avatar
Celine Mercier committed
277 278 279 280 281 282
		return -1;
	}

	sample_count = (sample_column->header)->nb_elements_per_line;

	// Create the output columns
283
	if (create_output_columns(o_view, sample_column, sample_count) < 0)
284 285
	{
		obidebug(1, "\nError creating the output columns");
Celine Mercier's avatar
Celine Mercier committed
286
		return -1;
287
	}
Celine Mercier's avatar
Celine Mercier committed
288 289 290

	// Get the created output columns
	status_column = obi_view_get_column(o_view, CLEAN_STATUS_COLUMN_NAME);
291 292 293 294 295
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean status column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
296
	head_column = obi_view_get_column(o_view, CLEAN_HEAD_COLUMN_NAME);
297 298 299 300 301
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean head column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
302
	internalcount_column = obi_view_get_column(o_view, CLEAN_INTERNALCOUNT_COLUMN_NAME);
303 304 305 306 307
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean internal count column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
308
	headcount_column = obi_view_get_column(o_view, CLEAN_HEADCOUNT_COLUMN_NAME);
309 310 311 312 313
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean head count column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
314
	singletoncount_column = obi_view_get_column(o_view, CLEAN_SINGLETONCOUNT_COLUMN_NAME);
315 316 317 318 319
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean singleton count column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
320
	samplecount_column = obi_view_get_column(o_view, CLEAN_SAMPLECOUNT_COLUMN_NAME);
321 322 323 324 325
	if (status_column == NULL)
	{
		obidebug(1, "\nError getting the obiclean sample count column");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
326 327 328 329 330 331 332 333 334 335 336 337

	// Build kmer tables
	ktable = hash_seq_column(i_view, iseq_column, 0);
	if (ktable == NULL)
	{
		obi_set_errno(OBI_CLEAN_ERROR);
		obidebug(1, "\nError building kmer tables before aligning");
		return -1;
	}

	seq_count = (i_view->infos)->line_count;

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
	// Allocate arrays for sample counts otherwise reading in mapped files takes longer
	complete_sample_count_array = (int*) malloc(seq_count * sample_count * sizeof(int));
	if (complete_sample_count_array == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for the array of sample counts, size: %lld", seq_count * sample_count * sizeof(int));
		return -1;
	}
	for (sample=0; sample < sample_count; sample++)
	{
		sample_count_array = complete_sample_count_array+(sample*seq_count);
		for (i=0; i<seq_count; i++)
			sample_count_array[i] = obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, i, sample);
	}

	// Allocate arrays for blobs otherwise reading in mapped files takes longer
	blob_array = (Obi_blob_p*) malloc(seq_count * sizeof(Obi_blob_p));
	if (blob_array == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for the array of blobs");
		return -1;
	}
	for (i=0; i<seq_count; i++)
	{
		blob_array[i] = obi_get_blob_with_elt_idx_and_col_p_in_view(i_view, iseq_column, i, 0);
	}

Celine Mercier's avatar
Celine Mercier committed
366 367
	// Allocate arrays of pointers to binary trees
	yes_trees = (void**) calloc(seq_count, sizeof(void*));
368 369 370 371 372 373
	if (yes_trees == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for 'yes' binary trees");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
374
	no_trees = (void**) calloc(seq_count, sizeof(void*));
375 376 377 378 379 380
	if (no_trees == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for 'no' binary trees");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
381 382 383

	// Allocate and fill index array for the binary trees to reference (they don't copy the data)
	index_array = (index_t*) malloc(seq_count * sizeof(index_t));
384 385 386 387 388 389
	if (index_array == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for the index array");
		return -1;
	}
Celine Mercier's avatar
Celine Mercier committed
390 391 392 393
	for (i=0; i < seq_count; i++)
		index_array[i]=i;

	// Initialize all sequences to singletons or NA if no sequences in that sample
394
	for (i=0; i<seq_count; i++)
Celine Mercier's avatar
Celine Mercier committed
395 396 397
	{
		for (sample=0; sample < sample_count; sample++)
		{
398
			if (obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, i, sample) != OBIInt_NA)  // Only initialize samples where there are some sequences
Celine Mercier's avatar
Celine Mercier committed
399 400
			{
				if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 's') < 0)
401 402
				{
					obidebug(1, "\nError initializing all sequences to singletons");
Celine Mercier's avatar
Celine Mercier committed
403
					return -1;
404
				}
Celine Mercier's avatar
Celine Mercier committed
405 406 407 408 409 410
			}
		}
	}

	for (sample=0; sample < sample_count; sample++)
	{
411
		sample_count_array = complete_sample_count_array+(sample*seq_count);
412
		for (i=0; i< (seq_count-1); i++)
Celine Mercier's avatar
Celine Mercier committed
413 414 415 416 417 418 419 420
		{
			if (i%1000 == 0)
			{
				p = ((i+(sample*(seq_count)))/(float)(seq_count*sample_count))*100;
			    fprintf(stderr,"\rDone : %f %%          ",p);
			}

			// Get first sequence
421 422
			blob1 = blob_array[i];
//			blob1 = obi_get_blob_with_elt_idx_and_col_p_in_view(i_view, iseq_column, i, 0);  // slower
Celine Mercier's avatar
Celine Mercier committed
423 424 425 426 427 428 429
			if (blob1 == NULL)
			{
				obidebug(1, "\nError retrieving sequences to align");
				return -1;
			}

			// Get count for this sample
430 431
			s1_count = sample_count_array[i];
			//s1_count = obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, i, sample);   // slower
Celine Mercier's avatar
Celine Mercier committed
432 433 434 435

			for (j=i+1; j < seq_count; j++)  // TODO parallelize this loop?
			{
				// Get second sequence
436 437
				blob2 = blob_array[j];
//				blob2 = obi_get_blob_with_elt_idx_and_col_p_in_view(i_view, iseq_column, j, 0);   // slower
Celine Mercier's avatar
Celine Mercier committed
438 439 440 441 442 443 444
				if (blob2 == NULL)
				{
					obidebug(1, "\nError retrieving sequences to align");
					return -1;
				}

				// Get count for this sample
445 446
				s2_count = sample_count_array[j];
				//s2_count = obi_get_int_with_elt_idx_and_col_p_in_view(i_view, sample_column, j, sample);    // slower
Celine Mercier's avatar
Celine Mercier committed
447 448 449 450 451 452

				// Checking ratio
				if (((s1_count!=OBIInt_NA && s2_count!=OBIInt_NA) && (s1_count>0 && s2_count>0)) &&
						((((s1_count >= s2_count) && (((double) s2_count / (double) s1_count) <= max_ratio))) ||
						(((s2_count >= s1_count) && (((double) s1_count / (double) s2_count) <= max_ratio)))))
				{
453

Celine Mercier's avatar
Celine Mercier committed
454 455 456 457 458 459 460
					yes=NULL;
					no=NULL;
					no = tfind(index_array+j, &(no_trees[i]), idxcmp);
					if (no == NULL)
						yes = tfind(index_array+j, &(yes_trees[i]), idxcmp);

					above_threshold = false;
461
					if ((no == NULL) && (yes == NULL))	// never compared before
Celine Mercier's avatar
Celine Mercier committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
					{
						// Check if the sequences are identical in a quick way (same index in the same indexer)
						if (obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, i, 0) == obi_get_index_with_elt_idx_and_col_p_in_view(i_view, iseq_column, j, 0))
							above_threshold = true;
						else // the sequences aren't identical
						{
							// kmer filter
							align_filters(ktable, blob1, blob2, i, j, threshold, normalize, reference, similarity_mode, &score, &lcs_min, false);

							// Compute alignment score if filter passed
							if (score == -1.0)
								score = obiblob_sse_banded_lcs_align(blob1, blob2, threshold, normalize, reference, similarity_mode, &lcs_length, &ali_length);

							above_threshold = ((score >= 0) && (score <= threshold));
						}
					}

					if (yes || above_threshold)
					{
						if (yes == NULL)
482 483 484 485 486 487 488 489 490 491
						// Put in 'yes' tree of 1st sequence
						{
							key_p = tsearch(index_array+j, &(yes_trees[i]), idxcmp);
							if (key_p == NULL)
							{
								obidebug(1, "\nError adding an index in a binary tree");
								return -1;
							}
						}

492
						// Might be worth having arrays to read values too for some datasets but unlikely
Celine Mercier's avatar
Celine Mercier committed
493 494 495 496
						// label as head or internal
						if (s1_count >= s2_count)
						{
							if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample) == 's')	// seq can become head ONLY if it's a singleton
497 498 499 500
							{
								if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'h') < 0)
									return -1;
							}
Celine Mercier's avatar
Celine Mercier committed
501
							// Otherwise it's an internal
502 503
							if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'i') < 0)
								return -1;
Celine Mercier's avatar
Celine Mercier committed
504 505 506 507
						}
						else
						{
							if (obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample) == 's')	// seq can become head ONLY if it's a singleton
508 509 510 511 512 513
								{
									if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, j, sample, 'h') < 0)
										return -1;
								}
							if (obi_set_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample, 'i') < 0)
								return -1;
Celine Mercier's avatar
Celine Mercier committed
514 515 516
						}
					}
					else if (no == NULL)
517 518 519 520 521 522 523 524 525
					// Put in 'no' tree of 1st sequence
					{
						key_p = tsearch(index_array+j, &(no_trees[i]), idxcmp);
						if (key_p == NULL)
						{
							obidebug(1, "\nError adding an index in a binary tree");
							return -1;
						}
					}
Celine Mercier's avatar
Celine Mercier committed
526 527 528 529 530 531
				}
			}
		}
	}

	free_kmer_tables(ktable, seq_count);
532
	free(index_array);
533 534
	free(complete_sample_count_array);
	free(blob_array);
535 536
	free(yes_trees);
	free(no_trees);
Celine Mercier's avatar
Celine Mercier committed
537 538 539

	fprintf(stderr, "\n");

540 541 542 543 544 545 546 547 548 549 550 551 552
	if (heads_only)
	{
		line_selection = malloc((o_view->infos)->line_count * sizeof(index_t));
		if (line_selection == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for line selection");
			return -1;
		}
		l=0;
	}

	for (i=0; i<seq_count; i++)
Celine Mercier's avatar
Celine Mercier committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	{
		if (i%1000 == 0)
		{
			p = (i/(float)(seq_count))*100;
		    fprintf(stderr, "\rAnnotating : %f %%          ",p);
		}

		head = false;
		head_count = 0;
		internal_count = 0;
		singleton_count = 0;
		ind_sample_count = 0;

		for (sample=0; sample < sample_count; sample++)
		{
			// Check if head or singleton in at least one sample
			status = obi_get_char_with_elt_idx_and_col_p_in_view(o_view, status_column, i, sample);
			if ((!head) && ((status == 'h') || (status == 's')))
571
			{
Celine Mercier's avatar
Celine Mercier committed
572
				head = true;
573 574 575 576 577 578
				if (heads_only)
				{
					line_selection[l] = i;
					l++;
				}
			}
Celine Mercier's avatar
Celine Mercier committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

			if (status == 's')
			{
				singleton_count++;
				ind_sample_count++;
			}
			else if (status == 'i')
			{
				internal_count++;
				ind_sample_count++;
			}
			else if (status == 'h')
			{
				head_count++;
				ind_sample_count++;
			}

		}

598 599 600 601 602 603 604 605 606 607 608 609 610 611
		if (!heads_only  || (heads_only && head))  // Label only if sequence is going to be kept in final view
		{
			if (obi_set_bool_with_elt_idx_and_col_p_in_view(o_view, head_column, i, 0, head) < 0)
				return -1;
			if (obi_set_int_with_elt_idx_and_col_p_in_view(o_view, singletoncount_column, i, 0, singleton_count) < 0)
				return -1;
			if (obi_set_int_with_elt_idx_and_col_p_in_view(o_view, internalcount_column, i, 0, internal_count) < 0)
				return -1;
			if (obi_set_int_with_elt_idx_and_col_p_in_view(o_view, headcount_column, i, 0, head_count) < 0)
				return -1;
			if (obi_set_int_with_elt_idx_and_col_p_in_view(o_view, samplecount_column, i, 0, ind_sample_count) < 0)
				return -1;
		}
	}
Celine Mercier's avatar
Celine Mercier committed
612

613 614 615 616 617 618 619 620 621 622
	// Flag the end of the line selection
	if (heads_only)
		line_selection[l] = -1;

	// Create new view with line selection if heads only
	if (heads_only)
	{
		if (obi_save_and_close_view(o_view) < 0)
		{
			obidebug(1, "\nError closing the temporary output view");
Celine Mercier's avatar
Celine Mercier committed
623
			return -1;
624 625 626 627 628
		}
		o_view = obi_clone_view_from_name(dms, o_view_name_temp, o_view_name, line_selection, o_view_comments);
		if (o_view == NULL)
		{
			obidebug(1, "\nError cloning the temporary output view to make the final output view");
Celine Mercier's avatar
Celine Mercier committed
629
			return -1;
630
		}
Celine Mercier's avatar
Celine Mercier committed
631

632 633 634 635 636 637 638
		// Delete the temporary view
		if (obi_delete_view(dms, o_view_name_temp) < 0)
		{
			obidebug(1, "\nError deleting the temporary output view");
			return -1;
		}

639 640
		free(o_view_name_temp);
		free(line_selection);
Celine Mercier's avatar
Celine Mercier committed
641 642 643 644 645 646 647 648
	}

	// Close views
	if (obi_save_and_close_view(i_view) < 0)
	{
		obidebug(1, "\nError closing the input view after aligning");
		return -1;
	}
649

Celine Mercier's avatar
Celine Mercier committed
650 651 652 653 654 655 656 657 658 659 660 661
	if (obi_save_and_close_view(o_view) < 0)
	{
		obidebug(1, "\nError closing the output view after aligning");
		return -1;
	}

	fprintf(stderr,"\rDone : 100 %%           ");
	return 0;
}