-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfreec2segment.pl
executable file
·145 lines (128 loc) · 5.3 KB
/
freec2segment.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
#!/usr/bin/env perl
=hey
Author: Shijian Sky Zhang
E-mail: [email protected]
=cut
use 5.010;
use warnings;
use strict;
use Getopt::Long;
use File::Basename;
use lib dirname $0;
use pm::common;
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName OPTION CNVs.tsv >segment.cbs 2>marker.tsv
If CNVs.tsv isn't specified, input from STDIN
Option:
-r --ratio TSV The CNV ratio file
-h --help Print this help information
NOTE: CNV.tsv and --ratio must be sort by chr, start and end
HELP
}
my ($ratio, $ROI, $sampleName);
GetOptions(
'r|ratio=s' => \$ratio,
'h|help' => sub{usage(); exit}
) || usage();
$ARGV[0] = '-' unless defined $ARGV[0];
open IN, "$ARGV[0]" or die "Can't read file ($ARGV[0]): $!";
open RATIO, "$ratio" or die "Can't read file ($ratio): $!";
say join "\t", ('Chromosome', 'Start', 'End', 'Num_Probes', 'Segment_Mean');
my $ratioLine = <RATIO>;
$ratioLine = <RATIO> if $ratioLine =~ /^Chromosome/;
while(<IN>){
chomp;
my ($chr, $start, $end) = split "\t";
next if $start eq "start";
my %neutralMarker;
my @chrs; # to store chromosomes in order as appearing in RATIO file
while(defined $ratioLine){
chomp $ratioLine;
my ($markerChr, $markerStart, $ratio, $gene) = (split "\t", $ratioLine)[0..2,5];
my $markerEnd = (split "-", $gene)[1];
if($markerChr eq $chr && $start < $markerStart && $markerEnd <= $end){
last;
}else{
push @chrs, $markerChr if !exists $neutralMarker{$markerChr};
$neutralMarker{$markerChr}{start} = [] if !exists $neutralMarker{$markerChr}{start};
push @{$neutralMarker{$markerChr}{start}}, $markerStart;
$neutralMarker{$markerChr}{end} = [] if !exists $neutralMarker{$markerChr}{end};
push @{$neutralMarker{$markerChr}{end}}, $markerEnd;
$neutralMarker{$markerChr}{log2Ratio} = [] if !exists $neutralMarker{$markerChr}{log2Ratio};
my $log2Ratio = $ratio == 0 ? -10 : log($ratio)/log(2);
push @{$neutralMarker{$markerChr}{log2Ratio}}, $log2Ratio;
}
$ratioLine = <RATIO>;
}
for my $markerChr (@chrs){
my @markerStarts = @{$neutralMarker{$markerChr}{start}};
my @log2Ratios = @{$neutralMarker{$markerChr}{log2Ratio}};
my ($segmentStart, $segmentEnd) = ($markerStarts[0], $neutralMarker{$markerChr}{end}->[-1]);
say join "\t", ($markerChr
, $segmentStart
, $segmentEnd
, scalar @markerStarts
, &common::average(@log2Ratios)
);
for my $markerStart(@markerStarts[0..($#markerStarts-1)]){
say STDERR join "\t", ("$markerChr:$markerStart", $markerChr, $markerStart)
}
say STDERR join "\t", ("$markerChr:$segmentEnd", $markerChr, $segmentEnd);
}
say STDERR join "\t", ("$chr:".($start+1), $chr, $start+1);
my $markerCount = 1;
my @log2Ratios;
while(defined $ratioLine){
chomp $ratioLine;
my ($markerChr, $markerStart, $ratio, $gene) = (split "\t", $ratioLine)[0..2,5];
my $markerEnd = (split "-", $gene)[1];
if($markerChr eq $chr && $start < $markerStart && $markerEnd <= $end){
my $log2Ratio = $ratio == 0 ? -10 : log($ratio)/log(2);
push @log2Ratios, $log2Ratio;
if($markerStart != $start+1 && $markerEnd != $end){
say STDERR join "\t", ("$markerChr:$markerStart", $markerChr, $markerStart);
$markerCount++;
}
}else{
last;
}
$ratioLine = <RATIO>;
}
say STDERR join "\t", ("$chr:$end", $chr, $end);
$markerCount++;
my $segment_mean = &common::average(@log2Ratios);
say join "\t", ($chr, $start+1, $end, $markerCount, $segment_mean);
}
my %neutralMarker;
my @chrs;
while(defined $ratioLine){
chomp $ratioLine;
my ($markerChr, $markerStart, $ratio, $gene) = (split "\t", $ratioLine)[0..2,5];
my $markerEnd = (split "-", $gene)[1];
push @chrs, $markerChr if !exists $neutralMarker{$markerChr};
$neutralMarker{$markerChr}{start} = [] if !exists $neutralMarker{$markerChr}{start};
push @{$neutralMarker{$markerChr}{start}}, $markerStart;
$neutralMarker{$markerChr}{end} = [] if !exists $neutralMarker{$markerChr}{end};
push @{$neutralMarker{$markerChr}{end}}, $markerEnd;
$neutralMarker{$markerChr}{log2Ratio} = [] if !exists $neutralMarker{$markerChr}{log2Ratio};
my $log2Ratio = $ratio == 0 ? -10 : log($ratio)/log(2);
push @{$neutralMarker{$markerChr}{log2Ratio}}, $log2Ratio;
$ratioLine = <RATIO>;
}
for my $markerChr (@chrs){
my @markerStarts = @{$neutralMarker{$markerChr}{start}};
my @log2Ratios = @{$neutralMarker{$markerChr}{log2Ratio}};
my ($segmentStart, $segmentEnd) = ($markerStarts[0], $neutralMarker{$markerChr}{end}->[-1]);
say join "\t", ($markerChr
, $segmentStart
, $segmentEnd
, scalar @markerStarts
, &common::average(@log2Ratios)
);
for my $markerStart(@markerStarts[0..($#markerStarts-1)]){
say STDERR join "\t", ("$markerChr:$markerStart", $markerChr, $markerStart)
}
say STDERR join "\t", ("$markerChr:$segmentEnd", $markerChr, $segmentEnd);
}