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();
28 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
29 else { sequence = currSeq.getUnaligned(); }
31 seqmap[name] = sequence;
32 map<string,group>::iterator it = data.find(sequence);
33 if (it == data.end()) { //it's unique.
34 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
35 // data[sequence].groupnumber = 1;
36 data[sequence].names = name;
37 }else { // its a duplicate.
38 data[sequence].names += "," + name;
39 // data[sequence].groupnumber++;
47 errorOut(e, "FastaMap", "readFastaFile");
52 /*******************************************************************************/
54 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
57 openInputFile(oldNameFileName, oldNameFile);
59 map<string,string> oldNameMap;
61 while(!oldNameFile.eof()){
62 oldNameFile >> name >> list;
63 oldNameMap[name] = list;
69 openInputFile(inFastaFile, inFASTA);
71 while(!inFASTA.eof()){
72 Sequence currSeq(inFASTA);
73 name = currSeq.getName();
76 if(currSeq.getIsAligned()) { sequence = currSeq.getAligned(); }
77 else { sequence = currSeq.getUnaligned(); }
79 seqmap[name] = sequence;
80 map<string,group>::iterator it = data.find(sequence);
81 if (it == data.end()) { //it's unique.
82 data[sequence].groupname = name; //group name will be the name of the first duplicate sequence found.
83 // data[sequence].groupnumber = 1;
84 data[sequence].names = oldNameMap[name];
85 }else { // its a duplicate.
86 data[sequence].names += "," + oldNameMap[name];
87 // data[sequence].groupnumber++;
97 /*******************************************************************************/
99 string FastaMap::getGroupName(string seq) { //pass a sequence name get its group
100 return data[seq].groupname;
103 /*******************************************************************************/
105 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
106 return data[seq].names;
109 /*******************************************************************************/
111 string FastaMap::getSequence(string name) {
113 map<string,string>::iterator it = seqmap.find(name);
114 if (it == seqmap.end()) { return "not found"; }
115 else { return it->second; }
119 /*******************************************************************************/
121 void FastaMap::push_back(string name, string seq) {
123 map<string,group>::iterator it = data.find(seq);
124 if (it == data.end()) { //it's unique.
125 data[seq].groupname = name; //group name will be the name of the first duplicate sequence found.
126 data[seq].names = name;
127 }else { // its a duplicate.
128 data[seq].names += "," + name;
133 /*******************************************************************************/
135 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
139 /*******************************************************************************/
141 void FastaMap::printNamesFile(string outFileName){ //prints data
144 openOutputFile(outFileName, outFile);
146 // two column file created with groupname and them list of identical sequence names
147 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
148 outFile << it->second.groupname << '\t' << it->second.names << endl;
152 catch(exception& e) {
153 errorOut(e, "FastaMap", "printNamesFile");
158 /*******************************************************************************/
160 void FastaMap::printCondensedFasta(string outFileName){ //prints data
163 openOutputFile(outFileName, out);
164 //creates a fasta file
165 for (map<string,group>::iterator it = data.begin(); it != data.end(); it++) {
166 out << ">" << it->second.groupname << endl;
167 out << it->first << endl;
171 catch(exception& e) {
172 errorOut(e, "FastaMap", "printCondensedFasta");
177 /*******************************************************************************/