2009-03-09 46 views
0

代替キー値からマップの作成:私は何をしたいかが、私はこのようになり、データ持っ入力

>day11:1:356617 
ACTTCTGATTCTGACAGACTCAGGAAGAAACCAT 
>day11:2:283282 
CTCAGCCCGTAGCCCGTCGGTTCCGGAGTAAGTT 
>day11:3:205058 
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 
>day11:4:202520 
AGTTCGATCGGTAGCGGGAGCGGAGAGCGGACCC 
>day11:5:107099 
AGGCATTCAGGCAGCGAGAGCAGAGCAGCGTAGA 
>day11:6:106715 
CTCTTTGCCCCATCTACTGCGAGGATGAAGACCA 

はラインで、マップを作成することです キーとして「>」で開始値としてのACGT。

しかし、私のこの構造は機能しませんか?マップは私が期待した通りに の値を取り込めないようです。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <map> 
int main() { 

    ifstream myfile ("mydata.txt"); 

    map <string,string>FastaMap; 

    cerr << "Read Fasta File to Map" << endl; 

    if (myfile.is_open()) 
    { 
     while (getline(myfile,line)) 
     { 
      stringstream ss(line); 
      string Fasta; 
      string Header = ""; 
      string Tag = ""; 

      ss >> Fasta; // read first column 

      if (Fasta[0] == '>') { 
       // get header only 
       Header = Fasta.substr(1); 
       //cerr << Header << endl; 
      } 
      else { 
       Tag = Fasta; 
      } 


      if (Header != "" || Tag != "") { 
       FastaMap[Header] = Tag; 
       //cout << "TAG: " << Tag << endl; 
       //cout << "Head: " << Header << endl; 
       // FastaMap.insert(make_pair(Header,Tag)); 
      } 
     } 
     myfile.close(); 
    } 
    else { 
     cout << "Unable to open file"; 
    } 

    // This doesn't print the second value, only prints the first 

    for (map<string,string>::iterator it = FastaMap.begin(); it!= 
      FastaMap.end(); it++) { 
     cout << "Head: " << (*it).first << ", End: " << (*it).second << endl; 
    } 

} 

予想される出力は次のとおりです。

Head: day11:1:356617, End: ACTTCTGATTCTGACAGACTCAGGAAGAAACCAT 
Head: day11:2:283282, End: CTCAGCCCGTAGCCCGTCGGTTCCGGAGTAAGTT 
...etc... 

答えて

4

クリーニングはそれぞれループFastaHeaderTagです。あなたがしなければならないことは次のとおりです。

  1. ではなく||&&を使用するif (Header != "" || Tag != "")ライン(論理エラーがあります)
  2. がリセット
  3. (直前に)変更しながら、外の変数を宣言しますタグとヘッダー変数をマップに追加するときに使用します。

正しいコードは次のとおりです。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <map> 
using namespace std; 
int main() { 
     string line; 
     ifstream myfile ("test"); 

     map <string,string> FastaMap; 

     cerr << "Read Fasta File to Map" << endl; 

     if (myfile.is_open()) 
     { 
       string Fasta; 
       string Header = ""; 
       string Tag = ""; 
       while (getline(myfile,line)) 
       { 

         stringstream ss(line); 

         ss >> Fasta; // read first column 

         if (Fasta[0] == '>') { 
           // get header only 
           Header = Fasta.substr(1); 
           //cerr << Header << endl; 
         } 
         else { 
           Tag = Fasta; 
         } 


         if (Header != "" && Tag != "") { 
           FastaMap[Header] = Tag; 
           cout << "TAG: " << Tag << endl; 
           cout << "Head: " << Header << endl; 
           Header = ""; 
           Tag = ""; 
           // FastaMap.insert(make_pair(Header,Tag)); 
         } 
       } 
       myfile.close(); 
     } 
     else { 
       cout << "Unable to open file"; 
     } 

     // This doesn't print the second value, only prints the first 

     for (map<string,string>::iterator it = FastaMap.begin(); it!= 
        FastaMap.end(); it++) { 
       cout << "Head: " << (*it).first << ", End: " << (*it).second << endl; 
     } 

} 

注意をコードする他の可能な拡張機能がありますが、それが今であるとして動作します。

1

バグの場合には: 場合(! "!||タグ=" ヘッダー= "")する必要があります: (ヘッダー= "" あれば! & &タグ= "")さらに

:!

if (Header != "" && Tag != "") { 
        FastaMap[Header] = Tag; 
        Header = ""; 
        Tag = ""; 
} 
関連する問題