Skip to content

Commit

Permalink
overload populate_from_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
AlaaALatif committed Feb 5, 2021
1 parent 9b4ee07 commit 60e8e6b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/get_masked_amplicons.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "get_masked_amplicons.h"

int get_primers_with_mismatches(std::string bed, std::string vpath, std::string out, std::string primer_pair_file){
int32_t primer_offset = 0;
std::vector<primer> primers = populate_from_file(bed, primer_offset);
std::vector<primer> primers = populate_from_file(bed);
std::vector<primer> mismatches_primers;
std::vector<primer> tmp;
if(primers.size() == 0){
Expand Down
69 changes: 69 additions & 0 deletions src/primer_bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,75 @@ std::vector<primer> populate_from_file(std::string path, int32_t offset = 0){
return primers;
}

std::vector<primer> populate_from_file(std::string path){
std::ifstream data(path.c_str());
std::string line;
std::vector<primer> primers;
int16_t indice = 0;
while(std::getline(data,line)){ // Remove extra lineStream
std::stringstream lineStream(line);
std::string cell;
int ctr = 0;
primer p;
p.set_strand(0); // Set strand to NULL
while(std::getline(lineStream,cell,'\t')){
switch(ctr){
case 0:
p.set_region(cell);
break;
case 1:
if(std::all_of(cell.begin(), cell.end(), ::isdigit)) {
p.set_start(std::stoul(cell));
} else {
print_bed_format();
primers.clear();
return primers;
}
break;
case 2:
if(std::all_of(cell.begin(), cell.end(), ::isdigit)) {
p.set_end(std::stoul(cell) - 1); // Bed format - End is not 0 based
} else {
print_bed_format();
primers.clear();
return primers;
}
break;
case 3:
p.set_name(cell);
break;
case 4:
if(std::all_of(cell.begin(), cell.end(), ::isdigit)) {
p.set_score(stoi(cell));
} else {
print_bed_format(); // score is missing, send warning but continue populating
std::cout << "\nWARNING: The BED file provided did not have the expected score column, but iVar will continue trimming\n" << std::endl;
p.set_score(-1);
}
break;
case 5:
if(cell[0] == '+' || cell[0] == '-')
p.set_strand(cell[0]);
else {
print_bed_format();
primers.clear();
return primers;
}
}
ctr++;
}
if(indice == 0 && ctr < 6)
std::cout << "Strand not found in primer BED file so strand will not be considered for trimming" << std::endl;
p.set_indice(indice);
p.set_pair_indice(-1);
p.set_read_count(0);
primers.push_back(p);
indice++;
}
std::cout << "Found " << primers.size() << " primers in BED file" << std::endl;
return primers;
}

std::vector<primer> get_primers(std::vector<primer> p, unsigned int pos){
std::vector<primer> primers_with_mismatches;
for(std::vector<primer>::iterator it = p.begin(); it != p.end(); ++it) {
Expand Down
1 change: 1 addition & 0 deletions src/primer_bed.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class primer {
};

std::vector<primer> populate_from_file(std::string path, int32_t offset);
std::vector<primer> populate_from_file(std::string path);
std::vector<primer> get_primers(std::vector<primer> p, unsigned int pos);
int get_primer_indice(std::vector<primer> p, std::string name);
int populate_pair_indices(std::vector<primer> &primers, std::string path);
Expand Down
3 changes: 1 addition & 2 deletions src/remove_reads_from_amplicon.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "remove_reads_from_amplicon.h"

int rmv_reads_from_amplicon(std::string bam, std::string region_, std::string bam_out, std::vector<std::string> amp, std::string bed, std::string cmd){
int32_t primer_offset = 0;
std::vector<primer> primers = populate_from_file(bed, primer_offset);
std::vector<primer> primers = populate_from_file(bed);
if(primers.size() == 0){
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/test_primer_bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ int main(){
int num_tests = 2;
int success = 0;
std::vector<primer>::iterator it;
int32_t primer_offset = 0;
std::vector<primer> primers = populate_from_file("../data/test.bed", primer_offset);
std::vector<primer> primers = populate_from_file("../data/test.bed");
std::string primer_names[] = {"WNV_400_1_LEFT", "WNV_400_1_LEFT_alt", "WNV_400_2_LEFT", "WNV_400_1_RIGHT", "WNV_400_2_RIGHT", "WNV_400_3_LEFT", "WNV_400_2_LEFT_alt", "WNV_400_2_RIGHT_alt"};
int primer_indices[] = {0,1,2,3,4,5,6,7,8};
unsigned int primer_start[] = {8,7, 230, 359,658,569,251,352};
Expand Down
3 changes: 1 addition & 2 deletions tests/test_primer_trim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
int main(){
int success = 0;
std::string bam = "../data/test.unmapped.sorted.bam";
int32_t primer_offset = 0;
std::vector<primer> primers = populate_from_file("../data/test.bed", primer_offset);
std::vector<primer> primers = populate_from_file("../data/test.bed");
int max_primer_len = 0;
max_primer_len = get_bigger_primer(primers);
std::string region_;
Expand Down
3 changes: 1 addition & 2 deletions tests/test_unpaired_trim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
int main(){
int success = 0;
std::string bam = "../data/test.sim.merged.sorted.bam";
int32_t primer_offset = 0;
std::vector<primer> primers = populate_from_file("../data/test_merged.bed", primer_offset);
std::vector<primer> primers = populate_from_file("../data/test_merged.bed");
int max_primer_len = 0;
max_primer_len = get_bigger_primer(primers);
std::string region_;
Expand Down

0 comments on commit 60e8e6b

Please sign in to comment.