File Coverage

blib/lib/Bio/MUST/Drivers/Cap3.pm
Criterion Covered Total %
statement 42 82 51.2
branch 0 10 0.0
condition 0 3 0.0
subroutine 14 15 93.3
pod 0 1 0.0
total 56 111 50.4


line stmt bran cond sub pod time code
1             package Bio::MUST::Drivers::Cap3;
2             # ABSTRACT: Bio::MUST driver for running the CAP3 assembly program
3             $Bio::MUST::Drivers::Cap3::VERSION = '0.193030';
4 5     5   33 use Moose;
  5         11  
  5         39  
5 5     5   35151 use namespace::autoclean;
  5         11  
  5         53  
6              
7 5     5   426 use autodie;
  5         10  
  5         41  
8 5     5   30329 use feature qw(say);
  5         13  
  5         469  
9              
10             # use Smart::Comments;
11              
12 5     5   33 use Carp;
  5         13  
  5         409  
13 5     5   35 use IPC::System::Simple qw(system);
  5         9  
  5         253  
14 5     5   29 use Module::Runtime qw(use_module);
  5         8  
  5         42  
15 5     5   411 use Path::Class qw(file);
  5         8  
  5         250  
16 5     5   31 use Tie::IxHash;
  5         103  
  5         193  
17              
18 5     5   42 use Bio::MUST::Core;
  5         11  
  5         223  
19             extends 'Bio::MUST::Core::Ali::Temporary';
20              
21 5     5   34 use Bio::MUST::Core::Constants qw(:files);
  5         8  
  5         872  
22 5     5   35 use aliased 'Bio::MUST::Core::Ali';
  5         10  
  5         42  
23 5     5   1019 use aliased 'Bio::MUST::Core::SeqId';
  5         11  
  5         18  
24              
25 5     5   974 use Bio::MUST::Drivers::Utils qw(stringify_args);
  5         13  
  5         3649  
26              
27              
28             has 'cap3_args' => (
29             is => 'ro',
30             isa => 'HashRef',
31             default => sub { {} },
32             );
33              
34             has '_contig_seq_ids' => (
35             traits => ['Hash'],
36             is => 'ro',
37             isa => 'HashRef[ArrayRef[Bio::MUST::Core::SeqId]]',
38             init_arg => undef,
39             writer => '_set_contig_seq_ids',
40             handles => {
41             all_contig_names => 'keys',
42             all_contig_seq_ids => 'values',
43             seq_ids_for => 'get',
44             },
45             );
46              
47             has '_contigs' => (
48             is => 'ro',
49             isa => 'Bio::MUST::Core::Ali',
50             init_arg => undef,
51             writer => '_set_contigs',
52             handles => {
53             all_contigs => 'all_seqs',
54             count_contigs => 'count_seqs',
55             },
56             );
57              
58             has '_singlets' => (
59             is => 'ro',
60             isa => 'Bio::MUST::Core::Ali',
61             init_arg => undef,
62             writer => '_set_singlets',
63             handles => {
64             all_singlets => 'all_seqs',
65             count_singlets => 'count_seqs',
66             },
67             );
68              
69              
70             sub BUILD {
71 0     0 0   my $self = shift;
72              
73             # provision executable
74 0           my $app = use_module('Bio::MUST::Provision::Cap3')->new;
75 0           $app->meet();
76              
77             # setup output files
78 0           my $infile = $self->filename;
79 0           my $basename = $infile . '.cap';
80 0           my $outfile = $basename . '.out';
81 0           my $outfile_contigs = $basename . '.contigs';
82 0           my $outfile_singlets = $basename . '.singlets';
83              
84             # format CAP3 (optional) arguments
85 0           my $args = $self->cap3_args;
86 0           my $args_str = stringify_args($args);
87              
88             # create CAP3 command
89 0           my $pgm = 'cap3';
90 0           my $cmd = "$pgm $infile $args_str > $outfile 2> /dev/null";
91             #### $cmd
92              
93             # try to robustly execute CAP3
94 0           my $ret_code = system( [ 0, 127 ], $cmd);
95 0 0         if ($ret_code == 127) {
96 0           carp "[BMD] Warning: cannot execute $pgm command;"
97             . ' returning without contigs!';
98 0           return;
99             }
100             # TODO: try to bypass shell (need for absolute path to executable then)
101              
102             # parse output file
103 0           open my $out, '<', $outfile;
104 0           tie my %ids_for, 'Tie::IxHash';
105 0           my $contig_id;
106              
107             # CAP3 output file extract
108             #
109             # Number of segment pairs = 342; number of pairwise comparisons = 8
110             # '+' means given segment; '-' means reverse complement
111             #
112             # Overlaps Containments No. of Constraints Supporting Overlap
113             #
114             # ******************* Contig 1 ********************
115             # seq8+
116             # seq9+ is in seq8+
117             # ******************* Contig 2 ********************
118             # seq10+
119             # seq11+
120             # ******************* Contig 3 ********************
121             # seq12+
122             # seq13+ is in seq12+
123             # seq14+ is in seq13+
124             # ******************* Contig 4 ********************
125             # seq15+
126             # seq16+
127             # ******************* Contig 5 ********************
128             # seq17+
129             # seq18+
130             # seq19+ is in seq18+
131             #
132             # DETAILED DISPLAY OF CONTIGS
133             # ******************* Contig 1 ********************
134             # . : . : . : . : . : . :
135             # seq8+ CTGGACGAGCTGCAGGAGGAGGCGCTGGCGCTGGTGGCGCAGGCCCGACGAGAGGGCGAC
136             # ____________________________________________________________
137             # consensus CTGGACGAGCTGCAGGAGGAGGCGCTGGCGCTGGTGGCGCAGGCCCGACGAGAGGGCGAC
138             #
139             # ...
140              
141             LINE:
142 0           while (my $line = <$out>) {
143 0           chomp $line;
144              
145 0 0         next LINE if $line =~ $EMPTY_LINE;
146 0 0         last LINE if $line =~ m{\A DETAILED \s+ DISPLAY \s+ OF \s+ CONTIGS}xms;
147              
148             # capture next contig id
149 0 0 0       if ($line =~ m{\A \*+ \s+ (Contig\s+\d+) \s+ \*+}xms) {
    0          
150 0           ($contig_id = $1) =~ tr/ //d;
151             }
152              
153             # capture fragment ids for current contig...
154             elsif ($line =~ m{\A ([^\'\+\-\ ]+)[+-]}xms
155             || $line =~ m{\A \s+ (\S+?)[+-]}xms) {
156 0           my $fragment_id = $1;
157 0           push @{ $ids_for{$contig_id} },
  0            
158             SeqId->new( full_id => $self->long_id_for($fragment_id) );
159             } # ... and restore original id on the fly from IdMapper
160             }
161              
162             # store contig and fragment ids
163 0           $self->_set_contig_seq_ids(\%ids_for);
164              
165             # read and store contig seqs
166 0           my $contigs = Ali->load($outfile_contigs);
167 0           $contigs->dont_guess;
168 0           $self->_set_contigs($contigs);
169              
170             # read and store singlet seqs...
171 0           my $singlets = Ali->load($outfile_singlets);
172 0           $singlets->dont_guess;
173 0           $singlets->restore_ids($self->mapper); # ... restoring original ids
174 0           $self->_set_singlets($singlets);
175              
176             # unlink temp files
177             my @files2rm = (
178             $outfile, $outfile_contigs, $outfile_singlets,
179 0           map { $basename . '.' . $_ } qw(info ace contigs.links contigs.qual)
  0            
180             );
181 0           file($_)->remove for @files2rm;
182              
183 0           return;
184             }
185              
186             __PACKAGE__->meta->make_immutable;
187             1;
188              
189             __END__
190              
191             =pod
192              
193             =head1 NAME
194              
195             Bio::MUST::Drivers::Cap3 - Bio::MUST driver for running the CAP3 assembly program
196              
197             =head1 VERSION
198              
199             version 0.193030
200              
201             =head1 SYNOPSIS
202              
203             # TODO
204              
205             =head1 DESCRIPTION
206              
207             # TODO
208              
209             =head1 AUTHOR
210              
211             Denis BAURAIN <denis.baurain@uliege.be>
212              
213             =head1 COPYRIGHT AND LICENSE
214              
215             This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
216              
217             This is free software; you can redistribute it and/or modify it under
218             the same terms as the Perl 5 programming language system itself.
219              
220             =cut