1

基本的な量的生物学操作を実行するプログラムを書いています。.h と .cpp という 2 つの別個のファイルがあります。.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;
};               

編集: できる限り多くの間違いを修正することができました。残っているのはいくつかのコンパイルエラーだけです。誰かが私にいくつかのヒントを教えてくれることを望んでいました. エラーは下部のコメントにあります

4

2 に答える 2

1

QubiEngine::QubiEngine(ifstream& dnaFile)しないで

dnaFile("dna.txt");

すでに初期化されており(でmain)、とにかく正しいシンタックスではありません。

dna2rna *itですstringので、*it == 't'と を比較しようとしstringますchar。動作しませんが、これを修正する方法は既に知っています。string::iterator他の場所で使用し、それを使用して文字列内のすべてを反復処理しchar、 と比較charcharます。

compute_revcomで初期化することはありませんがletterswitchで使用しますststring::iteratorまた、で を進めませんwhile't'の代わりに使用する必要もあります"t"。1 つはで、もう 1 つはchar文字列です。

再び aaとnucl_frequencya を比較し、を使用します。stringcharstring::iterator

count_CpGの型を忘れた場合、これdnaは文字列のベクトルです。適切なイテレータ型を使用してください。また、*it == CpG2 つの文字列が等しいかどうかを比較しますが、これは望ましくありません。

「cgcg」の場合*it、カウントを 2 にしたいのですが、実際には 0 になりstring::iteratorます。このタスクに使用できるかどうかを確認してください。

また

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

その「&」のために、それはあなたが思っていることをしませんtempdna(正確に)コピーではありません。

おそらく何トンもあります。

一度にすべてを記述して最後にビルドしないようにしてください。代わりに、関数または関数の一部を作成し、それをビルド (およびテスト) します。

コンパイルして動作する小さなプログラムから、コンパイルして動作する少し大きなプログラムに移行します。完了するまで繰り返します!

于 2013-05-10T22:31:02.220 に答える
0

ヘッダー ファイルに追加#include <fstream>してみてください。不足しているようです (の場合ifstream)

そして、実際には、main.cpp のような別のファイルに移動てみmainて ください。#include "jmcready_QubiEngine.h"

于 2013-05-10T21:30:20.713 に答える