2009-03-09 4 views
0

Ich habe ein Datum, die wie folgt aussieht:Erstellen Karte von Alternate Key Wert Eingang

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

Was ich tun möchte, ist eine Karte, mit der Linie mit „>“ als der Taste gestartet erstellen und dem ACGT als Wert.

Allerdings funktioniert dieses Konstrukt von mir nicht? Die Karte scheint den Wert nicht zu erfassen, wie ich erwartet habe.

#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; 
    } 

} 

Die erwartete Ausgabe lautet:

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

Antwort

4

Sie reinigen Fasta, Header und Tag jede Schleife. Was Sie tun müssen, ist:

  1. diese Variablen außerhalb der während Deklarieren (kurz bevor es)
  2. Ändern der if (Header != "" || Tag != "") Linie mit && statt || (es gibt einen Logikfehler gibt)
  3. das Zurücksetzen Tag- und Header-Variablen, wenn Sie sie der Map hinzufügen.

Der richtige Code folgt:

#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; 
     } 

} 

Hinweis gibt es andere mögliche Verbesserungen an den Code, aber wie es jetzt funktioniert, ist.

1

Bug, wenn: if (!-Header = ""! || Tag = "") sollte sein: wenn (Header = "" ! & & Tag = "")

mehr noch:

if (Header != "" && Tag != "") { 
        FastaMap[Header] = Tag; 
        Header = ""; 
        Tag = ""; 
}