blib/script/fasta_regex.pl untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 88 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 # -*-CPerl-*-
3 # Last changed Time-stamp: <2017-08-17 18:11:05 mtw>
4 #
5 # Find sequence motifs in (multi)-Fasta files
6 #
7 # usage: fasta_regex.pl --motif <STRING> --fa <FASTAFILE>
8 #
9 # ***********************************************************************
10 # * Copyright notice
11 # *
12 # * Copyright 2017 Michael T. Wolfinger <michael@wolfinger.eu>
13 # * All rights reserved
14 # *
15 # * This program is free software: you can redistribute it and/or modify
16 # * it under the terms of the GNU General Public License as published by
17 # * the Free Software Foundation, either version 3 of the License, or
18 # * (at your option) any later version.
19 # *
20 # * This program is distributed in the hope that it will be useful,
21 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # * GNU General Public License for more details.
24 # *
25 # * You should have received a copy of the GNU General Public License
26 # * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 # *
28 # * This copyright notice MUST APPEAR in all copies of the script!
29 # ***********************************************************************
30  
31 use strict;
32 use warnings;
33 use Getopt::Long qw( :config posix_default bundling no_ignore_case );
34 use Data::Dumper;
35 use Pod::Usage;
36 use Cwd;
37 use Path::Class;
38 use Bio::ViennaNGS::Fasta;
39 use Carp;
40  
41 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
42 #^^^^^^^^^^ Variables ^^^^^^^^^^^#
43 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
44  
45 my ($fastaO,$cwd);
46 my $motif = undef;
47 my $fa_in = undef;
48 my $strand = "+";
49 my $wantbed = 0;
50 my $name = "pattern";
51 my @fids = ();
52  
53  
54 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
55 #^^^^^^^^^^^^^^ Main ^^^^^^^^^^^^^#
56 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
57  
58 Getopt::Long::config('no_ignore_case');
59 pod2usage(-verbose => 1) unless GetOptions("fa|f=s" => \$fa_in,
60 "motif|m=s" => \$motif,
61 "name|n=s" => \$name,
62 "bed" => sub{$wantbed = 1},
63 "man" => sub{pod2usage(-verbose => 2)},
64 "help|h" => sub{pod2usage(1)}
65 );
66  
67 unless (-f $fa_in){
68 warn "Could not find input FASTA input file provided via ---fa option";
69 pod2usage(-verbose => 0);
70 }
71 $cwd = getcwd();
72 unless ($fa_in =~ /^\// || $fa_in =~ /^\.\//){$fa_in = file($cwd,$fa_in);}
73  
74 unless (defined $motif) {
75 warn "No search pattern found. Please provided a mtoif as regular expression.";
76 pod2usage(-verbose => 0);
77 }
78 $strand eq '-' ? ($strand = '-') : ($strand = '+') ;
79  
80 $fastaO = Bio::ViennaNGS::Fasta->new(fasta=>"$fa_in");
81 my $ps = $fastaO->primaryseqH;
82 foreach my $id (keys %$ps){
83 my $cnt = 0;
84 my $start = $$ps{$id}->{start};
85 my $end = $$ps{$id}->{stop};
86 # print ">FASTA Id $id ($start-$end)\n";
87 my $seq = $fastaO->stranded_subsequence($id,$start, $end,$strand);
88 # print "$seq\n";
89 while ($seq =~ m/($motif)/g){ # we need those parentheses here !!!
90 my $p = pos($seq)-length($1);
91 my $q = $p+length($1);
92 my $n = $name."_$p-$q";
93 if ($wantbed == 1){
94 print join "\t", ($id,$p,$q,$n,"0",$strand);
95 print "\n";
96 }
97 $cnt++;
98 }
99 #print "$cnt motifs found\n";
100 }
101  
102  
103 #print Dumper($ps);
104  
105 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
106 #^^^^^^^^^^^ Subroutines ^^^^^^^^^^#
107 #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^#
108  
109  
110  
111  
112 __END__
113  
114 =head1 NAME
115  
116 fasta_regex.pl - Find sequence motifs in a (multi) Fasta file
117  
118 =head1 SYNOPSIS
119  
120 fasta_regex.pl [--motif I<REGEX>] [--fa I<FILE>] [--name I<STRING>] [--bed]
121 [options]
122  
123 =head1 DESCRIPTION
124  
125 This script extracts sequence motifs, provided as regular expressionm
126 from Fasta files.
127  
128 The tool returns B<all motifs> matching the search criteria as a
129 (mutli-)Fasta file to STDOUT. This means if the motif is found more
130 than once within an annotated feature, all matches will be
131 reported. Optionally, all loci matching the regular expresion pattern
132 are reorted as BED6 intervals.
133  
134 =head1 OPTIONS
135  
136 =over
137  
138 =item B<fa|f>
139  
140 Reference genome in Fasta format
141  
142 =item B<motif|m>
143  
144 The motif to search for as regular expression.
145  
146 =item B<bed>
147  
148 Switch for printing BED6 intervals of matching motif loci.
149  
150 =item B<name|n>
151  
152 Optional name for BED6 intervals.
153  
154 =back
155  
156 =head1 AUTHOR
157  
158 Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt>
159  
160 =cut