]> git.donarmstrong.com Git - mothur.git/blob - readotucommand.cpp
2e2bbc90baf0fbcfe9abb79b049c1cf2589edfe0
[mothur.git] / readotucommand.cpp
1 /*
2  *  readotu.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/20/09.
6  *  Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
7  *
8  */
9
10 #include "readotucommand.h"
11
12 //**********************************************************************************************************************
13 ReadOtuCommand::ReadOtuCommand(string option){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 allLines = 1;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"list","order","shared", "label","group","sabund", "rabund"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string, string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                 
32                         //check to make sure all parameters are valid for command
33                         for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
34                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
35                         }
36                         
37                         globaldata->newRead();
38                         
39                         //check for required parameters
40                         listfile = validParameter.validFile(parameters, "list", true);
41                         if (listfile == "not open") { abort = true; }
42                         else if (listfile == "not found") { listfile = ""; }    
43                         else {  globaldata->setListFile(listfile);  globaldata->setFormat("list");      }
44                         
45                         sabundfile = validParameter.validFile(parameters, "sabund", true);
46                         if (sabundfile == "not open") { abort = true; } 
47                         else if (sabundfile == "not found") { sabundfile = ""; }
48                         else {  globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
49
50                         rabundfile = validParameter.validFile(parameters, "rabund", true);
51                         if (rabundfile == "not open") { abort = true; } 
52                         else if (rabundfile == "not found") { rabundfile = ""; }
53                         else {  globaldata->setRabundFile(rabundfile);  globaldata->setFormat("rabund");}
54                         
55                         sharedfile = validParameter.validFile(parameters, "shared", true);
56                         if (sharedfile == "not open") { abort = true; } 
57                         else if (sharedfile == "not found") { sharedfile = ""; }
58                         else {  globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile");     }
59                         
60                         groupfile = validParameter.validFile(parameters, "group", true);
61                         if (groupfile == "not open") { abort = true; }  
62                         else if (groupfile == "not found") { groupfile = ""; }
63                         else {  
64                                 globaldata->setGroupFile(groupfile); 
65                                 groupMap = new GroupMap(groupfile);
66                                 groupMap->readMap();
67                                 globaldata->gGroupmap = groupMap;
68                         }
69
70                         //you are doing a list and group shared
71                         if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
72                         
73                         //you have not given a file
74                         if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "")) {
75                                 mothurOut("You must enter either a listfile, rabundfile, sabundfile or a sharedfile with the read.otu command. "); mothurOutEndLine(); abort = true; 
76                         }
77                 
78                         //check for optional parameter and set defaults
79                         // ...at some point should added some additional type checking...
80                         label = validParameter.validFile(parameters, "label", false);                   
81                         if (label == "not found") { label = ""; }
82                         else { 
83                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
84                                 else { allLines = 1;  }
85                                 globaldata->labels = labels;
86                         }
87                         
88                         globaldata->allLines = allLines;
89                         
90                         orderfile = validParameter.validFile(parameters, "order", true);
91                         if (orderfile == "not open") { abort = true; }  
92                         else if (orderfile == "not found") { orderfile = ""; }
93                         else {  globaldata->setOrderFile(orderfile);    }
94                         
95                         
96                         if (abort == false) {
97                                 //gets whichever one of the above is set
98                                 filename = globaldata->inputFileName;
99                         }
100
101                 }
102
103         }
104         catch(exception& e) {
105                 errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
106                 exit(1);
107         }
108 }
109 //**********************************************************************************************************************
110
111 void ReadOtuCommand::help(){
112         try {
113                 mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
114                 mothurOut("collect.shared, rarefaction.shared or summary.shared command.   Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command \n");
115                 mothurOut("or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, group, order and label.\n");
116                 mothurOut("The read.otu command can be used in two ways.  The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single.\n");
117                 mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
118                 mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
119                 mothurOut("The label parameter is used to read specific labels in your input.\n");
120                 mothurOut("The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands.\n");
121                 mothurOut("In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile) or read.otu(shared=yourSharedFile).  \n");
122                 mothurOut("The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file\n");
123                 mothurOut("and separates it into groups.  It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .rabund file for each group. \n");
124                 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
125
126         }
127         catch(exception& e) {
128                 errorOut(e, "ReadOtuCommand", "help");
129                 exit(1);
130         }
131 }
132
133
134
135 //**********************************************************************************************************************
136
137 ReadOtuCommand::~ReadOtuCommand(){
138         }
139
140 //**********************************************************************************************************************
141
142 int ReadOtuCommand::execute(){
143         try {
144         
145                 if (abort == true) {    return 0;       }
146                 
147                 if (globaldata->getFormat() == "shared") {
148                         
149                         shared = new SharedCommand();
150                         int okay = shared->execute();
151                         delete shared;
152                         
153                         //problem with shared
154                         if (okay == 1) {
155                                 globaldata->setListFile("");
156                                 globaldata->setGroupFile("");
157                                 globaldata->setSharedFile("");
158                         }else{
159                                 
160                                 //change format to shared  to speed up commands
161                                 globaldata->setFormat("sharedfile");
162                                 globaldata->setListFile("");
163                                 globaldata->setGroupFile("");
164                                 globaldata->setSharedFile(getRootName(filename) + "shared");
165                         }
166                 }
167                 return 0;
168         }
169         catch(exception& e) {
170                 errorOut(e, "ReadOtuCommand", "execute");
171                 exit(1);
172         }
173 }
174 //**********************************************************************************************************************