forked from azizmithani/roast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment.cpp
More file actions
232 lines (172 loc) · 8.59 KB
/
Copy pathalignment.cpp
File metadata and controls
232 lines (172 loc) · 8.59 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: alignment.cpp
* Author: madiha
*
* Created on April 26, 2019, 6:37 PM
*/
#include <fstream>
#include "alignment.h"
#include "utils.h"
#include "iostream"
using namespace std;
alignment::alignment() {
}
alignment::alignment(const alignment& orig) {
}
alignment::~alignment() {
}
string alignment::align_reads(const string& reference_file, const string& left_reads_file, const string& right_reads_file) {//, string cov_file) {
utils utils;
stringstream ss;
string thread_sam;
if (allowed_threads > 1) {
ss.str(string());
ss << allowed_threads - 1;
thread_sam = ss.str();
ss.str(string());
} else
thread_sam = allowed_threads;
string sam_file = reference_file.substr(0, reference_file.length() - 6) + ".sam";
string bam_file = reference_file.substr(0, reference_file.length() - 6) + ".bam";
string bam_sorted = reference_file.substr(0, reference_file.length() - 6) + ".sorted.bam"; //position sorted
string bam_sorted_filt = bam_sorted.substr(0, bam_sorted.length() - 4) + ".filtered.bam";
string bam_sorted_filt_temp = bam_sorted.substr(0, bam_sorted.length() - 4) + ".minimap.temp.bam";
string command_minimap2, command_bam_sort, command_sam_to_bam, command_bam_to_cov, command_index;
command_minimap2 = exe_path + "external_tools/minimap2 -Y -ax sr " + reference_file + " " + left_reads_file + " " + right_reads_file + " -o " + sam_file + " -t " + threads + "--secondary=no > /dev/null 2>&1";
system(command_minimap2.c_str());
command_sam_to_bam = "samtools view -bS " + sam_file + " -o " + bam_file + " -@ " + thread_sam + " > /dev/null 2>&1";
system(command_sam_to_bam.c_str());
command_bam_sort = "samtools sort " + bam_file + " -o " + bam_sorted + " -@ " + threadsForSamSortS + " -m " + MemForSamSortS + "M > /dev/null 2>&1";
system(command_bam_sort.c_str());
utils.filter_sam(bam_sorted, bam_sorted_filt_temp);
// to filter supplementary alignments
string command_filter = "samtools view -b -F 2048 " + bam_sorted_filt_temp + " -o " + bam_sorted_filt + " -@ " + thread_sam + " > /dev/null 2>&1";
system(command_filter.c_str());
command_index = "samtools index " + bam_sorted_filt + " > /dev/null 2>&1";
system(command_index.c_str());
//remove indexes
// remove_indexes(reference_file);
utils.remove_file(sam_file);
utils.remove_file(bam_file);
utils.remove_file(bam_sorted);
utils.remove_file(bam_sorted_filt_temp);
// return the sam object
return bam_sorted_filt;
}
string alignment::align_reads(const string& reference_file, const string& left_reads_file, const string& right_reads_file, string cov_minimap, string cov_hisat) {
utils utils;
stringstream ss;
ss << allowed_threads;
threads = ss.str();
string thread_sam;
if (allowed_threads > 1) {
ss.str(string());
ss << allowed_threads - 1;
thread_sam = ss.str();
ss.str(string());
} else
thread_sam = allowed_threads;
size_t find = reference_file.find_last_of(".");
string sam_file = reference_file.substr(0, find) + ".sam";
string bam_file = reference_file.substr(0, find) + ".bam";
string bam_sorted = reference_file.substr(0, find) + ".sorted.bam"; //position sorted
find = bam_sorted.find_last_of(".");
string bam_sorted_minimap = bam_sorted.substr(0, find) + ".minimap.bam";
string bam_sorted_minimap_temp = bam_sorted.substr(0, find) + ".minimap.temp.bam";
string bam_sorted_hisat = bam_sorted.substr(0, find) + ".hisat2.bam";
string merged_bam_file = bam_file.substr(0, find) + ".merged.bam";
string bam = bam_file.substr(0, find) + ".bam";
string command_minimap, command_bam_sort, command_sam_to_bam, command_bam_to_cov, command_cufflinks, command_mergeBam;
string command_minimap2 = exe_path + "external_tools/minimap2 -Y -ax sr " + reference_file + " " + left_reads_file + " " + right_reads_file + " -o " + sam_file + " -t " + threads + " --secondary=no > /dev/null 2>&1";
system(command_minimap2.c_str());
command_sam_to_bam = "samtools view -bS -F 4 " + sam_file + " -o " + bam + " -@ " + thread_sam + " > /dev/null 2>&1";
system(command_sam_to_bam.c_str());
command_bam_sort = "samtools sort " + bam + " -o " + bam_sorted + " -@ " + threadsForSamSortS + " -m " + MemForSamSortS + "M > /dev/null 2>&1";
system(command_bam_sort.c_str());
utils.filter_sam(bam_sorted, bam_sorted_minimap_temp);
// to filter supplementary alignments
string command_filter = "samtools view -b -F 2048 " + bam_sorted_minimap_temp + " -o " + bam_sorted_minimap + " -@ " + thread_sam + " > /dev/null 2>&1";
system(command_filter.c_str());
string command_index = "samtools index " + bam_sorted_minimap + " > /dev/null 2>&1";
system(command_index.c_str());
command_bam_to_cov = "samtools depth -aa " + bam_sorted_minimap + " > " + cov_minimap + " 2> /dev/null";
system(command_bam_to_cov.c_str());
utils.remove_file(sam_file);
utils.remove_file(bam);
utils.remove_file(bam_sorted);
utils.remove_file(bam_sorted_minimap_temp);
// hisat
create_indexes_hisat(reference_file);
string command_hisat2 = exe_path + "external_tools/hisat2 --downstream-transcriptome-assembly -f -x " + reference_file + " -q -1" + left_reads_file + " -2 " + right_reads_file + " -S " + sam_file + " --dta-cufflinks -p " + threads + " > /dev/null 2>&1";
system(command_hisat2.c_str());
command_sam_to_bam = "samtools view -bS -F 4 " + sam_file + " -o " + bam + " -@ " + thread_sam + " > /dev/null 2>&1";
system(command_sam_to_bam.c_str());
command_bam_sort = "samtools sort " + bam + " -o " + bam_sorted + " -@ " + threadsForSamSortS + " -m " + MemForSamSortS + "M > /dev/null 2>&1";
system(command_bam_sort.c_str());
utils.filter_sam(bam_sorted, bam_sorted_hisat);
command_bam_to_cov = "samtools depth -aa " + bam_sorted_hisat + " > " + cov_hisat + " 2> /dev/null";
system(command_bam_to_cov.c_str());
// remove indexes
remove_indexes(reference_file);
utils.remove_file(sam_file);
utils.remove_file(bam);
utils.remove_file(bam_file);
utils.remove_file(bam_sorted);
// cufflink intron-exon splice site
size_t found = merged_bam_file.find_last_of("/\\");
command_cufflinks = exe_path + "external_tools/cufflinks " + bam_sorted_hisat + " -o " + merged_bam_file.substr(0, found) + " --library-type fr-firststrand -p " + threads + " > /dev/null 2>&1";
system(command_cufflinks.c_str());
return bam_sorted_minimap;
}
void alignment::create_indexes_minimap(const string& reference_file) {
stringstream ss;
ss << allowed_threads;
threads = ss.str();
ss.str(string());
ss << allowed_threads - 1;
string thread_sam = ss.str();
ss.str(string());
string command = exe_path + "external_tools/minimap2-build -f " + reference_file + " " + reference_file + " -p " + threads + " > /dev/null 2>&1"; //tmp_file;
system(command.c_str());
}
void alignment::create_indexes_hisat(const string& reference_file) {
//create the index
string command = exe_path + "external_tools/hisat2-build -f " + reference_file + " " + reference_file + " -p " + threads + " > /dev/null 2>&1"; //tmp_file;
system(command.c_str());
}
void alignment::remove_indexes(const string& reference_file) {
utils utils;
// remove index file
utils.remove_file(reference_file + ".1.ht2");
utils.remove_file(reference_file + ".2.ht2");
utils.remove_file(reference_file + ".3.ht2");
utils.remove_file(reference_file + ".4.ht2");
utils.remove_file(reference_file + ".5.ht2");
utils.remove_file(reference_file + ".6.ht2");
utils.remove_file(reference_file + ".7.ht2");
utils.remove_file(reference_file + ".8.ht2");
}
map<string, string> alignment::extract_fasta_data(string fastafile) {
AllFasta_data.clear(); // empty before updating again
string header = "", entry;
stringstream seq;
map<string, string> fasta_data;
ifstream fasta_file;
fasta_file.open(fastafile.c_str());
while (!(getline(fasta_file, entry).eof())) {
if (entry[0] == '>') { // contig numbers + 1
fasta_data[header] = seq.str(); // global variable
header = entry.substr(1, entry.size());
seq.str(string());
} else
seq << entry;
}
fasta_data[header] = seq.str(); // global variable
fasta_file.close();
return fasta_data;
}