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 ?
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.
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?
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
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.
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;
}
}
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.