]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
added chop.seqs command
[mothur.git] / chopseqscommand.cpp
1 /*
2  *  chopseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/10/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chopseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14
15 ChopSeqsCommand::ChopSeqsCommand(string option)  {
16         try {
17                 abort = false;
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[] =  {"fasta","end","outputdir","inputdir"};
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                         map<string,string>::iterator it;
32                         
33                         //check to make sure all parameters are valid for command
34                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //if the user changes the output directory command factory will send this info to us in the output parameter 
39                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
40                         
41                         //if the user changes the input directory command factory will send this info to us in the output parameter 
42                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
43                         if (inputDir == "not found"){   inputDir = "";          }
44                         else {
45                                 string path;
46                                 it = parameters.find("fasta");
47                                 //user has given a template file
48                                 if(it != parameters.end()){ 
49                                         path = hasPath(it->second);
50                                         //if the user has not given a path then, add inputdir. else leave path alone.
51                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
52                                 }
53                         }
54
55                         //check for required parameters
56                         fastafile = validParameter.validFile(parameters, "fasta", true);
57                         if (fastafile == "not open") { abort = true; }
58                         else if (fastafile == "not found") {  m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }    
59                         
60                         string temp = validParameter.validFile(parameters, "end", false);       
61                         if (temp == "not found") {  m->mothurOut("You must provide an end for the chops.seqs command."); m->mothurOutEndLine(); abort = true; } 
62                         else {  
63                                 convert(temp, end);   
64                                 if (end < 0) { m->mothurOut("End must be positive."); m->mothurOutEndLine(); abort = true;  }
65                         }
66                         
67                 }
68
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
72                 exit(1);
73         }
74 }
75 //**********************************************************************************************************************
76
77 void ChopSeqsCommand::help(){
78         try {
79                 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta with sequences trimmed to the end position.\n");
80                 m->mothurOut("The chop.seqs command parameters are fasta and end, both are required.\n");
81                 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n");
82                 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n");
83                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
84         }
85         catch(exception& e) {
86                 m->errorOut(e, "ChopSeqsCommand", "help");
87                 exit(1);
88         }
89 }
90
91 //**********************************************************************************************************************
92
93 int ChopSeqsCommand::execute(){
94         try {
95                 
96                 if (abort == true) { return 0; }
97                 
98                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
99                 
100                 ofstream out;
101                 openOutputFile(outputFileName, out);
102                 
103                 ifstream in;
104                 openInputFile(fastafile, in);
105                 
106                 while (!in.eof()) {
107                 
108                         Sequence seq(in, "no align");
109                         
110                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0;  }
111                         
112                         if (seq.getName() != "") {
113                                 string temp = seq.getUnaligned();
114                                 
115                                 //output sequence name
116                                 out << ">" << seq.getName() << endl;
117                                 
118                                 //if needed trim sequence
119                                 if (temp.length() > end) {  temp = temp.substr(0, end);         }
120                                 
121                                 //output trimmed sequence       
122                                 out << temp << endl;
123                         }
124                 }
125                 in.close();
126                 out.close();
127                 
128                 m->mothurOutEndLine();
129                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
130                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
131                 m->mothurOutEndLine();
132                 
133                 return 0;               
134         }
135
136         catch(exception& e) {
137                 m->errorOut(e, "ChopSeqsCommand", "execute");
138                 exit(1);
139         }
140 }
141
142 //**********************************************************************************************************************
143
144