-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMain_GenerateandFindIndependentSet_v1.cpp
288 lines (259 loc) · 7.59 KB
/
Main_GenerateandFindIndependentSet_v1.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <cstring>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <omp.h>
#include <sstream>
#include "getopt.h"
using namespace std;
//****U S E R S S H O U L D E N T E R T H E S E P A R A M E T E R S**************************//
//Value dictionary
typedef struct p{
int type;
void *var;
p() {
type = -1;
var = NULL;
};
p (int type_p, void* var_p) {
type = type_p;
var = var_p;
};
}parserEntry;
map<string,parserEntry*> parseMap;
//////////////////////////////////////
int SeqLen; //LENGTH OF THE SEQUENCE IDENTIFIER (BARCODE)
int padding; //PADDING AROUND SEMI-GLOBAL ALIGNMENT
unsigned long int DesiredNofBarcodes; //NUMBER OF BARCODES NEEDED
string LeftAdaptor; //THE ADAPTOR SEQUENCE ADDED TO THE LEFT OF THE BARCODE (5')
string RightAdaptor; //THE ADAPTOR SEQUENCE ADDED TO THE RIGHT OF THE BARCODE (3')
int homoplimit; //MAXIMUM NUMBER OF MONO, DI OR TRI-MERS ALLOWED WITHIN A BARCODE
double MinGC; // MINIMUM GC PERCENTAGE OF THE BARCODE
double MaxGC; //MAXIMUM GC PERCENTAGE OF THE BARCODE
int LenDiffThreshold;
double SelfHybT; //SELF HYBRIDIZATION TM THRESHOLD
double EditDistanceThreshold;
double EditDistanceThreshold_Self;
double Hyb_Temperature;
vector<string> AdaptorList;
vector<string> RestrictionList;
//**************************************************************************************************
//***********C O N S T A N T S******************
const double R = 0.0019872; //For Tm calculations
//**********************************************
const int N_THREADS=7;
#include <pthread.h>
pthread_mutex_t poolMutex;
std::vector<string> pool;
bool die = false;
//**D E P E N D E N C I E S*********************
#include "LZW.h"
#include "hybrid-ss-min.h"
#include "GenerateRandomDNASequences_v6.h"
#include "FindIndepentSet_v2.h"
#include "DegreeDistribution_v1.h"
//**********************************************
long int rejected=0;
//***************I/O FILE NAMES****************************************************
string PutSeqFNS="";
//************************************************************************
/**
* Print usage instructions
*/
static int print_usage()
{
fprintf(stderr, "\n");
fprintf(stderr, "Version: 1.0\n");
fprintf(stderr, "Usage: designBarcodes [options] <output>\n");
fprintf(stderr, "Options: \n");
fprintf(stderr, " -c [STR] Configuration file\n");
fprintf(stderr, " -n [INT] Number of barcodes to output.\n");
return 1;
}
void buildParserMap()
{
//Integers
parseMap["desiredBarcodeLength"] = new parserEntry(1,&SeqLen);
parseMap["homopolymerLimit"] = new parserEntry(1,&homoplimit);
parseMap["padding"] = new parserEntry(1,&padding);
//Strings
parseMap["leftAdaptor"] = new parserEntry(2,&LeftAdaptor);
parseMap["rightAdaptor"] = new parserEntry(2,&RightAdaptor);
//Doubles
parseMap["minGC"] = new parserEntry(3,&MinGC);
parseMap["maxGC"] = new parserEntry(3,&MaxGC);
parseMap["selfHybridizationTemperature"] = new parserEntry(3,&SelfHybT);
parseMap["hybridizationTemperature"] = new parserEntry(3,&Hyb_Temperature);
parseMap["minEditDistance"] = new parserEntry(3,&EditDistanceThreshold);
parseMap["minEditDistanceSelf"] = new parserEntry(3,&EditDistanceThreshold_Self);
}
bool parseConfig(string path)
{
FILE* cFile = fopen(path.c_str(),"r");
if (cFile == NULL)
return false;
vector<string> * listRef = NULL;
char line[1000];
while (fgets(line,1000,cFile) != NULL) {
if ((line[0] == '#') || (strlen(line) < 2))
continue;
string l = line;
if (l.find("</") != string::npos) {//End of a definition
listRef = NULL;
continue;
}
if (listRef) {
//Clean-up string
l.erase(l.find_last_not_of(" \n\r\t")+1);
listRef->push_back(l);
continue;
}
if (l[0] == '<') {//Beginning of a definition list
if (l.find("handles") != string::npos) {
listRef = &AdaptorList;
} else if (l.find("restrictionSites") != string::npos) {
listRef = &RestrictionList;
} else {
//Now a known list!
fprintf(stdout,"Wrong enumeration %s\n",l.c_str());
continue;
}
}
if (l.find("=") == string::npos)
continue;
string key = l.substr(0,l.find("="));
key.erase(key.find_last_not_of(" \n\r\t")+1);
string value = l.substr(l.find("=")+1,l.size()-l.find("="));
value.erase(value.find_last_not_of(" \n\r\t")+1);
//Check against known keys and populate the relevant values
if (parseMap.find(key) != parseMap.end()) {
switch (parseMap[key]->type) {
case 1://Integer
*static_cast<int*>(parseMap[key]->var) = atoi(value.c_str());
break;
case 2://String
*static_cast<string*>(parseMap[key]->var) = value;
break;
case 3://Double
*static_cast<double*>(parseMap[key]->var) = atof(value.c_str());
break;
default:
fprintf(stderr,"read wrong variable type\n");
break;
}
} else {
fprintf(stderr,"Unable to parse %s variable\n",key.c_str());
}
}
return true;
}
int main(int argc, char *argv[]){
//Build parsing map
buildParserMap();
//TODO: don't leak the parser map
string configPath = "";
//Parse arguments
int arg;
while ((arg = getopt(argc,argv,"c:n:")) >= 0) {
switch (arg) {
case 'c':
configPath = optarg;
break;
case 'n':
DesiredNofBarcodes = atoi(optarg);
break;
default:
fprintf(stderr,"Unknown argument %c\n",arg);
break;
}
}
if (optind + 1 != argc) {//Something is wrong with the parameters
print_usage();
return -1;
}
//Output file name
PutSeqFNS = argv[optind];
//Parse config file
if (!parseConfig(configPath)) {
fprintf(stdout,"Cannot read config file: %s\n",configPath.c_str());
return 1;
}
string temp,seq, FN,FN1,FN2,s0;
GenerateSequences Sequences;
Network NW;
DegreeDistribution DegreeDist;
char UniqueBarcodeFNC[150],EDDistFNC[150],DDFNC[150];
unsigned long int i=0; // Keep number of barcodes
//bool keepbarcode;
vector <string> randseqs;
string randseq_revcomp;
//omp_set_num_threads(N_THREADS);
pthread_mutex_init(&poolMutex,NULL);
unsigned long int old_count = 0;
unsigned long int same_count = 0;
//Sequences.initialisevars();
Sequences.DetermineFilteringThresholds();
//Startup generating threads!
Sequences.GenerateRandomSequence_SetGC();
//Create local buffer.
vector<string> sequenceVector;
long int consecutiveRejection = 0;
do{
//Wait for pool!
pthread_mutex_lock(&poolMutex);
//Check for size of pool
if (pool.size() >= 50) {
for (int i=0;i<pool.size();++i) {
sequenceVector.push_back(pool[i]);
}
pool.clear();
}
pthread_mutex_unlock(&poolMutex);
if (sequenceVector.empty()) {
//Do something silly for some time
clock_t goal = 500 + clock();
while (goal > clock());
continue;
}
i=0;
while (i < sequenceVector.size()) {
if (!NW.RetrieveUniqueNodes(sequenceVector[i])){
++rejected;
++consecutiveRejection;
if (consecutiveRejection > 1000000) {
goto Done;
}
//fprintf(stdout,"%s\n",sequenceVector[i].c_str());
} else {
consecutiveRejection = 0;
}
if (NW.CommonSet.size() >= DesiredNofBarcodes)
break;
++i;
}
sequenceVector.clear();
}while(NW.CommonSet.size()<DesiredNofBarcodes);
Done:
//Cleanup generating threads!
pthread_mutex_lock(&poolMutex);
die = true;
pool.clear();
pthread_mutex_unlock(&poolMutex);
FN.append(PutSeqFNS);
FN.append("_UniqueBarcodes.txt");
strcpy(UniqueBarcodeFNC,FN.c_str());
FN2.append(PutSeqFNS);
FN2.append("_EDDistribution.txt");
strcpy(EDDistFNC,FN2.c_str());
NW.PrintUniquePutBarcodes(UniqueBarcodeFNC);
NW.PrintEditDistanceDistribution(EDDistFNC);
return 0;
}