]> git.donarmstrong.com Git - mothur.git/blob - deconvolutecommand.cpp
broke up globaldata and moved error checking and help into commands
[mothur.git] / deconvolutecommand.cpp
1 /*
2  *  deconvolute.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/21/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "deconvolutecommand.h"
11
12 /**************************************************************************************/
13 DeconvoluteCommand::DeconvoluteCommand(string option) { 
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 
18                 //allow user to run help
19                 if(option == "help") { help(); abort = true; }
20                 
21                 else {
22                         //valid paramters for this command
23                         string Array[] =  {"fasta"};
24                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25                         
26                         parser = new OptionParser();
27                         parser->parse(option, parameters);  delete parser;
28                         
29                         ValidParameters* validParameter = new ValidParameters();
30                 
31                         //check to make sure all parameters are valid for command
32                         for (it = parameters.begin(); it != parameters.end(); it++) { 
33                                 if (validParameter->isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
34                         }
35                         
36                         //check for required parameters
37                         filename = validParameter->validFile(parameters, "fasta", true);
38                         if (filename == "not open") { abort = true; }
39                         else if (filename == "not found") { filename = ""; cout << "fasta is a required parameter for the unique.seqs command." << endl; abort = true;  }       
40                         else {  globaldata->setFastaFile(filename);  globaldata->setFormat("fasta");    }
41                         
42                         delete validParameter;
43                 }
44
45         }
46         catch(exception& e) {
47                 cout << "Standard Error: " << e.what() << " has occurred in the DeconvoluteCommand class Function DeconvoluteCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50         catch(...) {
51                 cout << "An unknown error has occurred in the DeconvoluteCommand class function DeconvoluteCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54 }
55 //**********************************************************************************************************************
56
57 void DeconvoluteCommand::help(){
58         try {
59                 cout << "The unique.seqs command reads a fastafile and creates a namesfile." << "\n";
60                 cout << "It creates a file where the first column is the groupname and the second column is a list of sequence names who have the same sequence. " << "\n";
61                 cout << "If the sequence is unique the second column will just contain its name. " << "\n";
62                 cout << "The unique.seqs command parameter is fasta and it is required." << "\n";
63                 cout << "The unique.seqs command should be in the following format: " << "\n";
64                 cout << "unique.seqs(fasta=yourFastaFile) " << "\n";    
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the DeconvoluteCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the DeconvoluteCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }       
74 }
75
76 /**************************************************************************************/
77 int DeconvoluteCommand::execute() {     
78         try {
79                 
80                 if (abort == true) { return 0; }
81         
82                 //prepare filenames and open files
83                 outputFileName = (getRootName(filename) + "names");
84                 outFastafile = (getRootName(filename) + "unique.fasta");
85                 
86                 openInputFile(filename, in);
87                 openOutputFile(outputFileName, out);
88                 openOutputFile(outFastafile, outFasta);
89
90                 //constructor reads in file and store internally
91                 fastamap = new FastaMap();
92         
93                 //two columns separated by tabs sequence name and then sequence
94                 fastamap->readFastaFile(in);
95                 
96                 //print out new names file 
97                 //file contains 2 columns separated by tabs.  the first column is the groupname(name of first sequence found.
98                 //the second column is the list of names of identical sequences separated by ','.
99                 fastamap->printNamesFile(out);
100                 fastamap->printCondensedFasta(outFasta);
101                 
102                 out.close();
103                 outFasta.close();
104         
105                 return 0;
106         }
107         catch(exception& e) {
108                 cout << "Standard Error: " << e.what() << " has occurred in the DeconvoluteCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111         catch(...) {
112                 cout << "An unknown error has occurred in the DeconvoluteCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113                 exit(1);
114         }
115 }
116 /**************************************************************************************/