]> git.donarmstrong.com Git - mothur.git/blob - seqsummarycommand.h
fixes while testing 1.33.0
[mothur.git] / seqsummarycommand.h
1 #ifndef SEQSUMMARYCOMMAND_H
2 #define SEQSUMMARYCOMMAND_H
3
4 /*
5  *  seqcoordcommand.h
6  *  Mothur
7  *
8  *  Created by Pat Schloss on 5/30/09.
9  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
10  *
11  */
12
13 #include "mothur.h"
14 #include "command.hpp"
15 #include "sequence.hpp"
16
17 /**************************************************************************************************/
18
19 class SeqSummaryCommand : public Command {
20 public:
21         SeqSummaryCommand(string);
22         SeqSummaryCommand();
23         ~SeqSummaryCommand(){}
24         
25         vector<string> setParameters();
26         string getCommandName()                 { return "summary.seqs";                        }
27         string getCommandCategory()             { return "Sequence Processing";         }
28         
29         string getHelpString(); 
30     string getOutputPattern(string);    
31         string getCitation() { return "http://www.mothur.org/wiki/Summary.seqs"; }
32         string getDescription()         { return "summarize the quality of sequences in an unaligned or aligned fasta file"; }
33         
34         int execute(); 
35         void help() { m->mothurOut(getHelpString()); }          
36 private:
37         bool abort;
38         string fastafile, outputDir, namefile, countfile;
39         int processors;
40         vector<string> outputNames;
41         map<string, int> nameMap;
42         
43         struct linePair {
44                 unsigned long long start;
45                 unsigned long long end;
46                 linePair(unsigned long long i, unsigned long long j) : start(i), end(j) {}
47         };
48
49         vector<linePair*> lines;
50         vector<int> processIDS;
51         
52         int createProcessesCreateSummary(vector<int>&, vector<int>&, vector<int>&, vector<int>&, vector<int>&, string, string);
53         int driverCreateSummary(vector<int>&, vector<int>&, vector<int>&, vector<int>&, vector<int>&, string, string, linePair*);       
54
55         #ifdef USE_MPI
56         int MPICreateSummary(int, int, vector<int>&, vector<int>&, vector<int>&, vector<int>&, vector<int>&, MPI_File&, MPI_File&, vector<unsigned long long>&);        
57         #endif
58
59
60 };
61
62 /**************************************************************************************************/
63 //custom data structure for threads to use.
64 // This is passed by void pointer so it can be any data type
65 // that can be passed using a single void pointer (LPVOID).
66 struct seqSumData {
67         vector<int> startPosition;
68         vector<int> endPosition;
69         vector<int> seqLength; 
70         vector<int> ambigBases; 
71         vector<int> longHomoPolymer; 
72         string filename; 
73         string sumFile; 
74         unsigned long long start;
75         unsigned long long end;
76         int count;
77         MothurOut* m;
78         bool hasNameMap;
79         map<string, int> nameMap;
80         
81         
82         seqSumData(){}
83         seqSumData(string f, string sf, MothurOut* mout, unsigned long long st, unsigned long long en, bool na, map<string, int> nam) {
84                 filename = f;
85                 sumFile = sf;
86                 m = mout;
87                 start = st;
88                 end = en;
89                 hasNameMap = na;
90                 nameMap = nam;
91                 count = 0;
92         }
93 };
94
95 /**************************************************************************************************/
96 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
97 #else
98 static DWORD WINAPI MySeqSumThreadFunction(LPVOID lpParam){ 
99         seqSumData* pDataArray;
100         pDataArray = (seqSumData*)lpParam;
101         
102         try {
103                 ofstream outSummary;
104                 pDataArray->m->openOutputFile(pDataArray->sumFile, outSummary);
105                 
106                 ifstream in;
107                 pDataArray->m->openInputFile(pDataArray->filename, in);
108
109                 //print header if you are process 0
110                 if ((pDataArray->start == 0) || (pDataArray->start == 1)) {
111                         outSummary << "seqname\tstart\tend\tnbases\tambigs\tpolymer\tnumSeqs" << endl;  
112                         in.seekg(0);
113                 }else { //this accounts for the difference in line endings. 
114                         in.seekg(pDataArray->start-1); pDataArray->m->gobble(in); 
115                 }
116                 
117                 for(int i = 0; i < pDataArray->end; i++){ //end is the number of sequences to process
118                         
119             pDataArray->count++;
120             
121                         if (pDataArray->m->control_pressed) { in.close(); outSummary.close(); pDataArray->count = 1; return 1; }
122                         
123                         Sequence current(in); pDataArray->m->gobble(in); 
124                         
125                         if (current.getName() != "") {
126                                 
127                                 int num = 1;
128                                 if (pDataArray->hasNameMap){
129                                         //make sure this sequence is in the namefile, else error 
130                                         map<string, int>::iterator it = pDataArray->nameMap.find(current.getName());
131                                         
132                                         if (it == pDataArray->nameMap.end()) { pDataArray->m->mothurOut("[ERROR]: " + current.getName() + " is not in your name or count file, please correct."); pDataArray->m->mothurOutEndLine(); pDataArray->m->control_pressed = true; }
133                                         else { num = it->second; }
134                                 }
135                                 
136                                 //for each sequence this sequence represents
137                                 for (int i = 0; i < num; i++) {
138                                         pDataArray->startPosition.push_back(current.getStartPos());
139                                         pDataArray->endPosition.push_back(current.getEndPos());
140                                         pDataArray->seqLength.push_back(current.getNumBases());
141                                         pDataArray->ambigBases.push_back(current.getAmbigBases());
142                                         pDataArray->longHomoPolymer.push_back(current.getLongHomoPolymer());
143                                 }
144                                 
145                                 outSummary << current.getName() << '\t';
146                                 outSummary << current.getStartPos() << '\t' << current.getEndPos() << '\t';
147                                 outSummary << current.getNumBases() << '\t' << current.getAmbigBases() << '\t';
148                                 outSummary << current.getLongHomoPolymer() << '\t' << num << endl;
149                         }
150                 }
151                 
152                 in.close();
153                 outSummary.close();
154                 
155                 return 0;
156                 
157         }
158         catch(exception& e) {
159                 pDataArray->m->errorOut(e, "SeqSummaryCommand", "MySeqSumThreadFunction");
160                 exit(1);
161         }
162
163 #endif
164
165
166
167
168 #endif
169
170 /**************************************************************************************************/
171
172