Merge pull request #5069 from solgenomics/topic/accession_upload_file
[sgn.git] / cgi-bin / tools / vector / index.pl
blobbcacbf72c513755658a5f58fdba51692aaa6c073
2 use strict;
3 use warnings;
5 use File::Temp qw/tempfile/;
6 use File::Basename;
7 use File::Spec;
8 use File::Slurp qw/slurp/;
10 use Bio::Seq;
11 use Bio::Restriction::Analysis;
13 use CXGN::Page;
14 use CXGN::Page::FormattingHelpers qw | info_section_html html_break_string |;
15 use CXGN::Cview::MapImage;
16 use CXGN::Cview::Chromosome::Vector;
17 use CXGN::Cview::Marker::VectorFeature;
18 use CXGN::Tools::WebImageCache;
19 use CXGN::Cview::VectorViewer;
20 use CatalystX::GlobalContext '$c';
21 ###############################################################################
23 my $p = CXGN::Page->new("Vector drawing application", "VeDraw");
24 my ($action, $format, $name, $insert_sequence, $insert_coord, $show_re, $del_start ,$del_end, $native_file, $edit_commands) = $p->get_arguments("action", "format", "name" , "insert_sequence", "insert_coord", "show_re", "del_start", "del_end", "native_file", "edit_commands");
26 ###############################################################################
29 our $native_commands;
31 #getting the file and url
32 our $tempfiles_subdir_rel = File::Spec->catdir($c->config->{'tempfiles_subdir'},'cview'); #path relative to website root dir
33 our $tempfiles_subdir_abs = File::Spec->catdir($c->config->{'basepath'},$tempfiles_subdir_rel); #absolute path
35 if ($native_file){
37 my $native_file_dir = File::Spec->catfile($tempfiles_subdir_abs, $native_file);
38 my $native_file_url = File::Spec->catfile($tempfiles_subdir_rel, $native_file);
40 $native_commands = slurp ($native_file_dir);
43 if (!$action) {
44 display_form($p);
45 exit();
48 my $upload;
49 my $data;
50 my $size;
51 my $s;
52 my $seq;
53 my $length;
54 my @features;
55 my @commands = ();
57 my @input_errors = (); #to collect validation errors
59 my $image_width = 800;
60 my $image_height = 600;
61 my $vv = CXGN::Cview::VectorViewer->new($name? $name : "vector", $image_width, $image_height);
63 if ($action eq "upload") {
65 $upload = $p->get_upload();
66 if (!$upload) { die "NO UPLOAD OBJ"; }
67 else {
68 my $size = $upload->slurp($data);
69 my $fh = $upload->fh;
71 if ($format eq "genbank") {
72 $vv->parse_genbank($fh);
74 elsif ($format eq "native") {
75 my @lines = split /\n/, $data;
76 $vv->parse_native(@lines);
83 elsif ($action eq "draw_native") {
84 my @lines = split /\n/ms, $edit_commands;
85 $vv->parse_native(@lines);
87 ###############################################################################
88 if ($action eq "insert") {
90 my @lines = split /\n/ms, $native_commands;
91 $vv->parse_native(@lines);
92 $seq = $vv->get_sequence;
94 chomp($seq);
95 my $preceding_seq = substr($seq, 0, $insert_coord);
96 my $following_seq = substr($seq, $insert_coord +1, length($seq));
98 $seq = $preceding_seq . $insert_sequence . $following_seq;
100 $vv->set_sequence($seq);
101 @commands = @{$vv->get_commands_ref};
102 foreach my $c (@commands) {
103 if ($c->[0] eq "SEQUENCE") {
104 $c->[1]=$seq;
107 $vv->set_commands_ref( \@commands);
108 #$vv->parse_native();
111 ###############################################################################
112 if ($action eq "delete") {
113 my @lines = split /\n/ms, $native_commands;
114 $vv->parse_native(@lines);
115 $seq = $vv->get_sequence;
117 chomp($seq);
118 my $seq_before = substr ($seq, 0 , $del_start);
119 my $seq_after = substr ($seq , $del_end , length($seq));
120 $seq = $seq_before . $seq_after;
121 $vv->set_sequence($seq);
122 @commands = @{$vv->get_commands_ref};
123 foreach my $c (@commands) {
124 if ($c->[0] eq "SEQUENCE") {
125 $c->[1]=$seq;
128 $vv->set_commands_ref( \@commands);
131 ###############################################################################
132 if ($action ne "draw_native") {
134 $vv->restriction_analysis($show_re);
136 ###############################################################################
138 my $image_html = $vv->generate_image();
139 my $commands_ref = $vv->get_commands_ref();
140 $seq = $vv->get_sequence();
142 ################################################################################
143 #making temp file for the native format
144 my $native_format_tempdir = File::Spec->catdir($c->config->{'basepath'},
145 $c->config->{'tempfiles_subdir'},
146 "cview",
148 my ($fh ,$native_tmp) = tempfile( DIR => $native_format_tempdir, TEMPLATE=>"vector_XXXXXX");
150 foreach my $c (@$commands_ref) {
151 print $fh join ", ", @$c;
152 print $fh "\n";
155 my $base_temp = basename ($native_tmp);
160 ################################################################################
161 display_results($p, $image_html, $seq, $commands_ref, $show_re, $base_temp);
162 ###############################################################################
164 sub display_form {
165 my $p = shift;
167 # display form
169 $p->header("Vector drawing application", "Vector drawing application");
171 print <<HTML;
173 <form enctype="multipart/form-data" method="post" >
174 Vector name <input type="text" name="name" value="" size="8" /><br /><br />
175 Upload a genbank file <input type="file" name="genbank_record" /><br /><br />
176 <b>Format:</b><br />
177 <input type="radio" name="format" value="genbank" checked="1" /> Genbank<br />
178 <input type="radio" name="format" value="native" /> Native<br /><br />
179 <b>Show restriction sites</b><br />
180 <input type="radio" name="show_re" value="all" /> all (not recommended)<br />
181 <input type="radio" name="show_re" value="unique" /> unique cutters<br />
182 <input type="radio" name="show_re" value="popular6bp" /> popular 6bp cutters<br />
183 <input type="radio" name="show_re" value="popular4bp" /> popular 4bp cutters<br />
184 <br /><br />
185 <input type="submit" value="upload" />
186 <input type="hidden" name="action" value="upload" />
187 </form>
190 HTML
192 $p->footer();
196 sub display_results {
198 my $p = shift;
199 my $image_html = shift;
200 my $seq = shift;
201 my $commands = shift;
202 my $show_re = shift;
203 my $native_file =shift;
204 $p->header("Vector Drawing", "Vector Drawing");
206 print <<HTML;
209 <center>
210 $image_html
211 </center>
213 HTML
215 my $html = qq{
217 <form method="post" >
218 <input type="hidden" name="format" value="native" />
219 <input type="hidden" name="action" value="draw_native" />
220 <center>
221 <textarea name="edit_commands" rows="20" cols="80">};
223 $native_commands = "";
224 foreach my $c (@$commands) {
225 $native_commands .= join ", ", @$c;
226 $native_commands .= "\n";
229 $html .= $native_commands;
230 $html .= qq { </textarea><br /><input type="submit" /></center></form> };
232 my $edit_native = info_section_html( title=> "Edit", contents=>$html, collapsible=>1, collapsed=>1);
234 print $edit_native;
237 my $insert_html = <<HTML;
238 <form method="post">
239 Insert Sequence <br /><br /><textarea name="insert_sequence" value="" rows = "4" cols="80" /></textarea><br />
240 Insert at position <input type="text" name="insert_coord" value="0" size="4" /><br />
241 <input type="hidden" name="action" value="insert" />
243 <input type="hidden" name="native_file" value="$native_file" />
244 <input type="hidden" name="show_re" value="$show_re" />
245 <input type="submit" />
246 </form>
248 HTML
250 print info_section_html( title=>"Insert sequence", contents=>$insert_html, collapsible=>1, collapsed=>1);
252 my $delete_html = <<HTML;
253 <form method="post">
254 Delete Sequence<br /><br />
255 From position <input type="text" name="del_start" />
256 to position <input type="text" name="del_end" />
257 <input type="hidden" name="action" value="delete" />
258 <input type="hidden" name="native_file" value="$native_file" />
259 <input type="hidden" name="show_re" value="$show_re" />
260 <input type="submit" />
261 HTML
263 print info_section_html (title=>"Delete sequence", contents=>$delete_html, collapsible=>1, collapsed=>1);
265 my $split_seq = html_break_string($seq, 100, "\n");
266 my $sequence_html = qq { <pre><div id="sequence_display">$split_seq</div></pre> };
268 print info_section_html( title=>"View sequence", contents=>$sequence_html, collapsible=>1, collapsed=>1);
270 $p->footer();