2009-03-09 7 views
0

J'ai données qui ressemble à ceci:Création Carte de la valeur de clé alternative d'entrée

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

Ce que je veux faire est de créer une carte, avec ligne commençant par « > » comme la clé et la ACGT comme valeur.

Cependant cette construction du mien ne fonctionne pas? La carte semble échouer à capturer la valeur comme je m'y attendais.

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

} 

Le résultat attendu est:

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

Répondre

4

Vous nettoyez Fasta, Header et Tag chaque boucle. Qu'est-ce que vous avez à faire est:

  1. Déclarez les variables en dehors du temps (juste avant)
  2. Modifier la ligne if (Header != "" || Tag != "") à l'aide && au lieu de || (il y a une erreur logique)
  3. Réinitialiser la Variables d'étiquette et d'en-tête lorsque vous les ajoutez à la carte.

Le code correct suit:

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

} 

Notez qu'il existe d'autres améliorations possibles au code, mais comme il est travaille maintenant.

1

Bug si: if (! En-tête! = "" || Tag = "") devrait être: si (en-tête = "" ! & & Tag = "")

Encore plus:

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