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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2009 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 <map>
#include <algorithm>
#include <bitset>
#include <limits>
#include <errno.h>
#include "plink.h"
#include "options.h"
#include "helper.h"
extern ofstream LOG;
void Plink::readDataLongFormat()
{
//////////////////////
// Check files exist
checkFileExists(par::lpedfile);
checkFileExists(par::mapfile);
checkFileExists(par::famfile);
///////////////////////////////////////////////
// .map file
vector<bool> include;
vector<int> include_pos(0);
int nl_actual=0;
// Read in MAP file: this function also allocates
// slots for SNP-major mode
readMapFile(par::mapfile,
include,
include_pos,
nl_actual);
//////////////////////////////////////////////
// First read a reference file?
map<string,string> refallele;
map<string,string> refallele2;
if ( par::ref_file )
{
set<string> mset;
for (int l=0; l< locus.size(); l++)
mset.insert( locus[l]->name );
int notfound = 0;
checkFileExists( par::ref_file_name );
ifstream REF( par::ref_file_name.c_str(), ios::in );
while ( ! REF.eof() )
{
vector<string> tok = tokenizeLine( REF );
if ( tok.size() == 0 )
continue;
if ( tok.size() != 2 && tok.size() != 3 )
error("Problem with line in [ "
+ par::ref_file_name
+ " ] :\n " + displayLine( tok ) );
if ( mset.find( tok[0] ) == mset.end() )
{
++notfound;
continue;
}
refallele.insert( make_pair( tok[0], tok[1] ));
// A second allele also specified?
if ( tok.size() == 3 )
refallele2.insert( make_pair( tok[0], tok[2] ));
}
printLOG("Read reference alleles for "
+ int2str( refallele.size() )
+ " sites\n");
if ( notfound>0 )
printLOG(int2str( notfound )
+ " SNPs in reference but not in map file\n");
if ( refallele.size() < locus.size() )
printLOG(int2str( locus.size() - refallele.size() )
+ " SNPs in map file but not in reference\n");
REF.close();
}
///////////////////////////////////////////////
// .fam
readFamFile(par::famfile);
// Allocate space for individual-major mode, set to
// missing by default...
// Either missing (TF) by default; or reference allele (FF)
bool code = ! par::ref_file;
if ( ! par::SNP_major)
{
for (int i=0; i<sample.size(); i++)
{
sample[i]->one.resize(nl_actual,code);
sample[i]->two.resize(nl_actual,false);
}
}
else
{
for (int l=0; l<SNP.size(); l++)
{
SNP[l]->one.resize(sample.size(),code);
SNP[l]->two.resize(sample.size(),false);
}
}
if ( par::ref_file )
{
for (int l=0; l< locus.size(); l++)
{
map<string,string>::iterator i = refallele.find( locus[l]->name );
map<string,string>::iterator i2 = refallele2.find( locus[l]->name );
// If we cannot find, we need to set genotypes to missing instead
if ( i != refallele.end() )
{
locus[l]->allele1 = i->second;
if ( i2 != refallele2.end() )
locus[l]->allele2 = i2->second;
else if ( par::lfile_allele_count )
locus[l]->allele2 = i->second + "v";
}
else
{
for (int i=0; i<sample.size(); i++)
{
if ( par::SNP_major )
{
SNP[l]->one[i] = true;
SNP[l]->two[i] = false;
}
else
{
sample[i]->one[l] = true;
sample[i]->two[l] = false;
}
}
}
}
}
///////////////////////////////////////////////
// .lgen
FILE * PED;
PED = fopen64(par::lpedfile.c_str(),"r");
if ( PED == NULL )
error("Problem opening LGEN file, errno = "+int2str(errno));
// We can now read any number of individual/genotype lines, in any
// order; we also do not assume that all genotypes are given --
// these will be missing by default
map<string,int> imap;
map<string,int> iperson;
for (int i=0; i<include.size(); i++)
{
if ( include[i] )
{
int k = include_pos[i];
imap.insert( make_pair ( locus[k]->name , k ) );
}
}
for (int i=0; i<sample.size(); i++)
{
iperson.insert( make_pair
( sample[i]->fid + "_" + sample[i]->iid ,
i ) );
}
// Whether or not we want to look at a locus is in the include[] vector
// The genomic position of locus i is k=include_pos[i] -> locus[k]
bool fatal = false;
string fmsg = "";
while( ! feof(PED) )
{
string fid = "";
string iid = "";
string snp = "";
string one = "";
string two = "";
int f = 0;
if ( readString( PED , fid ) ) f++;
if ( fid == "" )
continue;
if ( readString( PED , iid ) ) f++;
if ( readString( PED , snp ) ) f++;
if ( readString( PED , one ) ) f++;
map<string,int>::iterator im = imap.find(snp);
int k =
im != imap.end() ?
im->second : -1;
// Need to read second allele?
if ( ! ( par::compound_genotype_code || par::lfile_allele_count ) )
{
if ( readString( PED , two ) ) f++;
}
else
{
if ( par::compound_genotype_code )
{
if ( one.size() != 2 )
error("Problem with compound genotype not of length 2: [ " + one + " ]");
two = one[1];
one = one[0];
}
else if ( par::lfile_allele_count && k != -1 )
{
// expect either a 0,1 or 2, or missing code (anything other than 0,1 or 2)
int a;
if ( ! from_string<int>( a, one, std::dec ) )
a = -1;
if ( a < 0 || a > 2 )
{
one = two = par::missing_genotype;
}
else if ( a == 1 )
{
one = locus[k]->allele1;
two = locus[k]->allele2;
}
else if ( a == 2 )
one = two = locus[k]->allele2;
else if ( a == 0 )
one = two = locus[k]->allele1;
}
}
// cout << f << " " << "[" << fid << "] "
// << "[" << iid << "] "
// << "[" << snp << "] "
// << "[" << one << "] "
// << "[" << two << "] \n";
map<string,int>::iterator peri
= iperson.find( fid+"_"+iid );
Individual * person =
peri != iperson.end() ?
sample[peri->second] : NULL ;
// Ignore this genotype?
if ( ( ! person ) || k < 0 )
continue;
int ip = peri->second;
Locus * loc = locus[k];
/////////////////////////////////////////
// Add allele names to list, if needed
// If allele is not missing...
if (one!=par::missing_genotype && two!=par::missing_genotype)
{
// ...and not already listed
if (one!=loc->allele1 && one!=loc->allele2)
{
// ...then add to first empty slot
if(loc->allele1=="") loc->allele1=one;
else if(loc->allele2=="") loc->allele2=one;
else {
// .. or show an error if no empty slots
if (!fatal)
fmsg = "Locus " +
loc->name + " has >2 alleles:\n individual "
+ person->fid + " " + person->iid + " has genotype [ " + one +" "+two+" ]\n"
+ " but we've already seen [ " + loc->allele1 + " ] and [ " + loc->allele2 + " ]\n";
fatal=true;
}
}
}
// Repeat for second allele, if different
if (two!=one)
{
// If allele is not missing...
if (one!=par::missing_genotype)
// ...and not already listed
if (two!=loc->allele1 && two!=loc->allele2)
{
// ...then add to first empty slot
if(loc->allele1=="") loc->allele1=two;
else if(loc->allele2=="") loc->allele2=two;
else {
if (!fatal)
fmsg = "Locus " +
loc->name + " has >2 alleles:\n individual "
+ person->fid + " " + person->iid + " has genotype [ " + one +" "+two+" ]\n"
+ " but we've already seen [ " + loc->allele1 + " ] and [ " + loc->allele2 + " ]\n";
fatal=true;
}
}
}
// Give an error message
if ( fatal )
error(fmsg);
/////////////////////////////
// Add specific genotypes
if (par::SNP_major)
{
// 00 hom
if (one==loc->allele1 && two==loc->allele1)
{
SNP[k]->one[ip] = false;
SNP[k]->two[ip] = false;
}
// 01 het
else if (one!=par::missing_genotype &&
two!=par::missing_genotype &&
one!=two)
{
SNP[k]->one[ip] = false;
SNP[k]->two[ip] = true;
}
// 11 hom
else if (one==loc->allele2 && two==loc->allele2)
{
SNP[k]->one[ip] = true;
SNP[k]->two[ip] = true;
}
// 10 missing
else if (one==par::missing_genotype || two==par::missing_genotype)
{
SNP[k]->one[ip] = true;
SNP[k]->two[ip] = false;
}
}
else
{
// 00 hom
if (one==loc->allele1 && two==loc->allele1)
{
person->one[k]=false;
person->two[k]=false;
}
// 01 het
else if (one!=par::missing_genotype &&
two!=par::missing_genotype &&
one!=two)
{
person->one[k]=false;
person->two[k]=true;
}
// 11 hom
else if (one==loc->allele2 && two==loc->allele2)
{
person->one[k]=true;
person->two[k]=true;
}
// 10 missing
else if (one==par::missing_genotype || two==par::missing_genotype)
{
person->one[k]=true;
person->two[k]=false;
}
}
}
fclose(PED);
}
|