File: ext.in

package info (click to toggle)
loki 2.4.7.4-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,956 kB
  • ctags: 3,606
  • sloc: ansic: 38,653; yacc: 4,974; lex: 946; makefile: 329; sh: 70
file content (373 lines) | stat: -rw-r--r-- 9,831 bytes parent folder | download | duplicates (4)
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
#!+PERLPROG+
#
# Script to extract columns from loki.out
#
# Should work with output from all versions of Loki (up to loki_2.3)
#
# Simon Heath - September 2000
#
use Getopt::Std;
use IO::File;
use POSIX qw(tmpnam);
use strict;
my($i,$j,$k,%opt,$file,$hi,$lo,$flag,$out_type,$version,$link_fg,$start,$stop);
my($lg,%chrom,@map_start,@map_end,@map_length,$sex_map,$model);
my(@mkchrom,@mkpos,@mkname,$nmk,@col,$n_cov,$max_col,$ngroup);
my($it_col,$tv_col,$resvar_col,$mean_col,$addvar_col);
my($mean_set,$mean,$resvar_set,$resvar,$max_col1,$tau_col,$tau_beta,$tau,$tau_mode);
my($nc,$nqtl,$nqtl1,$line,$iter,$tv,$tv1,$sz,$nrand,@rand_col,@rand_fg,$tmp);
my(@no_output,$out_col_flag,$adj);
my($x1,$x2);
# Chromsome extracted defaults to the first one
$opt{c}=1;
getopts('xc:CDf:r:o:i:',\%opt);
# -r option sets a range of positions on the chromosome to look at
if($opt{r}) {
  $tmp=$opt{r};
  if($tmp=~/^([+-]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+))(?:[eE][-+]?[0-9]+)?)(.*)/) {
    $lo=$1;
	 $tmp=$2;
  }
  if($tmp=~/^[,-:](.*)/) {
    $tmp=$1;
	 if($tmp=~/^([+-]?(?:(?:[0-9]*\.[0-9]+)|(?:[0-9]+))(?:[eE][-+]?[0-9]+)?)/) {
	   $hi=$1;
	 }
  }
  die "Bad -r option\n" if(!defined($hi) && !defined($lo));
  if(defined($lo) && defined($hi) && $lo>$hi) {
    $tmp=$lo;
	 $lo=$hi;
	 $hi=$tmp;
  }
}
# -i option sets a range of iterations to consider
if($opt{i}) {
  $tmp=$opt{i};
  if($tmp=~/^([1-9][0-9]*)(.*)/) {
    $start=$1;
	 $tmp=$2;
  }
  if($tmp=~/^[,-:](.*)/) {
    $tmp=$1;
	 if($tmp=~/^([1-9][0-9]*)/) {
	   $stop=$1;
	 }
  }
  die "Bad -i option\n" if(!defined($start) && !defined($stop));
  if(defined($start) && defined($stop) && $start>$stop) {
    $tmp=$start;
	 $start=$stop;
	 $stop=$tmp;
  }
  $start=1 if(!defined($start));
}

# Set default number of genetic groups
$ngroup=1;
# Used to store columns where to find interesting quantitities
# -1 means not known
$it_col=-1; # Iteration count
$tv_col=-1; # Total genetic variance
$resvar_col=-1; # Residual variance
$mean_col=-1; # Grand mean
$max_col=-1; # No. fixed output columns
$n_cov=-1; # No. covariate columns
$ngroup=1; # No. genetic groups
$tau_col=-1;
$tau_mode=-1;
$out_type=-1; # Output type
# Open output file if specified, otherwise we use STDOUT
if($opt{o}) {
  open OFILE,">".$opt{o} or die "Couldn't open output file ",$opt{o},"\n";
} else {
  open OFILE,">&STDOUT" or die "Couldn't dup STDOUT\n";
}
$sex_map=0;
$i=$opt{x}+$opt{C}+$opt{D};
die "Can not specify more that 1 of the options C,D,x at once.\n" if($i>1);
$opt{x}=1 unless $i;
$file=$opt{f}?$opt{f}:"loki.out";
open FILE,$file or die "Could not open file '$file'\n";
# Go though file
while(<FILE>) {
  $line++;
  if($flag) { # Parse the data portion of the file
    split;
	 # Iteration number
	 $iter=$_[0];
	 if($opt{i}) {
	   next if($iter<$start);
		last if(defined($stop) && $iter>$stop);
	 }
	 $nc=@_;
	 next if($nc<$max_col);
	 if($version==-1) {
	   $i=$max_col+$_[6];
		last if($nc<$max_col);
		$ngroup=$_[7];
	 } else {$i=$max_col;}
	 $max_col1=$i;
	 if($opt{C}) {
	   for($i=0;$i<$max_col1;$i++) {print OFILE " $_[$i]";}
		print OFILE "\n";
		next;
	 }
	 # Total variance
	 $tv1=0; #first get non-genetic variance
	 for($j=0;$j<$nrand;$j++) {
	   if($rand_fg[$j]==2) {
		  $sz=$_[$rand_col[$j]];
		  $tv1+=$sz;
	   }
	 }
	 # Now get genetic variance
	 if($tv_col>=0) {$tv=$_[$tv_col];}
	 else { # Not specified, must calculate
	   $tv=0;
		for($j=0;$j<$nrand;$j++) {
		  $sz=$_[$rand_col[$j]];
		  $sz*=$sz if($rand_fg[$j]==1);
		  if($rand_fg[$j]!=2) {$tv+=$sz;}
		}
	 }
	 # Count QTL's
	 $nqtl=0;
	 $nqtl1=0;
	 while($i<$nc) {
	   $lg=$_[$i];
		die "Illegal linkage group $lg at line $line column ",$i+1,"\n" if($lg && !$chrom{$lg});
		$i+=1+$sex_map if($lg || !$out_type);
		$i+=4+$ngroup;
		if($tv_col<0) {
		  $sz=$_[$i-1];
		  $tv+=$sz*$sz;
		}
		$nqtl++;
		if($lg) {
		  $nqtl1++;
		}
	 }
	 # If QTL numbers are given, check against what we found
	 if($out_type<2) {
	   die "Mismatch in QTL numbers ($nqtl,$_[1]) at line $line\n" if($nqtl!=$_[1]);
	   die "Mismatch in linked QTL numbers ($nqtl1,$_[2]) at line $line\n" if($nqtl1!=$_[2]);
	 }
	 # Get residual variance
	 if(!$resvar_set) {
	   # Do we know where it is ?
		if($resvar_col>=0) {$resvar=$_[$resvar_col];}
		# if not, guess
		elsif($out_type<2) {$resvar=$_[4];}
		else {$resvar=$_[2];}
	 }
	 # Total non-genetic variance
	 $tv1+=$resvar;
	 # Print out a 'standard' (across output types) set of columns
	 if($opt{D}) {
		# Get Mean
		if(!$mean_set) {
		  if($mean_col>=0) {$mean=$_[$mean_col];}
		  elsif($out_type<2) {$mean=$_[3];}
		  else {$mean=$_[1];}
	   }
		# get Tau
		if($tau_col>=0) {$tau=$_[$tau_col];}
		elsif($tau_mode>=0) {
		  if($tau_mode==2) {$tau=$resvar*$tau_beta;}
		  else {$tau=$tau_beta;}
		} else {
		  # Must be a very early version, assume that we know where tau is...
		  $tau=$_[5];
		}
		print OFILE "$iter $nqtl $nqtl1 $mean $resvar $tau ";
		printf OFILE "%g",$tv;
		# Are we on a recent version with all column info?
		if($out_col_flag) {
		  for($i=1;$i<$max_col1;$i++) {
		    print OFILE " $_[$i]" if(!$no_output[$i]);
		  }
		# This is more complicated
		} else {
		  if(!$out_type) {$i=8;}
		  elsif($out_type<2) {$i=6;}
		  else {$i=3};
		  for(;$i<$max_col1;$i++) {
		    print OFILE " $_[$i]";
		  }
		}
		print OFILE "\n";
		# Extract information about linked QTL's
	 } elsif($opt{x} && $nqtl) {
	   # Go through QTL's
		$i=$max_col1;
		while($i<$nc) {
	     $lg=$_[$i];
		  if($lg eq $opt{c}) {
			 $j=$i+1;
		    if($lg) {
		      $x1=$_[$j++];
			   $x2=$_[$j++] if($sex_map);
			 }
			 # Select QTL based on linkage group and range
			 if(!$lg || !$opt{r} || ((!defined($lo) || $x1>=$lo) && (!defined($hi) || $x1<=$hi))) {
		      print OFILE $iter;
			   # Print position if linked
			   if($lg) {
				  print OFILE " $x1";
				  print OFILE " $x2" if($sex_map);
				}
				for($k=0;$k<2+$ngroup;$k++) {print OFILE " ",$_[$j++];}
				$sz=$_[$j];
				print OFILE " $sz";
				$sz*=$sz;
				if($tv>0.0) {printf OFILE " %g",$sz/$tv;}
				else {print OFILE " 0.0";}
				if(($tv1+$tv)>0.0) {printf OFILE " %g\n",$sz/($tv+$tv1)}
				else {print OFILE " 0.0\n";}
			 }
		  }
		  $i+=1+$sex_map if($lg || !$out_type);
		  $i+=4+$ngroup;
		}
    }
  # We've reached the end of the header
  } elsif(/^--*/) {
    $flag=1;
	 # $max_col will not be set if working with an earlier version
	 if($max_col<0) {
	   # but $n_cov should be set unless ...
	   if($n_cov>=0) {
		  if($out_type<0) {$out_type=1;}
	     $max_col=3+$n_cov;
		  $version=1;
		  if(!$out_type) {$max_col+=5;}
		  elsif($out_type==1) {$max_col+=3;}
		} else { # ... we are working with a genuine v2.0 copy
		  $out_type=0;
	     $max_col=8;
		  $version=-1;
	   }
	 }
  } elsif(!$flag) { # First parse the header
    if(/^Output format: (\d+)/) {
	   $out_type=$1;
		next;
	 }
	 if(/^Created by loki (\d+).(\d+).(\d+).*:/) {
		$version=2 if($1>2 || ($1==2 && $2>=3));
		next;
	 }
	 # Pull out linkage group information if it is there
	 if(/^Linkage groups:$/) {
      $link_fg=1;
	   next;
    }
	 if($link_fg==1) {
      # Check for linkage group names and map lengths
      if(/(\d+): (.*)$/) {
	     $lg=$1;
		  if($2=~/^(.*) Map range: (.*)/) {
		    $chrom{$lg}=$1;
		    if($2=~/\((-?[0-9.]+)cM to (-?[0-9.]+)cM\)(.*)/) {
		      $map_start[$lg][0]=$1;
		      $map_end[$lg][0]=$1;
			   if($3=~/\((-?[0-9.]+)cM to (-?[0-9.]+)cM\)/) {
		        $map_start[$lg][1]=$1;
		        $map_end[$lg][1]=$1;
				  $sex_map=1;
			   }
		    } else { die "Error reading linkage group map range\n"; }
		  } else {$chrom{$lg}=$1;}
		} elsif(/^\s+(.+) - ([\d\.]+)\s*([\d\.]*)/) {
		  if(!$opt{c} || $lg eq $opt{c}) {
	       $mkchrom[$nmk]=$lg;
			 $mkpos[$nmk][0]=$2;
			 $mkpos[$nmk][1]=$3;
			 $mkname[$nmk++]=$1;
		  }
	   } else {
	     # Check for total map length
        if(/^Total Map Length: (.*)$/) {
		    if($1=~/(\d\d*\.?\d*)cM(.*)/) {
		      $map_length[0]=$1;
			   if($2=~/(\d\d*\.?\d*)cM$/) {
			     $map_length[1]=$1;
			     $sex_map=1;
			   }
		    } else { die "Error reading map lengths\n"; }
		    $link_fg=2;
	       next;
	     }
	   }
	 }
	 $model=$1 if(/^Model: (.+)/);
	 if($link_fg<3) {
	   if(/^Output columns:$/) {
		  $link_fg=3;
		  $out_col_flag=1;
		  next;
		} elsif(/Output covariate data:$/) {
		  $link_fg=4;
		}
	 } elsif($link_fg>2) {
	   if(/(\d+): (.*)/) {
		  $col[$1]=$2;
		  $tmp=$1-1;
		  if($link_fg==4) {
		    $link_fg=3;
			 if(!$tmp) {$adj=6;}
		  }
		  $tmp+=$adj;
		  if($2 eq "Total genetic variance") {
		    $tv_col=$tmp;
			 $no_output[$tmp]=1;
		  } elsif($2 eq "Additive variance") {
		    $rand_fg[$nrand]=0;
			 $rand_col[$nrand++]=$tmp;
		  } elsif($2=~/^Additional random variance for/) {
		    $rand_fg[$nrand]=2;
			 $rand_col[$nrand++]=$tmp;
		  } elsif($2=~/\S+ size$/) {
		    $rand_fg[$nrand]=1;
			 $rand_col[$nrand++]=$tmp;
		  } elsif($2 eq "Residual variance") {
		    $resvar_col=$tmp;
			 $no_output[$tmp]=1;
		  } elsif($2 eq "Grand mean") {
		    $mean_col=$tmp;
			 $no_output[$tmp]=1;
		  } elsif($2 eq "Tau") {
		    $tau_col=$tmp;
			 $no_output[$tmp]=1;
		  } elsif($2 eq "No. QTL's in model") {
		    $no_output[$tmp]=1;
		  } elsif($2 eq "No. linked QTL's") {
		    $no_output[$tmp]=1;
		  } elsif($2 eq "No. covariate columns") {
		    $no_output[$tmp]=1;
		  } elsif($2 eq "No. genetic groups") {
		    $no_output[$tmp]=1;
		  }
		} elsif(/No. covariate columns: (\d+)/) {
		  $n_cov=$1;
		} elsif(/No. fixed output columns: (\d+)/) {
		  $max_col=$1;
		} elsif(/No. genetic groups: (\d+)/) {
		  $ngroup=$1;
		} elsif(/^Sex specific map$/) {
		  $sex_map=1;
		} elsif(/^Residual variance: ([0-9-.]+)/) {
		  $resvar_set=1;
		  $resvar=$1;
		} elsif(/^Grand mean: ([0-9-.]+)/) {
		  $mean_set=1;
		  $mean=$1;
		} elsif(/^Tau Mode: ([0-9]+)/) {
		  $tau_mode=$1;
		} elsif(/^Tau Beta: ([0-9.]+)/) {
		  $tau_beta=$1;
		}
	 }
  }
}