]> git.donarmstrong.com Git - mothur.git/blob - fastamap.cpp
fixed bug in fastmap readFastaFile so that if there is a blank line inbetween the...
[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                                         it = data.find(sequence);
30                                         if (it == data.end()) {         //it's unique.
31                                                 data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
32                                                 data[sequence].groupnumber = 1;
33                                                 data[sequence].names = name;
34                                         }else { // its a duplicate.
35                                                 data[sequence].names += "," + name;
36                                                 data[sequence].groupnumber++;
37                                         }
38                                         name = (line.substr(1, (line.npos))); //The line you just read is a new name so rip off '>'
39                                         sequence = "";
40                                 }
41                         }
42                 }
43         
44                 //store last sequence and name info.
45                 it = data.find(sequence);
46                 if (it == data.end()) {         //it's unique.
47                         data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
48                         data[sequence].groupnumber = 1;
49                         data[sequence].names = name;
50                 }else { // its a duplicate.
51                         data[sequence].names += "," + name;
52                         data[sequence].groupnumber++;
53                 }       
54         }
55         catch(exception& e) {
56                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57                 exit(1);
58         }
59         catch(...) {
60                 cout << "An unknown error has occurred in the FastaMap class function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61                 exit(1);
62         }
63 }
64 /*******************************************************************************/
65 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
66         return data[seq].groupname;
67 }
68 /*******************************************************************************/
69 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
70         return data[seq].names;
71 }
72 /*******************************************************************************/
73 int FastaMap::getGroupNumber(string seq) {      //pass a sequence get the number of identical sequences.
74         return data[seq].groupnumber;
75 }
76 /*******************************************************************************/
77 void FastaMap::push_back(string seq, string Name) {//sequencename, name
78         data[seq].groupname = Name;
79         data[seq].names = Name;
80 }
81 /*******************************************************************************/
82 void FastaMap::set(string seq, string groupName, string Names) {
83         data[seq].groupname = groupName;
84         data[seq].names = Names;
85 }
86 /*******************************************************************************/
87 void FastaMap::clear() { //clears out data
88         data.clear();
89 }
90 /*******************************************************************************/
91 int FastaMap::size(){ //returns datas size which is the number of unique sequences
92         return data.size();
93 }
94 /*******************************************************************************/
95 void FastaMap::print(ostream& out){ //prints data
96         try {
97                 // two column file created with groupname and them list of identical sequence names
98                 for (it = data.begin(); it != data.end(); it++) {
99                         out << it->second.groupname << '\t' << it->second.names << endl;
100                 }
101         }
102         catch(exception& e) {
103                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104                 exit(1);
105         }
106         catch(...) {
107                 cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108                 exit(1);
109         }
110 }
111 /*******************************************************************************/
112 void FastaMap::printCondensedFasta(ostream& out){ //prints data
113         try {
114                 // two column file created with groupname and them list of identical sequence names
115                 for (it = data.begin(); it != data.end(); it++) {
116                         out << ">" << it->second.groupname << endl;
117                         out << it->first << endl;
118                 }
119         }
120         catch(exception& e) {
121                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124         catch(...) {
125                 cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128 }
129 /*******************************************************************************/
130