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