]> git.donarmstrong.com Git - mothur.git/blob - fastamap.cpp
added trim.seqs command
[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(ifstream& in) {
16         try {
17                 string name, sequence, line;
18                 sequence = "";
19 //              int c;
20                 string temp;
21                 
22                 
23                 //read through file
24 //              while ((c = in.get()) != EOF) {
25 //                      name = ""; sequence = ""; 
26 //                      //is this a name
27 //                      if (c == '>') { 
28 //                              name = readName(in); 
29 //                              sequence = readSequence(in); 
30 //                      }else {  cout << "Error fasta in your file. Please correct." << endl; }
31
32                         //store info in map
33                         //input sequence info into map
34                 while(!in.eof()){
35                         Sequence currSeq(in);
36                         name = currSeq.getName();
37                         sequence = currSeq.getUnaligned();
38                         seqmap[name] = sequence;  
39                         it = data.find(sequence);
40                         if (it == data.end()) {         //it's unique.
41                                 data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
42                                 data[sequence].groupnumber = 1;
43                                 data[sequence].names = name;
44                         }else { // its a duplicate.
45                                 data[sequence].names += "," + name;
46                                 data[sequence].groupnumber++;
47                         }       
48                         
49                         gobble(in);
50                 }
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 /*******************************************************************************/
64
65 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
66         return data[seq].groupname;
67 }
68
69 /*******************************************************************************/
70
71 string FastaMap::getNames(string seq) { //pass a sequence get the string of names in the group separated by ','s.
72         return data[seq].names;
73 }
74
75 /*******************************************************************************/
76
77 int FastaMap::getGroupNumber(string seq) {      //pass a sequence get the number of identical sequences.
78         return data[seq].groupnumber;
79 }
80
81 /*******************************************************************************/
82
83 string FastaMap::getSequence(string name) {
84         it2 = seqmap.find(name);
85         if (it2 == seqmap.end()) {      //it's not found
86                 return "not found";
87         }else { // found it
88                 return it2->second;
89         }
90 }       
91
92 /*******************************************************************************/
93
94 void FastaMap::push_back(string name, string seq) {
95         it = data.find(seq);
96         if (it == data.end()) {         //it's unique.
97                 data[seq].groupname = name;  //group name will be the name of the first duplicate sequence found.
98                 data[seq].groupnumber = 1;
99                 data[seq].names = name;
100         }else { // its a duplicate.
101                 data[seq].names += "," + name;
102                 data[seq].groupnumber++;
103         }
104         
105         seqmap[name] = seq;
106 }
107
108 /*******************************************************************************/
109
110 int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
111         return data.size();
112 }
113
114 /*******************************************************************************/
115
116 void FastaMap::printNamesFile(ostream& out){ //prints data
117         try {
118                 // two column file created with groupname and them list of identical sequence names
119                 for (it = data.begin(); it != data.end(); it++) {
120                         out << it->second.groupname << '\t' << it->second.names << 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 /*******************************************************************************/
134
135 void FastaMap::printCondensedFasta(ostream& out){ //prints data
136         try {
137                 //creates a fasta file
138                 for (it = data.begin(); it != data.end(); it++) {
139                         out << ">" << it->second.groupname << endl;
140                         out << it->first << endl;
141                 }
142         }
143         catch(exception& e) {
144                 cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }
147         catch(...) {
148                 cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149                 exit(1);
150         }
151 }
152
153 /*******************************************************************************/
154