-
Notifications
You must be signed in to change notification settings - Fork 6
/
getFragmentLength.cpp
203 lines (177 loc) · 6.23 KB
/
getFragmentLength.cpp
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
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
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
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <zlib.h>
#include <random>
#include <iostream>
#include <cassert>
#include <stdlib.h>
#include "getFragmentLength.h"
#include "mrand.h"
#define LENS 4096
int BinarySearch_fraglength(double* SearchArray,int low, int high, double key){
/*
BinarySearch_fraglength - Performs binary search to find the index of the fragment length
in a sorted array of frequencies that is less than or equal to the key.
@param SearchArray: A pointer to a sorted array of frequency values.
@param low: The starting index of the range to search.
@param high: The ending index of the range to search.
@param key: The frequency value to search for.
@return: The index of the highest element in the array that is less than or equal to the key.
*/
int bin_idx = 0;
while (low <= high) {
int mid = low + (high - low + 1) / 2;
double midVal = SearchArray[mid];
if (midVal < key) {bin_idx = mid;low = mid + 1;}
else if (midVal > key) {high = mid - 1;}
else if (midVal == key){high = mid - 1;}
}
return bin_idx+1;
}
void ReadLengthFile(int& number,int*& Length, double*& Frequency,const char* filename){
/*
ReadLengthFile - Reads a length distribution file and create length and frequency array from which sampling occurs.
@param number: A reference to an integer where the number of read entries will be stored.
@param Length: A pointer to an integer array to store fragment lengths.
@param Frequency: A pointer to a double array to store frequencies corresponding to fragment lengths.
@param filename: The path to the file containing length and frequency data.
*/
int n =1;
gzFile gz = Z_NULL;
char buf[LENS];
gz = gzopen(filename,"r");
assert(gz!=Z_NULL);
Length[0] = 0; Frequency[0] = (float) 0;
while(gzgets(gz,buf,LENS)){
Length[n] = atoi(strtok(buf,"\n\t "));
Frequency[n] = atof(strtok(NULL,"\n\t "));
n++;
}
gzclose(gz);
number = n;
}
int getFragmentLength(sim_fragment *sf){
/*
getFragmentLength - Generates a fragment length based on the type of length distribution and associated parameters.
@param sf: A pointer to a sim_fragment struct containing distribution parameters and random number generators.
@return: The generated fragment length.
*/
// 27-08-2024 consider changing this to switch cases
int res = 0;
double rand_val;
if(sf->type == 0){
res = sf->FixLength;
}
else if(sf->type == 1){
//fprintf(stderr,"type 1 lengthf file\n");
rand_val = mrand_pop(sf->rand_alloc);
int lengthbin = BinarySearch_fraglength(sf->Frequency,0, sf->noRow - 1, rand_val);
int fraglength = sf->Frag_len[lengthbin];
res = fraglength;
}
else if(sf->type == 2){
//fprintf(stderr,"type 2 uni\n");
res = sf->UniDist(sf->Gen);//sf->LengthDist;
}
else if(sf->type == 3){
//fprintf(stderr,"type 3 norm\n");
res = sf->NormDist(sf->Gen);//sf->LengthDist;
}
else if(sf->type == 4){
//fprintf(stderr,"type 4 log\n");
res = sf->LogNormDist(sf->Gen);//sf->LengthDist;
}
else if(sf->type == 5){
//fprintf(stderr,"type 5 pois\n");
res = sf->PoisDist(sf->Gen);//sf->LengthDist;
}
else if(sf->type == 6){
//fprintf(stderr,"type 6 exp\n");
res = sf->ExpDist(sf->Gen);//sf->LengthDist;
}
else if(sf->type == 7){
//fprintf(stderr,"type 7 gamma\n");
res = sf->GammaDist(sf->Gen);//sf->LengthDist;
}
if(res<0)
return getFragmentLength(sf);
return res;
}
sim_fragment *sim_fragment_alloc(int type,double par1, double par2,int no_row,double*& FreqArray,
int*& FragArray,int RandType,unsigned int Thread_Seed,std::default_random_engine& generator){
/*
sim_fragment_alloc - Allocates and initializes a sim_fragment structure based on provided parameters.
@param type: The type of fragment length distribution.
@param par1: First parameter for the distribution (e.g., mean or shape).
@param par2: Second parameter for the distribution (e.g., standard deviation or scale).
@param no_row: Number of rows in the frequency and fragment length arrays.
@param FreqArray: Array of frequencies for fragment lengths.
@param FragArray: Array of fragment lengths.
@param RandType: Type of random number generator.
@param Thread_Seed: Seed for the random number generator.
@param generator: Reference to a default_random_engine object.
@return: A pointer to the allocated and initialized sim_fragment structure.
*/
sim_fragment *fp = new sim_fragment;
fp->rand_alloc= mrand_alloc(RandType,Thread_Seed);
fp->type = type;
fp->Gen =generator;
if(fp->type == 0){
fp->FixLength = par1;
}
else if(fp->type == 1){
fp->Frequency = FreqArray;
fp->Frag_len = FragArray;
fp->noRow = no_row;
}
else if(fp->type == 2){
std::uniform_int_distribution<int> distribution(par1,par2);
fp->UniDist = distribution;
}
else if(fp->type == 3){
std::normal_distribution<double> distribution(par1,par2);
fp->NormDist = distribution;
}
else if(fp->type == 4){
std::lognormal_distribution<double> distribution(par1,par2);
fp->LogNormDist = distribution;
}
else if(fp->type == 5){
std::poisson_distribution<int> distribution(par1);
fp->PoisDist = distribution;
}
else if(fp->type == 6){
std::exponential_distribution<double> distribution(par1);
fp->ExpDist = distribution;
}
else if(fp->type == 7){
std::gamma_distribution<double> distribution(par1,par2);
fp->GammaDist = distribution;
}
return fp;
}
#ifdef __WITH_MAIN__
int main(int argc,char **argv){
if(argc==1)
fprintf(stderr,"./a.out dist par1 par2 nrep");
int type = atoi(argv[1]);
double par1 = atof(argv[2]);
double par2 = atof(argv[3]);
int nrep = atoi(argv[4]);
const char *fname = "Test_Examples/Anc_size.txt";
char buf[LENS];
double* Frag_freq;
int* Frag_len;
int no_elem;
Frag_len = new int[LENS];Frag_freq = new double[LENS];
ReadLengthFile(no_elem,Frag_len,Frag_freq,fname);
int Thread_Seed = 2;
std::default_random_engine RndGen(Thread_Seed);
sim_fragment *sf = sim_fragment_alloc(type,par1,par2,0,Frag_freq,Frag_len,0,Thread_Seed,RndGen);
for(int i=0;i<nrep;i++)
fprintf(stdout,"%d\n",getFragmentLength(sf));
return 0;
}
#endif
//g++ getFragmentLength.cpp mrand.o -std=c++11 -lm -lz -D__WITH_MAIN__ -O3 -o FragLen