5 * Created by Sarah Westcott on 1/16/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
11 #include "sequence.hpp"
13 /*******************************************************************************/
15 void FastaMap::readFastaFile(string inFileName) {
18 m->openInputFile(inFileName, in);
19 string name, sequence, line;
22 map<string, string>::iterator itName;
26 if (m->control_pressed) { break; }
29 name = currSeq.getName();
32 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
33 else { sequence = currSeq.getUnaligned(); }
35 itName = seqmap.find(name);
36 if (itName == seqmap.end()) { seqmap[name] = sequence; }
37 else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); }
39 map<string,group>::iterator it = data.find(sequence);
40 if (it == data.end()) { //it's unique.
41 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
42 // data[sequence].groupnumber = 1;
43 data[sequence].names = name;
44 }else { // its a duplicate.
45 data[sequence].names += "," + name;
46 // data[sequence].groupnumber++;
54 m->errorOut(e, "FastaMap", "readFastaFile");
59 /*******************************************************************************/
61 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
64 m->openInputFile(oldNameFileName, oldNameFile);
66 map<string,string> oldNameMap;
67 map<string, string>::iterator itName;
69 while(!oldNameFile.eof()){
70 if (m->control_pressed) { break; }
72 oldNameFile >> name; m->gobble(oldNameFile);
74 oldNameMap[name] = list;
75 m->gobble(oldNameFile);
80 m->openInputFile(inFastaFile, inFASTA);
82 while(!inFASTA.eof()){
83 if (m->control_pressed) { break; }
85 Sequence currSeq(inFASTA);
86 name = currSeq.getName();
89 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
90 else { sequence = currSeq.getUnaligned(); }
92 itName = seqmap.find(name);
93 if (itName == seqmap.end()) { seqmap[name] = sequence; }
94 else { m->mothurOut("You already have a sequence named " + name + ", sequence names must be unique, please correct."); m->mothurOutEndLine(); }
96 seqmap[name] = sequence;
97 map<string,group>::iterator it = data.find(sequence);
98 if (it == data.end()) { //it's unique.
99 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
100 // data[sequence].groupnumber = 1;
101 data[sequence].names = oldNameMap[name];
102 }else { // its a duplicate.
103 data[sequence].names += "," + oldNameMap[name];
104 // data[sequence].groupnumber++;
114 /*******************************************************************************/
116 string FastaMap::getGroupName(string seq) { //pass a sequence name get its group
117 return data[seq].groupname;
120 /*******************************************************************************/
122 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
123 return data[seq].names;
126 /*******************************************************************************/
128 string FastaMap::getSequence(string name) {
130 map<string,string>::iterator it = seqmap.find(name);
131 if (it == seqmap.end()) { return "not found"; }
132 else { return it->second; }
136 /*******************************************************************************/
138 void FastaMap::push_back(string name, string seq) {
140 map<string,group>::iterator it = data.find(seq);
141 if (it == data.end()) { //it's unique.
142 data[seq].groupname = name; //group name will be the name of the first duplicate sequence found.
143 data[seq].names = name;
144 }else { // its a duplicate.
145 data[seq].names += "," + name;
150 /*******************************************************************************/
152 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
156 /*******************************************************************************/
158 void FastaMap::printNamesFile(string outFileName){ //prints data
161 m->openOutputFile(outFileName, outFile);
163 // two column file created with groupname and them list of identical sequence names
164 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
165 if (m->control_pressed) { break; }
166 outFile << it->second.groupname << '\t' << it->second.names << endl;
170 catch(exception& e) {
171 m->errorOut(e, "FastaMap", "printNamesFile");
176 /*******************************************************************************/
178 void FastaMap::printCondensedFasta(string outFileName){ //prints data
181 m->openOutputFile(outFileName, out);
182 //creates a fasta file
183 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
184 if (m->control_pressed) { break; }
185 out << ">" << it->second.groupname << endl;
186 out << it->first << endl;
190 catch(exception& e) {
191 m->errorOut(e, "FastaMap", "printCondensedFasta");
196 /*******************************************************************************/