]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
added get.oturep command
[mothur.git] / binsequencecommand.cpp
1 /*
2  *  binsequencecommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/3/09.
6  *  Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
7  *
8  */
9
10 #include "binsequencecommand.h"
11
12 //**********************************************************************************************************************
13 BinSeqCommand::BinSeqCommand(){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 fastafile = globaldata->getFastaFile();
17                 namesfile = globaldata->getNameFile();
18                 openInputFile(fastafile, in);
19                 
20                 fasta = new FastaMap();
21         }
22         catch(exception& e) {
23                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
24                 exit(1);
25         }
26         catch(...) {
27                 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
28                 exit(1);
29         }       
30 }
31
32 //**********************************************************************************************************************
33
34 BinSeqCommand::~BinSeqCommand(){
35         delete input;
36         delete read;
37         delete fasta;
38         delete list;
39 }
40
41 //**********************************************************************************************************************
42
43 int BinSeqCommand::execute(){
44         try {
45                 int count = 1;
46                 string binnames, name, sequence;
47                 
48                 //read fastafile
49                 fasta->readFastaFile(in);
50                 
51                 //set format to list so input can get listvector
52                 globaldata->setFormat("list");
53                 
54                 //if user gave a namesfile then use it
55                 if (namesfile != "") {
56                         readNamesFile();
57                 }
58                 
59                 //read list file
60                 read = new ReadPhilFile(globaldata->getListFile());     
61                 read->read(&*globaldata); 
62                 
63                 input = globaldata->ginput;
64                 list = globaldata->gListVector;
65                                 
66                 while(list != NULL){
67                         
68                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
69                                 
70                                 //create output file
71                                 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
72                                 openOutputFile(outputFileName, out);
73
74                                 cout << list->getLabel() << '\t' << count << endl;
75                                 
76                                 //for each bin in the list vector
77                                 for (int i = 0; i < list->size(); i++) {
78                                         binnames = list->get(i);
79                                         while (binnames.find_first_of(',') != -1) { 
80                                                 name = binnames.substr(0,binnames.find_first_of(','));
81                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
82                                                 
83                                                 //do work for that name
84                                                 sequence = fasta->getSequence(name);
85                                                 if (sequence != "not found") {
86                                                         name = name + "bin" + toString(i+1);
87                                                         out << ">" << name << endl;
88                                                         out << sequence << endl;
89                                                 }else { 
90                                                         cout << name << " is missing from your fasta or name file. Please correct. " << endl; 
91                                                         remove(outputFileName.c_str());
92                                                         return 0;
93                                                 }
94                                                 
95                                         }
96                                         
97                                         //get last name
98                                         sequence = fasta->getSequence(binnames);
99                                         if (sequence != "not found") {
100                                                 name = binnames + "bin" + toString(i+1);
101                                                 out << ">" << name << endl;
102                                                 out << sequence << endl;
103                                         }else { 
104                                                 cout << binnames << " is missing from your fasta or name file. Please correct. " << endl; 
105                                                 remove(outputFileName.c_str());
106                                                 return 0;
107                                         }
108                                 }
109                         }
110                         
111                         list = input->getListVector();
112                         count++;
113                 }
114                 
115                 return 0;
116         }
117         catch(exception& e) {
118                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
119                 exit(1);
120         }
121         catch(...) {
122                 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
123                 exit(1);
124         }       
125 }
126
127 //**********************************************************************************************************************
128 void BinSeqCommand::readNamesFile() {
129         try {
130                 vector<string> dupNames;
131                 openInputFile(namesfile, inNames);
132                 
133                 string name, names, sequence;
134         
135                 while(inNames){
136                         inNames >> name;                        //read from first column  A
137                         inNames >> names;               //read from second column  A,B,C,D
138                         
139                         dupNames.clear();
140                         
141                         //parse names into vector
142                         splitAtComma(names, dupNames);
143                         
144                         //store names in fasta map
145                         sequence = fasta->getSequence(name);
146                         for (int i = 0; i < dupNames.size(); i++) {
147                                 fasta->push_back(dupNames[i], sequence);
148                         }
149                 
150                         gobble(inNames);
151                 }
152                 inNames.close();
153
154         }
155         catch(exception& e) {
156                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }       
163 }
164 //**********************************************************************************************************************
165
166
167