obicount.c 1.59 KB
Newer Older
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 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

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>	/* mmap() is defined in this header */
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>

#include "../obitypes.h"
#include "../obidmscolumn.h"


/**
 * @brief Computes the size to map.
 *
 * * @param OBIDMSColumn_file The file to map.
 * @return The size to map.
 */
int get_size_to_map(int OBIDMSColumn_file)
// compute size to map : file size minus size of the header
{
	int size;
	struct stat s;

	fstat(OBIDMSColumn_file, &s);
	size = (s.st_size) - HEADER_SIZE;
	return(size);
}

/**
 * @brief Computes and prints the total number of sequences by summing their counts.
 *
 * * @param The count file.
 */
int main(int argc, char const *argv[])
{
	char* map;
	int size;
	int OBIDMSColumn_file;
	int count;
	char c;
	char num_str[10] = "";
	int num_int;
	int i,j;

	// initialize variables
	OBIDMSColumn_file = open(argv[1], O_RDONLY);	//read only
	count = 0;
	j = 0;

	// compute size to map
	size = get_size_to_map(OBIDMSColumn_file);

	// map the data
	map = obi_map_read_only(OBIDMSColumn_file, HEADER_SIZE, size);

	// sum the counts
	for (i=0; i<size; i++)
	{
		c = map[i];

		if (c != SEPARATOR)		// reading lines
		{
			num_str[j] = c;
			j++;
		}

		else if (c == SEPARATOR)	// end of a line
		{
			num_int = atoi(num_str);	// turn number from character string to int
			count = count + num_int;	// add the number to the sum
			j = 0;
			num_str[j] = '\0';
		}
	}

	// print the final count of sequences
	fprintf(stderr, "Sequence count = %d\n", count);

	// unmap
	obi_unmap(size);

	// close file
	close(OBIDMSColumn_file);

	return(0);
}