]> git.donarmstrong.com Git - mothur.git/blob - fastamap.cpp
added checks for ^C to quit command instead of program
[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                         if (m->control_pressed) { break; }
25                         
26                         Sequence currSeq(in);
27                         name = currSeq.getName();
28                         
29                         if (name != "") {
30                                 if(currSeq.getIsAligned())      {       sequence = currSeq.getAligned();        }
31                                 else                                            {       sequence = currSeq.getUnaligned();      }
32                                 
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++;
42                                 }       
43                         }
44                         gobble(in);
45                 }
46                 in.close();             
47         }
48         catch(exception& e) {
49                 m->errorOut(e, "FastaMap", "readFastaFile");
50                 exit(1);
51         }
52 }
53
54 /*******************************************************************************/
55
56 void FastaMap::readFastaFile(string inFastaFile, string oldNameFileName){ //prints data
57         
58         ifstream oldNameFile;
59         openInputFile(oldNameFileName, oldNameFile);
60         
61         map<string,string> oldNameMap;
62         string name, list;
63         while(!oldNameFile.eof()){
64                 if (m->control_pressed) { break; }
65                 
66                 oldNameFile >> name >> list;
67                 oldNameMap[name] = list;
68                 gobble(oldNameFile);
69         }
70         oldNameFile.close();
71         
72         ifstream inFASTA;
73         openInputFile(inFastaFile, inFASTA);
74         string sequence;
75         while(!inFASTA.eof()){
76                 if (m->control_pressed) { break; }
77                 
78                 Sequence currSeq(inFASTA);
79                 name = currSeq.getName();
80                 
81                 if (name != "") {
82                         if(currSeq.getIsAligned())      {       sequence = currSeq.getAligned();        }
83                         else                                            {       sequence = currSeq.getUnaligned();      }
84                         
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++;
94                         }       
95                 }
96                 gobble(inFASTA);
97         }
98         
99         
100         inFASTA.close();
101 }
102
103 /*******************************************************************************/
104
105 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
106         return data[seq].groupname;
107 }
108
109 /*******************************************************************************/
110
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;
113 }
114
115 /*******************************************************************************/
116
117 string FastaMap::getSequence(string name) {
118         
119         map<string,string>::iterator it = seqmap.find(name);
120         if (it == seqmap.end()) {       return "not found";             }
121         else                                    {       return it->second;              }
122         
123 }       
124
125 /*******************************************************************************/
126
127 void FastaMap::push_back(string name, string seq) {
128         
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;
135         }
136         seqmap[name] = seq;
137 }
138
139 /*******************************************************************************/
140
141 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
142         return data.size();
143 }
144
145 /*******************************************************************************/
146
147 void FastaMap::printNamesFile(string outFileName){ //prints data
148         try {
149                 ofstream outFile;
150                 openOutputFile(outFileName, outFile);
151                 
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;
156                 }
157                 outFile.close();
158         }
159         catch(exception& e) {
160                 m->errorOut(e, "FastaMap", "printNamesFile");
161                 exit(1);
162         }
163 }
164
165 /*******************************************************************************/
166
167 void FastaMap::printCondensedFasta(string outFileName){ //prints data
168         try {
169                 ofstream out;
170                 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;
176                 }
177                 out.close();
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "FastaMap", "printCondensedFasta");
181                 exit(1);
182         }
183 }
184
185 /*******************************************************************************/
186