forked from johnomics/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenome_kb_plot.pl
executable file
·166 lines (144 loc) · 4.84 KB
/
genome_kb_plot.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
#!/usr/bin/env perl
# genome_kb_plot.pl
# Make Kumar-Blaxter plots for multiple FASTA files
# -o Output PDF name
# -w Width of final PDF figure (default 10)
# -h Height of final PDF figure (default 10)
# List of files to process
# John Davey
# Updated Friday 20 March 2015
use strict;
use warnings;
use Carp;
use English;
use Getopt::Long;
use File::Basename 'fileparse';
use Data::Dumper;
use POSIX qw /ceil/;
my $output = "output";
my $width = 10;
my $height = 10;
my $ysize = 0;
my $xsize = 0;
my $config = "";
my $facet = '';
my $eps = '';
my $options_okay = GetOptions(
'output=s' => \$output,
'width=f' => \$width,
'height=f' => \$height,
'ysize=i' => \$ysize,
'xsize=i' => \$xsize,
'config=s' => \$config,
'facet' => \$facet,
'eps' => \$eps,
);
sub get_genome_files {
my ($config) = @_;
open my $configfh, '<', $config or croak "Can't open config file!\n";
my %genomefiles;
my @genomenames;
my @genomecolours;
while (my $configln = <$configfh>) {
chomp $configln;
my ($name, $file, $colour) = split "\t", $configln;
push @genomenames, $name;
push @genomecolours, $colour if $colour;
$genomefiles{$file}{name} = $name;
$genomefiles{$file}{colour} = $colour if $colour;
}
close $configfh;
return \@genomenames, \@genomecolours, \%genomefiles;
}
my %genomefiles;
my @genomenames;
my @genomecolours;
if ($config) {
my ($genomenamesref, $genomecoloursref, $genomefileref) = get_genome_files($config);
%genomefiles = %{$genomefileref};
@genomenames = @{$genomenamesref};
@genomecolours = @{$genomecoloursref};
}
else {
for my $file (@ARGV) {
$genomefiles{$file}{colour} = 0;
my ($filename, $dirs, $suffix) = fileparse ($file, qr/\.[^.]*/ );
$genomefiles{$file}{name} = $filename;
@genomenames = sort keys %genomefiles;
}
}
my %gls;
my %gsizes;
for my $genomefile (sort keys %genomefiles) {
print "$genomefile\n";
open my $genome, "<", $genomefile or die "Can't open $genomefile\n";
my $length = 0;
my $genomesize = 0;
my $scaffolds = 0;
while ( my $gl = <$genome> ) {
if ( $gl =~ /^>/ ) {
if ( $length > 0 ) {
push @{ $gls{$genomefile} }, $length;
$genomesize += $length;
$length = 0;
$scaffolds++;
}
}
else {
chomp $gl;
$length += length $gl;
}
}
push @{ $gls{$genomefile} }, $length if $length > 0;
$genomesize += $length;
$ysize = $genomesize if $genomesize > $ysize;
$xsize = $scaffolds if $scaffolds > $xsize;
close $genome;
$gsizes{$genomefile} = $genomesize;
@{ $gls{$genomefile} } = sort { $b <=> $a } @{ $gls{$genomefile} };
}
open my $tsv, '>', "$output.tsv" or croak "Can't open output TSV file $output.tsv! $OS_ERROR\n";
print $tsv "Genome\tScaffold\tGenomeLength\n";
for my $gf (sort keys %genomefiles) {
my $cumsum = 0;
my @milestones = (50, 90, 95);
for my $i ( 0 .. $#{ $gls{$gf} } ) {
$cumsum += $gls{$gf}[$i];
if (@milestones and $cumsum > $gsizes{$gf} * ($milestones[0]/100)) {
print "$gf\tN$milestones[0]=". ($i+1) . "\tL$milestones[0]=$gls{$gf}[$i]\n";
shift @milestones
}
print $tsv $genomefiles{$gf}{name} . "\t$i\t$cumsum\n";
}
}
close $tsv;
open my $rscript, '>', "$output.R" or croak "Can't open output R script $output.R! $OS_ERROR\n";
print $rscript "library(ggplot2)\n";
print $rscript "library(scales)\n";
print $rscript "genomecols<-c(" . join(",", @genomecolours) . ")\n" if @genomecolours;
print $rscript "kb.df<-read.delim(\"$output.tsv\")\n";
print $rscript "kb.df\$Genome<-factor(kb.df\$Genome, levels=c(\"". join("\",\"", @genomenames). "\"))\n";
if ($eps) {
print $rscript "postscript(\"$output.eps\", paper=\"special\", width=$width, heigh=$height, horizontal=FALSE)\n";
}
else {
print $rscript "pdf(\"$output.pdf\", width=$width, height=$height)\n";
}
my $ymultiple = 1 . 0 x (length($ysize) - 1);
my $ymax = ceil($ysize/$ymultiple) * $ymultiple;
if ($facet) {
print $rscript "ggplot(kb.df, aes(Scaffold, GenomeLength)) + geom_line() + facet_wrap(~Genome)";
}
else {
print $rscript "ggplot(kb.df, aes(Scaffold, GenomeLength, colour=Genome)) + geom_line()";
}
print $rscript " + scale_x_continuous(limits=c(0, $xsize)) + scale_y_continuous(limits=c(0, $ymax),breaks=seq(0,$ymax,$ymultiple),labels=seq(0,$ymax,$ymultiple)/1000000)";
print $rscript "+theme_bw(base_size = 10)+xlab(\"Number of scaffolds, ranked in order of size\")+ylab(\"Cumulative genome length (Mb)\")";
print $rscript "+scale_colour_manual(values=genomecols)" if @genomecolours;
print $rscript "\n";
print $rscript "dev.off()\n";
close $rscript;
system("Rscript $output.R");
unlink "$output.tsv";
unlink "$output.R";