~ubuntu-branches/debian/sid/libvcflib/sid

« back to all changes in this revision

Viewing changes to src/permuteSmooth.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2016-09-16 15:52:29 UTC
  • Revision ID: package-import@ubuntu.com-20160916155229-24mxrntfylvsshsg
Tags: upstream-1.0.0~rc1+dfsg
ImportĀ upstreamĀ versionĀ 1.0.0~rc1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
This program was created at:  Fri Apr 17 14:59:53 2015
 
4
This program was created by:  Zev N. Kronenberg
 
5
 
 
6
 
 
7
Contact: zev.kronenber@gmail.com
 
8
 
 
9
Organization: Unviersity of Utah
 
10
    School of Medicine
 
11
    Salt Lake City, Utah
 
12
 
 
13
 
 
14
The MIT License (MIT)
 
15
 
 
16
Copyright (c) <2015> <Zev N. Kronenberg>
 
17
 
 
18
Permission is hereby granted, free of charge, to any person obtaining a copy
 
19
of this software and associated documentation files (the "Software"), to deal
 
20
in the Software without restriction, including without limitation the rights
 
21
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
22
copies of the Software, and to permit persons to whom the Software is
 
23
furnished to do so, subject to the following conditions:
 
24
 
 
25
The above copyright notice and this permission notice shall be included in
 
26
all copies or substantial portions of the Software.
 
27
 
 
28
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
29
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
30
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
31
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
32
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
33
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
34
THE SOFTWARE.
 
35
 
 
36
 
 
37
*/
 
38
#include <fstream>
 
39
#include "split.h"
 
40
#include <vector>
 
41
#include <map>
 
42
#include <string>
 
43
#include <iostream>
 
44
#include <math.h>
 
45
#include <cmath>
 
46
#include <stdlib.h>
 
47
#include <time.h>
 
48
#include <stdio.h>
 
49
#include <unistd.h>
 
50
#include <stdlib.h>
 
51
#include "gpatInfo.hpp"
 
52
 
 
53
#include <omp.h>
 
54
// print lock
 
55
omp_lock_t lock;
 
56
 
 
57
struct options{
 
58
  std::string file    ;
 
59
  std::string smoothed;
 
60
  std::string format  ;
 
61
  double npermutation ;
 
62
  double nsuc         ;
 
63
  int threads         ;
 
64
  int chrIndex        ;
 
65
  int nIndex          ; 
 
66
  int valueIndex      ; 
 
67
}globalOpts;
 
68
 
 
69
struct score{
 
70
  std::string seqid;
 
71
  long int pos     ;
 
72
  double score     ;
 
73
};
 
74
 
 
75
struct smoothedInputData{
 
76
  std::string line;
 
77
  double score ; 
 
78
  double n     ;
 
79
  double nPer  ;
 
80
  double nSuc  ;
 
81
  double ePv   ;
 
82
};
 
83
 
 
84
static const char *optString = "x:y:u:f:n:s:";
 
85
 
 
86
using namespace std;
 
87
 
 
88
std::map<std::string, int> FORMATS;
 
89
 
 
90
//------------------------------- SUBROUTINE --------------------------------
 
91
/*
 
92
 Function input  : NA
 
93
 
 
94
 Function does   : prints help
 
95
 
 
96
 Function returns: NA
 
97
 
 
98
*/
 
99
void printHelp()
 
100
{
 
101
 
 
102
  cerr << endl << endl;
 
103
  cerr << "INFO: help" << endl;
 
104
  cerr << "INFO: description:" << endl;
 
105
  cerr << "     permuteSmooth is a method for adding empirical p-values  smoothed wcFst scores." << endl ;
 
106
  cerr << endl;
 
107
  cerr << "INFO: usage:  permuteSmooth -s wcFst.smooth.txt -f wcFst.txt -n 5 -s 1 "<< endl;
 
108
  cerr << endl;
 
109
  cerr << "Required:" << endl;
 
110
  cerr << "      file:     f   -- argument: original wcFst data     "<< endl;
 
111
  cerr << "      smoothed: s   -- argument: smoothed wcFst data     "<< endl;
 
112
  cerr << "      format:   y   -- argument: [swcFst, segwcFst]      "<< endl;
 
113
  cerr << "Optional:" << endl;
 
114
  cerr << "      number:   n   -- argument: the number of permutations to run for each value [1000]" << endl;
 
115
  cerr << "      success:  u   -- argument: stop permutations after \'s\' successes [1]"             << endl;
 
116
  cerr << "      success:  x   -- argument: number of threads [1]"             << endl;
 
117
  cerr << endl;
 
118
  cerr << "OUTPUT: permuteSmooth will append three additional columns:" << endl;
 
119
  cerr << "        1. The number of successes                            " << endl;
 
120
  cerr << "        2. The number of trials                               " << endl;
 
121
  cerr << "        3. The empirical p-value                              " << endl;
 
122
  cerr << endl;
 
123
  printVersion();
 
124
 
 
125
}
 
126
 
 
127
 
 
128
//-------------------------------   OPTIONS   --------------------------------
 
129
int parseOpts(int argc, char** argv)
 
130
{
 
131
  int opt = 0;
 
132
  globalOpts.file = "NA";
 
133
  
 
134
  globalOpts.nsuc         = 1;
 
135
  globalOpts.npermutation = 1000;
 
136
  
 
137
  opt = getopt(argc, argv, optString);
 
138
  while(opt != -1){
 
139
    switch(opt){
 
140
    case 'x':
 
141
      {
 
142
        globalOpts.threads = atoi(optarg);
 
143
      }
 
144
    case 'f':
 
145
      {
 
146
        globalOpts.file =  optarg;
 
147
        break;
 
148
      }
 
149
    case 'y':
 
150
      {
 
151
        globalOpts.format = optarg;
 
152
        if(FORMATS.find(globalOpts.format) == FORMATS.end()){
 
153
          std::cerr << "FATAL: format not supported: " << globalOpts.format;
 
154
          std::cerr << endl;
 
155
          printHelp();
 
156
          exit(1);
 
157
        }
 
158
        if(globalOpts.format == "swcFst"){
 
159
          globalOpts.chrIndex   = 0;
 
160
          globalOpts.nIndex     = 3;
 
161
          globalOpts.valueIndex = 4;
 
162
        }
 
163
        if(globalOpts.format == "segwcFst"){
 
164
          globalOpts.chrIndex   = 0;
 
165
          globalOpts.valueIndex = 3;
 
166
          globalOpts.nIndex     = 5;
 
167
        }       
 
168
        break;
 
169
      }
 
170
    case 'n':
 
171
      {
 
172
        globalOpts.npermutation = atof(((string)optarg).c_str());
 
173
        cerr << "INFO: permuteGPAT++ will do N permutations: " << globalOpts.npermutation << endl;
 
174
        break;
 
175
      }
 
176
    case 's':
 
177
      {
 
178
        globalOpts.smoothed = optarg;
 
179
        cerr << "INFO: smoothed file: " << globalOpts.smoothed << endl;
 
180
        break;
 
181
      } 
 
182
    case 'u':
 
183
      {
 
184
        globalOpts.nsuc = atof(((string)optarg).c_str());
 
185
        cerr << "INFO: permuteGPAT++ will stop permutations after N successes: " << globalOpts.nsuc << endl;
 
186
        break;
 
187
        }
 
188
    case '?':
 
189
      {
 
190
        break;
 
191
      }
 
192
    }
 
193
    
 
194
    opt = getopt( argc, argv, optString ); 
 
195
  }
 
196
  return 1;
 
197
}
 
198
 
 
199
 
 
200
//------------------------------- SUBROUTINE --------------------------------
 
201
/*
 
202
 Function input  : data, vector to load
 
203
 
 
204
 Function does   : returns a contguous window
 
205
 
 
206
 Function returns: bool
 
207
 
 
208
*/
 
209
 
 
210
bool getContiguousWindow(vector<score *> & data, 
 
211
                         vector<double> & load, 
 
212
                         int n, int * nfail){
 
213
  int r = rand() % data.size();    
 
214
 
 
215
  if(r+n >= data.size()){
 
216
    *nfail+=1;
 
217
    return false;
 
218
  }
 
219
 
 
220
  if(data[r]->seqid != data[r+n]->seqid){
 
221
    *nfail+=1;
 
222
    return false;
 
223
  }
 
224
  
 
225
  for(int i = r; i < r+n; i++){
 
226
    load.push_back(data[i]->score);
 
227
  }
 
228
  return true;
 
229
}
 
230
 
 
231
 
 
232
//------------------------------- SUBROUTINE --------------------------------
 
233
 
 
234
double mean(vector<double> & data){
 
235
 
 
236
  double sum = 0;
 
237
 
 
238
  for(vector<double>::iterator it = data.begin(); it != data.end(); it++){
 
239
    sum += (*it);
 
240
  }
 
241
  return sum / data.size();
 
242
}
 
243
 
 
244
 
 
245
 
 
246
//------------------------------- SUBROUTINE --------------------------------
 
247
/*
 
248
 Function input  : score, n, data
 
249
 
 
250
 Function does   : permutes the score.  requires that the window is contiguous
 
251
 
 
252
 Function returns: NA
 
253
 
 
254
*/
 
255
 
 
256
bool permute(double s, int n, vector<score *> & data, 
 
257
             double * nRep, double *nSuc, double * ePv){
 
258
 
 
259
  
 
260
  *ePv = 1 / globalOpts.npermutation;
 
261
  
 
262
  while(*nSuc < globalOpts.nsuc && *nRep < globalOpts.npermutation ){
 
263
    *nRep += 1;    
 
264
 
 
265
 
 
266
    std::vector<double> scores;
 
267
 
 
268
    bool getWindow = false;
 
269
 
 
270
    int nfail = 0;
 
271
 
 
272
    while(!getWindow){
 
273
      if(nfail > globalOpts.npermutation){
 
274
        return false;
 
275
      }
 
276
      getWindow = getContiguousWindow(data, scores, n, &nfail);
 
277
    }
 
278
 
 
279
    double ns = mean(scores);
 
280
    
 
281
    if(ns > s){
 
282
      *nSuc += 1;
 
283
    }
 
284
  }
 
285
  if(*nSuc > 0){
 
286
    *ePv = *nSuc / *nRep;
 
287
  }
 
288
 
 
289
  return true;
 
290
}
 
291
 
 
292
//-------------------------------    MAIN     --------------------------------
 
293
/*
 
294
 Comments:
 
295
*/
 
296
 
 
297
int main( int argc, char** argv)
 
298
{
 
299
srand (time(NULL));
 
300
 
 
301
 FORMATS["swcFst"]   = 1;
 
302
 FORMATS["segwcFst"] = 1;
 
303
 
 
304
 globalOpts.threads = 1;
 
305
 
 
306
int parse = parseOpts(argc, argv);
 
307
 
 
308
 omp_set_num_threads(globalOpts.threads);
 
309
 
 
310
 
 
311
 if(globalOpts.file.compare("NA") == 0){
 
312
   cerr << "FATAL: no file was provided" << endl;
 
313
   printHelp();
 
314
   exit(1);
 
315
 }
 
316
 if(globalOpts.format.empty()){
 
317
   std::cerr << "FATAL: no format specified." << std::endl;
 
318
   exit(1);
 
319
 }
 
320
 
 
321
 vector< score *> data;
 
322
 
 
323
 ifstream wcDat (globalOpts.file.c_str());
 
324
 
 
325
 string line;
 
326
 
 
327
 if(wcDat.is_open()){
 
328
 
 
329
   while(getline(wcDat, line)){
 
330
     vector<string> region = split(line, "\t");
 
331
     // will change for other output
 
332
     double value = atof(region[4].c_str());
 
333
 
 
334
     if(globalOpts.format == "swcFst" || globalOpts.format == "segwcFst"){
 
335
       
 
336
       if(region.size() != 5){
 
337
         cerr << "FATAL: wrong number of columns in wcFst input" << endl;
 
338
         exit(1);
 
339
       } 
 
340
       if(value < 0 ){
 
341
         value = 0;
 
342
       }
 
343
     }
 
344
 
 
345
     score * sp;
 
346
     sp = new score;
 
347
     sp->seqid = region[0]              ;
 
348
     sp->pos   = atoi(region[1].c_str());
 
349
     sp->score = value                  ;
 
350
 
 
351
     data.push_back(sp);
 
352
   }
 
353
 }
 
354
 else{
 
355
   cerr << "FATAL: coult not open file: " << globalOpts.file << endl;
 
356
   exit(1);
 
357
 }
 
358
 
 
359
 
 
360
 wcDat.close();
 
361
 line.clear();
 
362
 
 
363
 cerr << "INFO: raw values to permute against: " << data.size() << endl;
 
364
 
 
365
 ifstream smoothedFile (globalOpts.smoothed.c_str());
 
366
 
 
367
 vector<smoothedInputData*> sData;
 
368
 
 
369
 if(smoothedFile.is_open()){
 
370
   while(getline(smoothedFile, line)){
 
371
   
 
372
     vector<string> region = split(line, "\t");
 
373
     smoothedInputData * sp = new smoothedInputData;
 
374
 
 
375
     sp->line = line;
 
376
     sp->score = atof(region[globalOpts.valueIndex].c_str());
 
377
     sp->n     = atoi(region[globalOpts.nIndex].c_str());
 
378
     sp->nPer = 0;
 
379
     sp->nSuc = 0;
 
380
     sp->ePv  = 0;
 
381
     
 
382
     sData.push_back(sp);
 
383
   }
 
384
 }
 
385
 smoothedFile.close();
 
386
 
 
387
 cerr << "INFO: Number of smoothed windows to permute : " << sData.size() << endl;
 
388
 
 
389
#pragma omp parallel for schedule(dynamic, 20)
 
390
 for(int i = 0; i < sData.size(); i++){
 
391
   bool per = permute(sData[i]->score, sData[i]->n, 
 
392
           data, &sData[i]->nPer, 
 
393
           &sData[i]->nSuc, &sData[i]->ePv);
 
394
   
 
395
 
 
396
   if(per){
 
397
   omp_set_lock(&lock);
 
398
   cout << sData[i]->line 
 
399
        << "\t" << sData[i]->nSuc
 
400
        << "\t" << sData[i]->nPer 
 
401
        << "\t" << sData[i]->ePv << endl;
 
402
   omp_unset_lock(&lock);
 
403
   }
 
404
   else{
 
405
     omp_set_lock(&lock);
 
406
     cout << sData[i]->line
 
407
          << "\t" << "NA"
 
408
          << "\t" << "NA"
 
409
          << "\t" << "NA" << endl;
 
410
     omp_unset_lock(&lock);
 
411
   }
 
412
 }
 
413
 
 
414
 for(vector<score*>::iterator itz = data.begin(); 
 
415
     itz != data.end(); itz++){
 
416
   delete *itz;
 
417
 }
 
418
 for(vector<smoothedInputData*>::iterator itz = sData.begin();
 
419
     itz != sData.end(); itz++){
 
420
   delete *itz;
 
421
 }
 
422
 
 
423
 
 
424
return 0;
 
425
}