| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2017-07-02 12:49:01 mtw> | ||
| 4 | # | ||
| 5 | # Extract subsequences from a (multi) Fasta file | ||
| 6 | # | ||
| 7 | # *********************************************************************** | ||
| 8 | # * Copyright notice | ||
| 9 | # * | ||
| 10 | # * Copyright 2017 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 11 | # * All rights reserved | ||
| 12 | # * | ||
| 13 | # * This program is free software: you can redistribute it and/or modify | ||
| 14 | # * it under the terms of the GNU General Public License as published by | ||
| 15 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 16 | # * (at your option) any later version. | ||
| 17 | # * | ||
| 18 | # * This program is distributed in the hope that it will be useful, | ||
| 19 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 20 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 21 | # * GNU General Public License for more details. | ||
| 22 | # * | ||
| 23 | # * You should have received a copy of the GNU General Public License | ||
| 24 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 25 | # * | ||
| 26 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 27 | # *********************************************************************** | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 32 | use Pod::Usage; | ||
| 33 | use Data::Dumper; | ||
| 34 | use Path::Class; | ||
| 35 | use Bio::ViennaNGS::Fasta; | ||
| 36 | use Carp; | ||
| 37 | use Cwd; | ||
| 38 | use Data::Dumper; | ||
| 39 | |||
| 40 | my ($fastaO,$cwd,$seq); | ||
| 41 | my $fa_in = undef; | ||
| 42 | my $start = undef; | ||
| 43 | my $end = undef; | ||
| 44 | my $id = undef; | ||
| 45 | my $strand = "+"; | ||
| 46 | |||
| 47 | Getopt::Long::config('no_ignore_case'); | ||
| 48 | pod2usage(-verbose => 1) unless GetOptions('f|fa=s' => \$fa_in, | ||
| 49 | 's|start=s' => \$start, | ||
| 50 | 'e|end=s' => \$end, | ||
| 51 | 'id=s' => \$id, | ||
| 52 | 'strand=s' => \$strand, | ||
| 53 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 54 | "h|help" => sub{pod2usage(1)} | ||
| 55 | ); | ||
| 56 | |||
| 57 | unless (-f $fa_in){ | ||
| 58 | warn "Could not find input FASTA input file provided via -f|--fa option"; | ||
| 59 | pod2usage(-verbose => 0); | ||
| 60 | } | ||
| 61 | $cwd = getcwd(); | ||
| 62 | unless ($fa_in =~ /^\// || $fa_in =~ /^\.\//){$fa_in = file($cwd,$fa_in);} | ||
| 63 | croak "ERROR: Option -s|--start must be provided" | ||
| 64 | unless (defined($start)); | ||
| 65 | croak "ERROR: Option -e|--end must be provided" | ||
| 66 | unless (defined($end)); | ||
| 67 | $strand eq '-' ? ($strand = '-') : ($strand = '+') ; | ||
| 68 | |||
| 69 | $fastaO = Bio::ViennaNGS::Fasta->new(fasta=>"$fa_in"); | ||
| 70 | #print Dumper($fastaO); | ||
| 71 | if(scalar(@{$fastaO->fastaids}) > 1){ # multi-Fasta file | ||
| 72 | croak "ERROR: Option --id must be provided for multi-Fasta input files" | ||
| 73 | unless (defined($id)); | ||
| 74 | } | ||
| 75 | else { # single-Fasta file | ||
| 76 | carp "WARNING: The ID provided ($id) does not match the single ID presentin the input Fasta file (${$fastaO->fastaids}[0]). Using ${$fastaO->fastaids}[0] !" | ||
| 77 | unless ($id eq ${$fastaO->fastaids}[0]); | ||
| 78 | $id = ${$fastaO->fastaids}[0]; | ||
| 79 | } | ||
| 80 | |||
| 81 | confess "[ERROR]: ID $id not found in Fasta file $fa_in" | ||
| 82 | unless ($fastaO->has_sequid($id)); | ||
| 83 | $seq = $fastaO->stranded_subsequence($id, | ||
| 84 | $start, | ||
| 85 | $end, | ||
| 86 | $strand); | ||
| 87 | print ">$id\n"; | ||
| 88 | print join "\n", (unpack "(a70)*",$seq); | ||
| 89 | print "\n"; | ||
| 90 | |||
| 91 | __END__ |