]> git.donarmstrong.com Git - mothur.git/blob - summaryqualcommand.h
Merge remote-tracking branch 'mothur/master'
[mothur.git] / summaryqualcommand.h
1 #ifndef SUMMARYQUALCOMMAND_H
2 #define SUMMARYQUALCOMMAND_H
3
4 /*
5  *  summaryqualcommand.h
6  *  Mothur
7  *
8  *  Created by westcott on 11/28/11.
9  *  Copyright 2011 Schloss Lab. All rights reserved.
10  *
11  */
12
13
14 #include "command.hpp"
15 #include "qualityscores.h"
16
17 /**************************************************************************************************/
18
19 class SummaryQualCommand : public Command {
20 public:
21         SummaryQualCommand(string);
22         SummaryQualCommand();
23         ~SummaryQualCommand(){}
24         
25         vector<string> setParameters();
26         string getCommandName()                 { return "summary.qual";                        }
27         string getCommandCategory()             { return "Sequence Processing";         }
28         string getHelpString(); 
29         string getCitation() { return "http://www.mothur.org/wiki/Summary.qual"; }
30         string getDescription()         { return "summarize the quality of a set of sequences"; }
31         
32         int execute(); 
33         void help() { m->mothurOut(getHelpString()); }  
34         
35 private:
36         bool abort;
37         string qualfile, outputDir, namefile;
38         vector<string> outputNames;
39         map<string, int> nameMap;
40         int processors;
41         
42         struct linePair {
43                 unsigned long long start;
44                 unsigned long long end;
45                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
46         };
47         
48         vector<linePair> lines;
49         vector<int> processIDS;
50         
51         int createProcessesCreateSummary(vector<int>&, vector<int>&, vector< vector<int> >&, string);
52         int driverCreateSummary(vector<int>&, vector<int>&, vector< vector<int> >&, string, linePair);  
53         int printQual(string, vector<int>&, vector<int>&, vector< vector<int> >&);
54 };
55
56 /**************************************************************************************************/
57 //custom data structure for threads to use.
58 // This is passed by void pointer so it can be any data type
59 // that can be passed using a single void pointer (LPVOID).
60 struct seqSumQualData {
61         vector<int> position;
62         vector<int> averageQ;
63         vector< vector<int> > scores; 
64         string filename, namefile; 
65         unsigned long long start;
66         unsigned long long end;
67         int count;
68         MothurOut* m;
69         map<string, int> nameMap;
70         
71         ~seqSumQualData(){}
72         seqSumQualData(string f, MothurOut* mout, unsigned long long st, unsigned long long en, string n, map<string, int> nam) {
73                 filename = f;
74                 m = mout;
75                 start = st;
76                 end = en;
77                 namefile = n;
78                 nameMap = nam;
79                 count = 0;
80         }
81 };
82
83 /**************************************************************************************************/
84 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
85 #else
86 static DWORD WINAPI MySeqSumQualThreadFunction(LPVOID lpParam){ 
87         seqSumQualData* pDataArray;
88         pDataArray = (seqSumQualData*)lpParam;
89         
90         try {
91                 ifstream in;
92                 pDataArray->m->openInputFile(pDataArray->filename, in);
93                 
94                 //print header if you are process 0
95                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
96                         in.seekg(0);
97                 }else { //this accounts for the difference in line endings. 
98                         in.seekg(pDataArray->start-1); pDataArray->m->gobble(in); 
99                 }
100                 
101                 int count = 0;
102                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
103                                 
104                         if (pDataArray->m->control_pressed) { in.close(); pDataArray->count = 1; return 1; }
105                         
106                         QualityScores current(in); pDataArray->m->gobble(in);
107                         
108                         if (current.getName() != "") {
109                         
110                                 int num = 1;
111                                 if (pDataArray->namefile != "") {
112                                         //make sure this sequence is in the namefile, else error 
113                                         map<string, int>::iterator it = pDataArray->nameMap.find(current.getName());
114                                         
115                                         if (it == pDataArray->nameMap.end()) { pDataArray->m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); pDataArray->m->mothurOutEndLine(); pDataArray->m->control_pressed = true; }
116                                         else { num = it->second; }
117                                 }
118                                 
119                                 vector<int> thisScores = current.getQualityScores();
120                                 
121                                 //resize to num of positions setting number of seqs with that size to 1
122                                 if (pDataArray->position.size() < thisScores.size()) { pDataArray->position.resize(thisScores.size(), 0); }
123                                 if (pDataArray->averageQ.size() < thisScores.size()) { pDataArray->averageQ.resize(thisScores.size(), 0); }
124                                 if (pDataArray->scores.size() < thisScores.size()) { 
125                                         pDataArray->scores.resize(thisScores.size()); 
126                                         for (int i = 0; i < pDataArray->scores.size(); i++) { pDataArray->scores.at(i).resize(41, 0); }
127                                 }
128                                 
129                                 //increase counts of number of seqs with this position
130                                 //average is really the total, we will average in execute
131                                 for (int i = 0; i < thisScores.size(); i++) { 
132                                         pDataArray->position.at(i) += num; 
133                                         pDataArray->averageQ.at(i) += (thisScores[i] * num); //weighting for namesfile
134                                         if (thisScores[i] > 40) { pDataArray->m->mothurOut("[ERROR]: " + current.getName() + " has a quality scores of " + toString(thisScores[i]) + ", expecting values to be less than 40."); pDataArray->m->mothurOutEndLine(); pDataArray->m->control_pressed = true; }
135                                         else { pDataArray->scores.at(i)[thisScores[i]] += num; }  
136                                 }
137                                 
138                                 count += num;
139                         }
140                 }
141                 
142                 pDataArray->count = count;
143                 in.close();
144                 
145                 return 0;
146                 
147         }
148         catch(exception& e) {
149                 pDataArray->m->errorOut(e, "SummaryQualCommand", "MySeqSumQualThreadFunction");
150                 exit(1);
151         }
152
153 #endif
154
155
156 /**************************************************************************************************/
157
158
159 #endif
160