]> git.donarmstrong.com Git - mothur.git/blob - fastamap.cpp
fixed memory leak in parsimony calculator and added progress bars to parsimony and...
[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
12 /*******************************************************************************/
13 void FastaMap::readFastaFile(ifstream& in) {
14         try {
15                 string name, sequence, line;
16                 sequence = "";
17         
18                 getline(in, line);
19                 name = line.substr(1, line.length());  //rips off '>'
20         
21                 //read through file
22                 while (getline(in, line)) {
23                         if (line != "") {
24                                 if (isalnum(line.at(0))) {  //if it's a sequence line
25                                         sequence += line;
26                                 }
27                                 else{
28                                 //input sequence info into map
29                                         seqmap[name] = sequence;  
30                                         it = data.find(sequence);
31                                         if (it == data.end()) {         //it's unique.
32                                                 data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
33                                                 data[sequence].groupnumber = 1;
34                                                 data[sequence].names = name;
35                                         }else { // its a duplicate.
36                                                 data[sequence].names += "," + name;
37                                                 data[sequence].groupnumber++;
38                                         }
39                                         name = (line.substr(1, (line.npos))); //The line you just read is a new name so rip off '>'
40                                         sequence = "";
41                                 }
42                         }
43                 }
44         
45                 //store last sequence and name info.
46                 seqmap[name] = sequence;
47                 it = data.find(sequence);
48                 if (it == data.end()) {         //it's unique.
49                         data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
50                         data[sequence].groupnumber = 1;
51                         data[sequence].names = name;
52                 }else { // its a duplicate.
53                         data[sequence].names += "," + name;
54                         data[sequence].groupnumber++;
55                 }
56                         
57         }
58         catch(exception& e) {
59                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
60                 exit(1);
61         }
62         catch(...) {
63                 cout << "An unknown error has occurred in the FastaMap class function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
64                 exit(1);
65         }
66 }
67 /*******************************************************************************/
68 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
69         return data[seq].groupname;
70 }
71 /*******************************************************************************/
72 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
73         return data[seq].names;
74 }
75 /*******************************************************************************/
76 int FastaMap::getGroupNumber(string seq) {      //pass a sequence get the number of identical sequences.
77         return data[seq].groupnumber;
78 }
79 /*******************************************************************************/
80 string FastaMap::getSequence(string name) {
81         it2 = seqmap.find(name);
82         if (it2 == seqmap.end()) {      //it's not found
83                 return "not found";
84         }else { // found it
85                 return it2->second;
86         }
87 }       
88 /*******************************************************************************/
89 void FastaMap::push_back(string name, string seq) {
90         it = data.find(seq);
91         if (it == data.end()) {         //it's unique.
92                 data[seq].groupname = name;  //group name will be the name of the first duplicate sequence found.
93                 data[seq].groupnumber = 1;
94                 data[seq].names = name;
95         }else { // its a duplicate.
96                 data[seq].names += "," + name;
97                 data[seq].groupnumber++;
98         }
99         
100         seqmap[name] = seq;
101 }
102 /*******************************************************************************/
103 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
104         return data.size();
105 }
106 /*******************************************************************************/
107 void FastaMap::printNamesFile(ostream& out){ //prints data
108         try {
109                 // two column file created with groupname and them list of identical sequence names
110                 for (it = data.begin(); it != data.end(); it++) {
111                         out << it->second.groupname << '\t' << it->second.names << endl;
112                 }
113         }
114         catch(exception& e) {
115                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116                 exit(1);
117         }
118         catch(...) {
119                 cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122 }
123 /*******************************************************************************/
124 void FastaMap::printCondensedFasta(ostream& out){ //prints data
125         try {
126                 // two column file created with groupname and them list of identical sequence names
127                 for (it = data.begin(); it != data.end(); it++) {
128                         out << ">" << it->second.groupname << endl;
129                         out << it->first << endl;
130                 }
131         }
132         catch(exception& e) {
133                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
134                 exit(1);
135         }
136         catch(...) {
137                 cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138                 exit(1);
139         }
140 }
141 /*******************************************************************************/
142