| 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__ | ||
| 92 | |||
| 93 | |||
| 94 | =head1 NAME | ||
| 95 | |||
| 96 | fasta_subgrep.pl - Extract subsequence from a (multi) Fasta file | ||
| 97 | |||
| 98 | =head1 SYNOPSIS | ||
| 99 | |||
| 100 | fasta_subgrep.pl [--fa I<FILE>] [-s I<INT>] [-e I<INT>] [-id I<STRING>] [--strand I<+/->] | ||
| 101 | |||
| 102 | =head1 DESCRIPTION | ||
| 103 | |||
| 104 | Extract a subsequence from a (multi) Fasta file. Interval coordinates | ||
| 105 | are passed as start and end coordinates via the C<-s> and C<-e> | ||
| 106 | options, respectively. Output is written to STDOUT. | ||
| 107 | |||
| 108 | =head1 OPTIONS | ||
| 109 | |||
| 110 | =over | ||
| 111 | |||
| 112 | =item B<-f|--fa> | ||
| 113 | |||
| 114 | Input file in Fasta format (mandatory) | ||
| 115 | |||
| 116 | =item B<-s|--start> | ||
| 117 | |||
| 118 | Start of the sequence interval to extract | ||
| 119 | |||
| 120 | =item B<-e|--end> | ||
| 121 | |||
| 122 | End of the sequence interval to extract | ||
| 123 | |||
| 124 | =item B<--id> | ||
| 125 | |||
| 126 | Fasta ID of the sequence to extract from. This is only required for | ||
| 127 | multi Fasta input files. | ||
| 128 | |||
| 129 | =item B<--strand> | ||
| 130 | |||
| 131 | Specify the strand to extract sequence data from. Allowed arguments | ||
| 132 | are C<+> amd C<->. In the latter case, retrieve reverse complement of | ||
| 133 | the sequence interval from C<start> to C<end>. | ||
| 134 | |||
| 135 | =item B<--help -h> | ||
| 136 | |||
| 137 | Print short help | ||
| 138 | |||
| 139 | =item B<--man> | ||
| 140 | |||
| 141 | Prints the manual page and exits | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =head1 AUTHOR | ||
| 146 | |||
| 147 | Michael T. Wolfinger E<lt>michael@wolfinger.euE<gt> | ||
| 148 | |||
| 149 | =cut | ||
| 150 |