从备用键值输入创建映射

时间:2009-03-09 08:15:52

标签: c++ algorithm data-structures loops

我的数据如下:

>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...

2 个答案:

答案 0 :(得分:4)

您正在清除每个循环FastaHeaderTag。你要做的是:

  1. 在while(就在它之前)
  2. 之外声明那些变量
  3. if (Header != "" || Tag != "")行更改为使用&&而非||(此处存在逻辑错误)
  4. 将标记和标题变量添加到地图时重置它们。
  5. 正确的代码如下:

    #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 :(得分:1)

如果出现以下错误: if(Header!=“”|| Tag!=“”)应该是: if(Header!=“”&amp;&amp; Tag!=“”)

更多:

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