Map Insert Not Working (Read Latest)

I'm trying to open a file & create a map that contains codon/amino acid information. I'm really bad at maps and am just starting to learn/use them though, so I'm stuck :(

For example, there is a codon chart file called codons.txt that looks like:
1
2
3
4
5
TTT	Phe	
TCT	Ser	
TAT	Tyr	
TGT	Cys
etc.


Is there a way to have all of these set to a map called codon_map so that it's easy to decode a string of DNA such that "TTTTAT" translates to Phe-Tyr ?
Last edited on
closed account (o3hC5Di1)
Hi there,

Might I suggest the use of std::unordered_map for this case. It is an associative container, like std::map, but without the overhead of sorting. As you are aiming to maintain a "dictionary", it's probably more suited. Note that it is only available in C++11 though and that it works very similar to std::map.

In your case, you could something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <unordered_map>
#include <string>

std::unordered_map<std::string, std::string> dictionary;  //keytype=string, valuetype=string

//You'll need to read the data for the following from the file of course
dictionary["TTT"] = "Phe";
dictionary["TCT"] = "Ser";
dictionary["TAT"] = "Tyr";

//using simple substring
std::string input_string = "TTTTAT";
std::string output_string;

for (size_t i=0; i<input_string.size(); i+=3)
   output_string.append( input_string.(i, 3));


It's just a crude example to get you started of course, please do let us know if you have any further questions.

All the best,
NwN


Ther'es literally a textfile called codons.txt that is essentially a dictionary of codons/amino acids I'm supposed to use for this project. Is there a way to write the file to a map without manually writing out all of the combinations?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <map>
#include <string>
#include <fstream>

std::map<std::string,std::string> make_map( const char* path = "codons.txt" )
{
    std::map<std::string,std::string> map ;

    std::ifstream file(path) ; // *** EDIT *** fstream => ifstream
    std::string codon, amino_acid ;
    while( file >> codon >> amino_acid ) map.emplace( codon, amino_acid ) ;
                                          
    return map ;
}
Last edited on
Gives Error "unexpected token: ;" on lines 7,9,10 and unable to resolve identifier amino_aid & emplace.
closed account (o3hC5Di1)
Hi there,

I believe Mr. Borges snippet is meant to be included in your program, as it is a separate function, intended to be called from your main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include <map>
#include <string>
#include <fstream>

std::map<std::string,std::string> make_map( const char* path = "codons.txt" );

int main(int charc, char** argv[])
{
    std::map<std::string, std::string> dictionary = make_map();

   //now you can use dictionary in the rest of main()
}

//JLBorges function

std::map<std::string,std::string> make_map( const char* path = "codons.txt" )
{
    std::map<std::string,std::string> map ;

    std::fstream file(path) ;
    std::string codon, amino_acid ;
    while( file >> codon >> amino_acid ) map.emplace( codon, amino_acid ) ;
                                          
    return map ;
}


Hope that helps.

All the best,
NwN
Is there a way to do it with stringstreams?
closed account (o3hC5Di1)
Could you please be more specific? Do what with stringstreams - reading from the file would not require the use of stringstreams. You could use one to put together
ratham wrote:
"TTTTAT" translates to Phe-Tyr


All the best,
NwN
So, I have a function that correctly writes the text file line by line such as:
TTT Phe
TCT Ser
etc.

How do I get my code to write to the map? I want TTT to be assigned to be the first string (key) and Phe to be the second (value).

*Edit: The text file is 65 lines long so the map's length should be 65, right? When I run the code it says that the map is only 22 long and it repeats itself a bunch.

This is the "broken" output I'm getting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
ATG START codon_map.size() is 1
TAA STOP codon_map.size() is 2
TAG STOP codon_map.size() is 2
TGA STOP codon_map.size() is 2
TTT Phe codon_map.size() is 3
TCT Ser codon_map.size() is 4
TAT Tyr codon_map.size() is 5
TGT Cys codon_map.size() is 6
TTC Phe codon_map.size() is 6
TCC Ser codon_map.size() is 6
TAC Tyr codon_map.size() is 6
TGC Cys codon_map.size() is 6
TTA Leu codon_map.size() is 7
[code]

My function is:
void fill_codon (map<string, string> &codon_map, ifstream &in_file)
{
    string line = "";
    string codon, triname, word, word2;

    while (getline(in_file, line)) 
    {
        istringstream iss(line);
        
        while(iss >> word)
        {
            cout << word << + " ";
            iss >> word;
            
            codon_map.insert(pair<string, string> (word , word));
            
            cout << word + " " << "codon_map.size() is " << codon_map.size() << endl;
            
            
        }
        //cout << line + " " << endl;
       
    }
    
}
Last edited on
closed account (o3hC5Di1)
Hi there,

You have a little error:

29
30
31
iss >> word >> word2;
            
codon_map.insert(pair<string, string> (word , word2));


All the best,
NwN
Ah, so the problem was:

1
2
3
 iss >> word;
            
            codon_map.insert(pair<string, string> (word , word));


Declaring iss >> word the 2nd time made both of the "words" in the map.insert the same thing, correct?

Thank you for all of your help/input btw NwN :)
closed account (o3hC5Di1)
Since you only do iss >> word; you only extract the first word from the line. You need to get both words and then pass those correctly to the insert member function.

Most welcome :)

All the best,
NwN
Topic archived. No new replies allowed.