Mercurial > repos > dereeper > haplophyle
comparison Haplophyle.pl @ 0:6f11162b6fa2 draft
planemo upload commit 11382afe87364aaafb19973470d5066229a6e34f
author | dereeper |
---|---|
date | Tue, 14 Aug 2018 08:04:23 -0400 |
parents | |
children | 8e248dc6ad56 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6f11162b6fa2 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 use strict; | |
4 use Getopt::Long; | |
5 use Bio::SeqIO; | |
6 | |
7 use Cwd; | |
8 my $dir = getcwd; | |
9 | |
10 | |
11 my $usage = qq~Usage:$0 <args> [<opts>] | |
12 where <args> are: | |
13 -i, --input <input> | |
14 -o, --output <output> | |
15 -d, --dotfile <dotfile> | |
16 <opts> are: | |
17 -g, --groups <groupfile> | |
18 -s, --stats <statfile> | |
19 -t, --tool_path <tool_path> | |
20 ~; | |
21 $usage .= "\n"; | |
22 | |
23 my ($infile,$output,$outfile,$groupfile,$statfile,$tool_path); | |
24 | |
25 | |
26 GetOptions( | |
27 "input=s" => \$infile, | |
28 "output=s" => \$output, | |
29 "dot=s" => \$outfile, | |
30 "groups=s" => \$groupfile, | |
31 "stats=s" => \$statfile, | |
32 "tool_path=s"=> \$tool_path | |
33 ); | |
34 | |
35 | |
36 die $usage | |
37 if ( !$infile); | |
38 | |
39 | |
40 my $HAPLOPHYLE_EXE = "java -Xmx2048m -jar NetworkCreator_fat.jar"; | |
41 if ($tool_path){ | |
42 $HAPLOPHYLE_EXE = "java -Xmx2048m -jar $tool_path/NetworkCreator_fat.jar"; | |
43 } | |
44 | |
45 | |
46 my $out_png = "network.png"; | |
47 | |
48 my $command = "$HAPLOPHYLE_EXE -in $infile -out $outfile"; | |
49 system($command); | |
50 | |
51 | |
52 | |
53 my %groups; | |
54 my %groups2; | |
55 my %hash; | |
56 my %haplosize; | |
57 if ($groupfile && $statfile){ | |
58 open(G,$groupfile); | |
59 while(<G>){ | |
60 my $line = $_;$line=~s/\n//g;$line=~s/\r//g; | |
61 my ($ind,$group) = split(";",$line); | |
62 if ($group =~/\w+/ && $ind=~/\w+/){ | |
63 $groups{$group}.=$ind.","; | |
64 $groups2{$ind} = $group; | |
65 } | |
66 } | |
67 close(G); | |
68 | |
69 open(S,$statfile); | |
70 while(<S>){ | |
71 if (/^(haplo\d+):(\d+):(.*)/){ | |
72 my $haplo_num = $1; | |
73 my $nb_haplo = $2; | |
74 my $inds = $3; | |
75 my @inds = split(",",$3); | |
76 foreach my $ind(@inds){ | |
77 my ($indname,$rank) = split("_",$ind); | |
78 my $group = $groups2{$indname}; | |
79 $hash{$haplo_num}{$group}++; | |
80 $haplosize{$haplo_num}++; | |
81 } | |
82 } | |
83 } | |
84 close(S); | |
85 } | |
86 | |
87 my $nb_groups = scalar keys(%groups); | |
88 my @colors = ("#ed292a","#ed292a","#82ABA0","#2255a6","#6ebe43","#e76599","#662e91","#c180ff","#ea8b2f","#fff100","#666666","#01ffff","#bfbfbf","#2ac966","#666666"); | |
89 my $pie_block = ""; | |
90 my %correspondence_groups; | |
91 my $i = 0; | |
92 foreach my $group(keys(%groups)){ | |
93 $i++; | |
94 $correspondence_groups{$i} = $group; | |
95 $pie_block .= "'pie-$i-background-color': '$colors[$i]',\n"; | |
96 $pie_block .= "'pie-$i-background-size': 'mapData(group$i, 0, 10, 0, 100)',\n"; | |
97 } | |
98 open(JSON_CYTOSCAPE,">$output"); | |
99 my $json = "{\"elements\": {\"nodes\": ["; | |
100 my $done = 0; | |
101 open(OUTFILE,"$outfile"); | |
102 while(<OUTFILE>){ | |
103 if (/(^\w+)\s\[.*width=([\d\.]+),/){ | |
104 my $node = $1; | |
105 my $size = $2; | |
106 my $ref_hash = $hash{$node}; | |
107 if ($ref_hash){ | |
108 my %hash2 = %$ref_hash; | |
109 my $s = scalar keys(%hash2); | |
110 $json.= "{ \"data\": { \"id\": \"$node\", \"width\": $size"; | |
111 for (my $i = 1; $i <= $nb_groups; $i++){ | |
112 my $group = $correspondence_groups{$i}; | |
113 my $ratio = 0; | |
114 if ($haplosize{$node} > 0 && $hash{$node}{$group} > 0){ | |
115 $ratio = ($hash{$node}{$group}/$haplosize{$node}) * 10; | |
116 } | |
117 $json .= ", group$i: $ratio"; | |
118 } | |
119 $json.= " } },\n"; | |
120 } | |
121 else{ | |
122 $json.= "{ \"data\": { \"id\": \"$node\", \"width\": $size} },\n"; | |
123 } | |
124 } | |
125 if (/(\w+) -- (\w+)/){ | |
126 if ($done == 0){ | |
127 $done = 1; | |
128 chop($json); | |
129 chop($json); | |
130 $json .= "],\n"; | |
131 $json .= "\"edges\": [\n"; | |
132 } | |
133 $done = 1; | |
134 $json.= "{ \"data\": { \"id\": \"$1$2\", \"weight\": 1, \"source\": \"$1\", \"target\": \"$2\"} },\n"; | |
135 } | |
136 } | |
137 chop($json); | |
138 chop($json); | |
139 $json.="]}}"; | |
140 print JSON_CYTOSCAPE $json; | |
141 close(JSON_CYTOSCAPE); | |
142 |