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;
24 if (m->control_pressed) { break; }
27 name = currSeq.getName();
30 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
31 else { sequence = currSeq.getUnaligned(); }
33 seqmap[name] = sequence;
34 map<string,group>::iterator it = data.find(sequence);
35 if (it == data.end()) { //it's unique.
36 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
37 // data[sequence].groupnumber = 1;
38 data[sequence].names = name;
39 }else { // its a duplicate.
40 data[sequence].names += "," + name;
41 // data[sequence].groupnumber++;
49 m->errorOut(e, "FastaMap", "readFastaFile");
54 /*******************************************************************************/
56 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
59 m->openInputFile(oldNameFileName, oldNameFile);
61 map<string,string> oldNameMap;
63 while(!oldNameFile.eof()){
64 if (m->control_pressed) { break; }
66 oldNameFile >> name >> list;
67 oldNameMap[name] = list;
68 m->gobble(oldNameFile);
73 m->openInputFile(inFastaFile, inFASTA);
75 while(!inFASTA.eof()){
76 if (m->control_pressed) { break; }
78 Sequence currSeq(inFASTA);
79 name = currSeq.getName();
82 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
83 else { sequence = currSeq.getUnaligned(); }
85 seqmap[name] = sequence;
86 map<string,group>::iterator it = data.find(sequence);
87 if (it == data.end()) { //it's unique.
88 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
89 // data[sequence].groupnumber = 1;
90 data[sequence].names = oldNameMap[name];
91 }else { // its a duplicate.
92 data[sequence].names += "," + oldNameMap[name];
93 // data[sequence].groupnumber++;
103 /*******************************************************************************/
105 string FastaMap::getGroupName(string seq) { //pass a sequence name get its group
106 return data[seq].groupname;
109 /*******************************************************************************/
111 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
112 return data[seq].names;
115 /*******************************************************************************/
117 string FastaMap::getSequence(string name) {
119 map<string,string>::iterator it = seqmap.find(name);
120 if (it == seqmap.end()) { return "not found"; }
121 else { return it->second; }
125 /*******************************************************************************/
127 void FastaMap::push_back(string name, string seq) {
129 map<string,group>::iterator it = data.find(seq);
130 if (it == data.end()) { //it's unique.
131 data[seq].groupname = name; //group name will be the name of the first duplicate sequence found.
132 data[seq].names = name;
133 }else { // its a duplicate.
134 data[seq].names += "," + name;
139 /*******************************************************************************/
141 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
145 /*******************************************************************************/
147 void FastaMap::printNamesFile(string outFileName){ //prints data
150 m->openOutputFile(outFileName, outFile);
152 // two column file created with groupname and them list of identical sequence names
153 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
154 if (m->control_pressed) { break; }
155 outFile << it->second.groupname << '\t' << it->second.names << endl;
159 catch(exception& e) {
160 m->errorOut(e, "FastaMap", "printNamesFile");
165 /*******************************************************************************/
167 void FastaMap::printCondensedFasta(string outFileName){ //prints data
170 m->openOutputFile(outFileName, out);
171 //creates a fasta file
172 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
173 if (m->control_pressed) { break; }
174 out << ">" << it->second.groupname << endl;
175 out << it->first << endl;
179 catch(exception& e) {
180 m->errorOut(e, "FastaMap", "printCondensedFasta");
185 /*******************************************************************************/