comparison scripts/generate_density_json.pl @ 0:28d1a6f8143f draft

planemo upload for repository https://github.com/portiahollyoak/Tools commit 132bb96bba8e7aed66a102ed93b7744f36d10d37-dirty
author portiahollyoak
date Mon, 25 Apr 2016 13:08:56 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:28d1a6f8143f
1 #! /usr/bin/perl
2
3 use strict;
4 die "perl $0 <input.insertion.bp.summary> <chromInfo file> <genomic bin size>\n" if @ARGV<2;
5
6 my @colors=("blue","green","red","yellow","grey","orange","purple","brown", "black");
7
8 my $op_title=$ARGV[0];
9 $op_title =~ s/summary/json/;
10
11 my %chrs=();
12 system("cut -f1 $ARGV[0] | uniq > chr");
13 open (input, "<chr") or die "Can't open chr since $!\n";
14 while (my $line=<input>) {
15 chomp($line);
16 $chrs{$line}=1;
17 }
18 close input;
19 system("rm chr");
20
21 open (output, ">>$op_title") or die "Can't open $op_title since $!\n";
22 print output "{\"ideograms\":[\n";
23
24 my $i=0;
25 open (input, "<$ARGV[1]") or die "Can't open $ARGV[1] since $!\n";
26 while (my $line=<input>) {
27 chomp($line);
28 my @a=split(/\t/, $line);
29 if ($chrs{$a[0]}==1) {
30 my $len=int($a[1]/$ARGV[2])+1;
31 if ($len < 5) {
32 $chrs{$a[0]}=0;
33 next;
34 }
35 if ($i > 0) {print output ",\n";}
36 print output "{\"id\":\"$a[0]\",\"length\":$len,\"color\":\"$colors[$i % 9]\"}";
37 $i++;
38 }
39 }
40 close input;
41
42 print output "\n],\n\"tracks\":[\n{\n";
43 print output "\"name\": \"Density\",\n";
44 print output "\"type\": \"plot\",\n";
45 print output "\"values\":\n[\n";
46
47 my @hist=();
48 my $last_chr="";
49 my $i=0;
50 my $k=0;
51 open (input, "<$ARGV[0]") or die "Can't open $ARGV[0] since $!\n";
52 #my $header=<input>;
53 while (my $line=<input>) {
54 chomp($line);
55 my @a=split(/\t/, $line);
56 if ($a[0] eq $last_chr) {
57 my $mid=int(($a[1]+$a[2])/2);
58 if (int($mid/$ARGV[2]) > $i) {
59 $i++;
60 $hist[$i]=1;
61 }
62 else {$hist[$i]++;}
63 }
64 else {
65 if (($last_chr ne "") && ($chrs{$last_chr} == 1)) {
66 if ($k > 0) {print output ",\n";}
67 print output "{\"color\":\"$colors[$k % 9]\",\"chr\":\"$last_chr\",\"values\":[";
68 for my $j (0..$i-1) {print output "$hist[$j],";}
69 print output "$hist[$i]]}";
70 $k++;
71 }
72 $i=0;
73 $hist[0]=1;
74 $last_chr=$a[0];
75 }
76 }
77 close input;
78
79 print output "\n]}\n]\n}\n";
80 close output;