]> git.donarmstrong.com Git - mothur.git/blob - fastamap.cpp
added logfile feature
[mothur.git] / fastamap.cpp
1 /*
2  *  fastamap.cpp
3  *  mothur
4  *
5  *  Created by Sarah Westcott on 1/16/09.
6  *  Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
7  *
8  */
9
10 #include "fastamap.h"
11 #include "sequence.hpp"
12
13 /*******************************************************************************/
14
15 void FastaMap::readFastaFile(string inFileName) {
16         try {
17                 ifstream in;
18                 openInputFile(inFileName, in);
19                 string name, sequence, line;
20                 sequence = "";
21                 string temp;
22
23                 while(!in.eof()){
24                         Sequence currSeq(in);
25                         name = currSeq.getName();
26                         
27                         if(currSeq.getIsAligned())      {       sequence = currSeq.getAligned();        }
28                         else                                            {       sequence = currSeq.getUnaligned();      }
29                         
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++;
39                         }       
40                         
41                         gobble(in);
42                 }
43                 in.close();             
44         }
45         catch(exception& e) {
46                 errorOut(e, "FastaMap", "readFastaFile");
47                 exit(1);
48         }
49 }
50
51 /*******************************************************************************/
52
53 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
54         
55         ifstream oldNameFile;
56         openInputFile(oldNameFileName, oldNameFile);
57         
58         map<string,string> oldNameMap;
59         string name, list;
60         while(!oldNameFile.eof()){
61                 oldNameFile >> name >> list;
62                 oldNameMap[name] = list;
63                 gobble(oldNameFile);
64         }
65         oldNameFile.close();
66         
67         ifstream inFASTA;
68         openInputFile(inFastaFile, inFASTA);
69         string sequence;
70         while(!inFASTA.eof()){
71                 Sequence currSeq(inFASTA);
72                 name = currSeq.getName();
73                 
74                 if(currSeq.getIsAligned())      {       sequence = currSeq.getAligned();        }
75                 else                                            {       sequence = currSeq.getUnaligned();      }
76                 
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++;
86                 }       
87                 
88                 gobble(inFASTA);
89         }
90         
91         
92         inFASTA.close();
93 }
94
95 /*******************************************************************************/
96
97 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
98         return data[seq].groupname;
99 }
100
101 /*******************************************************************************/
102
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;
105 }
106
107 /*******************************************************************************/
108
109 string FastaMap::getSequence(string name) {
110         
111         map<string,string>::iterator it = seqmap.find(name);
112         if (it == seqmap.end()) {       return "not found";             }
113         else                                    {       return it->second;              }
114         
115 }       
116
117 /*******************************************************************************/
118
119 void FastaMap::push_back(string name, string seq) {
120         
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;
127         }
128         seqmap[name] = seq;
129 }
130
131 /*******************************************************************************/
132
133 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
134         return data.size();
135 }
136
137 /*******************************************************************************/
138
139 void FastaMap::printNamesFile(string outFileName){ //prints data
140         try {
141                 ofstream outFile;
142                 openOutputFile(outFileName, outFile);
143                 
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;
147                 }
148                 outFile.close();
149         }
150         catch(exception& e) {
151                 errorOut(e, "FastaMap", "printNamesFile");
152                 exit(1);
153         }
154 }
155
156 /*******************************************************************************/
157
158 void FastaMap::printCondensedFasta(string outFileName){ //prints data
159         try {
160                 ofstream out;
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;
166                 }
167                 out.close();
168         }
169         catch(exception& e) {
170                 errorOut(e, "FastaMap", "printCondensedFasta");
171                 exit(1);
172         }
173 }
174
175 /*******************************************************************************/
176