]> git.donarmstrong.com Git - mothur.git/blob - readfasta.cpp
fixed some bugs
[mothur.git] / readfasta.cpp
1 /*
2  *  readfasta.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 4/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "readfasta.h"
11 #include <iostream>
12 #include <fstream>
13
14 /*******************************************************************************/
15 ReadFasta::ReadFasta(string file) : ReadSeqs(file) {}
16 /*******************************************************************************/
17 ReadFasta::~ReadFasta(){
18         //for(int i = 0; i < sequencedb.getNumSeqs(); i++)
19                 //delete sequencedb.get(i);
20 }
21 /*******************************************************************************/
22 void ReadFasta::read() {
23         try {
24         /*string name = "";
25         string sequence = "";
26         string temp;
27         int count = 0;
28         
29         while(!filehandle.eof()){
30                 if(count == 0)
31                         filehandle >> temp;
32                 if(temp.substr(0,1).compare(">") == 0) {
33                         if(count != 0) {
34                                 Sequence newSequence(name, sequence);
35                                 sequencedb.add(newSequence);
36                                 sequence = "";
37                         }
38                         else
39                                 count++;
40                         name = temp.substr(1,temp.length()-1);
41                 }
42                 else {
43                         sequence += temp;
44                 }
45                 
46                 filehandle >> temp;
47                 gobble(filehandle);
48                 
49                 if(filehandle.eof())
50                         sequence += temp;
51                         
52         }
53         Sequence newSequence(name, sequence);
54         sequencedb.add(newSequence); */
55
56                 string name, sequence, line;
57                 sequence = "";
58                 int c;
59                 string temp;
60                 
61                 
62                 //read through file
63                 while ((c = filehandle.get()) != EOF) {
64                         name = ""; sequence = ""; 
65                         //is this a name
66                         if (c == '>') { 
67                                 name = readName(filehandle); 
68                                 sequence = readSequence(filehandle); 
69                         }else {  cout << "Error fasta in your file. Please correct." << endl; }
70
71                         //input sequence info into sequencedb
72                         Sequence newSequence(name, sequence);
73                         sequencedb.add(newSequence);
74                         
75                         //takes care of white space
76                         gobble(filehandle);
77                 }
78
79                 filehandle.close();
80         }
81         catch(exception& e) {
82                 cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83                 exit(1);
84         }
85         catch(...) {
86                 cout << "An unknown error has occurred in the ReadFasta class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
87                 exit(1);
88         }
89 }
90
91 /*********************************************************************************/
92 SequenceDB* ReadFasta::getDB() {
93         return &sequencedb;
94 }
95 /*******************************************************************************/
96 string ReadFasta::readName(ifstream& in) {
97         try{
98                 string name = "";
99                 int c;
100                 string temp;
101                 
102                 while ((c = in.get()) != EOF) {
103                         //if c is not a line return
104                         if (c != 10) {
105                                 name += c;
106                         }else { break;  }
107                 }
108                         
109                 return name;
110         }
111         catch(exception& e) {
112                 cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113                 exit(1);
114         }
115         catch(...) {
116                 cout << "An unknown error has occurred in the ReadFasta class function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119 }
120
121 /*******************************************************************************/
122 string ReadFasta::readSequence(ifstream& in) {
123         try{
124                 string sequence = "";
125                 string line;
126                 int pos, c;
127                 
128                 while (!in.eof()) {
129                         //save position in file in case next line is a new name.
130                         pos = in.tellg();
131                         line = "";
132                         in >> line;                     
133                         //if you are at a new name
134                         if (line[0] == '>') {
135                                 //put file pointer back since you are now at a new name
136                                 in.seekg(pos, ios::beg);
137                                 c = in.get();  //because you put it back to a newline char
138                                 break;
139                         }else {  sequence += line;      }
140                 }
141                         
142                 return sequence;
143         }
144         catch(exception& e) {
145                 cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
146                 exit(1);
147         }
148         catch(...) {
149                 cout << "An unknown error has occurred in the ReadFasta class function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150                 exit(1);
151         }
152 }
153 /*******************************************************************************/
154