-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwas_segment_maker.pl
282 lines (251 loc) · 7.73 KB
/
gwas_segment_maker.pl
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
#!/usr/bin/perl
#inputvariables file sig_col_name sig_thresh extend_sig_thresh distance join_distance
#inputfile SNP CHR BP P qvalue
#outputfile CHR BP Start Stop Peak Peak_val
use strict;
use Getopt::Std;
use Math::Complex;
use Scalar::Util qw(looks_like_number);
#reading options
our ($opt_i, $opt_c, $opt_t, $opt_e, $opt_d, $opt_j);
getopts('i:c:t:e:d:j:');
if (!$opt_i) {
print STDERR "\nInput file name (-i) required\n\n\n";
die;
}
if (!$opt_c) {
print STDERR "\nSignificance column name (-c) required\n\n\n";
die;
}
if (!$opt_t) {
print STDERR "\n-Log10 significance threshold (-t) required\n\n\n";
die;
}
if (!$opt_e) {
print STDERR "\n-Log10 segment extension significance threshold (-e) required\n\n\n";
die;
}
if (!$opt_d) {
print STDERR "\nDistance surrounding segment (-d) required\n\n\n";
die;
}
if (!$opt_j) {
print STDERR "\nSegment join distance (-j) required\n\n\n";
die;
}
#todo: make help option to print and explain options
if ($opt_d*2 >= $opt_j) {
print STDERR "\nSegment join distance (-j) must be at least two times the distance surrounding the segment (-d)\n\n\n";
die;
}
#open the file or die
open INFILE, "<", $opt_i or die "No such input file $opt_i";
my $first_line = 1; #used to recognize the header;
my @header_row;
my $chr_col;
my $bp_col;
my $sig_col;
my $peak_snp;
my $peak_snp_value;
my @segments_discovered;
my $segment_open = 0;
my $back_extend_segment_open = 0;
my $back_extend_start;
my $back_extend_current;
my $segment_start;
my $segment_end;
my $segment_last;
my $current_chromosome;
my %chr_max_pos;
#reads the file line by line
while (<INFILE>) {
# the variable $_ contains the current line.
chomp $_; #strips the new line character from the current input
#skips the header
if ($first_line) {
@header_row = split('\t', $_);
my $col_count = 0;
foreach my $col_name (@header_row) {
if ($col_name eq "Chr") {
$chr_col = $col_count;
}
if ($col_name eq "Pos") {
$bp_col = $col_count;
}
if ($col_name eq $opt_c) {
$sig_col = $col_count;
}
$col_count++;
}
#todo: check that all required columns are found or die
$first_line = 0;
next;
}
my @row = split('\t', $_);
#keep track of max chromosome positions
$chr_max_pos{$row[$chr_col]} = $row[$bp_col];
#skip missing data
if ($row[$sig_col] eq 'NaN') {
next;
}
#check for new chromosome
if ($current_chromosome) {
if ($row[$chr_col] ne $current_chromosome) {
print stderr "Finished chr $current_chromosome, starting chr ".$row[$chr_col]."\n";
if ($segment_open == 1) {
$segment_end = $segment_last;
my %segment_hash;
$segment_hash{'chr'} = $current_chromosome;
$segment_hash{'start'} = $segment_start;
$segment_hash{'end'} = $segment_end;
$segment_hash{'peak'} = $peak_snp;
$segment_hash{'peak_val'} = $peak_snp_value;
print stderr "current_chromosome $segment_start $segment_end $peak_snp $peak_snp_value\n";
push @segments_discovered, \%segment_hash;
$segment_open = 0;
$back_extend_segment_open = 0;
}
$current_chromosome = $row[$chr_col];
}
}
# keep track of how far back an extend threshold goes.
if ($back_extend_segment_open == 0 ) {
if (-log10($row[$sig_col]) > $opt_e) {
$back_extend_segment_open = 1;
$back_extend_start = $row[$bp_col];
$back_extend_current = $row[$bp_col];
}
} else {
if ($row[$bp_col] > $back_extend_current + $opt_d) {
if (-log10($row[$sig_col]) > $opt_e) {
#last extend is over but start a new one
$back_extend_start = $row[$bp_col];
$back_extend_current = $row[$bp_col];
#keep extend open but set new values
} else {
#past the extend distance so close the extend
$back_extend_segment_open = 0;
}
} else {
#still within extend distance
if (-log10($row[$sig_col]) > $opt_e) {
#update the current last extend position if crossing extend threshold within the extend distance
$back_extend_current = $row[$bp_col];
}
}
}
# open a new segment for significant SNPs if not already open
if ($segment_open == 0) {
#skip non significant rows
if (-log10($row[$sig_col]) > $opt_t) {
$segment_open = 1;
$peak_snp = $row[$bp_col];
$peak_snp_value = $row[$sig_col];
$segment_start = $row[$bp_col];
$segment_last = $row[$bp_col];
$current_chromosome = $row[$chr_col];
}
next;
}
#segment is open
if ($row[$bp_col] > $segment_last + $opt_d) {
#close segment
$segment_end = $segment_last;
my %segment_hash;
$segment_hash{'chr'} = $row[$chr_col];
$segment_hash{'start'} = $segment_start;
$segment_hash{'end'} = $segment_end;
$segment_hash{'peak'} = $peak_snp;
$segment_hash{'peak_val'} = $peak_snp_value;
$segment_hash{'merged'} = "single";
print stderr "$current_chromosome $segment_start $segment_end $peak_snp $peak_snp_value\n";
push @segments_discovered, \%segment_hash;
$segment_open = 0;
print stderr "Segment saved\n";
next;
}
#segment is open and not past extend distance
if (-log10($row[$sig_col]) > $opt_e) {
$segment_last = $row[$bp_col];
if (-log10($row[$sig_col]) > -log10($peak_snp_value)) {
#print stderr "updating peak snp $peak_snp value $peak_snp_value to $row[$sig_col]\n";
$peak_snp = $row[$bp_col];
$peak_snp_value = $row[$sig_col];
}
next;
}
}
close INFILE;
my %scaffold_segments;
my $first_segment = 1;
my $last_segment;
my $scaf_chr;
my $scaf_start;
my $scaf_end;
my $scaf_peak_snp;
my $scaf_peak_snp_val;
my $last_segment_printed = 0;
if (scalar @segments_discovered == 0) {
print STDERR "No segments found\n";
print "No segments found\n";
} else {
#print the header to the output
print "CHR\tStart\tStop\tPeak\tPeak_val\tMerged\n";
sub write_scaffold {
#adjust tracking threshold backwards
# if ($back_extend_segment_open == 1 && $last_segment->{'start'} - $back_extend_current < $opt_d) #{
# $last_segment->{'start'} = $back_extend_start;
# $back_extend_segment_open == 0;
# }
my $adj_start = $last_segment->{'start'} - $opt_d;
#don't extend past chromosome ends
if ($adj_start < 1) {
$adj_start = 1;
}
my $adj_end = $last_segment->{'end'} + $opt_d;
if ($chr_max_pos{$last_segment->{'chr'}} < $adj_end) {
$adj_end = $chr_max_pos{$last_segment->{'chr'}};
}
#print the segment
print $last_segment->{'chr'}."\t".$adj_start."\t".$adj_end."\t".$last_segment->{'peak'}."\t".$last_segment->{'peak_val'}."\t".$last_segment->{'merged'}."\n";
}
foreach my $segment (@segments_discovered) {
if ($first_segment == 1) {
$current_chromosome = $segment->{'chr'};
$first_segment = 0;
$last_segment = $segment;
next;
}
$last_segment_printed = 1;
#catch and print joined segments at end of chromosome
if ($segment->{'chr'} ne $last_segment->{'chr'}) {
if ($last_segment_printed == 0) {
write_scaffold();
$last_segment_printed = 1;
}
}
#see if within join distance
if (($segment->{'chr'} eq $last_segment->{'chr'}) && ($last_segment->{'end'} + $opt_j > $segment->{'start'})) {
print stderr "Merging adjacent scaffolds :".$last_segment->{'chr'}." ".$last_segment->{'start'}." ".$last_segment->{'end'}."\n";
$last_segment->{'end'} = $segment->{'end'};
#$last_segment->{'end'} = 0;
$last_segment->{'merged'} = "merged";
if (-log10($last_segment->{'peak_val'}) < -log10($segment->{'peak_val'})) {
$last_segment->{'peak_val'} = $segment->{'peak_val'};
$last_segment->{'peak'} = $segment->{'peak'};
}
print stderr "New end is :".$last_segment->{'end'}."\n";
$last_segment_printed = 0;
#write_scaffold();
next;
} else {
#print the current segment
write_scaffold();
$last_segment = $segment;
$last_segment_printed = 1;
}
}
#write last scaffold
write_scaffold();
}
# todo: collect and print stats