]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
added get.repseqs command, started matrix output 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 ReadOTUFile(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                                 string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta";
71                                 openOutputFile(outputFileName, out);
72
73                                 cout << list->getLabel() << '\t' << count << endl;
74                                 
75                                 //for each bin in the list vector
76                                 for (int i = 0; i < list->size(); i++) {
77
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 + "|" + 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 + '|' + 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                                 out.close();
111                         }
112                         
113                         delete list;
114                         list = input->getListVector();
115                         count++;
116                 }
117                 
118                 return 0;
119         }
120         catch(exception& e) {
121                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124         catch(...) {
125                 cout << "An unknown error has occurred in the BinSeqCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }       
128 }
129
130 //**********************************************************************************************************************
131 void BinSeqCommand::readNamesFile() {
132         try {
133                 vector<string> dupNames;
134                 openInputFile(namesfile, inNames);
135                 
136                 string name, names, sequence;
137         
138                 while(inNames){
139                         inNames >> name;                        //read from first column  A
140                         inNames >> names;               //read from second column  A,B,C,D
141                         
142                         dupNames.clear();
143                         
144                         //parse names into vector
145                         splitAtComma(names, dupNames);
146                         
147                         //store names in fasta map
148                         sequence = fasta->getSequence(name);
149                         for (int i = 0; i < dupNames.size(); i++) {
150                                 fasta->push_back(dupNames[i], sequence);
151                         }
152                 
153                         gobble(inNames);
154                 }
155                 inNames.close();
156
157         }
158         catch(exception& e) {
159                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160                 exit(1);
161         }
162         catch(...) {
163                 cout << "An unknown error has occurred in the BinSeqCommand class function readNamesFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
164                 exit(1);
165         }       
166 }
167 //**********************************************************************************************************************
168
169
170