lib/Bio/App/SELEX/RNAmotifAnalysis.pm

filter: Statementstmt 96.6% 290 / 300 Branchbran 85.1% 46 / 54 Conditioncond 51.4% 18 / 35 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 96.9% 32 / 33 Podpod 0.0% 0 / 14 totaltotal 85.2% 387 / 454 CC 51 SCAR 16.1
CC
51
Cov
91%
write_out_clusters
10.1
cluster
9.1
write_cluster
7.0
CRAP 5.0
« blib/script/selex_clustal2stockholm.pl ↑ Summary lib/Bio/App/SELEX/selex_covarianceSearch »
1 #!/usr/bin/env perl
2 package Bio::App::SELEX::RNAmotifAnalysis;
3 # ABSTRACT: Cluster SELEX sequences and calculate their structures
4 9 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Text::LevenshteinXS qw( distance );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Hash::Util qw( lock_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Carp qw( croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 my $DEFAULT_CONFIG = 'cluster.cfg';
16  
17 # CONSTANTS
18 3 my $TRUE = 1;
19 3 my $FALSE = 0;
20 3 my $SPACE = q{ };
21 3 my $EMPTY_STRING = q{};
22 3 my $VERBOSE = 1;
23  
24 3 my $FASTQ_TYPE = 'fastq';
25 3 my $SIMPLE_TYPE = 'simple';
26  
27 # Act like a script if called as one
28 0 ▶unless ( caller() ) { main(); }
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 3 3
29  
30 3 ▶sub main {
Subroutine main: called CC=6 SCAR=18.9
Pod undocumented
31  
32 3 my $max_clusters = 10;
33 3 my $num_cpus = 5;
34 3 my $max_distance = 5;
35 3 my $max_top_seqs = 300;
36 3 my $config_filename = $DEFAULT_CONFIG;
37 3 my $options = GetOptions(
38  
39 # Required (one of these)
40 "$FASTQ_TYPE=s" => \my $fastq,
41 "$SIMPLE_TYPE=s" => \my $simple,
42  
43 # Optional
44 'max_distance=i' => \$max_distance,
45 'max_clusters=i' => \$max_clusters,
46 'max_top_seqs=i' => \$max_top_seqs,
47 'seed=s' => \my $seed_filename,
48 'cpus=i' => \$num_cpus,
49 'config=s' => \$config_filename,
50 'run' => \my $run_scripts,
51 );
52  
53 3 my $file_type;
54 my $infile;
55 3 ▶ if(defined $fastq){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $fastq) { } 5 1 6
elsif (defined $simple) { } 1 0 1
56 2 ▶ if(defined $simple){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $simple) 0 2 2
57 0 warn "--$FASTQ_TYPE and --$SIMPLE_TYPE flags are mutually exclusive!\n";
58 0 help();
59 }
60 2 $infile = $fastq;
61 2 $file_type = $FASTQ_TYPE;
62 }elsif(defined $simple){
63 1 $infile = $simple;
64 1 $file_type = $SIMPLE_TYPE;
65 }else{
66 0 warn "either --$FASTQ_TYPE or --$SIMPLE_TYPE must be used!\n";
67 0 help();
68 }
69  
70 # Eliminate case sensitivity for 'type'
71 3 $file_type = lc $file_type;
72  
73 3 my $config = get_config($config_filename);
74  
75 3 open( my $fh_in, '<', $infile );
76  
77 2 my $seed_fh;
78 2 ▶ if ( defined $seed_filename && -e $seed_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed_filename and -e $seed_filename) 0 2 2
Condition33%1/3
defined $seed_filename and -e $seed_filename
!ll&&!rl&&r
200
MC/DC0%0/2
defined $seed_filename and -e $seed_filename
defined $seed_filename -e $seed_filename
Truth table33%1/3
A and B
A= defined $seed_filenameB= -e $seed_filename
ABresult
0X0
100
111
79 0 open( $seed_fh, '<', $seed_filename );
80 }
81  
82 2 my ($cluster_href, $distance_href) = cluster(
83 fh => $fh_in,
84 max_distance => $max_distance,
85 max_clusters => $max_clusters,
86 seed_fh => $seed_fh,
87 file_type => $file_type,
88 );
89  
90 2 open( my $fh_all_clusters, '>', 'clusters.txt' );
91 2 write_out_clusters(
92 cluster_href => $cluster_href,
93 distance_href => $distance_href,
94 fh_all_clusters => $fh_all_clusters,
95 max_top_seqs => $max_top_seqs,
96 );
97 2 create_batch_files( $config, $num_cpus, $run_scripts );
98 2 return;
99 }
100  
101  
102 4 ▶sub get_config {
Subroutine get_config: called CC=2 SCAR=6.9
Pod undocumented
103 4 my $config_filename = shift;
104  
105 4 my $config;
106 4 ▶ if ( ! -e $config_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $config_filename) 3 1 4
107 3 my $home_dir = $ENV{'HOME'};
108 3 $config = Config::Tiny->new();
109 $config->{executables} = {
110 3 mafft => 'mafft',
111 RNAalifold => 'RNAalifold',
112 cmalign => 'cmalign',
113 cmbuild => 'cmbuild',
114 cmcalibrate => 'cmcalibrate',
115 cmsearch => 'cmsearch',
116 CreateStockholm => 'selex_clustal2stockholm.pl',
117 stock2fasta => 'selex_stock2fasta.pl',
118 };
119 $config->{Flags_for} = {
120 3 RNAalifold => '-r -cv 0.6 -nc 10 -p -d2 -noLP -color -aln',
121 mafft => '--preservecase --clustalout',
122 };
123 3 $config->write($config_filename);
124 3 warn "\nNo configuration file found. Creating new configuration file '$config_filename'\n";
125 3 warn external_dependecnies();
126 3 warn <<"MSG";
127  
128 If you have problems, you may need to ensure that each executable
129 listed in '$config_filename' is located in a directory that is found
130 in your PATH environment variable.\n";
131 MSG
132 }
133 4 $config = Config::Tiny->read($config_filename);
134 4 return $config;
135 }
136  
137 2 ▶sub create_batch_files {
Subroutine create_batch_files: called CC=4 SCAR=13.9
Pod undocumented
138 2 my $config = shift;
139 2 my $num_cpus = shift;
140 2 my $run_scripts = shift;
141  
142 # Get all the file names to be processed
143 6 ▶ my @fasta_filenames = glob '*_top.fasta';
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 # Reduce number of cpus if there are fewer files
146 2 $num_cpus = min(scalar @fasta_filenames, $num_cpus);
147  
148 # Create a batch of commands for each CPU to work on
149 2 my @workload = map { $EMPTY_STRING } 1 .. $num_cpus;
150 2 my $add_work = __add_work(
151 {
152 workload_aref => \@workload,
153 num_cpus => $num_cpus,
154 config => $config,
155 }
156 );
157 2 $add_work->($_) for @fasta_filenames;
158  
159 # Execute the commands for each CPU
160 2 for my $batch_num ( 1 .. $num_cpus ){
161  
162 # name script file for each batch
163 8 my $batch_filename = "batch_$batch_num";
164  
165 # Create a directory for each batch
166 8 system("mkdir $batch_filename.dir");
167  
168 # Move into batch directory
169 8 chdir "$batch_filename.dir";
170  
171 # Write batch instruction to script file
172 8 open( my $fh, '>', $batch_filename);
173 8 print {$fh} $workload[ $batch_num - 1];
174 8 close $fh;
175  
176 # Make script file executable
177 8 system("chmod u+x $batch_filename");
178  
179 # Run the script, if desired
180 8 ▶ if($run_scripts){
Branch: 1 branch
BranchTrueFalseTotal
if ($run_scripts) 0 8 8
181 0 system("./$batch_filename &");
182 }
183  
184 # Return to directory about batch directory
185 8 chdir '..';
186 }
187 2 return;
188 }
189  
190 2 ▶sub __add_work {
Subroutine __add_work: called CC=4 SCAR=14.1
191 2 my %opt = %{ shift() };
192 2 ▶ my $workload_aref = $opt{workload_aref} || croak "'workload_aref' required";
Condition50%1/2
$opt{'workload_aref'} || croak(q['workload_aref' required])
l!l
20
MC/DC0%0/1
$opt{'workload_aref'} || croak(q['workload_aref' required])
$opt{'workload_aref'}
Truth table50%1/2
A
A= $opt{'workload_aref'}
Aresult
11
00
193 2 ▶ my $num_cpus = $opt{num_cpus} || croak "'num_cpus' required";
Condition50%1/2
$opt{'num_cpus'} || croak(q['num_cpus' required])
l!l
20
MC/DC0%0/1
$opt{'num_cpus'} || croak(q['num_cpus' required])
$opt{'num_cpus'}
Truth table50%1/2
A
A= $opt{'num_cpus'}
Aresult
11
00
194 2 ▶ my $config = $opt{config} || croak "'config' required";
Condition50%1/2
$opt{'config'} || croak(q['config' required])
l!l
20
MC/DC0%0/1
$opt{'config'} || croak(q['config' required])
$opt{'config'}
Truth table50%1/2
A
A= $opt{'config'}
Aresult
11
00
195 2 my $work_index = 0;
196 2 my @filenames_to_rename = _filenames_to_rename();
197  
198 2 my $MAFFT_cmd = "$config->{executables}{mafft} $config->{Flags_for}{mafft}";
199 2 my $RNAalifold_cmd = "$config->{executables}{RNAalifold} $config->{Flags_for}{RNAalifold}";
200 8 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
201 8 my $fasta_filename = shift;
202 8 my %file = file_name_hash($fasta_filename);
203  
204 # Don't allow accidentally creating new keys
205 8 lock_keys %file;
206  
207 # Cycle through the sets number of work orders
208 8 ▶ if ( $work_index >= $num_cpus ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_index >= $num_cpus) 0 8 8
209 0 $work_index = 0;
210 }
211  
212 $workload_aref->[$work_index] .= join(
213 "\n",
214  
215 # Pull fasta file into current directory
216 "mv ../$file{fasta} .",
217  
218 # Do alignment of sequences against each other
219 "$MAFFT_cmd $file{fasta} > $file{aligned}",
220  
221 # Calculate the secondary structure
222 "$RNAalifold_cmd < $file{aligned} > $file{sec_struct}",
223  
224 # Rename resulting files
225 8 ( map { "mv $_ $file{$_}" } @filenames_to_rename ),
226  
227 # Convert secondary structure file to Stockholm format
228 "$config->{executables}{CreateStockholm} $file{aligned} $file{sec_struct} > $file{stock}",
229  
230 # Determine covariance model
231 "$config->{executables}{cmbuild} $file{covar_model} $file{stock}",
232  
233 ) . "\n";
234  
235 # Increment work index
236 8 $work_index++;
237  
238 8 return;
239 2 };
240 }
241  
242 8 ▶sub _pairs_from_filenames_to_rename {
Subroutine _pairs_from_filenames_to_rename: called CC=1 SCAR=0.0
243 8 my $base_filename = shift;
244 8 my @pairs = _pairs_from_array( $base_filename . '_', _filenames_to_rename());
245 8 return @pairs;
246 }
247  
248 8 ▶sub _pairs_from_array {
Subroutine _pairs_from_array: called CC=1 SCAR=0.0
249 8 my ($added_string, @array) = @_;
250 8 return map { $_ => $added_string . $_ } @array;
251 }
252  
253 10 ▶sub _filenames_to_rename {
Subroutine _filenames_to_rename: called CC=1 SCAR=0.0
254 10 return qw( alirna.ps alidot.ps aln.ps alifold.out);
255 }
256  
257 8 ▶sub file_name_hash {
Subroutine file_name_hash: called CC=1 SCAR=0.0
Pod undocumented
258 8 my $fasta_filename = shift;
259 8 my $base_filename = base_filename($fasta_filename);
260 8 my %file = (
261 fasta => $fasta_filename,
262 aligned => $base_filename . '.aln',
263 sec_struct => $base_filename . '.gc',
264 _pairs_from_filenames_to_rename($base_filename),
265 stock => $base_filename . '.sto', #stockholm format
266 covar_model => $base_filename . '.cm',
267 );
268 8 return %file;
269 }
270  
271 8 ▶sub base_filename {
Subroutine base_filename: called CC=1 SCAR=0.0
Pod undocumented
272 8 my $filename = shift;
273 8 $filename =~ s/(\.\w+)\z//;
274 8 return $filename;
275 }
276  
277 #: PUBLIC_SUBS
278 9 ▶sub cluster {
Subroutine cluster: called CC=9 SCAR=22.0
Pod undocumented
279 9 my %opt = @_;
280  
281 # Required parameters
282 9 ▶ my $max_distance = $opt{max_distance} || croak 'max_distance required';
Condition50%1/2
$opt{'max_distance'} || croak('max_distance required')
l!l
90
MC/DC0%0/1
$opt{'max_distance'} || croak('max_distance required')
$opt{'max_distance'}
Truth table50%1/2
A
A= $opt{'max_distance'}
Aresult
11
00
283 9 ▶ my $fh = $opt{fh} || croak 'fh required';
Condition50%1/2
$opt{'fh'} || croak('fh required')
l!l
90
MC/DC0%0/1
$opt{'fh'} || croak('fh required')
$opt{'fh'}
Truth table50%1/2
A
A= $opt{'fh'}
Aresult
11
00
284 9 ▶ my $max_clusters = $opt{max_clusters} || croak 'max_clusters required';
Condition50%1/2
$opt{'max_clusters'} || croak('max_clusters required')
l!l
90
MC/DC0%0/1
$opt{'max_clusters'} || croak('max_clusters required')
$opt{'max_clusters'}
Truth table50%1/2
A
A= $opt{'max_clusters'}
Aresult
11
00
285 9 ▶ my $file_type = $opt{file_type} || confess 'file_type required';
Condition50%1/2
$opt{'file_type'} || confess('file_type required')
l!l
90
MC/DC0%0/1
$opt{'file_type'} || confess('file_type required')
$opt{'file_type'}
Truth table50%1/2
A
A= $opt{'file_type'}
Aresult
11
00
286  
287 # Optional parameters
288 9 my $seed_fh = $opt{seed_fh};
289  
290 9 my @seed_pairs;
291  
292 9 my @seq_count_pairs = get_sequences_from($fh, $file_type);
293  
294 # Add any seed sequences to the beginning of the sequence list
295 # Oops, this also sorts and counts the sequences in the seed list.
296 # Is that a problem?
297 9 ▶ if( defined $seed_fh){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed_fh) 2 7 9
298 2 @seed_pairs = get_sequences_from($seed_fh, $file_type);
299 2 unshift @seq_count_pairs, @seed_pairs;
300 }
301  
302 9 my %cluster_for;
303 my %distance_for; # Hash that keeps track of edit distances (parallel to %cluster_for)
304 9 my $next_id = 1;
305  
306 # Create the first cluster with the first sequence count pair.
307 9 my $first_pair = shift @seq_count_pairs;
308 9 $cluster_for{$next_id} = [ $first_pair];
309  
310 # No distance from itself!
311 9 $distance_for{$next_id}{$first_pair->[0]} = 0;
312  
313 # Increment next cluster id, since it has already been used.
314 9 $next_id++;
315  
316 # Add sequences to existing cluster, or create new ones up to the maximum
317 9 ▶ while ( my $seq_count_pair = shift @seq_count_pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq_count_pair = shift @seq_count_pairs) 50 9 59
318 50 my ($cluster_id, $distance) = matching_cluster_and_distance( $max_distance, \%cluster_for, $seq_count_pair );
319  
320 # Create new cluster, if one wasn't found
321 50 ▶ if( ! defined $cluster_id){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cluster_id) 17 33 50
322 17 $cluster_id = $next_id;
323 17 $distance = 0;
324  
325 # Increment counter
326 17 $next_id++;
327 }
328  
329 # Add sequence info to the cluster, if the cluster is within the maximum requested
330 50 ▶ if( $cluster_id <= $max_clusters){
Branch: 1 branch
BranchTrueFalseTotal
if ($cluster_id <= $max_clusters) 49 1 50
331 49 push @{ $cluster_for{$cluster_id} }, $seq_count_pair ;
332 49 $distance_for{$cluster_id}{$seq_count_pair->[0]} = $distance;
333 }
334 }
335  
336 9 return (\%cluster_for, \%distance_for);
337 }
338  
339 17 ▶sub total_plus_cluster {
Subroutine total_plus_cluster: called CC=2 SCAR=6.9
Pod undocumented
340 17 my $opt = shift;
341 17 my $cluster = $opt->{cluster};
342 17 my @seqs_with_count = @{$cluster};
343 17 my $total_count = 0;
344  
345 17 for my $seq_with_count (@seqs_with_count) {
346 39 $total_count += $seq_with_count->[1];
347 }
348  
349 return {
350 total => $total_count,
351 cluster => \@seqs_with_count,
352 original_id => $opt->{original_id},
353 17 };
354 }
355  
356 5 ▶sub write_out_clusters {
Subroutine write_out_clusters: called CC=10 SCAR=23.2
Pod undocumented
357 5 my %opt = @_;
358  
359 # Required input
360 5 ▶ my $cluster_href = $opt{cluster_href} || croak 'cluster_href required';
Condition50%1/2
$opt{'cluster_href'} || croak('cluster_href required')
l!l
50
MC/DC0%0/1
$opt{'cluster_href'} || croak('cluster_href required')
$opt{'cluster_href'}
Truth table50%1/2
A
A= $opt{'cluster_href'}
Aresult
11
00
361 5 ▶ my $distance_href = $opt{distance_href} || croak 'distance_href required';
Condition50%1/2
$opt{'distance_href'} || croak('distance_href required')
l!l
50
MC/DC0%0/1
$opt{'distance_href'} || croak('distance_href required')
$opt{'distance_href'}
Truth table50%1/2
A
A= $opt{'distance_href'}
Aresult
11
00
362 5 ▶ my $fh_all_clusters = $opt{fh_all_clusters} || croak 'fh_all_clusters required';
Condition50%1/2
$opt{'fh_all_clusters'} || croak('fh_all_clusters required')
l!l
50
MC/DC0%0/1
$opt{'fh_all_clusters'} || croak('fh_all_clusters required')
$opt{'fh_all_clusters'}
Truth table50%1/2
A
A= $opt{'fh_all_clusters'}
Aresult
11
00
363 5 ▶ my $max_top_seqs = $opt{max_top_seqs} || croak 'max_top_seqs required';
Condition50%1/2
$opt{'max_top_seqs'} || croak('max_top_seqs required')
l!l
50
MC/DC0%0/1
$opt{'max_top_seqs'} || croak('max_top_seqs required')
$opt{'max_top_seqs'}
Truth table50%1/2
A
A= $opt{'max_top_seqs'}
Aresult
11
00
364  
365 # Optional input
366 5 ▶ my $fh_href = $opt{fh_href} || {};
Condition100%2/2
$opt{'fh_href'} || {}
l!l
32
MC/DC100%1/1
$opt{'fh_href'} || {}
$opt{'fh_href'}
Truth table100%2/2
A
A= $opt{'fh_href'}
Aresult
11
00
367  
368 # Sort clusters by number of sequences they contain (including redundant
369 # ones).
370 my @total_plus_clusters =
371 17 map { total_plus_cluster( {original_id => $_, cluster => $cluster_href->{$_}} ) }
372 5 keys %{$cluster_href};
373  
374 5 my $new_id = 1;
375 # Sort clusters by number of sequences they contain (including redundant ones).
376 5 for my $total_plus_cluster ( reverse sort { $a->{total} <=> $b->{total} } @total_plus_clusters ) {
377  
378 17 my $number_of_sequences_in_cluster = scalar @{ $total_plus_cluster->{cluster} };
379  
380 17 my $grouping;
381  
382 # Call it a 'single' if only one unique sequence
383 17 ▶ if($number_of_sequences_in_cluster == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($number_of_sequences_in_cluster == 1) { } 5 12 17
384 5 $grouping = 'single';
385 }else{
386 12 $grouping = 'cluster';
387 };
388  
389 # Print header for each cluster/single
390 17 print {$fh_all_clusters} "######## $grouping $new_id ########\n";
391  
392 # Use prescribed filehandle for each sequence, or create one
393 17 my $fh;
394 17 ▶ if ( defined $fh_href->{$new_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh_href->{$new_id}) { } 9 8 17
395 9 $fh = $fh_href->{$new_id};
396 }
397 else {
398 8 my $filename = $grouping . '_' . $new_id . '_top.fasta';
399 8 open( $fh, '>', $filename );
400 8 ▶ print "created output file '$filename'\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $VERBOSE 8 0 8
401 }
402  
403 # Write cluster info to the "all" and individual cluster files
404 write_cluster(
405 {
406 fh_all_clusters => $fh_all_clusters,
407 fh => $fh,
408 cluster_aref => $total_plus_cluster->{cluster},
409 cluster_number => $new_id,
410 original_cluster_id => $total_plus_cluster->{original_id},
411 17 max_top_seqs => $max_top_seqs,
412 distance_href => $distance_href,
413 }
414 );
415  
416 # Increment cluster id
417 17 $new_id++;
418 }
419 5 return;
420 }
421  
422 17 ▶sub write_cluster {
Subroutine write_cluster: called CC=7 SCAR=19.5
Pod undocumented
423 17 my $opt = shift;
424  
425 17 my $fh_all_clusters = $opt->{fh_all_clusters};
426 17 my $fh = $opt->{fh};
427 17 my $cluster_aref = $opt->{cluster_aref};
428 17 my $distance_href = $opt->{distance_href};
429 17 my $cluster_number = $opt->{cluster_number};
430 17 my $max_top_seqs = $opt->{max_top_seqs};
431 17 my $original_id = $opt->{original_cluster_id};
432  
433 17 my @seq_w_counts = @{$cluster_aref};
434  
435 # Remove total count, leaving just pairs with counts
436 17 my $internal_seq_id = 1;
437  
438 17 my $is_single;
439 17 ▶ $is_single = $TRUE if @seq_w_counts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @seq_w_counts == 1 5 12 17
440  
441 17 my $num_seqs = scalar @seq_w_counts;
442  
443 # If there are more than max_top_seqs, then split them into two arrays:
444 # One that will be processed and the other that will simply be output
445 17 my @overage_seqs;
446 17 ▶ if ( $num_seqs > $max_top_seqs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_seqs > $max_top_seqs) 2 15 17
447 2 @overage_seqs = @seq_w_counts[ $max_top_seqs .. ( $num_seqs - 1 ) ];
448 2 @seq_w_counts = @seq_w_counts[ 0 .. ( $max_top_seqs - 1 ) ];
449 }
450  
451 # SEQ_W_COUNT_LOOP
452 17 for my $seq_w_count (@seq_w_counts) {
453 37 my ( $seq, $count ) = @{$seq_w_count};
454  
455 37 my $distance = $distance_href->{$original_id}{$seq};
456  
457 37 my $unique_id = join( '.', $cluster_number, $internal_seq_id, $count, $distance );
458  
459 # Print to individual cluster file and all clusters file
460 37 print {$fh_all_clusters} "$unique_id\t$seq\n";
461  
462 37 print {$fh} ">$unique_id\n$seq\n";
463  
464 # Print second copy if this is a singleton (to make
465 # multi-sequence alignment behave well)
466 37 ▶ if ($is_single) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_single) 5 32 37
467 5 $unique_id .= 'b';
468 5 print {$fh} ">$unique_id\n$seq\n";
469 }
470 37 $internal_seq_id++;
471 }
472  
473 # OVERAGE LOOP
474 17 ▶ if (@overage_seqs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@overage_seqs) 2 15 17
475 2 my $filename = "cluster_${cluster_number}_overage.fasta";
476  
477 2 open( my $overage_fh, '>', $filename );
478 2 for my $seq_w_count (@overage_seqs) {
479 2 my ( $seq, $count ) = @{$seq_w_count};
480  
481 2 my $distance = $distance_href->{$original_id}{$seq};
482 2 my $unique_id = join( '.', $cluster_number, $internal_seq_id, $count, $distance );
483 2 print {$overage_fh} ">$unique_id\n$seq\n";
484 2 print {$fh_all_clusters} "$unique_id\t$seq\n";
485 2 $internal_seq_id++;
486 }
487 }
488  
489 17 return;
490 }
491  
492 51 ▶sub matching_cluster_and_distance {
Subroutine matching_cluster_and_distance: called CC=3 SCAR=11.0
Pod undocumented
493 51 my $max_distance = shift;
494 51 my $cluster_href = shift;
495 51 my $seq_aref = shift;
496  
497 ID_LOOP:
498 51 for my $id ( keys %{$cluster_href} ) {
499 91 my $cluster_seq = $cluster_href->{$id}->[0];
500  
501 91 my $distance = distance( $seq_aref->[0], $cluster_seq->[0] );
502  
503 # Short circuit ID_LOOP when one is found (supposedly only one will match)
504 91 ▶ if ( $distance < $max_distance ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance < $max_distance) 34 57 91
505 34 return ($id, $distance);
506 }
507 }
508 17 return;
509 }
510  
511 12 ▶sub get_sequences_from {
Subroutine get_sequences_from: called CC=6 SCAR=17.9
Pod undocumented
512 12 ▶ my $fh = shift || croak 'fh required';
Condition50%1/2
shift() || croak('fh required')
l!l
120
MC/DC0%0/1
shift() || croak('fh required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
513 12 ▶ my $type = shift || confess 'file type required';
Condition50%1/2
shift() || confess('file type required')
l!l
120
MC/DC0%0/1
shift() || confess('file type required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
514 12 my %count_of;
515 12 my $next_seq = _next_sequence_for($fh, $type);
516 12 while (1) {
517 115 my $seq = $next_seq->();
518 115 ▶ last if ! defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 12 103 115
519 103 ▶ next if $seq eq $EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
if $seq eq $EMPTY_STRING 1 102 103
520  
521 # If sequence has not been seen before, start counting it. Otherwise, add to the count.
522 102 ▶ if( ! exists $count_of{$seq} ){
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $count_of{$seq}) { } 66 36 102
523 66 $count_of{$seq} = 1;
524 }else {
525 36 $count_of{$seq} = $count_of{$seq} + 1;
526 }
527 }
528  
529 # Convert hash into an array containing paired values.
530 # First value in the pair is the sequence.
531 # | The second value in the pair is the number of times (the
532 # | | count) that that sequence is seen in the file.
533 # v v
534 12 my @sequences = map { [ $_, $count_of{$_} ] } keys %count_of;
535  
536 # Sort the sequences so that the most abundant occur first in the array.
537 12 @sequences = sort { $b->[1] <=> $a->[1] } @sequences;
538  
539 12 return @sequences;
540 }
541  
542 12 ▶sub _next_sequence_for {
Subroutine _next_sequence_for: called CC=5 SCAR=16.2
543 12 ▶ my $fh = shift || croak 'fh (first positional parameter) required';
Condition50%1/2
shift() || croak('fh (first positional parameter) required')
l!l
120
MC/DC0%0/1
shift() || croak('fh (first positional parameter) required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
544 12 ▶ my $type = shift || confess 'file_type (second positional parameter) required';
Condition50%1/2
shift() || confess('file_type (second positional parameter) required')
l!l
120
MC/DC0%0/1
shift() || confess('file_type (second positional parameter) required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
545  
546 # Simply use next_line if we want each and every line
547 99 ▶ return sub { next_line($fh) }
Subroutine __ANON__: called CC=1 SCAR=0.0
548 12 ▶ if $type eq $SIMPLE_TYPE;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $SIMPLE_TYPE 11 1 12
549  
550 # Only other supported file type is 'fastq'
551 1 ▶ confess "Unrecognized type '$type'. Only '$SIMPLE_TYPE' and '$FASTQ_TYPE' are currently recognized."
Branch: 1 branch
BranchTrueFalseTotal
if $type ne $FASTQ_TYPE 0 1 1
552 if $type ne $FASTQ_TYPE;
553  
554 # Skip first header line
555 1 readline $fh;
556  
557 16 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
558 16 my $line = readline $fh;
559 16 ▶ return if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 15 16
560  
561 # remove newline and carriage return
562 15 chomp $line;
563 15 $line =~ s/\r//g;
564  
565 # Skip quality header, quality score, and next sequence header
566 15 readline $fh;
567 15 readline $fh;
568 15 readline $fh;
569  
570 15 return $line;
571 1 };
572 }
573  
574 99 ▶sub next_line {
Subroutine next_line: called CC=2 SCAR=6.9
Pod undocumented
575 99 my $fh = shift;
576  
577 # Get next line from file
578 99 my $line = readline $fh;
579  
580 # return undef if nothing left to read.
581 99 ▶ return if ! defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 11 88 99
582  
583 #remove newline
584 88 chomp $line;
585  
586 #remove carriage return
587 88 $line =~ s/\r//g;
588 88 return $line;
589 }
590  
591 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod undocumented
592 0 print << "END";
593 $0 --$FASTQ_TYPE=FILENAME [OPTIONS]
594 $0 --$SIMPLE_TYPE=FILENAME [OPTIONS]
595  
596 OPTIONS (showing defaults)
597 --max_distance 5
598 --cpus 5
599 --max_clusters 10
600 --max_top_seqs 300
601 --config cluster.cfg
602 END
603 0 exit();
604 }
605  
606 3 ▶sub external_dependecnies {
Subroutine external_dependecnies: called CC=1 SCAR=0.0
Pod undocumented
607 3 return <<END;
608 External dependencies:
609 mafft (see http://mafft.cbrc.jp/alignment/software/)
610 Infernal (see http://infernal.janelia.org/), specifically:
611 cmalign
612 cmbuild
613 cmcalibrate
614 cmsearch
615 RNA Vienna Package (see http://www.tbi.univie.ac.at/~ivo/RNA/), specifically:
616 RNAalifold
617 These must be installed and in a directory that is your PATH environment variable.
618 END
619  
620 };
621  
622 3 1;
623  
624 =pod
625  
626 =head1 NAME
627  
628 Bio::App::SELEX::RNAmotifAnalysis - Cluster SELEX sequences and calculate their structures
629  
630 =head1 SYNOPSIS
631  
632 RNAmotifAnalysis --fastq seqs.fq --cpus 4 --run
633  
634 =head1 DESCRIPTION
635  
636 This module pipelines steps in the analysis of SELEX (Systematic Evolution
637 of Ligands through EXponential enrichment) data.
638  
639 This main module creates scripts to do the following:
640  
641 (1) Cluster similar sequences based on edit distance.
642  
643 (2) Align sequences within each cluster (using mafft).
644  
645 (3) Calculate the secondary structure of the aligned sequences (using
646 RNAalifold, from the Vienna RNA package)
647  
648 (4) Build covariance models using cmbuild from Infernal.
649  
650 Another useful utility installed with this distribution is
651 "selex_covarianceSearch" for doing iterative refinements of
652 covariance models.
653  
654 If you want to use files that simply list sequences, then use
655 the "--simple" flag instead of the "--fastq" flag.
656  
657 This script assumes that you've already done all of the quality
658 control of your sequences beforehand. If the FASTQ format is
659 used, quality scores are ignored.
660  
661 =head1 EXAMPLE USE
662  
663 RNAmotifAnalysis --infile seqs.fq --cpus 4 --run
664  
665 This will cluster the sequences found in 'seqs.fq' and create a FASTA file
666 for each one. The FASTA files will be grouped into batches (i.e. one per
667 cpu requested) that will be placed in a separate directory for each batch,
668 and processed within that directory. At the end of processing, for each
669 cluster there will be a covariance model and postscript illustration
670 files. The batch script used to process each batch will be located in the
671 respective batch directory. To produce the scripts without running them,
672 simply exclude the --run flag from the command line.
673  
674 The output file contains names that contain four period delimited values
675 For example, 2.3.1.5 means
676 that this is the second cluster
677 this is the third sequence in the cluster
678 there is one copy of this sequences
679 it is an edit distance of 5 from the reference sequence
680  
681 =head1 CONFIGURATION AND ENVIRONMENT
682  
683 As written, this code makes heavy use of UNIX utilities and is
684 therefore only supported on UNIX-like environemnts (e.g. Linux, UNIX, Mac
685 OS X).
686  
687 Install Infernal, MAFFT, and the RNA Vienna package ahead of time and add
688 the directories containing their executables to your PATH, so that the
689 first time you run RNAmotifAnalysis.pm the configuration file (cluster.cfg)
690 that is generated will have all of the correct parameters. Otherwise,
691 you'll need to update the configuration file manually.
692  
693 To update the PATH environment variable with the directory '/usr/local/myapps/bin/',
694 update your .bashrc file, thus:
695  
696 echo 'export PATH=/usr/local/myapps/bin:$PATH' >> ~/.bashrc.
697  
698 Now, every time you open a new terminal window, the PATH environment
699 variable will contain '/usr/local/myapps/bin/'. To make your new .bashrc
700 file effective immediately (i.e. without having to open a new terminal
701 window), use the following command:
702  
703 source ~/.bashrc
704  
705 =head1 INSTALLATION
706  
707 These installation instructions assume being able to open and use a
708 terminal window on Linux.
709  
710 (0) Some systems need several dependencies installed ahead of time.
711  
712 You may be able to skip this step. However, if subsequent steps don't
713 work, then be sure that some basic libraries are installed, as shown
714 below (or ask a system administrator to take care of it). For the
715 applicable distribution, open a terminal and then type the commands as
716 indicated:
717  
718 For RedHat or CentOS 5.x systems (tested on CentOS 5.5)
719  
720 sudo yum install gcc
721  
722 For RedHat or CentOS 6.x systems (tested on "Minimal Desktop" CentOS 6.0)
723  
724 sudo yum install gcc
725 sudo yum install perl-devel
726  
727 For Ubuntu systems (tested on Ubuntu 12-04 LTS)
728  
729 sudo apt-get install curl
730  
731 For Debian 5.x systems:
732  
733 sudo apt-get install gcc
734 sudo apt-get install make
735  
736 (1) Install the non-Perl dependencies:
737 (Versions shown are those that we've tested. Please contact us if
738 newer versions do not work.)
739  
740 Infernal 1.0.2 (http://infernal.janelia.org/)
741 MAFFT 6.849b (http://mafft.cbrc.jp/alignment/software/)
742 RNA Vienna package 1.8.4 (http://www.tbi.univie.ac.at/~ivo/RNA/)
743  
744 After installing these, make sure all of the foloowing executables are
745 in directories within your PATH:
746  
747 cmbuild
748 cmcalibrate
749 cmsearch
750 cmalign
751 mafft
752 RNAalifold
753  
754 (2) Use a CPAN client to install Bio::App::SELEX::RNAmotifAnalysis.
755  
756 Here we demonstrate the use of cpanminus to install it to a local Perl module directory. These instructions assume absolutely no experience with cpanminus.
757  
758 1. Download cpanminus
759  
760 curl -LOk http://xrl.us/cpanm
761  
762  
763 2. Make it executable
764  
765 chmod u+x cpanm
766  
767  
768 3. Make a local lib/perl5 directory (if it doesn't already exist)
769  
770 mkdir -p ~/lib/perl5
771  
772  
773 4. Add relevant directories to your PERL5LIB and PATH environment
774 variables by adding the following text to your ~/.bashrc
775 file:
776  
777  
778 # Set PERL5LIB if it doesn't already exist
779 : ${PERL5LIB:=~/lib/perl5}
780  
781 # Prepend to PERL5LIB if directory not already found in PERL5LIB
782 if ! echo $PERL5LIB | egrep -q "(^|:)~/perl5/lib/perl5($|:)"; then
783 export PERL5LIB=~/lib/perl5:$PERL5LIB;
784 fi
785  
786 # Prepend to PATH if directory not already found in PATH
787 if ! echo $PATH | egrep -q "(^|:)~/perl5/bin($|:)"; then
788 export PATH=~/bin:$PATH;
789 fi
790  
791  
792 5. Update environment variables immediately
793  
794 source ~/.bashrc
795  
796  
797 6. Install Module::Build
798  
799 ./cpanm Module::Build
800  
801  
802 7. Install Text::LevenshteinXS (even if you already have it installed elsewhere)
803  
804 ./cpanm Text::LevenshteinXS
805  
806  
807 8. Install Bio::App::SELEX::RNAmotifAnalysis
808  
809 ./cpanm Bio::App::SELEX::RNAmotifAnalysis
810  
811  
812 Please contact the author if, after consulting this documentation and
813 searching Google with error messages, you still encounter difficulties
814 during the installation process.
815  
816 =head1 INCOMPATIBILITIES
817  
818 Windows: lacks necessary *nix utilities
819 SGI: problems with compiled dependency Text::LevenshteinXS
820 Sun/Solaris: problems with compiled dependency Text::LevenshteinXS
821 BSD: problems with compiled dependency Text::LevenshteinXS
822  
823 =head1 BUGS AND LIMITATIONS
824  
825 There are no known bugs in this module.
826 Please report problems to molecules <at> cpan <dot> org
827 Patches are welcome.
828  
829 =head1 RELATED PUBLICATIONS
830  
831 Ditzler MA, Lange MJ, Bose D, Bottoms CA, Virkler KF, et al. (2013) High-
832 throughput sequence analysis reveals structural diversity and improved
833 potency among RNA inhibitors of HIV reverse transcriptase. Nucleic Acids
834 Res 41(3):1873-1884. doi: 10.1093/nar/gks1190
835  
836 =cut
« blib/script/selex_clustal2stockholm.pl ↑ Summary lib/Bio/App/SELEX/selex_covarianceSearch »