forked from fanagislab/EndHiC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster2bed.pl
More file actions
executable file
·59 lines (57 loc) · 1.77 KB
/
Copy pathcluster2bed.pl
File metadata and controls
executable file
·59 lines (57 loc) · 1.77 KB
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
#!/usr/bin/perl
use strict;
if (not $ARGV[0] or $ARGV[0] eq "-h") {
die "
Description: read gfa.cluster and matrix_abs.bed file, reorder bins according to the ordered and oriented contigs in each cluster, and output new cluster.bed and cluster.len.
Author: Sen Wang, wangsen1993@163.com, 2021/9/2.
Usage: perl cluster2bed.pl formal_100000_abs.bed z.EndHiC.results.summary.and.analysis.A.cluster >z.EndHiC.results.summary.and.analysis.A.cluster_abs.bed 2>z.EndHiC.results.summary.and.analysis.A.cluster.id.len
\n";
}
# read bed and map bins to contigs
my %ctg2bin;
open IN, "<$ARGV[0]" or die "Cannot open $ARGV[0]!\n";
while (<IN>) {
chomp;
my @f = split(/\t/, $_);
my $c = shift @f;
push @{$ctg2bin{$c}}, join("\t", @f);
}
close IN;
# read cluster and map contigs to clusters
my %clu2ctg;
open IN, "<$ARGV[1]" or die "Cannot open $ARGV[1]!\n";
while (<IN>) {
next if /^#/;
chomp;
my @f = split(/\t/, $_);
print STDERR "$f[0]\t$f[2]\n";
$clu2ctg{$f[0]} = $f[4];
}
close IN;
# reorder the bins according to the mapped contigs in each cluster
foreach my $clu (sort keys %clu2ctg) {
my @ctgs = split(/;/, $clu2ctg{$clu});
my $start = 0;
my $end = 0;
#print STDOUT "@ctgs\n";
foreach my $c (@ctgs) {
my $ctg = substr($c, 0, length($c) - 1);
die "Cannot get the bins of $ctg! check $ARGV[0]!\n" if not $ctg2bin{$ctg};
my $strand = substr($c, -1, 1);
if ($strand eq "+") {
foreach my $line (@{$ctg2bin{$ctg}}) {
my ($s, $e, $i) = split(/\t/, $line);
$end = $start + ($e - $s);
print STDOUT "$clu\t$start\t$end\t$i\n";
$start = $end;
}
} elsif ($strand eq "-") {
foreach my $line (reverse @{$ctg2bin{$ctg}}) {
my ($s, $e, $i) = split(/\t/, $line);
$end = $start + ($e - $s);
print STDOUT "$clu\t$start\t$end\t$i\n";
$start = $end;
}
}
}
}