Created
July 30, 2016 14:25
-
-
Save Swarchal/b118b8e3e57a8fe646e5a43b9e2a4349 to your computer and use it in GitHub Desktop.
revp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <algorithm> | |
#include <cassert> | |
using namespace std; | |
////////////////////////////////////////////////////// | |
// Finding restriction sites (reverse palindromes) // | |
////////////////////////////////////////////////////// | |
vector<string> parse_fasta(string file_path) | |
{ | |
vector<string> fasta_out; | |
string line, first_char; | |
ifstream infile(file_path); | |
if (infile.is_open()) { | |
while (getline(infile, line)) { | |
first_char = line[0]; | |
if (first_char != ">") { | |
fasta_out.push_back(line); | |
} | |
} | |
} else cerr << "OH NO! can't open file"; | |
infile.close(); | |
return fasta_out; | |
} | |
string complement(const string& s) | |
{ | |
string n, out_string; | |
for (int i=s.length()-1; i>=0; i--) { | |
n = toupper(s[i]); | |
if (n == "A") { | |
n = "T"; | |
} else if (n == "T") { | |
n = "A"; | |
} else if (n == "C") { | |
n = "G"; | |
} else if (n == "G") { | |
n = "C"; | |
} else { | |
cerr << "Unknown char: " << static_cast<int>(s[i]) << endl; | |
} | |
out_string += n; | |
} | |
assert(out_string.length() == s.length()); | |
return out_string; | |
} | |
void find_restrction_site(const vector<string> v, const int floor=4, const int ceiling=12) | |
{ | |
// find reverse palindromes between length floor and ceiling | |
const string s = string(v[0]); | |
string win, first_half, second_half, c_second_half; | |
// sliding window? ... sliding window!! | |
for (int win_size = floor; win_size <= ceiling; win_size+=2) { | |
for (int i=0; i<=(s.length()-win_size); ++i) { | |
win = s.substr(i, win_size); // window sub-sequence | |
// split sub-sequence into two halves | |
first_half = win.substr(0, win.length()/2); | |
second_half = win.substr(win.length()/2, win.length()); | |
// get reverse complement of second half | |
c_second_half = complement(second_half); | |
if (first_half == c_second_half) { | |
// return index and window size | |
cout << i+1 << "\t" << win_size << endl; | |
} | |
} | |
} | |
cout << endl; | |
} | |
int main() | |
{ | |
vector<string> fasta = parse_fasta("/home/scott/Downloads/rosalind_revp.txt"); | |
find_restrction_site(fasta); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compile with
g++ -std=c++11 revp.cpp
output: