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