File: metaem.cpp

package info (click to toggle)
plink 1.07%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,036 kB
  • sloc: cpp: 69,728; makefile: 123; sh: 12
file content (413 lines) | stat: -rw-r--r-- 9,220 bytes parent folder | download | duplicates (6)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2007 Shaun Purcell                  //
//                                                              //
// This file is distributed under the GNU General Public        //
// License, Version 2.  Please see the file COPYING for more    //
// details                                                      //
//                                                              //
//////////////////////////////////////////////////////////////////


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <vector>
#include <map>
#include <cassert>

#include "plink.h"
#include "options.h"
#include "helper.h"

#include "genogroup.h"
#include "phase.h"
#include "haplowindow.h"

extern ofstream LOG;
using namespace std;



bool HaploPhase::makeWaplotype(vector<int> & wCounter, vector<int> & wMax)
{
  
  int j = 0;

  while (1)
    {
      if (wCounter[j] < wMax[j])
	{
	  ++wCounter[j];
	  return true;
	}
      else // if this position is a max
	{
	  wCounter[j] = 0;
	  ++j;

	  // Or are we done
	  if (j == actual_nw )
	    return false;
	}
    }

  return true;

}




void HaploPhase::enumeratePhasedWindows(int i)
{
  
  // Form "waplotypes" (haplotypes of windows)
  
  vector<int> wCounter(actual_nw, 0);
  vector<int> wMax(actual_nw, 0);
  
  // Allow at least one move
  wCounter[0] = -1;
  
  // Clear phases for precaution
  hap1[i].clear();
  hap2[i].clear();
  
  for (int w = startWindow; w <= finishWindow ; w++)
    {
      int wc = w - startWindow;
      
      if (windows[w]->hap1[i].size() > 0)
	wMax[wc] = windows[w]->hap1[i].size() - 1;
      else
	{
	  include[i] = false;
	  return;
	}
    }


  //////////////////////////////////////
  // Consider all possible combinations

  while ( makeWaplotype(wCounter, wMax) )
    {

      // But is this a legal window, i.e. given stubs?
      // i.e. scan all intermediate windows and check
      
      bool okay = true;
      
      vector<bool> leftAlignListA;
      vector<bool> leftAlignListB;

      vector<bool> rightAlignListA;
      vector<bool> rightAlignListB;

      for (int w = startWindow; w <= finishWindow; w++)
	{
	  
	  int wc = w - startWindow;
	  
	  // Check left side / left window
	  
	  HaploWindow * currentWindow = windows[w];
	  int currentH1 = currentWindow->hap1[i][ wCounter[wc] ];
	  int currentH2 = currentWindow->hap2[i][ wCounter[wc] ];

	  int l1 = currentWindow->leftStub[ currentH1 ];
	  int l2 = currentWindow->leftStub[ currentH2 ];
	  
	  int r1 = currentWindow->rightStub[ currentH1 ];
	  int r2 = currentWindow->rightStub[ currentH2 ];
	  
	  
	  // Left alignment? 
	  
	  if ( w > startWindow ) 
	    {
	      HaploWindow * leftWindow = windows[w-1];
	      int leftH1 = leftWindow->hap1[i][ wCounter[wc-1] ];
	      int leftH2 = leftWindow->hap2[i][ wCounter[wc-1] ];
	      
	      int ol1 = leftWindow->rightStub[ leftH1 ];
	      int ol2 = leftWindow->rightStub[ leftH2 ];
	      
	      bool leftAlignA = l1 == ol1 && l2 == ol2;
	      bool leftAlignB = l1 == ol2 && l2 == ol1;
	      
	      if ( ! ( leftAlignA || leftAlignB ) )
		{
		  okay = false;
		  break;
		}
	      else
		{
		  leftAlignListA.push_back( leftAlignA );
		  leftAlignListB.push_back( leftAlignB );
		}
	    }
	  else
	    {
	      leftAlignListA.push_back( false );
	      leftAlignListB.push_back( false );
	    }
	  


	  /////////////////////
	  // Right alignment?
	  
	  if ( w < finishWindow ) 
	    {
	      HaploWindow * rightWindow = windows[w+1];
	      int rightH1 = rightWindow->hap1[i][ wCounter[wc+1] ];
	      int rightH2 = rightWindow->hap2[i][ wCounter[wc+1] ];
	      
	      int or1 = rightWindow->leftStub[ rightH1 ];
	      int or2 = rightWindow->leftStub[ rightH2 ];
	      
	      bool rightAlignA = r1 == or1 && r2 == or2;
	      bool rightAlignB = r1 == or2 && r2 == or1;

	      if ( ! ( rightAlignA || rightAlignB ) )
		{
		  okay = false;
		  break;
		}
	      else // save which possible pairings align
		{
		  rightAlignListA.push_back( rightAlignA );
		  rightAlignListB.push_back( rightAlignB );
		}
	    }
	  else
	    {
	      rightAlignListA.push_back( false );
	      rightAlignListB.push_back( false );
	    }
	}
      
      

      /////////////////////////////////////////////////////////
      // For legal combinations, enumerate possible waplotypes

      if ( okay )
	{
	  
	  vector<bool> flipable(actual_nw,false);
	  int nflip = 0;	  

	  bool firstSkipped = false;
	  
	  // Consider all A/B and B/A pairings of haplotypes for 
	  // windows other than the first (i.e. we will be lining up 
	  // haplotypes in hap1/hap2 meaningfully then)

//	  if ( startWindow > 0 ) firstSkipped = true;
	  
	  for (int w = startWindow; w <= finishWindow; w++)
	    {
	      int wc = w - startWindow;

	      HaploWindow * currentWindow = windows[w];
	      int currentH1 = currentWindow->hap1[i][ wCounter[wc] ];
	      int currentH2 = currentWindow->hap2[i][ wCounter[wc] ];
	      
	      if ( currentH1 != currentH2 )
		{
		  if ( firstSkipped )
		    {
		      flipable[wc] = true;
		      nflip++;
		    }
  //		  firstSkipped = true;
		}		 
	    }
	  
	  
	  int nperm = (int)pow(2.0,(double)nflip);
	  
	  unsigned int h=0;
	  
	  while (h<nperm)
	    {

	      vector<bool> f1;
	      
	      unsigned int p=1;
	      for (int flip=0; flip<nflip; flip++)
		{
		  if ( h & p )
		    f1.push_back(false);
		  else
		    f1.push_back(true);
		  p <<= 1;
		}
	      
	      vector<int> waplotype1;
	      vector<int> waplotype2;

	      int f2 = 0;
	      for (int w = startWindow; w <= finishWindow ; w++)
		{

		  int wc = w - startWindow;

		  HaploWindow * currentWindow = windows[w];

		  int currentH1 = currentWindow->hap1[i][ wCounter[wc] ];
		  int currentH2 = currentWindow->hap2[i][ wCounter[wc] ];
		  
		  if ( flipable[wc] )
		    {
		      if ( f1[f2++] )
			{
			  int tmp = currentH1;
			  currentH1 = currentH2;
			  currentH2 = tmp;
			}
		    }
		  

		  waplotype1.push_back( currentH1 );
		  waplotype2.push_back( currentH2 );

		}
	      
	      
	      ///////////////////////////////
	      // Test for ultimate alignment

	      bool allAligned = true;

	      // Skip first window
	      for ( int w = startWindow+1; w <= finishWindow; w++)
		{
		  int wc = w - startWindow;

		  HaploWindow * currentWindow = windows[w];
		  
		  int l1 = currentWindow->leftStub[ waplotype1[wc] ];
		  int l2 = currentWindow->leftStub[ waplotype2[wc] ];
		  
		  HaploWindow * leftWindow = windows[w-1];

		  int ol1 = leftWindow->rightStub[ waplotype1[wc-1] ];
		  int ol2 = leftWindow->rightStub[ waplotype2[wc-1] ];
		  
		  bool leftAlign = l1 == ol1 && l2 == ol2;
		  
		  if ( ! leftAlign ) 
		    allAligned = false;
		}
	      

	      if ( allAligned )
		{


		  // Have we already seen these two waplotypes?
		  
		  map< vector<int>, int>::iterator wi = hapmap.find( waplotype1 );
		  
		  // First waplotype 


		  if ( wi == hapmap.end() )
		    {

		      hapmap.insert( make_pair( waplotype1 , nh ));

		      vector<bool> thisHaplotype;
		      for(int w= startWindow; w <= finishWindow; w++)
			{
			  int wc = w - startWindow;

			  HaploWindow * currentWindow = windows[w];
			  int start = par::haplo_plem_overlap;
			  if (wc==0) start = 0;
			  for (int s = start; s < currentWindow->ns; s++)
			    thisHaplotype.push_back( currentWindow->hap[waplotype1[wc]][s]);
			}
		      
		      hap.push_back( thisHaplotype );
		      hapmapb.insert( make_pair( thisHaplotype, nh ));
		      hapi.push_back( waplotype1 );

		      hap1[i].push_back( nh );

		      nh++;
		    }
		  else
		    {
		      hap1[i].push_back( wi->second );
		      hapmapb.insert( make_pair( hap[wi->second], wi->second ));
		    }

	
		  wi = hapmap.find( waplotype2 );
		  
		  if ( wi == hapmap.end() )
		    {
		      hapmap.insert( make_pair( waplotype2 , nh ));
		      
		      vector<bool> thisHaplotype;
		      for(int w=startWindow; w <= finishWindow; w++)
			{
			  int wc = w - startWindow;

			  HaploWindow * currentWindow = windows[w];
			  int start = par::haplo_plem_overlap;
			  if (wc==0) start = 0;
			  for (int s = start; s < currentWindow->ns; s++)
			    thisHaplotype.push_back( currentWindow->hap[waplotype2[wc]][s]);
			}
		      hap.push_back( thisHaplotype );
		      hapmapb.insert( make_pair( thisHaplotype, nh ));
		      hapi.push_back( waplotype2 );
		      hap2[i].push_back( nh );
		      
		      nh++;
		    }
		  else
		    {
		      hap2[i].push_back( wi->second );
		      hapmapb.insert( make_pair( hap[wi->second], wi->second ));
		    }

		}

			  
	      // Consider next waplotype
	      h++;
	      
	    }
       	
	} // end of 'add if legal' condition      

    } // consider next possible legal set
  
  
  // If more than a single phased set exists declare ambigious

  ambig[i] = hap1[i].size() > 1;
  include[i] = hap1[i].size() > 0;
  
  if ( par::haplo_plem_follow && i == par::haplo_plem_follow_ind )
    {
      VPHASE << "Added " << hap1[i].size() 
	     << " phases for followed individual\n";      
    }

}