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;
68 while(!oldNameFile.eof()){
69 if (m->control_pressed) { break; }
71 oldNameFile >> name >> list;
72 oldNameMap[name] = list;
73 m->gobble(oldNameFile);
78 m->openInputFile(inFastaFile, inFASTA);
80 while(!inFASTA.eof()){
81 if (m->control_pressed) { break; }
83 Sequence currSeq(inFASTA);
84 name = currSeq.getName();
87 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
88 else { sequence = currSeq.getUnaligned(); }
90 seqmap[name] = sequence;
91 map<string,group>::iterator it = data.find(sequence);
92 if (it == data.end()) { //it's unique.
93 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
94 // data[sequence].groupnumber = 1;
95 data[sequence].names = oldNameMap[name];
96 }else { // its a duplicate.
97 data[sequence].names += "," + oldNameMap[name];
98 // data[sequence].groupnumber++;
108 /*******************************************************************************/
110 string FastaMap::getGroupName(string seq) { //pass a sequence name get its group
111 return data[seq].groupname;
114 /*******************************************************************************/
116 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
117 return data[seq].names;
120 /*******************************************************************************/
122 string FastaMap::getSequence(string name) {
124 map<string,string>::iterator it = seqmap.find(name);
125 if (it == seqmap.end()) { return "not found"; }
126 else { return it->second; }
130 /*******************************************************************************/
132 void FastaMap::push_back(string name, string seq) {
134 map<string,group>::iterator it = data.find(seq);
135 if (it == data.end()) { //it's unique.
136 data[seq].groupname = name; //group name will be the name of the first duplicate sequence found.
137 data[seq].names = name;
138 }else { // its a duplicate.
139 data[seq].names += "," + name;
144 /*******************************************************************************/
146 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
150 /*******************************************************************************/
152 void FastaMap::printNamesFile(string outFileName){ //prints data
155 m->openOutputFile(outFileName, outFile);
157 // two column file created with groupname and them list of identical sequence names
158 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
159 if (m->control_pressed) { break; }
160 outFile << it->second.groupname << '\t' << it->second.names << endl;
164 catch(exception& e) {
165 m->errorOut(e, "FastaMap", "printNamesFile");
170 /*******************************************************************************/
172 void FastaMap::printCondensedFasta(string outFileName){ //prints data
175 m->openOutputFile(outFileName, out);
176 //creates a fasta file
177 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
178 if (m->control_pressed) { break; }
179 out << ">" << it->second.groupname << endl;
180 out << it->first << endl;
184 catch(exception& e) {
185 m->errorOut(e, "FastaMap", "printCondensedFasta");
190 /*******************************************************************************/