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
|
//////////////////////////////////////////////////////////////////
// //
// 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 <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <map>
#include "plink.h"
#include "stats.h"
#include "helper.h"
#include "options.h"
using namespace std;
void Plink::tagMode()
{
////////////////////////////
// Look-up table by SNP name
map<string,int> mlocus;
for (int l = 0 ; l < nl_all ; l++ )
mlocus.insert(make_pair( locus[l]->name, l ));
set<int> toTag;
bool testAll = par::gettag_file == "all";
if ( !testAll )
{
checkFileExists(par::gettag_file);
printLOG("Reading SNPs to tag from [ "
+ par::gettag_file + " ] \n");
ifstream IN2(par::gettag_file.c_str(),ios::in);
int cnt = 0;
while ( ! IN2.eof() )
{
string snp;
string code;
if ( par::gettag_mode1 )
IN2 >> snp;
else if ( par::gettag_mode2 )
{
vector<string> tokens = tokenizeLine(IN2);
if ( tokens.size() == 0 )
continue;
if ( tokens.size() != 2 )
error("Expected two columns per line\n");
snp = tokens[0];
code = tokens[1];
if ( code == "0" )
continue;
}
if ( snp == "" )
continue;
++cnt;
// Can we find this SNP?
map<string,int>::iterator i = mlocus.find( snp );
if ( i == mlocus.end() )
continue;
// Add to list
toTag.insert(i->second);
}
printLOG("Read " + int2str(cnt)
+ " SNPs to tag, of which "
+ int2str(toTag.size())
+ " are unique and present\n");
IN2.close();
}
else
{
printLOG("Setting to tag all " + int2str(nl_all) + " SNPs in dataset\n");
for (int l=0;l<nl_all;l++)
toTag.insert(l);
}
///////////////////////
// Find tags and output
// input
// rs0001
// rs0004
// output
// rs0001
// rs0002
// rs0003
// rs0004
ofstream O2;
if ( par::gettag_listall )
{
printLOG("Writing list of specific tags per SNP to [ " + par::output_file_name + ".tags.list ]\n");
O2.open( ( par::output_file_name + ".tags.list").c_str() , ios::out );
O2 << setw( par::pp_maxsnp ) << "SNP" << " "
<< setw(4) << "CHR" << " "
<< setw(10) << "BP" << " "
<< setw(4) << "NTAG" << " "
<< setw(10) << "LEFT" << " "
<< setw(10) << "RIGHT" << " "
<< setw(8) << "KBSPAN" << " "
<< "TAGS\n";
}
set<int>::iterator i = toTag.begin();
set<int> tagged;
while ( i != toTag.end() )
{
// SNP to tag
int l = *i;
tagged.insert(l);
set<int> thisTagged;
int dist_left = locus[l]->bp;
int dist_right = locus[l]->bp;
// Move forwards and backwards, within range, and
// add any with r^2 above threshold to tagged set
int j = l - 1;
int chr = locus[l]->chr;
int pos = locus[l]->bp;
while (1)
{
if ( j < 0 )
break;
if ( locus[j]->chr != chr )
break;
if ( pos - locus[j]->bp > par::gettag_kb )
break;
if ( ! par::gettag_listall )
{
if ( tagged.find(j) != tagged.end() )
{
--j;
continue;
}
}
double rsq = correlation2SNP( l,j,true,false);
if ( realnum(rsq) && rsq >= par::gettag_r2 )
{
tagged.insert(j);
if ( par::gettag_listall )
{
thisTagged.insert(j);
if ( locus[j]->bp < dist_left )
dist_left = locus[j]->bp;
}
}
--j;
}
// Now move right
j = l+1;
while (1)
{
if ( j == nl_all )
break;
if ( locus[j]->chr != chr )
break;
if ( locus[j]->bp - pos > par::gettag_kb )
break;
if ( ! par::gettag_listall )
{
if ( tagged.find(j) != tagged.end() )
{
++j;
continue;
}
}
double rsq = correlation2SNP( l,j,true,false);
if ( realnum(rsq) && rsq >= par::gettag_r2 )
{
tagged.insert(j);
if ( par::gettag_listall )
{
thisTagged.insert(j);
if ( locus[j]->bp > dist_right )
dist_right = locus[j]->bp;
}
}
++j;
}
if ( par::gettag_listall )
{
O2 << setw( par::pp_maxsnp ) << locus[l]->name << " "
<< setw(4) << locus[l]->chr << " "
<< setw(10) << locus[l]->bp << " "
<< setw(4) << thisTagged.size() << " "
<< setw(10) << dist_left << " "
<< setw(10) << dist_right << " "
<< setw(8) << (dist_right - dist_left ) / 1000.0 << " ";
set<int>::iterator s = thisTagged.begin();
bool first = true;
while ( s != thisTagged.end() )
{
if ( first )
{
first = false;
}
else
O2 << "|";
O2 << locus[*s]->name;
++s;
}
if ( first )
O2 << "NONE";
O2 << "\n";
}
// Consider next SNP to tag
++i;
}
if ( par::gettag_listall )
O2.close();
if ( ! testAll )
{
printLOG("In total, added "
+ int2str( tagged.size() - toTag.size() )
+ " tag SNPs\n");
ofstream O1;
printLOG("Writing tag list to [ " + par::output_file_name + ".tags ]\n");
O1.open( (par::output_file_name+".tags").c_str(), ios::out);
// Mode 1 : just write list
if ( par::gettag_mode1 )
{
set<int>::iterator l = tagged.begin();
while ( l != tagged.end() )
{
O1 << locus[*l]->name << "\n";
++l;
}
}
// Mode 2 : Write all SNPs, with 0/1 code
if ( par::gettag_mode2 )
{
for (int l = 0; l < nl_all ; l++ )
{
O1 << locus[l]->name << "\t";
if ( tagged.find(l) != tagged.end() )
O1 << "1\n";
else
O1 << "0\n";
}
}
O1.close();
}
}
|