1

I'm writing a program that performs basic quantitative biology operations. I have two separate files, a .h and .cpp. The .h contains my class with function prototypes and my .cpp contains my function definitions. I feel like I wrote the code correctly but it won't compile. Here is my code:

//.h file

#ifndef __JMCREADY_QUBIENGINE_H_
#define __JMCREADY_QUBIENGINE_H_
#include <vector>
#include <string>
#include <fstream>
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,
                                        //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
   const char* foo = "t";
   for (vector<string>::iterator it = dna.begin(), endOfString = dna.end(); it != endOfString; it++){
    if ( find(dna.begin(), dna.end(), foo) != dna.end() ){              //for loop with a nested if statement to run through each
        dna.at(*foo) = "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 = '\0';

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.
reverse( dna.begin(), dna.end() );  //Stores the reverse of vector dna to temp vector.
               for (vector<string>::iterator it = temp.begin(), endOfString = temp.end(); it != endOfString; it++){
                   while ( it != dna.end() ){       //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;
    string a = "a";
    string t = "t";
    string c = "c";
    string g = "g";

    for (vector<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 (vector<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 (vector<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{
                    return false;
            }                            //the reverse of it's original,
                                                    //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 << "The total number of CG islands is : " << cpg << endl;

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

EDIT: I've managed to fix as many mistakes as I can. The only thing left are a few compiler errors, was hoping if someone can give me some tips. The errors are in the comments at the bottom

4

2 回答 2

1

QubiEngine::QubiEngine(ifstream& dnaFile)不做

dnaFile("dna.txt");

它已经初始化(in main),无论如何它不是正确的sintax。

In dna2rna *itis a stringso in*it == 't'您尝试将 astring与 a进行比较char。它不起作用,但您已经知道如何解决这个问题:您string::iterator在其他地方使用,用它来遍历char字符串中的所有内容并charchar.

compute_revcom你从不初始化letter但你在 a 中使用它时switch,使用st. 您也不要string::iteratorwhile. 您还需要使用't'代替"t",一个是 a char,另一个是字符串。

nucl_frequency再次将 aastring与 a进行比较,char使用string::iterator.

如果count_CpG您忘记了 的类型dna,它是一个字符串向量,请使用正确的迭代器类型。此外,*it == CpG将比较两个字符串是否相等,这不是您想要的。

如果*it是“cgcg”,您希望计数为 2,但实际上为 0。看看您是否可以string::iterator用于此任务。

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

因为那个“&”它不会按照你的想法做,temp将是dna(确切地)而不是副本。

有吨的可能性更大。

尽量不要一次编写所有内容并最终构建。相反,编写一个函数或函数的一部分并构建(和测试)它。

从一个可以编译和工作的小程序转移到一个稍微大一点的可以编译和工作的程序。重复直到完成!

于 2013-05-10T22:31:02.220 回答
0

尝试添加#include <fstream>到您的头文件中,您似乎缺少它(for ifstream

而且,实际上,尝试移动到单独的文件main例如 main.cpp,其中只有 .cpp#include "jmcready_QubiEngine.h"

于 2013-05-10T21:30:20.713 回答