]> git.donarmstrong.com Git - mothur.git/blob - chimera.cpp
started work on sffinfo command. fixed bug across all paralellized commands if the...
[mothur.git] / chimera.cpp
1 /*
2  *  chimera.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 8/11/09.
6  *  Copyright 2009 Schloss Lab Umass Amherst. All rights reserved.
7  *
8  */
9
10 #include "chimera.h"
11
12 //***************************************************************************************************************
13 //this is a vertical soft filter
14 string Chimera::createFilter(vector<Sequence*> seqs, float t) {
15         try {
16                 filterString = "";
17                 int threshold = int (t * seqs.size());
18 //cout << "threshhold = " << threshold << endl;
19                 
20                 vector<int> gaps;       gaps.resize(seqs[0]->getAligned().length(), 0);
21                 vector<int> a;          a.resize(seqs[0]->getAligned().length(), 0);
22                 vector<int> t;          t.resize(seqs[0]->getAligned().length(), 0);
23                 vector<int> g;          g.resize(seqs[0]->getAligned().length(), 0);
24                 vector<int> c;          c.resize(seqs[0]->getAligned().length(), 0);
25         
26                 filterString = (string(seqs[0]->getAligned().length(), '1'));
27                 
28                 //for each sequence
29                 for (int i = 0; i < seqs.size(); i++) {
30                 
31                         if (m->control_pressed) { return filterString; }
32                 
33                         string seqAligned = seqs[i]->getAligned();
34                         
35                         if (seqAligned.length() != filterString.length()) {  m->mothurOut(seqs[i]->getName() + " is not the same length as the template sequences. Aborting!\n");  exit(1); }
36                 
37                         for (int j = 0; j < seqAligned.length(); j++) {
38                                 //if this spot is a gap
39                                 if ((seqAligned[j] == '-') || (seqAligned[j] == '.'))   {       gaps[j]++;      }
40                                 else if (toupper(seqAligned[j]) == 'A')                                 {       a[j]++;         }
41                                 else if (toupper(seqAligned[j]) == 'T')                                 {       t[j]++;         }
42                                 else if (toupper(seqAligned[j]) == 'G')                                 {       g[j]++;         }
43                                 else if (toupper(seqAligned[j]) == 'C')                                 {       c[j]++;         }
44                         }
45                 }
46                 
47                 //zero out spot where all sequences have blanks
48                 int numColRemoved = 0;
49                 for(int i = 0;i < seqs[0]->getAligned().length(); i++){
50                 
51                         if (m->control_pressed) { return filterString; }
52                         
53                         if(gaps[i] == seqs.size())      {       filterString[i] = '0';  numColRemoved++;  }
54                         
55                         else if (((a[i] < threshold) && (t[i] < threshold) && (g[i] < threshold) && (c[i] < threshold))) {      filterString[i] = '0';  numColRemoved++;  }
56                         //cout << "a = " << a[i] <<  " t = " << t[i] <<  " g = " << g[i] <<  " c = " << c[i] << endl;
57                 }
58
59                 if (threshold != 0.0) {  m->mothurOut("Filter removed " + toString(numColRemoved) + " columns.");  m->mothurOutEndLine();  }
60                 
61                 return filterString;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "Chimera", "createFilter");
65                 exit(1);
66         }
67 }
68 //***************************************************************************************************************
69 map<int, int> Chimera::runFilter(Sequence* seq) {
70         try {
71                 map<int, int> maskMap;
72                 string seqAligned = seq->getAligned();
73                 string newAligned = "";
74                 int count = 0;
75                         
76                 for (int j = 0; j < seqAligned.length(); j++) {
77                         //if this spot is a gap
78                         if (filterString[j] == '1') { 
79                                 newAligned += seqAligned[j]; 
80                                 maskMap[count] = j;
81                                 count++;
82                         }
83                 }
84                         
85                 seq->setAligned(newAligned);
86                 
87                 return maskMap;
88         }
89         catch(exception& e) {
90                 m->errorOut(e, "Chimera", "runFilter");
91                 exit(1);
92         }
93 }
94 //***************************************************************************************************************
95 vector<Sequence*> Chimera::readSeqs(string file) {
96         try {
97         
98                 vector<Sequence*> container;
99                 int count = 0;
100                 length = 0;
101                 unaligned = false;
102
103                 m->mothurOut("Reading sequences from " + file + "..."); cout.flush();
104                 
105                 #ifdef USE_MPI  
106                         int pid, processors;
107                         vector<unsigned long int> positions;
108                         int numSeqs;
109                         int tag = 2001;
110                 
111                         MPI_Status status; 
112                         MPI_File inMPI;
113                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
114                         MPI_Comm_size(MPI_COMM_WORLD, &processors);
115
116                         //char* inFileName = new char[file.length()];
117                         //memcpy(inFileName, file.c_str(), file.length());
118                         
119                         char inFileName[1024];
120                         strcpy(inFileName, file.c_str());
121         
122                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
123                         //delete inFileName;
124
125                         if (pid == 0) {
126                                 positions = setFilePosFasta(file, numSeqs); //fills MPIPos, returns numSeqs
127
128                                 //send file positions to all processes
129                                 for(int i = 1; i < processors; i++) { 
130                                         MPI_Send(&numSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
131                                         MPI_Send(&positions[0], (numSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
132                                 }
133                         }else{
134                                 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
135                                 positions.resize(numSeqs+1);
136                                 MPI_Recv(&positions[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
137                         }
138                         
139                         //read file 
140                         for(int i=0;i<numSeqs;i++){
141                         
142                                 if (m->control_pressed) { MPI_File_close(&inMPI); return container; }
143         
144                                 //read next sequence
145                                 int seqlength = positions[i+1] - positions[i];
146                                 char* buf4 = new char[seqlength];
147
148                                 MPI_File_read_at(inMPI, positions[i], buf4, seqlength, MPI_CHAR, &status);
149                                 
150                                 string tempBuf = buf4;
151                                 if (tempBuf.length() > seqlength) { tempBuf = tempBuf.substr(0, seqlength); }
152                                 delete buf4;
153
154                                 istringstream iss (tempBuf,istringstream::in);
155                 
156                                 Sequence* current = new Sequence(iss);   
157                                 if (current->getName() != "") {
158                                         if (count == 0) {  length = current->getAligned().length();  count++;  } //gets first seqs length
159                                         else if (length != current->getAligned().length()) {    unaligned = true;       }
160                         
161                                         container.push_back(current);  
162                                 }
163                         }
164                         
165                         MPI_File_close(&inMPI);
166                         MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
167         #else
168
169                 ifstream in;
170                 openInputFile(file, in);
171                 
172                 //read in seqs and store in vector
173                 while(!in.eof()){
174                         
175                         if (m->control_pressed) { return container; }
176                         
177                         Sequence* current = new Sequence(in);  gobble(in);
178                         
179                         if (count == 0) {  length = current->getAligned().length();  count++;  } //gets first seqs length
180                         else if (length != current->getAligned().length()) {    unaligned = true;       }
181                                                 
182                         if (current->getName() != "") {  container.push_back(current);  }
183                 }
184                 in.close();
185         #endif
186         
187                 m->mothurOut("Done."); m->mothurOutEndLine();
188                 
189                 return container;
190         }
191         catch(exception& e) {
192                 m->errorOut(e, "Chimera", "readSeqs");
193                 exit(1);
194         }
195 }
196 //***************************************************************************************************************
197 void Chimera::setMask(string filename) {
198         try {
199                 
200                 if (filename == "default") {
201                         //default is from wigeon  236627 EU009184.1 Shigella dysenteriae str. FBD013
202                         seqMask = ".....................................................................................................AAATTGAAGAGTTT-GA--T-CA-T-G-GCTC-AG-AT-TGAA-C-GC--TGG-C--G-GC-A-GG--C----C-T--AACACA-T-GC-A-AGT-CGA-A-CG----------G-TAA-CA-G----------------------------GAAG-A-AG----------------------------------------------------CTT-G----------------------------------------------------------------------------------CT-TCTTT----------------G-CT--G--AC--G--AG-T-GG-C-GG-A--C-------------GGG-TGAGT-A--AT-GT-C-T-G-GG---A-A--A-CT-G--C-C-TGA--TG-G------------------------------------------------------------------A-GG----GGG-AT-AA-CTA-------------------------C-T-G-----------------------GAA-A---CGG-TAG-CTAA-TA---CC-G--C-AT-A----------A--------------------C-------------------------------------GT-C-----------------------------------------------------------------------------------------------------------------------G-CA-A--------------------------------------------------------------------------------------------------------------------------------------G-A-C---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CAAA--G-A-G-GG-----G--GA-C-CT--------------------------------------------------------------------------------------------------------------------TCG-G----------------------------------------------------------------------------------------------------------------------G----CC-TC--T---T-G--------------C----C-A---T-CG-G---AT---G-T-----G-CCC-AGA--T-GGG--A------TT--A--G-CT-A----G---TAGG-T-G-GG-G-T----AAC-GG-C-T-C-ACCT--A-GG-C-G--A-CG-A------------TCC-C-T------AG-CT-G-G-TCT-G-AG----A--GG-AT--G-AC-C-AG-CCAC-A-CTGGA--A-C-TG-A-GA-C-AC-G-G-TCCAGA-CTCC-TAC-G--G-G-A-G-GC-A-GC-A-G-TG---GG-G-A-ATA-TTGCA-C-AA-T-GG--GC-GC-A----A-G-CC-T-GA-TG-CA-GCCA-TGCC-G-CG-T---G-T-A--T--GA-A-G--A--A-G-G-CC-----TT-CG---------G-G-T-T-G-T--A---AA-G-TAC--------TT-TC-A-G--C-GGG----GA-G--G---AA-GGGA---GTAA-AG----T--T--AA-T---A----C-----CT-T-TGC-TCA-TT-GA-CG-TT-A-C-CC-G-CA-G---------AA-----------GAAGC-ACC-GG-C-TAA---C--T-CCGT--GCCA--G-C---A--GCCG---C-GG--TA-AT--AC---GG-AG-GGT-GCA-A-G-CG-TTAA-T-CGG-AA-TT-A--C-T--GGGC-GTA----AA-GCGC-AC--G-CA-G-G-C-G------------G--T-TT-G-T-T-AA----G-T-C-A---G-ATG-TG-A-AA-TC--CC-CGG-G--------------------------------------------------------------------CT-C-AA-------------------------------------------------------------------------CC-T-G-GG-AA-C----T-G-C-A-T-C--------T--GA-T-A-C-T-G-GCA--A-G-C---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------T-T-G-A-G-T-C-----T-CG--TA-G-A------------G-GG-G-GG-T----AG--AATT-CCA-G-GT--GT-A-GCG-GTGAAA-TG-CGT-AGAG-A-TC-T-GGA--GG-A-AT-A-CC-GG--T--G--GC-GAA-G--G-C---G----G--C-C-CCCTG------G-AC-GA--------------------------------------------------------------AG-A-C-T--GA--CG-----CT-CA-GG--T-G-CGA--AA-G-C--------------G-TGGG-GAG-C-A-AACA--GG-ATTA-G-ATA-C-----CC-T-G-GTA-G-T----C-CA--C-G-CCG-T-AAA--C-GATG-TC--GA-CT---------T-GG--A--G-G-TT-G-TG-C--C--------------------------------------------------------------------------------------CTT-GA--------------------------------------------------------------------------------------------------------------------------------------------------G-G-C-GT--G-G-C-T-TC-C------GG--A----GC-TAA--CG-C-G-T--T--AA-GT--C----G-ACC-GCC-T-G-GG-GAG-TA---CGG-----C-C--G-C-A-A-GGT-T--AAA-ACTC-AAA---------TGAA-TTG-ACGGG-G-G-CCCG----C-A--C-A-A-GCG-GT-G--G--AG-CA-T--GT-GGT-TT-AATT-C-G-ATG-CAAC-G-CG-A-AG-A-A-CC-TT-A-CC-TGGTC-TT-G-AC-A-T-C--------------CAC-G-G-------------A-AG-T-T-T--TC--A-GA-G-A-T--G-A-G--A-A-T-G--T-G-----CC-------------------------------------T--TC-G------------------------------------------GG----A----A---CC-GTG---A--GA---------------------------------------------------C-A-G-G-T-GCTG-CA-TGG-CT--GTC-GTC-A-GC-TC---G-TG-TT-G--TGA-AA-TGT-T-GG-G-TT-AA-GT-CCCGC-AA--------C-GAG-CGC-A-ACC-C-T-TA--TC--C-TTTG--T-T-G-C-C---AG-C-G-----G-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------TCC------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GG---C----C-G------------G----G---A-A--CT---------------C-A-A-A-G-GA-G--AC-T-G-CCA--G-T------------------------------------G-A---TAA----------------------------------A-C-T-G--G-A-GG-A--AGG-T--GGGG-A-TGAC-GTC--AAGT-C---ATC-A-T-G-G-C-C-CTT----AC-G--AC-C-A-GG-GC-TA-CAC-ACGTG-C--TA--CAATG---G-CGCA-T-A--C-AAA-GA-GA--------------------------------------------------------------------------------------------------A-G-C-G-A--C-CTCG-C--G---------------------------------------A-GA-G-C-----------A--A-G-CG---G----------A--CCT-C------A-T-AAAGT-GC-G-T-C-G-TAG-TCC--------GGA-T-TGGAG-TC--T-GCAA-CT-C-------------------------------------------------------------------------------------------------G-ACTCC-A-T-G-AA-G-TC-GGAAT-CG-C-TA--G-TA-AT-C-G-T----GGA-TC-A-G--A------AT--GCC-AC-G-GT-G-AAT-ACGT-T-CCCGGGCCT-TGTA----CACACCG-CCC-GTC-----A---CA--CCA-TG-GG-A--G---TGG-G-TT-GC-AAA--A-GAA------G--T-AGG-TA-G-C-T-T-AA-C-C--------------------------------------------------------------TT----C-------------------------------------------------------------------------------------------------G--GG-A--GG-G--C---GC-TTA--CC--ACT-T----T-GTG-AT-TCA------------------------TG--ACT-GGGG-TG-AAG-TCGTAACAA-GGTAA-CCGT-AGGGGAA-CCTG-CGGT-TGGATCACCTCCTTA................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................";
203                 }else if (filename == "") {  //do nothing 
204                         seqMask = "";
205                 }else{
206                 
207         #ifdef USE_MPI  
208                         MPI_File inMPI;
209                         MPI_Offset size;
210                         MPI_Status status;
211                         
212                         //char* inFileName = new char[filename.length()];
213                         //memcpy(inFileName, filename.c_str(), filename.length());
214                         
215                         char inFileName[1024];
216                         strcpy(inFileName, filename.c_str());
217         
218                         MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  //comm, filename, mode, info, filepointer
219                         MPI_File_get_size(inMPI, &size);
220
221                         //delete inFileName;
222                         
223                         char* buffer = new char[size];
224                         MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
225                         
226                         string tempBuf = buffer;
227                         if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size);  }
228                         istringstream iss (tempBuf,istringstream::in);
229
230                         delete buffer;
231                         
232                         if (!iss.eof()) {
233                                 Sequence temp(iss);
234                                 seqMask = temp.getAligned();
235                         }else {
236                                 m->mothurOut("Problem with mask."); m->mothurOutEndLine(); 
237                                 seqMask = "";
238                         }
239                         
240                         MPI_File_close(&inMPI);
241         #else
242         
243                         ifstream infile;
244                         openInputFile(filename, infile);
245                         
246                         if (!infile.eof()) {
247                                 Sequence temp(infile);
248                                 seqMask = temp.getAligned();
249                         }else {
250                                 m->mothurOut("Problem with mask."); m->mothurOutEndLine(); 
251                                 seqMask = "";
252                         }
253                         infile.close();
254         #endif
255         
256                 }
257         }
258         catch(exception& e) {
259                 m->errorOut(e, "Chimera", "setMask");
260                 exit(1);
261         }
262 }
263 //***************************************************************************************************************
264 Sequence* Chimera::getSequence(string name) {
265         try{
266                 Sequence* temp;
267                 
268                 //look through templateSeqs til you find it
269                 int spot = -1;
270                 for (int i = 0; i < templateSeqs.size(); i++) {
271                         if (name == templateSeqs[i]->getName()) {  
272                                 spot = i;
273                                 break;
274                         }
275                 }
276                 
277                 if(spot == -1) { m->mothurOut("Error: Could not find sequence."); m->mothurOutEndLine(); return NULL; }
278                 
279                 temp = new Sequence(templateSeqs[spot]->getName(), templateSeqs[spot]->getAligned());
280                 
281                 return temp;
282         }
283         catch(exception& e) {
284                 m->errorOut(e, "Chimera", "getSequence");
285                 exit(1);
286         }
287 }
288 //***************************************************************************************************************
289
290
291
292