-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrackHubGenerator.pl
More file actions
133 lines (117 loc) · 4.84 KB
/
TrackHubGenerator.pl
File metadata and controls
133 lines (117 loc) · 4.84 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
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
#!/usr/bin/perl -w
#
#
#------------------------------------------------------------------------------
# TrackHub Generator
#------------------------------------------------------------------------------
# TrackHub Generator allows the automatic generation of track hubs from a collection of BED files.
# To run the TrackHub Generator enter the following command prompt in a unix shell:
# perl TrackHubGenerator.pl PATH/TO/NANE ASSEMBLY FBED UCSC EMAIL
#
# Positional arguments:
# PATH/TO/NAME Path to location where track hub will be created ending with the name of the track hub
# ASSEMBYL Assembly of the genome for which the track hub shall be used, e.g. hg38
# FBED Path to the folder containing all BED files which should be included in the track hub. Each BED file will result in a separate track
# UCSC Path to the filder containing the bedTOBigBed binary and the fetchChromSizes script downloaded from UCSC
# EMAIL Email address to be used as contact for people using the track hub
#
#
# Dependencies:
# The TrackHub Generator relies on two tools provided through UCSC Genomes which can be downloaded from http://hgdownload.soe.ucsc.edu/admin/exe/
# - bedToBigBed binary tool to create binary BigBed files from input bed files
# - fetchChromSizes script to download chromosome sizes for indexing BigBed files during generation through bedToBigBed
#
#
# For more information please refere to www.sanger.ac.uk/science/tools/trackhub-generator
#------------------------------------------------------------------------------
use File::Basename;
use File::Find::Rule;
my $hubpath = shift;
my $genome = shift;
my $bedfiles = shift;
my $toolfiles = shift;
my $email = shift;
my $hubname = basename($hubpath);
my $name = $hubname;
$name =~ tr/ //ds;
my $dirname = dirname($hubpath);
$hubpath = join("",$dirname,"/",$hubname,"/");
my $shortlabel = substr($hubname,0,17);
my $longlabel = substr($hubname,0,80);
my $genomepath = join("",$hubpath,$genome);
if (!-d $hubpath) {
mkdir $hubpath or die "Failed to create path for hub: $hubpath";
mkdir $genomepath or die "Failed to create path for genome in hub path: $genomepath";
} else {
if(!-d $genomepath) {
mkdir $genomepath or die "Failed to create path for genome in hub path: $genomepath";
}
}
if(-d $hubpath) {
my $strlength = length $hubname;
my $hubfile = join("",$hubpath,"hub.txt");
open(OUT, '>', $hubfile) or die "Could not open file $hubfile";
print OUT "hub ${name}\nshortLabel ${shortlabel}\nlongLabel ${longlabel}\ngenomesFile genomes.txt\nemail ${email}";
close OUT;
my @genomes = File::Find::Rule->new->directory->mindepth(1)->maxdepth(1)->in($hubpath);
my $temp ="";
my $genomesfile = join("",$hubpath,"genomes.txt");
open(OUT, '>', $genomesfile) or die "Could not open file $genomesfile";
foreach(@genomes) {
$temp = basename($_);
print OUT "genome $temp\ntrackDb $temp/trackDb.txt\n\n";
}
close OUT;
my $dir = dirname($toolfiles);
my $base = basename($toolfiles);
$toolfiles = join("",$dir,"/",$base,"/");
if(-d $toolfiles) {
my $fetchchromsizes = join("",$toolfiles,"fetchChromSizes.sh");
if(! -f $fetchchromsizes) {
$fetchChromSizes = join("",$toolfiles,"fetchChromSizes");
}
my $chromsizes = join("",$toolfiles,$genome,".chrom.sizes");
if(!-f $chromsizes) {
if(-f $fetchchromsizes) {
print("Fetching chromosome sizes for $genome from UCSC\n");
`${fetchchromsizes} ${genome} > ${chromsizes}`;
} else {
die "Tool \"fetchChromSizes\" not found: $toolfiles";
}
}
} else {
die "There is no directory $toolfiles";
}
my $bedtobigbedfile = join("",$toolfiles,"bedToBigBed");
if(-f $bedtobigbedfile) {
my $trackDbstring = "";
$dir = dirname($bedfiles);
$base = basename($bedfiles);
$bedfiles = join("",$dir,"/",$base,"/");
my $chromsizes = join("",$toolfiles,$genome,".chrom.sizes");
my @bedfiles = glob "$bedfiles*.bed";
for my $file (@bedfiles) {
my $newtempbed = join("",$file,".2");
my $result = `sort -k1,1 -k2,2n "${file}" > "${newtempbed}"`;
my $filename = basename($file);
$filename =~ s/\.bed//g;
my $shortfilename = substr($filename,0,17);
$filename = substr($filename,0,80);
my $bbfile = join("",$genomepath,"/",$filename,".bb");
print "Converting $file to bigBed format\n";
$result = `${bedtobigbedfile} "${newtempbed}" ${chromsizes} "${bbfile}"`;
$result = `rm "${newtempbed}"`;
my $trackname = $filename;
$trackname =~ tr/ //ds;
$trackDbstring = $trackDbstring . "track ".$trackname."\nbigDataUrl ".$filename.".bb\nshortLabel ".$shortfilename."\nlongLabel ".$filename."\ntype bigBed 12\nvisibility dense\nitemRgb on\n\n";
}
my $trackDbfile = join("",$genomepath,"/","trackDb.txt");
open(OUT, '>', $trackDbfile) or die "Could not open file $trackDbfile";
print OUT $trackDbstring;
close OUT;
} else {
die "Tool \"bedToBigBed\" not found: $bedtobigbedfile";
}
} else {
die "No path for track hub avaliable: $hubpath";
}