Quantitative Biology Program

So I need to write a c++ program that performs basic quantitative biology. We are supposed to have two separate files, a .h and .cpp file. I have written this code but it doesn't compile and I can't find out why. Any help is appreciated.

//.h file

#ifndef __JMCREADY_QUBIENGINE_H_
#define __JMCREADY_QUBIENGINE_H_
#include <vector>
#include <string>
using namespace std;

class QubiEngine{
private:
vector<string> dna;
public:
QubiEngine(); //Default constructor
QubiEngine(ifstream& dnaFile); //Reads lines from given input file and pushes every read DNA sequence into the dna vector.
void dna2rna(); //Reads the DNA from the dna vector and computes it's RNA, then stores them in a file rna.txt
void compute_revcom(); //Reads the DNA sequences from the dna vector, computes the reverse, and stores in revcom.txt
void nucl_frequency(); //Counts the total occurances of each nucleotide in the DNA sequence.
int count_CpG(); //Counts the occurance of the string "CG"
bool isPalindromic(); //Checks if the string is palindromic.

};

#endif


//.cpp file

#include "jmcready_QubiEngine.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;

QubiEngine::QubiEngine(ifstream& dnaFile){
string neucleotides; //Declares an empty string of nucelotides,
ifstream dnaFile("dna.txt"); //opens the file to be stored into the vector,
while (getline(dnaFile, neucleotides)){ //and while it is reading the whole line from the
dna.push_back(neucleotides); //dnaFile, it is pushing the nucelotides into the vector.
}
}


void QubiEngine::dna2rna(){
ofstream outfile;
outfile.open("rna.txt"); //Creates a blank text file
for (string::iterator it = dna.begin(), endOfString = dna.end(); it != endOfString; it++){
if (*it == 't'){ //for loop with a nested if statement to run through each
*it = 'u'; //member of the vector and check if the iterator is equal
outfile << *it; //to "t", if it is, it is changed to "u."
} //Then saves the new iterator value to the outfile.
}
outfile.close(); //Closes the file so no further changes can be made.
}

void QubiEngine::compute_revcom(){
char letter;

ofstream outfile;
outfile.open ("revcom.txt"); //Another blank text file.
vector<string> temp; //A dummy blank vector to store the reverse of the dna vector.
temp = reverse(dna.begin(), dna.end(); //Stores the reverse of vector dna to temp vector.
for (string::iterator it = temp.begin(), endOfString = temp.end(); it != endOfString; it++){
while (it != endOfString()){ //For loop with nested while loop to change
switch(letter){ //each element in the temp vector to it's complement element.
case 'a' :
*it = "t";
break;
case 't' :
*it = "a";
break;
case 'c' :
*it = "g";
break;
case 'g' :
*it = "c";
break;
default :
cout << "File contains elements that aren't valid, please fix."<< endl;
break;
}
}
}
outfile.close();
}

void QubiEngine::nucl_frequency(){
int numOfA = 0; //Declares multiple counts and letters
int numOfT = 0; //to keep track of each nucleotide seperately
int numOfC = 0;
int numOfG = 0;
char a = 'a';
char t = 't';
char c = 'c';
char g = 'g';

for (string::iterator it = dna.begin(), endOfString = dna.end(); it != endOfString; it++){
if (*it == a){ //For loop with multiple nested if statements
numOfA++; //to check each individual element
} //and count it towards the total.
if(*it == t){
numOfT++;
}
if (*it == c){
numOfC++;
}
if (*it == g){
numOfG++;
}
}
}

int QubiEngine::count_CpG(){
int count = 0;
string CpG = "cg";

for (string::iterator it = dna.begin(), endOfString = dna.end(); it != endOfString; it++){
if (*it == CpG){ //For loop with nested if statement to check
count++ //for the string "cg" and add them to an int
} //counter.
}

return count;
}

bool QubiEngine::isPalindromic(){
for (string::iterator it = dna.begin(), endOfString = dna.end(); it != endOfString; it++){
if(*it == string(dna.rbegin(), dna.rend())){ //For loop with nested if statement
return true; //to check if *it is equal to
else //the reverse of it's original,
return false; //if it is equal than it is palindromic.
}
}
}

int main (int args, char* argv[]) { //Main function given in the pdf, calls each

ifstream dnaFile; //public function to check if it works, and how
dnaFile.open(“dna.txt”);

QubiEngine qbengine(dnaFile); //effectively.
qbengine.dna2rna();

qbengine.compute_revcom();

qbengine.nucl_frequency();

int cpg = qbengine.count_CpG();
cout << “Total number of CpG islands: “ << cpg << endl;

if ( qbengine.isPalindromic() )
cout << “Palindromic” << endl;
else
cout << “Not Palindromic” << endl;
return 0;
}


Any help is appreciated, thanks.
You are mixing datatypes and using strings and vector<string>s as if it was one type.
Thanks for the reply, how do I fix this?
closed account (N85iE3v7)
As JockX said, instead of using

std::string::iterator

to iterate over your dna vector<string> use

 
std::vector<string>::iterator it;


std::string are a wrapper over old C char strings, but they do support iterators as well, they are still STL containers.

http://www.cplusplus.com/reference/string/string/
Last edited on
Thank you very much! I'll see how it works
I'm still getting compiler errors about failed conversions.
closed account (N85iE3v7)
try this instead

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//vector<string> temp; //A dummy blank vector to store the reverse of the dna vector.
//temp reverse(dna.begin(), dna.end() ); //Stores the reverse of vector dna to temp vector.

vector<string>& temp = dna ; //A dummy blank vector to store the reverse of the dna vector.

reverse(temp.begin(), temp.end() ); //Stores the reverse of vector dna to temp vector.


//for (string::iterator it = temp.begin(), endOfString = temp.end(); it != endOfString; it++)
for (vector<string>::const_iterator it = temp.begin() ; it != temp.end(); it++)
{
	std::string tmpst = *it;

	string::iterator st = tmpst.begin();

	while ( st  != tmpst.end() )
	{   //For loop with nested while loop to change
		
		char c = *st;
		st++;
	}
}


Please check http://www.cplusplus.com/reference/algorithm/reverse/

the reverse algorithm will work on a container, it is not a member function of an STL container.

Edit: if you are using a C++11 compliant compiler drop the whole declarations for iterators for auto It.
Last edited on
Thank you.
closed account (N85iE3v7)
hey I made a mistake above, sorry:

vector<string>& temp = dna ;

You don't want to change the contents of temp, you want to copy it.
Sorry, if you do like I said above you will change the contents of dna.

Replace for::

vector<string> temp = dna ;

The reference operator would create an alias and ruin down all your dna vector.
Topic archived. No new replies allowed.