File: filefile.cpp

package info (click to toggle)
mothur 1.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 13,684 kB
  • sloc: cpp: 161,854; makefile: 122; sh: 31
file content (283 lines) | stat: -rw-r--r-- 11,283 bytes parent folder | download | duplicates (3)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
//  filefile.cpp
//  Mothur
//
//  Created by Sarah Westcott on 12/5/18.
//  Copyright © 2018 Schloss Lab. All rights reserved.
//

#include "filefile.hpp"
#include "utils.hpp"

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

FileFile::FileFile(string f, string md) : filename(f), mode(md) {
    try {
        m = MothurOut::getInstance();
        
        current = CurrentFile::getInstance();
        mpath = current->getProgramPath();
        
        columnWithGroups = false;
        fileOption = 0;
        gz = false;
        hasIndex = false;
        read(f, mode);
    }
    catch(exception& e) {
        m->errorOut(e, "FileFile", "FileFile");
        exit(1);
    }
}
/**************************************************************************************************

 /*
  file option 1
  
  sfffile1   oligosfile1
  sfffile2   oligosfile2
  ...
  
  file option 2
  
  fastqfile1 oligosfile1
  fastqfile2 oligosfile2
  ...
  
  file option 3
  
  ffastqfile1 rfastqfile1
  ffastqfile2 rfastqfile2
  ...
  
  file option 4 - only vaild if mode is set to parsefastqpacbio
  
  group1 pacBiofastqfile1
  group2 pacBiofastqfile2
  ...
  
  file option 5
  
  group fastqfile  fastqfile
  group fastqfile  fastqfile
  group fastqfile  fastqfile
  ...
  
  file option 6
  
  My.forward.fastq My.reverse.fastq none My.rindex.fastq //none is an option is no forward or reverse index file
  ...
  
  file option 7 - for make.count command
  
  group fastafile
  
  file option 8 - for make.count command
  
  group forwardFasta reverseFasta
  
  */

/**************************************************************************************************/
void FileFile::read(string f, string mode){
    try {
    
            filename = f;
            
            bool allGZ = true; bool allPlainTxt = true;
            
            ifstream in; util.openInputFile(filename, in);
            
            while(!in.eof()) {
                
                if (m->getControl_pressed()) { break; }
                
                bool skip = false;
                string line = util.getline(in);  gobble(in);
                
                if (m->getDebug()) {  m->mothurOut("[DEBUG]: " + line +"\n");  }
                
                if(line[0] == '#'){  } //ignore
                else {
                    
                    vector<string> pieces = util.splitWhiteSpace(line);
                    
                    if (mode == "make.count") {
                        vector<string> thisGroupsFiles;
                        
                        if ((pieces.size() == 2) || (pieces.size() == 3)) {
                            util.checkGroupName(pieces[0]);
                            bool skip = false;
                            
                            //check to make fasta file opens
                            bool ableToOpen = util.checkLocations(pieces[1], current->getLocations());
                            if (ableToOpen) {
                                if (util.isBlank(pieces[1])) { m->mothurOut("[WARNING]: " + pieces[1] + " is blank, skipping.\n"); skip=true; }
                            }else { m->mothurOut("[WARNING]: can't find " + pieces[1] + ", ignoring.\n"); skip = true; }
                            
                            if (pieces.size() == 3) {
                                //check to make fasta file opens
                                ableToOpen = util.checkLocations(pieces[2], current->getLocations());
                                if (ableToOpen) {
                                    if (util.isBlank(pieces[2])) { m->mothurOut("[WARNING]: " + pieces[2] + " is blank, skipping.\n"); skip=true; }
                                }else { m->mothurOut("[WARNING]: can't find " + pieces[2] + ", ignoring.\n"); skip = true; }
                            }
                        
                            if (!skip) {
                                groupNames.push_back(pieces[0]);
                                thisGroupsFiles.push_back(pieces[1]);
                                if (pieces.size() == 3) { thisGroupsFiles.push_back(pieces[2]); }
                                files.push_back(thisGroupsFiles);
                            }
                    
                        }else {
                            m->mothurOut("[ERROR]: Found " + toString(pieces.size()) + " columns. mothur expects the file file for make.count to be in 2 or 3 column form.  \n");
                        }
                    }else {
                        string group = ""; string forward, reverse, findex, rindex;
                        skip = validateFiles(pieces, forward, reverse, findex, rindex, group);
                        
                        if (!skip) { //good pair
                            groupNames.push_back(group);
                            if (((findex != "") || (rindex != ""))) { hasIndex = true; }
                            
                            if ((mode == "contigs") || (mode == "sra")) { setGZ(forward, reverse, findex, rindex, allGZ, allPlainTxt); }
                            
                            vector<string> pair;
                            pair.push_back(forward); pair.push_back(reverse); pair.push_back(findex); pair.push_back(rindex);
                            files.push_back(pair);
                        }
                    }
                }
            }
            in.close();
            
            if ((mode == "contigs") || (mode == "sra")){ if (allGZ) { gz = true; } else { gz = false; } }
            
        if (files.size() == 0) { m->setControl_pressed(true); return; }
        
        if (mode == "make.count") {
            fileOption = 6 + (int)files[0].size(); //either 1 or 2
        }
        
    }
    catch(exception& e) {
        m->errorOut(e, "FileFile", "read");
        exit(1);
    }
}
/**************************************************************************************************/
bool FileFile::validateFiles(vector<string> pieces, string& forward, string& reverse, string& findex, string& rindex, string& group){
    try {
        bool skip = false; //innocent until proven guilty
        group = "";
        if (pieces.size() == 2) {
            if (mode == "parsefastqpacbio") {
                group = pieces[0];
                util.checkGroupName(group);
                forward = pieces[1];
                reverse = "";
            }else {
                forward = pieces[0];
                reverse = pieces[1];
                group = "";
            }
            findex = "";
            rindex = "";
            fileOption = 1;
        }else if (pieces.size() == 3) {
            group = pieces[0];
            util.checkGroupName(group);
            forward = pieces[1];
            reverse = pieces[2];
            if ((reverse == "none") || (reverse == "NONE")){ reverse = "NONE"; }
            findex = "";
            rindex = "";
            fileOption = 2;
            columnWithGroups = true;
        }else if (pieces.size() == 4) {
            forward = pieces[0];
            reverse = pieces[1];
            findex = pieces[2];
            rindex = pieces[3];
            if ((findex == "none") || (findex == "NONE")){ findex = "NONE"; }
            if ((rindex == "none") || (rindex == "NONE")){ rindex = "NONE"; }
            fileOption = 3;
        }else {
            m->mothurOut("[ERROR]: file lines can be 2, 3, or 4 columns. The forward fastq files in the first column and their matching reverse fastq files in the second column, or a groupName then forward fastq file and reverse fastq file, or forward fastq file then reverse fastq then forward index and reverse index file.  If you only have one index file add 'none' for the other one. \n"); m->setControl_pressed(true);
        }
        
        if (m->getDebug()) { m->mothurOut("[DEBUG]: group = " + group + ", forward = " + forward + ", reverse = " + reverse + ", forwardIndex = " + findex + ", reverseIndex = " + rindex + ".\n"); }
        
        //check to make sure both are able to be opened
        bool openForward = util.checkLocations(forward, current->getLocations());
        if (openForward) {
            if (util.isBlank(forward)) { m->mothurOut("[WARNING]: " + forward + " is blank, skipping.\n"); skip=true; }
        }else { m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n"); }
        
        bool openReverse = true;
        if ((reverse != "") && (reverse != "NONE")){
            openReverse = util.checkLocations(reverse, current->getLocations());
            if (openReverse) {
                if (util.isBlank(reverse)) { m->mothurOut("[WARNING]: " + reverse + " is blank, skipping.\n"); skip=true; }
            }else { m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n"); }
        }
        
        bool openFindex = true;
        if ((findex != "") && (findex != "NONE")){
            openFindex = util.checkLocations(findex, current->getLocations());
            if (openFindex) {
                if (util.isBlank(findex)) { m->mothurOut("[WARNING]: " + findex + " is blank, skipping.\n"); skip=true; }
            }else { m->mothurOut("[WARNING]: can't find " + findex + ", ignoring pair.\n"); }
        }
        
        bool openRindex = true;
        if ((rindex != "") && (rindex != "NONE")) {
            openRindex = util.checkLocations(rindex, current->getLocations());
            if (openRindex) {
                if (util.isBlank(rindex)) { m->mothurOut("[WARNING]: " + rindex + " is blank, skipping.\n"); skip=true; }
            }else { m->mothurOut("[WARNING]: can't find " + rindex + ", ignoring pair.\n"); }
        }

        if ((openForward) && (openReverse) && (openFindex) && (openRindex) && (!skip)) { //good pair
            return false;
        }else { return true; }
    }
    catch(exception& e) {
        m->errorOut(e, "FileFile", "validateFiles");
        exit(1);
    }
}
/**************************************************************************************************/
void FileFile::setGZ(string forward, string reverse, string findex, string rindex, bool& allGZ, bool& allPlainTxt){
    try {
        
#ifdef USE_BOOST
        if (util.isGZ(forward)[1]) { allPlainTxt = false;  }
        else {   allGZ = false;  }
        if (util.isGZ(reverse)[1]) { allPlainTxt = false;  }
        else {   allGZ = false;  }
        if ((findex != "") && (findex != "NONE")) {
            if (util.isGZ(findex)[1]) { allPlainTxt = false;  }
            else {   allGZ = false;  }
        }
        if ((rindex != "") && (rindex != "NONE")) {
            if (util.isGZ(rindex)[1]) { allPlainTxt = false;  }
            else {   allGZ = false;  }
        }
        if (!allGZ && !allPlainTxt) { //mixed bag of files, uh oh...
            m->mothurOut("[ERROR]: Your files must all be in compressed .gz form or all in plain text form.  Please correct. \n"); m->setControl_pressed(true);
        }
#else
        allGZ=false;
#endif
        
    }
    catch(exception& e) {
        m->errorOut(e, "FileFile", "setGZ");
        exit(1);
    }
}
/**************************************************************************************************/