File: sharing.cpp

package info (click to toggle)
plink 1.07%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: cpp: 72,375; makefile: 123; sh: 12
file content (316 lines) | stat: -rw-r--r-- 7,085 bytes parent folder | download | duplicates (7)
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


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2006 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 "plink.h"
#include "perm.h"
#include "options.h"
#include "helper.h"

void Plink::perm_sharingIBSTest(Perm & perm)
{

  // This is a SNP-major test
  if (!par::SNP_major) Ind2SNP();

  // Test statistic (set-based)
  vector<double> delta(snpset.size());
  
  // Empirical p-values
  perm.setTests(snpset.size());

  ////////////////////////////////
  // Fast binary affection coding

  if (!par::qt)
    affCoding(*this);


  ////////////////////////////////
  // Set up permutation structure 
  // (we need to perform this step
  //  whether or not we also 
  //  subsequently permute)

  perm.setPermClusters(*this);
  perm.originalOrder();


  /////////////////////
  // Create original
  
  delta = sharingIBSTest(perm);
  

  //////////////////////
  // Begin permutations
  
  bool finished = false;
  while(!finished)
    {
      
      perm.permuteInCluster();

      vector<double> pr = sharingIBSTest(perm);	  
      
      
      ////////////////////////////////
      // Standard permutation counting
      
      finished = perm.update(pr,delta);
     
    } // next permutation

  if (!par::silent)
    cout << "\n\n";
  

  ////////////////////
  // Display results
  
  ofstream ASC;
  string f;
  if (par::adaptive_perm) f = par::output_file_name + ".sharing.perm";
  else f = par::output_file_name + ".sharing.mperm";
  
  ASC.open(f.c_str(),ios::out);
  ASC.precision(4);
  printLOG("Writing IBS sharing association results to [ " + f + " ] \n");
  
  ASC << setw(20) << "SET" << " " 
      << setw(12) << "EMP1" << " ";
  if (par::adaptive_perm)
    ASC << setw(12)<< "NP" << " ";
  else
    ASC	<< setw(12)<< "EMP2" << " ";
  ASC << "\n";
  
  
  for (int l=0; l<snpset.size(); l++)
    {

      ASC << setw(20) << setname[l] << " " 
	  << setw(12) << perm.pvalue(l) << " ";
      
      if (par::adaptive_perm) 
	ASC << setw(12) << perm.reps_done(l) << " ";
      else
	ASC << setw(12) << perm.max_pvalue(l) << " ";
      
      ASC << "\n";
    }

  ASC.close();
     
}

vector<double> Plink::sharingIBSTest(Perm & perm)
{

  
  
//   // number of rare (F) alleles shared
//   // FF FF  ->    
//   // FF FT  ->  
//   // FF TT  ->  -2 
  
//   // FT FF  ->  
//   // FT FT  ->  +1
//   // FT TT  ->  
  
//   // TT FF  ->  -2
//   // TT FT  -> 
//   // TT TT  -> 
  
//   // Number of sets
//   int ns = snpset.size();
  
//   // Test statistics
//   vector<double> delta(ns,0);
  
//   // Iterate over sets
//   for (int i=0; i<ns; i++)
//     {
      
//       // Temporary copy of test statistic
//       double d = 0;

//       // Consider each locus in set
      
//       for (int j=0; j< snpset[i].size(); j++)
// 	{
	  
// 	  CSNP * loc = SNP[snpset[i][j]];
	  
// 	  vector<bool>::iterator a1 = (*loc)->one.begin();
// 	  vector<bool>::iterator a2 = (*loc)->two.begin();
// 	  vector<Individual*>::iterator gperson1 = sample.begin();
// 	  int i1 = 0;

// 	  ////////////////
// 	  // Individual A 
	  
// 	  while ( i1 < n-1 )
// 	    {
	      
// 	      // Permuted self for first individual
// 	      Individual * pperson1 = (*gperson1)->pperson;
	      
// 	      // U-? -- first individual unaffected
// 	      if ( ! ( sample[perm_pheno[i1]]->aff )
// 	      {
		
// 		// Consider each locus: FF x TT -> -2
// 		for (int l=0; l<nl_all; l++)
// 		  {
// 		    // First member FF
// 		    if ( (!g1->one[l]) && (!g1->two[l]) )
// 		      {
// 			// Consider all other individuals
// 			for (int i2=i1+1; i2<n; i2++)
// 			  {
			    
// 			    // is FF x TT ?
// 			    if ( sample[perm_geno[i2]]->one[l] && 
// 				 sample[perm_geno[i2]]->two[l] ) 
// 			{
// 			  // only count discordant pairs
// 			  if ( sample[perm_geno[i2]]->aff )  
// 			    s1[l]-=2;
			  
// 			}
// 		    }
// 		}
// 	      else if ( (!g1->one[l]) && g1->two[l] )
// 		{
// 		  // ... if first member is FT
		  
// 		  // Consider all other individuals
// 		  for (int i2=i1+1; i2<n; i2++)
// 		    {
		      
// 		      Individual * g2 = sample[perm_geno[i2]];
		      
// 		      // FT x FT ->  +1
		      
// 		      if ( (!sample[perm_geno[i2]]->one[l]) && 
// 			   sample[perm_geno[i2]]->two[l] ) 
// 			{
// 			  // Discordant pair
// 			  if ( sample[perm_geno[i2]]->aff )
// 			    s1[l]++;
// 			}
// 		    }
// 		}
// 	      else if ( g1->one[l] && g1->two[l] )
// 		{
// 		  // ... if first member is TT
		  
// 		  // Consider all other individuals
// 		  for (int i2=i1+1; i2<n; i2++)
		    
// 		    {
// 		      if ( (!sample[perm_geno[i2]]->one[l]) && 
// 			   (!sample[perm_geno[i2]]->two[l]) ) 
// 			{
// 			  // Discordant pair
// 			  if ( sample[perm_geno[i2]]->aff )
// 			    s1[l]-=2;
// 			}
// 		    }
// 		}
// 	    } // next locus
	  
// 	}
//       else
// 	{
// 	  // otherwise, we know first individul is affected
// 	  // and so we must now scan for AU and AA pairs (s1, s2)
	  
// 	  // Consider each locus: FF x TT -> -2
// 	  for (int l=0; l<nl_all; l++)
// 	    {
// 	      // First member FF
// 	      if ( (!g1->one[l]) && (!g1->two[l]) )
// 		{
// 		  // Consider all other individuals
// 		  for (int i2=i1+1; i2<n; i2++)
// 		    {
		      
// 		      // is FF x TT ?
// 		      if ( sample[perm_geno[i2]]->one[l] && 
// 			   sample[perm_geno[i2]]->two[l] ) 
// 			{
// 			  // only count discordant pairs
// 			  if ( sample[perm_geno[i2]]->aff )  
// 			    s2[l]-=2; // conc aff
// 			  else
// 			    s1[l]-=2;  // disc
// 			}
// 		    }
// 		}
// 	      else if ( (!g1->one[l]) && g1->two[l] )
// 		{
// 		  // ... if first member is FT
		  
// 		  // Consider all other individuals
// 		  for (int i2=i1+1; i2<n; i2++)
// 		    {
// 		      // FT x FT ->  +1		      
// 		      if ( (!sample[perm_geno[i2]]->one[l]) && 
// 			   sample[perm_geno[i2]]->two[l] ) 
// 			{
// 			  if ( sample[perm_geno[i2]]->aff )
// 			    s2[l]++;
// 			  else 
// 			    s1[l]++;
// 			}
// 		    }
// 		}
// 	      else if ( g1->one[l] && g1->two[l] )
// 		{
// 		  // ... if first member is TT
		  
// 		  // Consider all other individuals
// 		  for (int i2=i1+1; i2<n; i2++)
		    
// 		    {
// 		      if ( (!sample[perm_geno[i2]]->one[l]) && 
// 			   (!sample[perm_geno[i2]]->two[l]) ) 
// 			{
// 			  if ( sample[perm_geno[i2]]->aff )
// 			    s2[l]-=2;
// 			  else
// 			    s1[l]-=2;
// 			}
// 		    }
// 		}
// 	    } // next locus
	  
	

// 	}
	  
      
//     } // next, first individual of pair

  vector<double> t(1);
  return t;
}