comparison VelvetOptimiser-2.1.7_modified/VelvetOpt/hwrap.pm @ 0:7363fee7f20c default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author konradpaszkiewicz
date Tue, 07 Jun 2011 17:42:26 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7363fee7f20c
1 # VelvetOpt::hwrap.pm
2 #
3 # Copyright 2008 Simon Gladman <simon.gladman@csiro.au>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19 #
20 # Version 1.1 - 14/07/2010 - Added support for changing input file types
21 # Version 1.2 - 11/08/2010 - Changed velveth help parser for new velvet help format
22 # Thanks to Alexie Papanicolaou - CSIRO for the patch.
23
24 package VelvetOpt::hwrap;
25
26 =head1 NAME
27
28 VelvetOpt::hwrap.pm - Velvet hashing program wrapper module.
29
30 =head1 AUTHOR
31
32 Simon Gladman, CSIRO, 2007, 2008.
33
34 =head1 LICENSE
35
36 Copyright 2008 Simon Gladman <simon.gladman@csiro.au>
37
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2 of the License, or
41 (at your option) any later version.
42
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
47
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
51 MA 02110-1301, USA.
52
53 =head1 SYNOPSIS
54
55 use VelvetOpt::hwrap;
56 use VelvetOpt::Assembly;
57 my $object = VelvetOpt::Assembly->new(
58 timestamph => "23 November 2008 15:00:00",
59 ass_id => "1",
60 versionh => "0.7.04",
61 pstringh => "test 21 -fasta test_reads.fna",
62 ass_dir => "/home/gla048/Desktop/newVelvetOptimiser/data_1"
63 );
64 my $worked = VelvetOpt::hwrap::objectVelveth($object);
65 if($worked){
66 print $object->toString();
67 }
68 else {
69 die "Error in velveth..\n" . $object->toString();
70 }
71
72 =head1 DESCRIPTION
73
74 A wrapper module to run velveth on VelvetAssembly objects or on velveth
75 parameter strings. Also contains private methods to check velveth
76 parameter strings, run velveth and return results.
77
78 =head2 Uses
79
80 =over 8
81
82 =item strict
83
84 =item warnings
85
86 =item Carp
87
88 =item VelvetOpt::Assembly
89
90 =item POSIX qw(strftime)
91
92 =back
93
94 =head2 Private Fields
95
96 =over 8
97
98 =item interested
99
100 STDERR printing debug message toggle. 1 for on, 0 for off.
101
102 =back
103
104 =head2 Methods
105
106 =over 8
107
108 =item _runVelveth
109
110 Private method which runs velveth with the supplied velveth parameter string and returns velveth output messages as a string.
111
112 =item _checkVHString
113
114 Private method which checks for a correctly formatted velveth string. Returns 1 or 0.
115
116 =item objectVelveth
117
118 Accepts a VelvetAssembly object and the number of categories velvet was compiled with, looks for the velveth parameter string it contains, checks it, sends it to _runVelveth, collects the results and stores them in the VelvetAssembly object.
119
120 =item stringVelveth
121
122 Accepts a velveth parameter string and the number of categories velvet was compiled with, checks it, sends it to _runVelveth and then collects and returns the velveth output messages.
123
124 =back
125
126 =cut
127
128 use warnings;
129 use strict;
130 use Carp;
131 use VelvetOpt::Assembly;
132 use POSIX qw(strftime);
133
134 my $interested = 0;
135
136 my @Fileformats;
137 my @Readtypes;
138 my $usage;
139 my $inited = 0;
140
141 sub init {
142 #run a velveth to get its help lines..
143 my $response = &_runVelveth(" ");
144
145 $response =~ m/CATEGORIES = (\d+)/;
146 my $cats = $1;
147 unless($cats){$cats = 2;}
148
149 $response =~ m/(File format options:(.*)Read type options)/s;
150 my @t = split /\n/, $1;
151 foreach(@t){
152 #if(/\s+(-\S+)/){
153 while(/\s+(-\S+)/g){
154 push @Fileformats, $1;
155 }
156 }
157
158 $response =~ m/(Read type options:(.*)Options:)/s;
159
160 @t = ();
161 @t = split /\n/, $1;
162 foreach(@t){
163 #if(/\s+(-\S+)/){
164 while(/\s+(-\S+)/g){
165 push @Readtypes, $1;
166 }
167 }
168
169 for(my $i = 3; $i <= $cats; $i++){
170 push @Readtypes, "-short$i";
171 push @Readtypes, "-shortPaired$i";
172 }
173
174 $usage = "Incorrect velveth parameter string: Needs to be of the form\n{[-file_format][-read_type] filename}\n";
175 $usage .= "Where:\n\tFile format options:\n";
176 foreach(@Fileformats){
177 $usage .= "\t$_\n";
178 }
179 $usage .= "Read type options:\n";
180 foreach(@Readtypes){
181 $usage .= "\t$_\n";
182 }
183 $usage .= "\nThere can be more than one filename specified as long as its a different type.\nStopping run\n";
184
185 $inited = 1;
186 }
187
188 sub _runVelveth {
189 #unless($inited){ &init(); }
190 my $cmdline = shift;
191 my $output = "";
192 print STDERR "About to run velveth!\n" if $interested;
193 $output = `velveth $cmdline`;
194 $output .= "\nTimestamp: " . strftime("%b %e %Y %H:%M:%S", localtime) . "\n";
195 return $output;
196 }
197
198 sub _checkVHString {
199 unless($inited){ &init(); }
200 my $line = shift;
201 my $cats = shift;
202
203
204
205 my %fileform = ();
206 my %readform = ();
207
208 foreach(@Fileformats){ $fileform{$_} = 1;}
209 foreach(@Readtypes){ $readform{$_} = 1;}
210
211 my @l = split /\s+/, $line;
212
213 #first check for a directory name as the first parameter...
214 my $dir = shift @l;
215 if(!($dir =~ /\w+/) || ($dir =~ /^\-/)){
216 carp "**** $line\n\tNo directory name specified as first parameter in velveth string. Internal error!\n$usage";
217 return 0;
218 }
219 #print "VH Check passed directory..\n";
220 my $hash = shift @l;
221 unless($hash =~ /^\d+$/){
222 carp "**** $line\n\tHash value in velveth string not a number. Internal error!\n$usage";
223 return 0;
224 }
225
226 #print "VH check passed hash value..\n";
227
228 my $i = 0;
229 my $ok = 1;
230 foreach(@l){
231 if(/^-/){
232 #s/-//;
233 if(!$fileform{$_} && !$readform{$_}){
234 carp "**** $line\n\tIncorrect fileformat or readformat specified.\n\t$_ is an invalid velveth switch.\n$usage";
235 return 0;
236 }
237 elsif($fileform{$_}){
238 if(($i + 1) > $#l){
239 carp "$line\n\tNo filename supplied after file format type $l[$i].\n$usage";
240 return 0;
241 }
242 if($readform{$l[$i+1]}){
243 if(($i+2) > $#l){
244 carp "$line\n\tNo filename supplied after read format type $l[$i+1].\n$usage";
245 return 0;
246 }
247 if(-e $l[$i+2]){
248 $ok = 1;
249 }
250 else{
251 carp "**** $line\n\tVelveth filename " . $l[$i+2] . " doesn't exist.\n$usage";
252 return 0;
253 }
254 }
255 elsif (-e $l[$i+1]){
256 $ok = 1;
257 }
258 else {
259 carp "**** $line\n\tVelveth filename " . $l[$i+1] . " doesn't exist.$usage\n";
260 return 0;
261 }
262 }
263 elsif($readform{$_}){
264 if(($i + 1) > $#l){
265 carp "$line\n\tNo filename supplied after read format type $l[$i].\n$usage";
266 return 0;
267 }
268 if($fileform{$l[$i+1]}){
269 if(($i+2) > $#l){
270 carp "$line\n\tNo filename supplied after file format type $l[$i+1].\n$usage";
271 return 0;
272 }
273 if(-e $l[$i+2]){
274 $ok = 1;
275 }
276 else{
277 carp "**** $line\n\tVelveth filename " . $l[$i+2] . " doesn't exist.\n$usage";
278 return 0;
279 }
280 }
281 elsif (-e $l[$i+1]){
282 $ok = 1;
283 }
284 else {
285 carp "**** $line\n\tVelveth filename " . $l[$i+1] ." doesn't exist.\n$usage";
286 return 0;
287 }
288 }
289 }
290 elsif(!-e $_){
291 carp "**** $line\n\tVelveth filename $_ doesn't exist.\n$usage";
292 return 0;
293 }
294 $i ++;
295 }
296 if($ok){
297 return 1;
298 }
299 }
300
301 sub objectVelveth {
302 unless($inited){ &init(); }
303 my $va = shift;
304 my $cats = shift;
305 my $cmdline = $va->{pstringh};
306 if(_checkVHString($cmdline, $cats)){
307 $va->{velvethout} = _runVelveth($cmdline);
308 my @t = split /\n/, $va->{velvethout};
309 $t[$#t] =~ s/Timestamp:\s+//;
310 $va->{timestamph} = $t[$#t];
311 return 1;
312 }
313 else {
314 $va->{velvethout} = "Formatting errors in velveth parameter string.$usage";
315 return 0;
316 }
317 }
318
319 sub stringVelveth {
320 unless($inited){ &init(); }
321 my $cmdline = shift;
322 my $cats = shift;
323 if(_checkVHString($cmdline,$cats)){
324 return _runVelveth($cmdline);
325 }
326 else {
327 return "Formatting errors in velveth parameter string.$usage";
328 }
329 }
330
331 1;