load.c 2.54 KB
Newer Older
Eric Coissac committed
1 2 3 4 5 6 7 8 9
/*
 * load.c
 *
 *  Created on: 3 oct. 2012
 *      Author: coissac
 */

#include "orgasm.h"

Eric Coissac committed
10 11 12 13 14 15 16
void indexReverseComplement(buffer_t *reads)
{
	int32_t i;

	if (reads->complement)
		FREE(reads->complement);

Eric Coissac committed
17
	reads->complement=(uint32_t*) MALLOC(sizeof(uint32_t) * reads->readCount);
Eric Coissac committed
18 19 20 21

	for (i=0;i < reads->readCount; i++)
		reads->complement[reads->order1[i]]=i;
}
Eric Coissac committed
22 23 24 25 26 27 28 29
buffer_t *loadIndexedReads(const char *indexname)
{
	buffer_t tmp;
	buffer_t *reads;
	char *indexFileName;
	FILE *index;
	size_t maxsize;

Eric Coissac committed
30 31 32 33
	//
	// Read the general data about the sequence index
	//

Eric Coissac committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	asprintf(&indexFileName,"%s.ogx",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);

	loadGeneralData(&tmp,index);

	free(indexFileName);
	fclose(index);

	maxsize = (tmp.recordSize  + \
			   2 * sizeof(uint32_t)) * tmp.readCount + 48;

50
	reads = newBuffer(maxsize,tmp.readSize,0);
Eric Coissac committed
51 52 53

	reads->readCount = tmp.readCount;

Eric Coissac committed
54 55 56 57
	//
	// Read the forward sequence data
	//

Eric Coissac committed
58 59 60 61 62 63 64 65 66 67 68 69 70
	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);

Eric Coissac committed
71 72 73 74
	//
	// Read the pair-end data --> into order2
	//

Eric Coissac committed
75 76 77 78 79 80 81 82 83 84 85 86 87
	asprintf(&indexFileName,"%s.opx",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);

	loadPairData(reads,index);

	fclose(index);
	free(indexFileName);

Eric Coissac committed
88 89 90 91
	//
	// Read index for the reverse complemented sequences --> into order1
	//

Eric Coissac committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	asprintf(&indexFileName,"%s.orx",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);

	fprintf(stderr,"\nLoading reverse index...\n\n");
	loadOrder1(reads,index);
	fprintf(stderr,"Done.\n");

	fclose(index);
	free(indexFileName);

Eric Coissac committed
107 108 109 110
	reads->complement=NULL; // Provisoire...

	fprintf(stderr,"\nIndexing reverse complement sequences ...\n\n");
	indexReverseComplement(reads);
Eric Coissac committed
111 112 113 114 115 116 117 118 119

	fprintf(stderr,"\nFast indexing forward reads...\n\n");
	indexForward(reads);
	fprintf(stderr,"\nFast indexing reverse reads...\n\n");
    indexReverse(reads);
	fprintf(stderr,"Done.\n");

	return reads;
}