-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (239 loc) · 10.6 KB
/
Copy pathmain.cpp
File metadata and controls
285 lines (239 loc) · 10.6 KB
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
#include <unistd.h>
#include <stdio.h>
#ifdef _WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <string.h>
#include <iostream>
#include <ctype.h>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <getopt.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <chrono>
#include <sstream>
#include <sys/resource.h>
#include "kmer.h"
#include "read.h"
#include "aligning.h"
#include "bitarray.h"
#include <nthash/nthash.hpp>
using namespace std;
using namespace nthash;
using namespace chrono;
// Funzione per ottenere l'uso della memoria
long getMemoryUsage() {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return usage.ru_maxrss; // La memoria massima residente in KB
}
// Funzione per scrivere i benchmark su un file CSV
void writeBenchmarkToCSV(double elapsedSeconds, long memoryUsage) {
ofstream file("benchmark_results_w303.csv");
if (file.is_open()) {
file << "Running time,Memory (Mb)\n";
file << elapsedSeconds << " s," << (memoryUsage / 1024) << " Mb\n";
file.close();
}
}
void print_usage() {
printf("\nLROD può rilevare regioni di sovrapposizione tra letture lunghe.\n");
printf("\nIl formato del comando è il seguente: ");
printf("\nLROD -r <long-read-file> -c <kmer-frequency-file> -o result-file [opzioni]\n");
printf("\nUso:\n");
printf("\t-r long-read-file: file di input in formato FASTA\n");
printf("\t-c kmer-frequency-file: ogni riga nel file delle frequenze dei kmer deve essere \"kmer kmer-frequency\"\n");
printf("\t-o result-file: file di risultati\n");
printf("\t-t count: numero di thread (predefinito 1)\n");
printf("\t-k kmerLength: lunghezza del kmer (predefinito 15)\n");
printf("\t-q smallKmerLength: lunghezza del piccolo kmer (predefinito 9)\n");
printf("\t-f minimumKmerFrequency: frequenza minima del kmer (predefinito 2)\n");
printf("\t-m maxKmerFrequencyRatio: rapporto massimo di frequenza del kmer (deve essere inferiore a 1, predefinito 0.9)\n");
printf("\t-s kmerStep: passo del kmer (predefinito 1)\n");
printf("\t-d distance: piccola distanza usata per determinare se due kmer comuni sono coerenti (predefinito 400)\n");
printf("\t-e distance: grande distanza usata per determinare se due kmer comuni sono coerenti (predefinito 1500)\n");
printf("\t-a min-overlap-length: lunghezza minima di sovrapposizione tra due letture lunghe (predefinito 500)\n");
printf("\t-b length-ratio: rapporto massimo di lunghezza tra due regioni allineate (predefinito 0.3)\n");
printf("\t-h, -help\n");
printf("\t--generate-kmer-file-only: genera solo il file delle frequenze dei kmer e termina\n");
}
std::unordered_map<std::string, int> computeKmerFrequencies(const std::string& sequence, int kmerLength, int step) {
std::unordered_map<std::string, int> kmerFrequencies;
NtHash nth(sequence, 1, kmerLength);
while (nth.roll()) {
std::string kmer = sequence.substr(nth.get_pos(), kmerLength);
kmerFrequencies[kmer]++;
}
return kmerFrequencies;
}
std::string readLongReadFile(const std::string& filePath) {
std::ifstream file(filePath);
std::string sequence;
std::string line;
while (std::getline(file, line)) {
if (line[0] != '>') {
sequence += line;
}
}
return sequence;
}
void generateKmerFrequencyFile(const std::string& inputFilePath, const std::string& outputFilePath, int kmerLength, int step) {
std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath);
std::string sequence = readLongReadFile(inputFilePath);
std::unordered_map<std::string, int> kmerFrequencies = computeKmerFrequencies(sequence, kmerLength, step);
for (const auto& entry : kmerFrequencies) {
outputFile << entry.first << " " << entry.second << "\n";
}
}
void convertToFASTA(const std::string& inputFilePath, const std::string& outputFilePath) {
std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath);
if (!inputFile.is_open() || !outputFile.is_open()) {
std::cerr << "Error opening files!" << std::endl;
return;
}
std::string line;
int kmerCount = 1;
while (std::getline(inputFile, line)) {
size_t spaceIndex = line.find(' ');
if (spaceIndex != std::string::npos) {
std::string kmer = line.substr(0, spaceIndex);
outputFile << ">kmer" << kmerCount << std::endl;
outputFile << kmer << std::endl;
kmerCount++;
}
}
inputFile.close();
outputFile.close();
std::cout << "Conversion completed. Output file: " << outputFilePath << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 4) {
print_usage();
return 1;
}
long int maxSize = 1000000;
char* StrLine = (char*)malloc(sizeof(char) * maxSize);
char* readFile = NULL;
char* kmerFrequencyFile = NULL;
char* outputKmerFile = (char*)malloc(sizeof(char) * 500);
FILE* fp4;
strcpy(outputKmerFile, "output");
long int step = 1;
long int kmerLength = 15;
long int smallKmerLength = 9;
long int threadCount = 1;
long int smallIntervalDistance = 400;
long int largeIntervalDistance = 1500;
long int overlapLengthCutOff = 500;
float lengthRatio = 0.3;
int frequencyCutOff = 3;
long int minimumKmerFrequency = 2;
float maxKmerFrequencyRatio = 0.9;
bool generateKmerFileOnly = false;
struct option long_options[] = {
{"readFile", required_argument, NULL, 'r'},
{"kmerFrequencyFile", required_argument, NULL, 'c'},
{"outputKmerFile", optional_argument, NULL, 'o'},
{"step", optional_argument, NULL, 's'},
{"kmerLength", optional_argument, NULL, 'k'},
{"smallKmerLength", optional_argument, NULL, 'q'},
{"minimumKmerFrequency", optional_argument, NULL, 'f'},
{"maxKmerFrequencyRatio", optional_argument, NULL, 'm'},
{"threadCount", optional_argument, NULL, 't'},
{"smallIntervalDistance", optional_argument, NULL, 'd'},
{"largeIntervalDistance", optional_argument, NULL, 'e'},
{"overlapLengthCutOff", optional_argument, NULL, 'a'},
{"lengthRatio", optional_argument, NULL, 'b'},
{"generate-kmer-file-only", no_argument, NULL, 'g'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
int ch = 0;
while ((ch = getopt_long(argc, argv, "c:r:o:m:n:d:k:e:a:s:t:f:q:b:gh", long_options, NULL)) != -1) {
switch (ch) {
case 'r': readFile = (char*)(optarg); break;
case 'c': kmerFrequencyFile = (char*)(optarg); break;
case 'o': outputKmerFile = (char*)optarg; break;
case 'k': kmerLength = atoi(optarg); break;
case 'q': smallKmerLength = atoi(optarg); break;
case 'f': minimumKmerFrequency = atoi(optarg); break;
case 'm': maxKmerFrequencyRatio = atof(optarg); break;
case 's': step = atoi(optarg); break;
case 'd': smallIntervalDistance = atoi(optarg); break;
case 'e': largeIntervalDistance = atoi(optarg); break;
case 'a': overlapLengthCutOff = atoi(optarg); break;
case 't': threadCount = atoi(optarg); break;
case 'b': lengthRatio = atof(optarg); break;
case 'g': generateKmerFileOnly = true; break;
case 'h':
print_usage();
return 0;
default:
return -1;
}
}
if (minimumKmerFrequency >= maxKmerFrequencyRatio) {
printf("Regolazione dei parametri: impostazione di maxKmerFrequencyRatio su minimumKmerFrequency + 0.1\n");
maxKmerFrequencyRatio = minimumKmerFrequency + 0.1;
}
if ((fp4 = fopen(readFile, "r")) == NULL) {
printf("File delle letture lunghe mancante!\n");
printf("Per un uso dettagliato di LROD, utilizzare il comando: -h o -help!\n");
return 2;
}
fclose(fp4);
if ((fp4 = fopen(kmerFrequencyFile, "r")) == NULL) {
printf("Generazione del file delle frequenze dei kmer...\n");
generateKmerFrequencyFile(readFile, kmerFrequencyFile, kmerLength, step);
} else {
fclose(fp4);
}
// Se l'opzione --generate-kmer-file-only è stata specificata, termina l'esecuzione qui
if (generateKmerFileOnly) {
printf("File delle frequenze dei kmer generato: %s\n", kmerFrequencyFile);
return 0;
}
strcat(outputKmerFile, ".csv");
if ((fp4 = fopen(outputKmerFile, "w")) == NULL) {
printf("%s, non esiste!\n", outputKmerFile);
return 5;
}
fclose(fp4);
printf("\nInizio caricamento delle letture lunghe!\n");
printf("funzione GetReadSetHead\n");
auto start = high_resolution_clock::now();
long initialMemoryUsage = getMemoryUsage();
ReadSetHead* readSetHead = GetReadSetHead(readFile, StrLine, maxSize);
if (readSetHead->readCount <= 1) {
printf("Il numero di letture è inferiore a uno!\n");
return 6;
}
printf("Inizio costruzione della tabella hash dei kmer!\n");
KmerHashTableHead* kmerHashTableHead = GetKmerHashTableHead(kmerFrequencyFile, readSetHead, kmerLength, step, minimumKmerFrequency, maxKmerFrequencyRatio);
if (GetKmerHashTableHead_UnitTest(kmerHashTableHead) == 0) {
printf("Errore nella costruzione della tabella hash dei kmer!\n");
return 7;
}
long int subReadCount = 50000;
KmerReadNodeHead* kmerReadNodeHead = GetKmerReadNodeHeadSub(readSetHead, kmerLength, step, subReadCount);
printf("Preparazione per rilevare sovrapposizioni tra letture lunghe!\n");
GetCommonKmerHeadAllThreadNew(kmerHashTableHead, kmerReadNodeHead, readSetHead, kmerLength, readFile, outputKmerFile, step, threadCount, smallKmerLength, smallIntervalDistance, largeIntervalDistance, overlapLengthCutOff, lengthRatio, subReadCount);
if (GetCommonKmerHeadAllThreadNew_UnitTest(outputKmerFile, readSetHead->readCount) == 0) {
return 8;
}
auto end = high_resolution_clock::now();
long finalMemoryUsage = getMemoryUsage();
double elapsedSeconds = duration_cast<seconds>(end - start).count();
long memoryUsage = finalMemoryUsage - initialMemoryUsage;
writeBenchmarkToCSV(elapsedSeconds, memoryUsage);
printf("Fatto!\n");
printf("Nome del file di risultati è: %s!\n", outputKmerFile);
return 0;
}