Added eval; site now shows clean dataset missing message instead of server error...
[sgn.git] / lib / SGN / Controller / AJAX / SimSearch.pm
blob73379bb33c5dda13d1bf21d7820b237dbcd2f98f
2 package SGN::Controller::AJAX::SimSearch;
4 use Moose;
5 use File::Temp "tempdir";
6 use File::Basename;
8 BEGIN { extends 'Catalyst::Controller::REST'; }
11 __PACKAGE__->config(
12 default => 'application/json',
13 stash_key => 'rest',
14 map => { 'application/json' => 'JSON' },
19 sub process_file :Path('/ajax/tools/simsearch/process_file') :Args(0) {
20 my $self = shift;
21 my $c = shift;
24 my $filename = $c->config->{basepath}."/".$c->config->{tempfiles_subdir}."/simsearch/".$c->req->param("filename");
25 my $fileurl = '/simsearch/'.$c->req->param("filename");
26 my $format = $c->req->param("format");
28 print STDERR "FORMAT = $format\n";
30 # if ($format eq "vcf") {
31 # print STDERR "Converting vcf to dosage...\n";
32 # system("perl ../gtsimsrch/src/vcf2dosage.pl < $filename > $filename.dosage");
33 # $filename = $filename.".dosage";
34 # }
38 print STDERR "READING FROM $filename\n";
39 my $reference_file = $c->req->param("reference_file");
41 my $reference_file_path = $c->config->{simsearch_datadir}."/".$reference_file;
42 # do not specify -r option when there is no reference file
44 my $ref_option = "";
45 if ($reference_file) {
46 $ref_option = " -ref $reference_file_path ";
49 # my $cmd = "../gtsimsrch/src/simsearch -i $filename $ref_option -o $filename.out";
51 my $cmd = "../gtsimsrch/src/duplicate_finder.pl -alt_marker_ids -nofull_cluster_output -max_distance 0.5 -in $filename $ref_option -output $filename.out -graphics GD -histogram_filename $filename.out_distances_histogram.png -histogram_path /home/production/cxgn/gtsimsrch/src/histogram.pl";
52 print STDERR "running command $cmd...\n";
53 system($cmd);
56 my $results;
57 open(my $F , "<", $filename.".out_clusters") || die "Can't open file $filename.out_clusters";
59 my @data;
60 my @line;
62 my $group =1;
64 print STDERR "Parsing output file...\n";
65 while(<$F>) {
66 print STDERR "Processing group $group...\n";
67 chomp;
68 if (/^#/) { next; }
69 @line = split " ";
70 my @members = @line[9..@line-1];
72 push @data, [ $group, $line[0], $line[3], $line[2], join("<br />", @members) ];
73 $group++;
75 close($F);
76 print STDERR "Done.\n";
78 # plot the agmr score distribution histogram using the 6th column in $filename.out
80 # (use gnuplot or R)
82 $c->stash->{rest} = { data => \@data,
83 histogram => '/documents/tempfiles/'.$fileurl.".out_distances_histogram.png" };