File: haploQTL.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 (269 lines) | stat: -rw-r--r-- 6,158 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


//////////////////////////////////////////////////////////////////
//                                                              //
//           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 "plink.h"
#include "options.h"
#include "phase.h"
#include "helper.h"
#include "stats.h"


void HaploPhase::haplotypicQTL(map<int,int> & tests, 
			       int nt, 
			       bool display_results )
{
  
  // Quantitative trait test based on a test vector; like TDT, assumes
  // only ever two groups, for now
  
  // No implementation of QTL omnibus test yet
  
  if (nt!=2) return;

  
  // Genotypic and phenotype mean, variance, covariance

  double genotypic_mean = 0;
  double genotypic_variance = 0;
  double qt_mean = 0;
  double qt_variance = 0;
  double covariance = 0;

  // number of individuals in analysis
  
  int numberIndividuals = 0;


  // For this, we will make the male X coding equivalent to
  // --xchr-model 1; except we will not add a covariate for 
  // sex; 

  //  Females:  0, 1, 2
  //  Males:    0,    1
        

  /////////////////////////////
  // Iterate over individuals
  
  for (int i = 0 ; i < P.n; i++)
    {
      
      if ( hap1[i].size() == 0 )
	continue;

      Individual * pperson = P.sample[i]->pperson;
      Individual * gperson = P.sample[i];

      if (!pperson->missing)
	{
	  
	  qt_mean += pperson->phenotype;
	  
	  
	  // Consider all possible phases
	  for (int z = 0 ; z < hap1[i].size(); z++)
	    {
	      
	      map<int,int>::iterator i1 = tests.find(hap1[i][z]);
	      map<int,int>::iterator i2 = tests.find(hap2[i][z]);
	      
	      // i1 and i2 should always point to a 0/1 variable; 
	      // but the coding is reversed (for god knows what reason)
	      // such as convention means the to-be-tested variant(s)
	      // have a 0; therefore reverse here.

	      int c1 = 1 - i1->second;
	      int c2 = 1 - i2->second;
	      
	      if ( i1 != tests.end() )
		{
		  if (!ambig[i]) 
		    genotypic_mean += c1;
		  else
		    genotypic_mean += c1 * pp[i][z];
		}
	      
	      if ( ! ( haploid || ( X && gperson->sex ) ) )
		{
		  if ( i2 != tests.end() )
		    {
		      if (!ambig[i]) 
			genotypic_mean += c2;
		      else
			genotypic_mean += c2 * pp[i][z];
		    }
		}
	    }
	  
	  numberIndividuals++;
	}

    } // Next individual

      
  qt_mean /= (double)numberIndividuals;
  genotypic_mean /= (double)numberIndividuals;      
      
  
  //////////////////////////////////
  // Iterate over individuals again
        
  for (int i=0; i< P.n; i++)
    {
      
      if ( hap1[i].size() == 0 )
	continue;
	  
      Individual * pperson = P.sample[i]->pperson;
      Individual * gperson = P.sample[i];
      
      if (!pperson->missing)
	{
	  	  
	  double g = 0;
	  
	  // Consider all possible phases
	  for (int z = 0 ; z < hap1[i].size(); z++)
	    {
	      
	      map<int,int>::iterator i1 = tests.find(hap1[i][z]);
	      map<int,int>::iterator i2 = tests.find(hap2[i][z]);
	      
	      // i1 and i2 should always point to a 0/1 variable; 
	      // but the coding is reversed (for god knows what reason)
	      // such as convention means the to-be-tested variant(s)
	      // have a 0; therefore reverse here.

	      int c1 = 1 - i1->second;
	      int c2 = 1 - i2->second;

	      if ( i1 != tests.end() )
		{
		  if (!ambig[i]) 
		    g += c1;
		  else
		    g += c1 * pp[i][z];
		}
	      
	      if ( ! ( haploid || ( X && gperson->sex ) ) )
		{
		  if ( i2 != tests.end() )
		    {
		      if (!ambig[i]) 
			g += c2;
		      else
			g += c2 * pp[i][z];
		    }
		}
	    }

	  qt_variance += (pperson->phenotype-qt_mean) 
	    * ( pperson->phenotype-qt_mean ) ;
	  
	  genotypic_variance += (g-genotypic_mean) 
	    * ( g-genotypic_mean ) ;
	  
	  covariance += ( pperson->phenotype - qt_mean ) 
	    * ( g - genotypic_mean ) ;
	  	  
	}
            

    } // Next individual
  

  // Statistics

  qt_variance /= (double)numberIndividuals - 1;
  genotypic_variance /= (double)numberIndividuals - 1;
  covariance /= (double)numberIndividuals - 1;
  
  // Test statistic
  
  double beta = covariance / genotypic_variance;
  
  double vbeta = ( qt_variance/genotypic_variance 
		   - (covariance * covariance ) 
		   / (genotypic_variance* genotypic_variance) 
		   ) / (numberIndividuals-2);
  
  double t = beta / sqrt(vbeta);

  double t_p = pT(t,numberIndividuals-2);  

  // Display results?

  if ( display_results ) 
	{
	  
	  // Skip?, if filtering p-values
	  if ( par::pfilter && ( t_p > par::pfvalue || t_p < 0 ) ) 
	    goto skip_p2;
	  
	  double r2 =   (covariance * covariance ) 
	    / ( qt_variance * genotypic_variance ) ;
	  	  
	  
	  HTEST << setw(10) << hname << " ";
  
	  // Find test haplotype (assuming there is a single one; 
	  // otherwise we won't be in display mode, i.e. proxy
	  // association has it's own display)

	  int hh=0;
	  
	  map<int,int>::iterator i1 = tests.begin();
	  while ( i1 != tests.end() )
	    {
	      if ( i1->second == 0 )
		hh = i1->first;
	      i1++;
	    }
	  
	  HTEST << setw(12) << haplotypeName(hh) << " ";
	  
	  HTEST << setw(8) << numberIndividuals << " " 
		<< setw(10) << beta << " " 
		<< setw(10) << r2 << " " ;

	  if (t_p >= 0) 
 	    HTEST << setw(8) << t  << " " << setw(12) << t_p << " " ;
 	  else
 	    HTEST << setw(8) << "NA"  << " " << setw(12) << "NA"  << " " ;

	  // Display SNPs

	  for (int snps=0; snps<ns-1; snps++)
	    HTEST << P.locus[S[snps]]->name << "|";
	  
	  HTEST << P.locus[S[ns-1]]->name << "\n";
	}
  
 skip_p2:
  
  // Store chi-sq and regression coefficient

  result = t;
  pvalue = t_p;
  odds = beta;

}