| 1 | #!/usr/bin/env perl | ||
| 2 | # -*-CPerl-*- | ||
| 3 | # Last changed Time-stamp: <2017-07-18 09:22:28 mtw> | ||
| 4 | # | ||
| 5 | # Extract nucleotide and amino acid sequence data from Fasta file, | ||
| 6 | # provided by a BED file | ||
| 7 | # | ||
| 8 | # *********************************************************************** | ||
| 9 | # * Copyright notice | ||
| 10 | # * | ||
| 11 | # * Copyright 2017 Michael T. Wolfinger <michael@wolfinger.eu> | ||
| 12 | # * All rights reserved | ||
| 13 | # * | ||
| 14 | # * This program is free software: you can redistribute it and/or modify | ||
| 15 | # * it under the terms of the GNU General Public License as published by | ||
| 16 | # * the Free Software Foundation, either version 3 of the License, or | ||
| 17 | # * (at your option) any later version. | ||
| 18 | # * | ||
| 19 | # * This program is distributed in the hope that it will be useful, | ||
| 20 | # * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | # * GNU General Public License for more details. | ||
| 23 | # * | ||
| 24 | # * You should have received a copy of the GNU General Public License | ||
| 25 | # * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 26 | # * | ||
| 27 | # * This copyright notice MUST APPEAR in all copies of the script! | ||
| 28 | # *********************************************************************** | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | use Getopt::Long qw( :config posix_default bundling no_ignore_case ); | ||
| 33 | use Pod::Usage; | ||
| 34 | use Data::Dumper; | ||
| 35 | use Cwd; | ||
| 36 | use Carp; | ||
| 37 | use Path::Class; | ||
| 38 | use Bio::ViennaNGS::Fasta; | ||
| 39 | use Bio::ViennaNGS::FeatureIO; | ||
| 40 | use Bio::SeqUtils; | ||
| 41 | |||
| 42 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 43 | #^^^^^^^^^^ Variables ^^^^^^^^^^^# | ||
| 44 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# | ||
| 45 | |||
| 46 | my ($fastaO,$bed,$line,$cwd,$f,$seq,$theid,$intervals,$out); | ||
| 47 | my $bed_in = undef; | ||
| 48 | my $fa_in = undef; | ||
| 49 | my $stdout = 0; | ||
| 50 | my $translate = 0; # translate nucleotide sequence | ||
| 51 | |||
| 52 | Getopt::Long::config('no_ignore_case'); | ||
| 53 | pod2usage(-verbose => 1) unless GetOptions("bed=s" => \$bed_in, | ||
| 54 | "fa=s" => \$fa_in, | ||
| 55 | "a|aa" => sub{$translate=1}, | ||
| 56 | "s|stdout" => sub{$stdout=1}, | ||
| 57 | "man" => sub{pod2usage(-verbose => 2)}, | ||
| 58 | "help|h" => sub{pod2usage(1)} | ||
| 59 | ); | ||
| 60 | |||
| 61 | unless(-f $bed_in){ | ||
| 62 | warn "Could not find input BED input file provided via ---bed option"; | ||
| 63 | pod2usage(-verbose => 0); | ||
| 64 | } | ||
| 65 | unless(-f $fa_in){ | ||
| 66 | warn "Could not find input FASTA input file provided via ---fa option"; | ||
| 67 | pod2usage(-verbose => 0); | ||
| 68 | } | ||
| 69 | $cwd = getcwd(); | ||
| 70 | unless ($bed_in =~ /^\// || $bed_in =~ /^\.\//){$bed_in = file($cwd,$bed_in);} | ||
| 71 | unless ($fa_in =~ /^\// || $fa_in =~ /^\.\//){$fa_in = file($cwd,$fa_in);} | ||
| 72 | |||
| 73 | $fastaO = Bio::ViennaNGS::Fasta->new(fasta=>"$fa_in"); | ||
| 74 | $intervals = Bio::ViennaNGS::FeatureIO->new( | ||
| 75 | file => "$bed_in", | ||
| 76 | filetype => 'Bed6', | ||
| 77 | instanceOf => 'Feature', | ||
| 78 | ); | ||
| 79 | |||
| 80 | my $fn = "sequence.fa"; | ||
| 81 | $stdout == 0 ? open($out, ">", $fn) : open($out, ">&STDOUT"); | ||
| 82 | |||
| 83 | foreach $f (@{$intervals->data}){ | ||
| 84 | # print Dumper($f); | ||
| 85 | confess "ERROR: ID ".$f->chromosome." not found in Fasta file $fa_in" | ||
| 86 | unless ($fastaO->has_sequid($f->chromosome)); | ||
| 87 | $seq = $fastaO->stranded_subsequence($f->chromosome, | ||
| 88 | $f->start+1, | ||
| 89 | $f->end, | ||
| 90 | $f->strand); | ||
| 91 | #my $fn = $f->chromosome.".".$f->name.".".eval($f->start+1)."-".$f->end.".fa"; | ||
| 92 | |||
| 93 | my $newid = $f->chromosome."|".$f->name."|".eval($f->start+1)."-".$f->end."|"; | ||
| 94 | print "++ $newid\n"; | ||
| 95 | |||
| 96 | print $out ">$newid\n"; | ||
| 97 | print $out join "\n", (unpack "(a70)*",$seq); | ||
| 98 | print $out "\n"; | ||
| 99 | if($translate == 1){ | ||
| 100 | my $seqobj = Bio::Seq->new (-seq => $seq, | ||
| 101 | -id => 'foobar', | ||
| 102 | -accession_number => 'ABCD'); | ||
| 103 | my @prots = Bio::SeqUtils->translate_3frames($seqobj); | ||
| 104 | my $i=1; | ||
| 105 | foreach my $s (@prots){ | ||
| 106 | print $out ">$newid|AA frame $i|\n"; $i++; | ||
| 107 | print $out join "\n", (unpack "(a70)*",$s->seq); | ||
| 108 | print $out "\n"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | close $out; | ||
| 113 | |||
| 114 | __END__ |