| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2018-03-08 11:52:26 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)/gi){ # 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__ |