File: lookup.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 (305 lines) | stat: -rw-r--r-- 6,838 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


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2008 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 <sstream>

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

using namespace std;

#define  PORT_NUM                80     
#define  IP_ADDR    "132.183.161.22"  

void Plink::lookup()
{

#ifdef SKIP
  printLOG("Web-lookup not implemented on this system...\n");
  return;
#else

  printLOG("PLINK-SNP (WGAS SNP annotation courtesy of Patrick Sullivan)\n");

  vector<string> tokens;
  vector< string > gene_list;

    string GET_STRING;

  if ( par::lookup_gene )
    {
      if ( par::lookup_multiple_genes ) 
	{
	  
 	  string query = "";
 	  int c = 0;
	  
 	  string window= int2str(par::lookup_snp_kb_window * 1000);
	  
 	  // Read list, append to query and send
 	  checkFileExists(par::lookup_gene_name);
 	  ifstream S(par::lookup_gene_name.c_str(),ios::in);

	  string cmd;
	  int x = 0;

	  GET_STRING = "GET /~purcell/cgi-bin/gene.pl?win=" + window;

 	  while(!S.eof())
 	    {
 	      string gene;
 	      S >> gene;
 	      if (gene=="")
 		continue;
	      std::string s;
	      std::stringstream out;

	      out << x;
	      s = out.str();
	      cmd += "&gene" + s + "=" + gene;
	      x++;
	      c++;
	      gene_list.push_back(gene);

 	      if ( c>100 ) 
 		error("Please do not send large batch queries to PLINK-SNP");
 	    }
 	  S.close();      
	  GET_STRING += cmd + " HTTP/1.0 \n Content Length: 10000 \nHost: pngu.mgh.harvard.edu\nConnection: close\n\n";
	  tokens = socketConnection( this ,
				     IP_ADDR,
				     PORT_NUM,
				     GET_STRING );

	}
      else
	{
	  string window = int2str( par::lookup_gene_kb_window * 1000 );
	  
	  printLOG("Looking up gene information (and SNPs +/- " 
		   + int2str(par::lookup_gene_kb_window)+" kb)\n");
	  
	  GET_STRING = "GET /~purcell/cgi-bin/gene.pl?win=" + window + "&gene=" + par::lookup_gene_name + " HTTP/1.0 \nHost: pngu.mgh.harvard.edu\nConnection: close\n\n";
	  tokens = socketConnection( this , 
				     IP_ADDR,
				     PORT_NUM,
				     GET_STRING );

	}
    }
  else if ( par::lookup_single_snp )
    {

      string window= int2str(par::lookup_snp_kb_window * 1000);

      printLOG("Looking up SNP information, listing genes within " 
	       + int2str(par::lookup_snp_kb_window)+" kb\n");


      GET_STRING = "GET /~purcell/cgi-bin/snp.pl?win=" + window + "&snp=" + par::lookup_snp + " HTTP/1.0 \nHost: pngu.mgh.harvard.edu\nConnection: close\n\n";
 
      tokens = socketConnection( this , 
				 IP_ADDR,
				 PORT_NUM,
				 GET_STRING );      
    }
  else
    {
      string query = "";
      int c = 0;

      string window= int2str(par::lookup_snp_kb_window * 1000);

      // Read list, append to query and send
      checkFileExists(par::lookup_snp);
      ifstream S(par::lookup_snp.c_str(),ios::in);
      string cmd;
      int x = 0;

      GET_STRING = "GET /~purcell/cgi-bin/snp.pl?win=" + window;
      while(!S.eof())
	{
	  string snp;
	  S >> snp;
	
	  if (snp=="")
	    continue;
	  std::string s;
	  std::stringstream out;

	  out << x;
	  s = out.str();
	  cmd += "&snp" + s + "=" + snp;	
	  x++;
	  c++;
	  if ( c>100 ) 
	    error("Please do not send large batch queries to PLINK-SNP");
	}
      S.close();      
      GET_STRING += cmd + " HTTP/1.0\nHost: pngu.mgh.harvard.edu\nConnection: close\n\n";

      tokens = socketConnection( this ,
      				 IP_ADDR,
      				 PORT_NUM,
      				 GET_STRING );

      printLOG("Looking up SNP information, listing genes within " 
	       + int2str(par::lookup_snp_kb_window)+" kb)\n");

    }
   


  if ( tokens.size() < 25 ) {
      cout << "\n\n";
      if( par::lookup_single_snp ) 
	  cout << par::lookup_snp;
      else
	  cout << par::lookup_gene_name;

      cout << " not found\n";
      cout << "-----------------------------\n";
      return;
  }

   if ( par::lookup_to_file )
    {
      
      ofstream OUT;
      string f = par::output_file_name;
      
      // Need to handle both single and multiple instances

      if ( par::lookup_gene ) 
	{

	  f += ".snp.list";
	  
	  if ( tokens[0] == "-1") 
	    {
	      printLOG("\n\nCould not find gene " 
		       + par::lookup_gene_name 
		       + " in database\n");
	      return;
	    }

	  printLOG("Writing SNP details to [ " + f + " ]\n\n\n");	  
	  OUT.open( f.c_str(), ios::out );

	  bool geneInfo = true;
	  int totalSNPs = 0;
	  int gcount = 0;

	    for (int i=25; i<tokens.size()-1; i++)
	      {
	      if (tokens[i]=="__END")
		{
		  
		  // Skip following line return
		  
		  
		  if ( geneInfo )
		    
		    {
		      // Move from gene information to SNPs
		      
//			  if ( par::lookup_multiple_genes )
//			      OUT << tokens[i+1]; // "\n";
//			      OUT << gene_list[gcount++]; // << "\n";
			  
		      geneInfo = false;
		      continue;
		    }
		  else
		    {
		      // Move from SNPs to gene information
		      // Skip count 
		       ++i;
		      geneInfo = true;
		      printLOG("----------------------------------------------------------\n");
		      printLOG("\n\n");
		      
//		      if ( par::lookup_multiple_genes )
			OUT << "END\n\n";
		      
		      continue;
		    }
		}
	      
	      if ( geneInfo ) 
		{
		  if (tokens[i]=="|n")
		    printLOG("\n");
		  else if ( tokens[i]=="|t")
		    printLOG("\t");
		  else
		    printLOG(tokens[i]+" ");	     
		}
	      else
		{
		  if (tokens[i]=="|n")
		    OUT << "\n";
		  else if ( tokens[i]=="|t")
		    OUT << "\t";
		  else
		    OUT << tokens[i] << " ";	  
		}
	      
	  }
	    OUT.close();
	}
	  else // multiple SNP queries selected
	    {
	      
	      f += ".snp.annot";
	      
	      printLOG("\nWriting SNP details to [ " + f + " ]\n");
	      
	      OUT.open( f.c_str(), ios::out );
	      for (int i=25; i<tokens.size()-1; i++)
		{
		  if (tokens[i]=="|n")
		    OUT << "\n";
		  else if ( tokens[i]=="|t")
		    OUT << "\t";
		  else
		    OUT << tokens[i] << " ";	  
		}      
	      OUT.close();
	    }

    }
    
      else // Standard to LOG (single SNP query)
	{
	  printLOG("\n\n");
	  for (int i=25; i<tokens.size()-1; i++)
	    {
	      if (tokens[i]=="|n")
		printLOG("\n");
	      else if ( tokens[i]=="|t")
		printLOG("\t");
	      else
		printLOG(tokens[i]+" ");
	    }
	}
    
   return;
   
#endif
   
}