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