blib/lib/PRIMERVIEW.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 #
2 # module for PrimerView
3 #
4 # uses the software primer3
5 # https://sourceforge.net/projects/primer3/
6 # by Steve Rozen et al.
7 # and the MUSCLE alignment tool
8 # http://www.drive5.com/muscle/
9 # by Robert Edgar
10 # and the Bio::Graphics modules within Bioperl
11 # by Lincoln Stein
12 # and by Ewan Birney
13 #
14 # Copyright Damien O'Halloran
15 #
16 # You may distribute this module under the same terms as perl itself
17 # History
18 # initial release 2015, new release 2017
19 # POD documentation - main docs before the code
20  
21 =head1 NAME
22  
23 PrimerView - generates graphical outputs that map the position and distribution of primers to the target sequence
24
25 =head1 SYNOPSIS
26  
27 use strict;
28 use warnings;
29 use PRIMERVIEW;
30  
31 my $in_file = "test_seqs.fasta";
32 my $tmp = PRIMERVIEW->new();
33  
34 $tmp->load_selections(
35 in_file => $in_file,
36 single_view => "1",
37 batch_view => "1",
38 clean_up => "1"
39 );
40 $tmp->run_primerview();
41  
42 =head1 DESCRIPTION
43  
44 designs forward and reverse primers from multi-sequence datasets, and generates graphical outputs that map the position and distribution of primers to the target sequence
45  
46 =head1 FEEDBACK
47  
48 damienoh@gwu.edu
49  
50 =head2 Mailing Lists
51  
52 User feedback is an integral part of the evolution of this module. Send your comments and suggestions preferably to one of the mailing lists. Your participation is much appreciated.
53  
54 =head2 Support
55  
56 Please direct usage questions or support issues to:
57  
58 <damienoh@gwu.edu>
59  
60 Please include a thorough description of the problem with code and data examples if at all possible.
61  
62 =head2 Reporting Bugs
63  
64 Report bugs to the GitHub bug tracking system to help keep track of the bugs and their resolution. Bug reports can be submitted via the GitHub page:
65 https://github.com/dohalloran/PrimerView/issues
66  
67 =head1 AUTHORS - Damien OHalloran
68  
69 Email: damienoh@gwu.edu
70 =head1 APPENDIX
71  
72 The rest of the documentation details each of the object methods
73  
74 =cut
75  
76 # Let the code begin...
77  
78 package PRIMERVIEW;
79  
80 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 1 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use Bio::AlignIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use Bio::Tools::Run::Alignment::Muscle;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 0 ▶use Bio::Align::Graphics;
Subroutine BEGIN: called CC=1 SCAR=2.6
89 use Bio::SeqFeature::Generic;
90 use Bio::Graphics;
91  
92 #########################
93  
94 our $VERSION = '3.0';
95  
96 #########################
97  
98 =head2 new()
99  
100 Title : new()
101 Usage : my $tmp = PRIMERVIEW->new()
102 Function: constructor routine
103 Returns : a blessed object
104 Args : none
105
106 =cut
107  
108 ##################################
109  
110 sub new {
111 my $class = shift;
112 my $self = {};
113 bless $self, $class;
114  
115 return $self;
116 }
117  
118 ##################################
119  
120 =head2 load_selections()
121  
122 Title : load_selections()
123 Usage : $tmp->load_selections(
124 in_file => $in_file,
125 single_view => "1",
126 batch_view => "1",
127 clean_up => "1"
128 )
129 Function: Populates the user data into $self hash
130 Returns : nothing returned
131 Args :
132 -in_file, the name of the file containing the sequences in fasta format
133 -single_view, return a single graphical file depicting the primer mapped to the sequence for every primer 1=Yes, 0=No
134 -batch_view, return a single graphical file depicting all primers mapped to the sequence for each sequence 1=Yes, 0=No
135 -cleanup, option to delete tmp file: 1=Yes, 0=No
136
137 =cut
138  
139 ##################################
140  
141 sub load_selections {
142 my ( $self, %arg ) = @_;
143  
144 if ( defined $arg{in_file} ) {
145 $self->{in_file} = $arg{in_file};
146 }
147 if ( defined $arg{single_view} ) {
148 $self->{single_view} = $arg{single_view};
149 }
150 if ( defined $arg{batch_view} ) {
151 $self->{batch_view} = $arg{batch_view};
152 }
153 if ( defined $arg{clean_up} ) {
154 $self->{clean_up} = $arg{clean_up};
155 }
156 }
157 ###############################3
158  
159 #define global variables
160 my $len_seq;
161 my $out_image;
162 my $outputfile;
163 my $id;
164 my $id_uniq;
165 my $len_uniq;
166 my @read_len;
167 my @array_length;
168 my @array_name;
169  
170 ##################################
171  
172 =head2 run_primerview()
173  
174 Title : run_primerview()
175 Usage : $tmp->run_primerview()
176 Function: parses input and executes commands based on %arg
177 Returns : graphical files based on user selections
178 Args : none provided
179
180 =cut
181  
182 ##################################
183  
184 sub run_primerview {
185 my ( $self, %arg ) = @_;
186  
187 my $fasta = $self->{in_file};
188  
189 #parse the input file into arrays of sequence and id
190 my ( $seq_file, $id_file ) = parser($fasta);
191 my $specificty = join( ",", @$seq_file );
192  
193 #input each sequence into primerview()
194 foreach my $sequence (@$seq_file) {
195 my $len_seq = length($sequence);
196 my $shift_id = shift(@$id_file);
197 $shift_id =~ tr/a-zA-Z0-9//cd;
198 my $id_uniq = $len_seq . "'" . $shift_id;
199 my $len_uniq = $shift_id . "'" . $len_seq;
200 my $new_sequence = "SEQUENCE_TEMPLATE=" . $sequence . "\n" . "=";
201 my $temp_seq = "primer3_temp.txt";
202  
203 open my $fh, ">", $temp_seq or die;
204 print $fh($new_sequence);
205  
206 _primerview(
207 $fasta, $temp_seq, $len_seq, $shift_id,
208 $sequence, $id_uniq, $len_uniq
209 );
210  
211 if ( $self->{single_view} eq "1" ) {
212 $self->_align_muscle(%arg);
213 $self->_align_convert(%arg);
214 $self->_graphics(%arg);
215 }
216 if ( $self->{batch_view} eq "1" ) {
217 $self->_graphics_all_primers(%arg);
218 }
219  
220 }
221 $self->clean_up(%arg);
222 }
223  
224 ##################################
225  
226 =head2 parser()
227  
228 Title : parser()
229 Usage : parser($input_file)
230 Function: parses the contents of the fasta file into ids and sequences
231 Returns : send back a reference to the arrays containing seqs and ids
232 Args : fasta file
233
234 =cut
235  
236 ##################################
237  
238 sub parser {
239 my $fasta = shift;
240  
241 my @sequence;
242 my @id;
243  
244 #create an instance of a Seqio object
245 my $seqio = Bio::SeqIO->new(
246 -file => $fasta,
247 -format => "fasta",
248 );
249  
250 while ( my $seqobj = $seqio->next_seq() ) {
251 my $seqStream = $seqobj->seq();
252 my $idStream = $seqobj->id();
253 push @sequence, $seqStream;
254 push @id, $idStream;
255 }
256  
257 #send back a reference to the arrays containing seqs and ids
258 return ( \@sequence, \@id );
259  
260 }
261  
262 ##################################
263  
264 =head2 _primerview()
265  
266 Title : _primerview()
267 Usage : _primerview(
268 $fasta, $temp_seq, $len_seq, $shift_id,
269 $sequence, $id_uniq, $len_uniq
270 )
271 Function: runs and parses the primer3 side of things
272 Returns : primer feature files
273 Args : $fasta, $temp_seq, $len_seq, $shift_id, $sequence, $id_uniq, $len_uniq
274
275 =cut
276  
277 ##################################
278 sub _primerview {
279  
280 #collect GetOpts, sequences, and IDs
281 my $fasta = shift;
282 my $sequence = shift;
283 $len_seq = shift;
284 $id = shift;
285 my $read = shift;
286 $id_uniq = shift;
287 $len_uniq = shift;
288  
289 push @read_len, length($read);
290  
291 $id =~ tr/a-zA-Z0-9//cd;
292  
293 $outputfile = $fasta . "_primers.txt";
294  
295 #declare output file
296 $out_image = "GRAPHIC_$id.txt";
297  
298 my $p3 = $id . "_p3_temp.txt";
299  
300 my $command = "primer3_core -format_output < $sequence >> $p3";
301 system($command);
302  
303 open my $fh, "<", $p3 or die;
304 open my $fh_out, ">>", $out_image or die;
305 my $count = 0;
306 while ( my $row = <$fh> ) {
307  
308 #print $row;
309 if ( $row =~
310 /LEFT PRIMER\s+(\d*)\s+(\d+)\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s(.*)\n/
311 )
312 {
313 $count++;
314 push @array_length, $len_uniq;
315 push @array_name, $id_uniq;
316  
317 my $temp_fa = $id . "_" . $count . "_temp.fa";
318 open my $fh2, ">>", $temp_fa or die;
319 print $fh2( ">"
320 . $id . "\n"
321 . $read . "\n" . ">"
322 . $count . "\n"
323 . $3
324 . "\n" );
325 my $add = $1 + $2;
326 print $fh_out( $1 . "\t" . $2 . "\t" . $1 . "\t" . $add . "\n" );
327 }
328 elsif ( $row =~
329 /RIGHT PRIMER\s+(\d*)\s+(\d+)\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s+\d*\.\d+\s(.*)\n/
330 )
331 {
332 $count++;
333 push @array_length, $len_uniq;
334 push @array_name, $id_uniq;
335  
336 my $temp_fa = $id . "_" . $count . "_temp.fa";
337 open my $fh2, ">>", $temp_fa or die;
338 my $minus = $1 - $2;
339 print $fh2( ">"
340 . $id . "\n"
341 . $read . "\n" . ">"
342 . $count . "\n"
343 . $3
344 . "\n" );
345 print $fh_out( $1 . "\t" . $2 . "\t" . $1 . "\t" . $minus . "\n" );
346 }
347 }
348  
349 }
350  
351 ##################################
352  
353 =head2 _align_muscle()
354  
355 Title : _align_muscle()
356 Usage : _align_muscle()
357 Function: generates an alignment using MUSCLE of the primer and sequence
358 Returns : alignment
359 Args : none (globs the files)
360
361 =cut
362  
363 ##################################
364 sub _align_muscle {
365  
366 my $dir = cwd();
367  
368 foreach my $fp ( glob("$dir/*.fa") ) {
369 open my $fh, "<", $fp or die;
370  
371 my @params = ( 'IN' => "$fp", 'OUT' => "$fp.fasta", 'MAXITERS' => 1 );
372  
373 my $factory = Bio::Tools::Run::Alignment::Muscle->new(@params);
374  
375 my $aln = $factory->align( my $inputfilename );
376  
377 }
378  
379 }
380  
381 ##################################
382  
383 =head2 _align_convert()
384  
385 Title : _align_convert()
386 Usage : _align_convert()
387 Function: converts alignment from aln to clustalw
388 Returns : clustalw alignment
389 Args : none (globs the files)
390
391 =cut
392  
393 ##################################
394  
395 sub _align_convert {
396  
397 my $dir = cwd();
398  
399 foreach my $fp ( glob("$dir/*.fasta") ) {
400 open my $fh, "<", $fp or die;
401  
402 my $in = Bio::AlignIO->new(
403 -file => "$fp",
404 -format => 'fasta'
405 );
406 my $out = Bio::AlignIO->new(
407 -file => ">$fp.aln",
408 -format => 'clustalw'
409 );
410  
411 while ( my $aln = $in->next_aln ) {
412 $out->write_aln($aln);
413 }
414  
415 }
416  
417 }
418  
419 ##################################
420  
421 =head2 _graphics()
422  
423 Title : _graphics()
424 Usage : _graphics()
425 Function: converts each clustal alignment to a graphical output
426 Returns : jpeg
427 Args : none (globs the files)
428
429 =cut
430  
431 ##################################
432  
433 sub _graphics {
434  
435 my $dir = cwd();
436  
437 foreach my $fp ( glob("$dir/*.fa.fasta.aln") ) {
438 open my $fh, "<", $fp or die;
439  
440 my $output = "$fp.jpeg";
441  
442 #Create an AlignI object using AlignIO
443 my $in = new Bio::AlignIO( -file => $fp, -format => 'clustalw' );
444  
445 #Read the alignment
446 my $aln = $in->next_aln();
447  
448 my $print_align = new Bio::Align::Graphics(
449 align => $aln,
450 output => $output,
451 font => 5,
452 x_label => "true",
453 y_label => "true",
454 bg_color => "white",
455 font_color => "black",
456 x_label_color => "red",
457 y_label_color => "blue",
458 pad_top => 5,
459 pad_bottom => 5,
460 pad_left => 5,
461 pad_right => 5,
462 x_label_space => 1,
463 y_label_space => 1,
464 reference_id => "First sequence supplied in alignment",
465 block_size => 20,
466 block_space => 2,
467 show_nonsynonymous => 0,
468 out_format => "jpeg"
469 );
470  
471 $print_align->draw();
472  
473 }
474  
475 }
476  
477 ##################################
478  
479 =head2 _graphics_all_primers()
480  
481 Title : _graphics_all_primers()
482 Usage : _graphics_all_primers()
483 Function: converts each feature file to a single graphical output
484 Returns : png
485 Args : none (globs the files)
486
487 =cut
488  
489 ##################################
490 sub _graphics_all_primers {
491  
492 my $dir = cwd();
493 my $orientation;
494 my @gene_length;
495 my @gene_id;
496 my @unique_length = uniq(@array_length);
497 my @unique_name = uniq(@array_name);
498 my %hash;
499 my $len;
500 my $name_id;
501  
502 foreach my $unique_name (@unique_name) {
503 if ( $unique_name =~ m/^\d+'(.+)?/i ) {
504 push @gene_id, $1;
505 }
506 }
507  
508 foreach my $unique_length (@unique_length) {
509 if ( $unique_length =~ m/^.+'(\d+)?/i ) {
510 push @gene_length, $1;
511 }
512 }
513  
514 foreach my $fp ( glob("$dir/GRAPHIC_*.txt") ) {
515 open my $fh, "<", $fp or die;
516  
517 @hash{@gene_id} = @gene_length;
518 while ( my ( $key, $val ) = each %hash ) {
519 $len = $val if $fp =~ m/$key/i;
520 }
521 while ( my ( $key, $val ) = each %hash ) {
522 $name_id = $key if $fp =~ m/$key/i;
523 }
524  
525 my $outputfile = "$fp.png";
526 open( OUTFILE, ">$outputfile" ) or die;
527  
528 my $panel = Bio::Graphics::Panel->new(
529 -length => $len,
530 -width => 800,
531 -pad_left => 30,
532 -pad_right => 30,
533 -pad_top => 20,
534 -pad_bottom => 20,
535 );
536  
537 my $full_length = Bio::SeqFeature::Generic->new(
538 -start => 1,
539 -end => $len,
540 -display_name => $name_id,
541 );
542  
543 $panel->add_track(
544 $full_length,
545 -glyph => 'arrow',
546 -tick => 2,
547 -fgcolor => 'black',
548 -double => 1,
549 -label => 1,
550 -strand_arrow => 1,
551 );
552  
553 my $track = $panel->add_track(
554 -glyph => 'transcript2',
555 -label => 1,
556 -strand_arrow => 1,
557 -bgcolor => 'blue',
558 -bump => +1,
559 -height => 12,
560 );
561  
562 while (<$fh>) {
563 chomp;
564  
565 my ( $name, $score, $start, $end ) = split /\t/;
566 if ( $start - $end < 1 ) {
567 $orientation = +1;
568 }
569 elsif ( $start - $end > 1 ) {
570 $orientation = -1;
571 }
572 my $feature = Bio::SeqFeature::Generic->new(
573 -display_name => $start,
574 -score => $score,
575 -start => $start,
576 -strand => $orientation,
577 -end => $end
578 );
579 $track->add_feature($feature);
580  
581 }
582  
583 binmode OUTFILE;
584  
585 print OUTFILE $panel->png;
586  
587 }
588  
589 }
590  
591 ####################################
592 sub uniq {
593 my %seen;
594 return grep { !$seen{$_}++ } @_;
595 }
596  
597 ##################################
598  
599 =head2 _cleanup()
600  
601 Title : _cleanup()
602 Usage : _cleanup()
603 Function: option to delete tmp files
604 Returns : nothing
605 Args : 1=yes, 0=no
606
607 =cut
608  
609 ##################################
610  
611 sub clean_up {
612 my ( $self, %arg ) = @_;
613 if ( $self->{clean_up} eq "1" ) {
614 my $dir = cwd();
615 unlink glob "$dir/*_temp.txt";
616 unlink glob "$dir/*_temp.TXT";
617 unlink glob "$dir/*.fa";
618 unlink glob "$dir/GRAPHIC_*.txt";
619 unlink glob "$dir/GRAPHIC_*.TXT";
620 unlink glob "$dir/*.aln";
621 unlink glob "$dir/*.fa.fasta";
622 unlink glob "$dir/*.fa.fasta.aln";
623 }
624 else {
625 print "...all done...";
626 }
627 }
628  
629 ####################################
630  
631 =head1 LICENSE AND COPYRIGHT
632
633 Copyright (C) 2017 Damien M. O'Halloran
634
635 GNU GENERAL PUBLIC LICENSE Version 2, June 1991
636  
637 =cut
638  
639 1;
640  
↑ Summary