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 openInputFile(inFileName, in);
19 string name, sequence, line;
25 name = currSeq.getName();
27 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
28 else { sequence = currSeq.getUnaligned(); }
30 seqmap[name] = sequence;
31 map<string,group>::iterator it = data.find(sequence);
32 if (it == data.end()) { //it's unique.
33 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
34 // data[sequence].groupnumber = 1;
35 data[sequence].names = name;
36 }else { // its a duplicate.
37 data[sequence].names += "," + name;
38 // data[sequence].groupnumber++;
46 errorOut(e, "FastaMap", "readFastaFile");
51 /*******************************************************************************/
53 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
56 openInputFile(oldNameFileName, oldNameFile);
58 map<string,string> oldNameMap;
60 while(!oldNameFile.eof()){
61 oldNameFile >> name >> list;
62 oldNameMap[name] = list;
68 openInputFile(inFastaFile, inFASTA);
70 while(!inFASTA.eof()){
71 Sequence currSeq(inFASTA);
72 name = currSeq.getName();
74 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
75 else { sequence = currSeq.getUnaligned(); }
77 seqmap[name] = sequence;
78 map<string,group>::iterator it = data.find(sequence);
79 if (it == data.end()) { //it's unique.
80 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
81 // data[sequence].groupnumber = 1;
82 data[sequence].names = oldNameMap[name];
83 }else { // its a duplicate.
84 data[sequence].names += "," + oldNameMap[name];
85 // data[sequence].groupnumber++;
95 /*******************************************************************************/
97 string FastaMap::getGroupName(string seq) { //pass a sequence name get its group
98 return data[seq].groupname;
101 /*******************************************************************************/
103 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
104 return data[seq].names;
107 /*******************************************************************************/
109 string FastaMap::getSequence(string name) {
111 map<string,string>::iterator it = seqmap.find(name);
112 if (it == seqmap.end()) { return "not found"; }
113 else { return it->second; }
117 /*******************************************************************************/
119 void FastaMap::push_back(string name, string seq) {
121 map<string,group>::iterator it = data.find(seq);
122 if (it == data.end()) { //it's unique.
123 data[seq].groupname = name; //group name will be the name of the first duplicate sequence found.
124 data[seq].names = name;
125 }else { // its a duplicate.
126 data[seq].names += "," + name;
131 /*******************************************************************************/
133 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
137 /*******************************************************************************/
139 void FastaMap::printNamesFile(string outFileName){ //prints data
142 openOutputFile(outFileName, outFile);
144 // two column file created with groupname and them list of identical sequence names
145 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
146 outFile << it->second.groupname << '\t' << it->second.names << endl;
150 catch(exception& e) {
151 errorOut(e, "FastaMap", "printNamesFile");
156 /*******************************************************************************/
158 void FastaMap::printCondensedFasta(string outFileName){ //prints data
161 openOutputFile(outFileName, out);
162 //creates a fasta file
163 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
164 out << ">" << it->second.groupname << endl;
165 out << it->first << endl;
169 catch(exception& e) {
170 errorOut(e, "FastaMap", "printCondensedFasta");
175 /*******************************************************************************/