File: splitgroupscommand.h

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (229 lines) | stat: -rwxr-xr-x 9,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef SPLITGROUPSCOMMAND_H
#define SPLITGROUPSCOMMAND_H

/*
 *  splitgroupscommand.h
 *  Mothur
 *
 *  Created by westcott on 9/20/10.
 *  Copyright 2010 Schloss Lab. All rights reserved.
 *
 */


/* split.groups - given a group file, split sequences and names files in to separate files *.group1.fasta and .group1.names. */


#include "command.hpp"
#include "groupmap.h"
#include "sequence.hpp"
#include "fastqread.h"
#include "getseqscommand.h"

//**********************************************************************************************************************
struct flowOutput {
    string output;
    string filename;
    int total;
    
    flowOutput(string f) { filename = f; output = ""; total = 0;  }
    flowOutput() { filename = ""; output = ""; total = 0;  }
    flowOutput(string f, string o, int t) : filename(f), output(o), total(t) {}
    
};
//**********************************************************************************************************************
struct fastqOutput {
    vector<FastqRead> output;
    string filename;
    int total;
    
    fastqOutput(string f) { filename = f;  total = 0;  }
    fastqOutput() { filename = "";  total = 0;  }
};
//**********************************************************************************************************************
struct splitGroupsStruct {
    string groupfile, countfile, namefile, inputFileName, format;
    int start, end;
    bool isFastq;
    vector<string> Groups;
    map<string, fastqOutput> parsedFastqData;
    map<string, flowOutput> parsedFlowData;
    map<string, vector<string> > outputTypes;
    vector<string> outputNames;
    MothurOut* m;
    Utils util;
    
    splitGroupsStruct(string group, string count, string name, vector<string> g, int st, int en) : groupfile(group), countfile(count), namefile(name), start(st), end(en) {
        m = MothurOut::getInstance(); format = "illumina1.8+"; isFastq = true;
        for (int i = st; i < en; i++) { Groups.push_back(g[i]); }
    }
    
    void setFiles(string input, string outfileRoot, string ext) {
        inputFileName = input;
        
        int numFlows = 0;
        if (ext != ".fastq") {
            ifstream in; util.openInputFile(input, in);
            in >> numFlows; in.close();
        }
        
        for (int i = 0; i < Groups.size(); i++) {
            string newFileName = outfileRoot + Groups[i] + ext;
            
            if (ext == ".fastq") {
                fastqOutput thisGroupsInfo(newFileName);
                parsedFastqData[Groups[i]] = thisGroupsInfo;
                ofstream out; util.openOutputFile(newFileName, out);  out.close(); //clear file for append
            }else {
                flowOutput thisGroupsInfo(newFileName);
                parsedFlowData[Groups[i]] = thisGroupsInfo;
                isFastq = false;
                
                ofstream out; util.openOutputFile(newFileName, out);  out << numFlows << endl; out.close(); //clear file for append
            }

            if (m->getControl_pressed()) { break; }
        }
    }
    
    void setFormat(string form) {  format = form;  }
};
//**********************************************************************************************************************
struct splitGroups2Struct {
    string groupfile, countfile, namefile, fastafile, listfile, outputDir;
    int start, end;
    vector<string> Groups;
    map<string, vector<string> > group2Files; //GroupName -> files(fasta, list, count) or  GroupName -> files(fasta, list, group, name) 
    map<string, vector<string> > outputTypes;
    vector<string> outputNames;
    MothurOut* m;
    Utils util;
    
    splitGroups2Struct(string group, string count, string name, vector<string> g, int st, int en) : groupfile(group), countfile(count), namefile(name), start(st), end(en) {
        m = MothurOut::getInstance();
        for (int i = st; i < en; i++) { Groups.push_back(g[i]); }
    }
    
    void setFiles(string fasta, string list, string outd) {
        fastafile = fasta;
        listfile = list;
        outputDir = outd;
        
        string fastaFileRoot = outputDir + util.getRootName(util.getSimpleName(fastafile));
        string listFileRoot = outputDir + util.getRootName(util.getSimpleName(listfile));
        string listExt = util.getExtension(listfile);
        string fastaExt = util.getExtension(fastafile);
        
        string countFileRoot, countExt, nameFileRoot, nameExt, groupFileRoot, groupExt;
        if (countfile != "") {
            countFileRoot = outputDir + util.getRootName(util.getSimpleName(countfile));
            countExt = util.getExtension(countfile);
            groupFileRoot = ""; groupExt = ""; nameFileRoot = ""; nameExt = "";
        }else {
            groupFileRoot = outputDir + util.getRootName(util.getSimpleName(groupfile));
            groupExt = util.getExtension(groupfile);
            if (namefile != "") {
                nameFileRoot = outputDir + util.getRootName(util.getSimpleName(namefile));
                nameExt = util.getExtension(namefile);
            }else { nameFileRoot = ""; nameExt = ""; }
            countFileRoot = ""; countExt = "";
        }
        
        for (int i = 0; i < Groups.size(); i++) {
            
            if (m->getControl_pressed()) { break; }
            
            string newListFileName = listFileRoot  + Groups[i] + listExt;
            string newFastaFileName = fastaFileRoot  + Groups[i] + fastaExt;
            string newCountFileName = countFileRoot  + Groups[i] + countExt;
            string newGroupFileName = groupFileRoot  + Groups[i] + groupExt;
            string newNameFileName = nameFileRoot  + Groups[i] + nameExt;
            
            vector<string> files;
            if (fastafile != "")    { files.push_back(newFastaFileName);    }else { files.push_back(""); }
            if (listfile != "")     { files.push_back(newListFileName);     }else { files.push_back(""); }
            
            if (countfile != "")    {
                files.push_back(newCountFileName);
            }else if (groupfile != "") {
                files.push_back(newGroupFileName);
                if (namefile != "") {
                    files.push_back(newNameFileName);
                }//else{ files.push_back(""); }
            }
            
            group2Files[Groups[i]] = files;
        }
    }
};
//**********************************************************************************************************************
struct splitGroups3Struct {
    string countfile, fastafile, outputDir;
    int start, end, splitNum;
    vector<string> Groups;
    vector< string > fileRootExts; // fastafileRoot, fastaFileExtentsions, countfileRoot, countFileExtension,
    map<string, vector<string> > outputTypes;
    vector<string> outputNames;
    MothurOut* m;
    Utils util;
    
    splitGroups3Struct(string count, string fasta, string outd, vector<string> g, int st, int en) : fastafile(fasta), countfile(count), outputDir(outd), start(st), end(en) {
        m = MothurOut::getInstance();
        for (int i = st; i < en; i++) { Groups.push_back(g[i]); }
        
        splitNum = 10;
        
        string thisOutputDir = outputDir;
        if (outputDir == "") {  thisOutputDir = util.hasPath(fastafile);  }
        string fastaFileRoot = thisOutputDir + util.getRootName(util.getSimpleName(fastafile));
        string fastaExt = util.getExtension(fastafile);
       
        if (outputDir == "") {  thisOutputDir = util.hasPath(countfile);  }
        string countFileRoot = thisOutputDir + util.getRootName(util.getSimpleName(countfile));
        string countExt = util.getExtension(countfile);
        
        fileRootExts.push_back(fastaFileRoot); fileRootExts.push_back(fastaExt);
        fileRootExts.push_back(countFileRoot); fileRootExts.push_back(countExt);
    }
};
/***************************************************************************************/

class SplitGroupCommand : public Command {
	
public:
	SplitGroupCommand(string);
    SplitGroupCommand(vector<string>, string fasta, string count, string o); //used by splitbySample algos
	~SplitGroupCommand() = default;
	
	vector<string> setParameters();
	string getCommandName()			{ return "split.groups";				}
	string getCommandCategory()		{ return "Sequence Processing";		}
	
	string getHelpString();	
    string getOutputPattern(string);	
	string getCitation() { return "http://www.mothur.org/wiki/Split.group"; }
	string getDescription()		{ return "split a name or fasta file by group"; }

	
	int execute(); 
	void help() { m->mothurOut(getHelpString()); }	
	
private:
	vector<string> outputNames;
	vector<linePair> lines;
	string  namefile, groupfile, countfile, groups, fastafile, flowfile, fastqfile, format, listfile;
	vector<string> Groups;
	bool abort;
    int processors;
    
    void splitCountOrGroup(bool);
    void splitFastaCount();
    void splitFastqOrFlow(string, string);
};

/***************************************************************************************/

#endif