]> git.donarmstrong.com Git - mothur.git/blobdiff - fastamap.cpp
version working on mac with .tellg changed to unget for windows
[mothur.git] / fastamap.cpp
index 4f4bf33206d77e437dd32bd2ff14b7f5f1290042..a0652d996fa648a7130928e93c7f3c6b0fae583a 100644 (file)
@@ -8,24 +8,34 @@
  */
 
 #include "fastamap.h"
+#include "sequence.hpp"
 
 /*******************************************************************************/
- FastaMap::FastaMap(ifstream& in) {
-       //int numberOfSequences = 0;
-       
-       string name, sequence, line;
-       sequence = "";
-       
-       getline(in, line);
-       name = line.substr(1, line.length());  //rips off '>'
-       
-       //read through file
-       while (getline(in, line)) {
-               if (isalnum(line.at(0))){  //if it's a sequence line
-                       sequence += line;
-               }
-               else{
+
+void FastaMap::readFastaFile(ifstream& in) {
+       try {
+               string name, sequence, line;
+               sequence = "";
+//             int c;
+               string temp;
+               
+               
+               //read through file
+//             while ((c = in.get()) != EOF) {
+//                     name = ""; sequence = ""; 
+//                     //is this a name
+//                     if (c == '>') { 
+//                             name = readName(in); 
+//                             sequence = readSequence(in); 
+//                     }else {  cout << "Error fasta in your file. Please correct." << endl; }
+
+                       //store info in map
                        //input sequence info into map
+               while(!in.eof()){
+                       Sequence currSeq(in);
+                       name = currSeq.getName();
+                       sequence = currSeq.getUnaligned();
+                       seqmap[name] = sequence;  
                        it = data.find(sequence);
                        if (it == data.end()) {         //it's unique.
                                data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
                                data[sequence].names = name;
                        }else { // its a duplicate.
                                data[sequence].names += "," + name;
-                               data[sequence].groupnumber++;   
-                       }
-                       name = (line.substr(1, (line.npos))); //The line you just read is a new name so rip off '>'
+                               data[sequence].groupnumber++;
+                       }       
+                       
+                       gobble(in);
                }
+                                       
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the FastaMap class function readFastaFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
        }
-       
-       //store last sequence and name info.
-       it = data.find(sequence);
-       if (it == data.end()) {         //it's unique.
-               data[sequence].groupname = name;  //group name will be the name of the first duplicate sequence found.
-               data[sequence].groupnumber = 1;
-               data[sequence].names = name;
-       }else { // its a duplicate.
-               data[sequence].names += "," + name;
-               data[sequence].groupnumber++;   
-       }       
 }
+
 /*******************************************************************************/
+
 string FastaMap::getGroupName(string seq) {  //pass a sequence name get its group
        return data[seq].groupname;
 }
+
 /*******************************************************************************/
-int FastaMap::getGroupNumber(string seq) {  //pass a sequence name get number of sequence in its group
-       return data[seq].groupnumber;
-}
-/*******************************************************************************/
+
 string FastaMap::getNames(string seq) {        //pass a sequence get the string of names in the group separated by ','s.
        return data[seq].names;
 }
+
 /*******************************************************************************/
-void FastaMap::push_back(string seq, string Name) {//sequencename, name
-       data[seq].groupname = Name;
-       data[seq].groupnumber = 1;
-       data[seq].names = Name;
+
+int FastaMap::getGroupNumber(string seq) {     //pass a sequence get the number of identical sequences.
+       return data[seq].groupnumber;
 }
+
+/*******************************************************************************/
+
+string FastaMap::getSequence(string name) {
+       it2 = seqmap.find(name);
+       if (it2 == seqmap.end()) {      //it's not found
+               return "not found";
+       }else { // found it
+               return it2->second;
+       }
+}      
+
 /*******************************************************************************/
-void FastaMap::clear() { //clears out data
-       data.clear();
+
+void FastaMap::push_back(string name, string seq) {
+       it = data.find(seq);
+       if (it == data.end()) {         //it's unique.
+               data[seq].groupname = name;  //group name will be the name of the first duplicate sequence found.
+               data[seq].groupnumber = 1;
+               data[seq].names = name;
+       }else { // its a duplicate.
+               data[seq].names += "," + name;
+               data[seq].groupnumber++;
+       }
+       
+       seqmap[name] = seq;
 }
+
 /*******************************************************************************/
-int FastaMap::size(){ //returns datas size which is the number of unique sequences
+
+int FastaMap::sizeUnique(){ //returns datas size which is the number of unique sequences
        return data.size();
 }
+
+/*******************************************************************************/
+
+void FastaMap::printNamesFile(ostream& out){ //prints data
+       try {
+               // two column file created with groupname and them list of identical sequence names
+               for (it = data.begin(); it != data.end(); it++) {
+                       out << it->second.groupname << '\t' << it->second.names << endl;
+               }
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+}
+
 /*******************************************************************************/
-void FastaMap::print(ostream&){ //prints data
 
+void FastaMap::printCondensedFasta(ostream& out){ //prints data
+       try {
+               //creates a fasta file
+               for (it = data.begin(); it != data.end(); it++) {
+                       out << ">" << it->second.groupname << endl;
+                       out << it->first << endl;
+               }
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the FastaMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the FastaMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
 }
+
 /*******************************************************************************/
+