buildindex.c 2.56 KB
Newer Older
Eric Coissac's avatar
Eric Coissac committed
1 2 3 4 5 6 7 8 9 10 11
/** buildindex.c
 *
 *  Created on: 26 sept. 2012
 *      Author: coissac
 */

#include "orgasm.h"

buffer_t *buildIndex(const char *forwardFileName, const char* reverseFileName,
					    const char *indexname,
		 	 	 	 	uint32_t minword,
12
		 	 	 	 	size_t maxread,
Eric Coissac's avatar
Eric Coissac committed
13 14
					    size_t maxbuffersize,
					    uint32_t readLength
Eric Coissac's avatar
Eric Coissac committed
15 16 17 18 19 20 21 22
					   )
{
	FILE* forward;
	FILE* reverse;
	FILE* index;

	char* indexFileName;

Eric Coissac's avatar
Eric Coissac committed
23
//	uint32_t indexcount;
Eric Coissac's avatar
Eric Coissac committed
24
//	uint32_t i;
Eric Coissac's avatar
Eric Coissac committed
25 26 27 28 29 30 31 32 33 34 35 36
	buffer_t *reads;

	forward = fopen(forwardFileName,"r");

	if (forward == NULL)
	FATALERROR("Cannot open file %s",forwardFileName)

	reverse = fopen(reverseFileName,"r");

	if (reverse == NULL)
	FATALERROR("Cannot open file %s",reverseFileName)

Eric Coissac's avatar
Eric Coissac committed
37
	reads = loadPairedFastq(forward,reverse,maxread,maxbuffersize,readLength);
Eric Coissac's avatar
Eric Coissac committed
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

	fclose(forward);
	fclose(reverse);

	sortBuffer(reads);

	asprintf(&indexFileName,"%s.ofx",indexname);
	if (indexFileName == NULL)
		FATALERROR("Cannot allocate memory for file name %s",indexname);

	index = fopen(indexFileName,"w");
	if (index == NULL)
		FATALERROR("Cannot open file %s",indexFileName);

	writeOrdoredSequences(reads,index);

	fclose(index);
	free(indexFileName);

	asprintf(&indexFileName,"%s.opx",indexname);
	if (indexFileName == NULL)
		FATALERROR("Cannot allocate memory for file name %s",indexname);

	index = fopen(indexFileName,"w");
	if (index == NULL)
		FATALERROR("Cannot open file %s",indexFileName);

	writePairData(reads,index);

	fclose(index);
	free(indexFileName);

	asprintf(&indexFileName,"%s.ofx",indexname);
	if (indexFileName == NULL)
		FATALERROR("Cannot allocate memory for file name %s",indexname);

	index = fopen(indexFileName,"r");
	if (index == NULL)
		FATALERROR("Cannot open file %s",indexFileName);

	loadSequences(reads,index);

	fclose(index);
	free(indexFileName);

	compSortBuffer(reads);

	asprintf(&indexFileName,"%s.orx",indexname);
	if (indexFileName == NULL)
		FATALERROR("Cannot allocate memory for file name %s",indexname);

	index = fopen(indexFileName,"w");
	if (index == NULL)
		FATALERROR("Cannot open file %s",indexFileName);

	writeOrderData(reads,index);

	fclose(index);
	free(indexFileName);

Eric Coissac's avatar
Eric Coissac committed
98 99 100
//	indexcount = reads->readSize - minword;
//	reads->minword  =minword;
//	reads->suffixidx=indexcount;
Eric Coissac's avatar
Eric Coissac committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	asprintf(&indexFileName,"%s.ogx",indexname);
	if (indexFileName == NULL)
		FATALERROR("Cannot allocate memory for file name %s",indexname);

	index = fopen(indexFileName,"w");
	if (index == NULL)
		FATALERROR("Cannot open file %s",indexFileName);

	writeGeneralData(reads,index);

	free(indexFileName);
	fclose(index);

	indexForward(reads);
    indexReverse(reads);

	return reads;
}