基本使用类来执行基本的定量生物学

时间:2013-05-10 21:24:46

标签: c++

我正在编写一个执行基本定量生物学操作的程序。我有两个单独的文件,.h和.cpp。 .h包含我的带有函数原型的类,而我的.cpp包含我的函数定义。我觉得我正确编写了代码,但它无法编译。这是我的代码:

//.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;
};               
编辑:我已尽力解决尽可能多的错误。唯一剩下的就是一些编译器错误,希望有人能给我一些提示。错误发生在底部的评论中

2 个答案:

答案 0 :(得分:1)

QubiEngine::QubiEngine(ifstream& dnaFile)中不要

dnaFile("dna.txt");

它已经初始化(在main中)并且它无论如何都不是正确的sintax。

dna2rna *itstring*it == 't',因此在string您尝试将charstring::iterator进行比较。它不起作用,但您已经知道如何解决此问题:您在其他位置使用char,使用它来遍历字符串中的所有char并将char与{{1}进行比较}。

compute_revcom中,您永远不会初始化letter,但是您在switch中使用它,请使用st。 您也不会提前string::iterator中的while。您还需要使用't'代替"t",一个是char,另一个是字符串。

再次在nucl_frequency中,您将stringchar进行比较,使用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.

因为那个“&amp;”它不符合您的想法,tempdna(确切地)而不是副本。

可能还有更多。

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

从编译和工作的小程序转移到编译和工作的稍大程序。重复直到完成!

答案 1 :(得分:0)

尝试将#include <fstream>添加到您的头文件中,似乎您遗漏了它(对于ifstream

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