| 1 | #!/usr/bin/env perl | ||
| 2 | # created on 2015-01-14 | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.010; | ||
| 7 | |||
| 8 | use Bio::Gonzales::Seq::IO; | ||
| 9 | use Data::Dumper; | ||
| 10 | use List::MoreUtils qw/indexes/; | ||
| 11 | use Bio::Gonzales::Seq; | ||
| 12 | use Bio::Gonzales::Seq::Filter qw/clean_dna_seq/; | ||
| 13 | |||
| 14 | use Bio::Gonzales::Feat::IO::GFF3; | ||
| 15 | use Bio::Gonzales::Feat; | ||
| 16 | |||
| 17 | use Pod::Usage; | ||
| 18 | use Getopt::Long; | ||
| 19 | |||
| 20 | my %opt = (); | ||
| 21 | GetOptions( \%opt, 'help', 'clean_seq', 'include_junk', 'seq_f=s' ) or pod2usage(2); | ||
| 22 | |||
| 23 | pod2usage( -exitval => 0, -verbose => 2 ) if ( $opt{help} ); | ||
| 24 | pod2usage(2) | ||
| 25 | unless ( @ARGV && @ARGV == 2 ); | ||
| 26 | |||
| 27 | my ( $regex, $file ) = @ARGV; | ||
| 28 | |||
| 29 | pod2usage( { -msg => "regular expression or file argument missing", -exit_val => 2 } ) | ||
| 30 | unless ( $regex && $file ); | ||
| 31 | |||
| 32 | my $gffo = Bio::Gonzales::Feat::IO::GFF3->new( fh => \*STDOUT, mode => '>', escape_whitespace => 1 ); | ||
| 33 | my $seq_fh; | ||
| 34 | |||
| 35 | if ( $opt{seq_f} ) { | ||
| 36 | open $seq_fh, '>', $opt{seq_f} or die "Can't open filehandle: $!"; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $fait = faiterate($file); | ||
| 40 | |||
| 41 | while ( my $so = $fait->() ) { | ||
| 42 | if ( $opt{clean_seq} ) { | ||
| 43 | clean_dna_seq($so); | ||
| 44 | } | ||
| 45 | repl( $regex, $so ); | ||
| 46 | } | ||
| 47 | |||
| 48 | if ($seq_fh) { | ||
| 49 | $seq_fh->close; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub repl { | ||
| 53 | my $regex = shift; | ||
| 54 | my $so = shift; | ||
| 55 | my $id = $so->id; | ||
| 56 | |||
| 57 | my $full_seq = $so->seq; | ||
| 58 | my $last_pos = 0; | ||
| 59 | my $match_cnt = 0; | ||
| 60 | while ( $full_seq =~ /$regex/g ) { | ||
| 61 | $match_cnt++; | ||
| 62 | # we found a "restriction site" | ||
| 63 | if ( $-[0] != 0 ) { | ||
| 64 | # omit strings starting with "restriction site" | ||
| 65 | my $gap = $+[0] - $-[0]; | ||
| 66 | my $match_id = sprintf( "%s|nomatch:%d|%d..%d+%d", $id, $match_cnt, $last_pos + 1, $-[0], $gap ); | ||
| 67 | $gffo->write_feat( | ||
| 68 | Bio::Gonzales::Feat->new( | ||
| 69 | seq_id => $id, | ||
| 70 | source => 'fasplit', | ||
| 71 | type => 'match', | ||
| 72 | start => $last_pos + 1, | ||
| 73 | end => $-[0], | ||
| 74 | strand => 0, | ||
| 75 | attributes => { ID => [$match_id], followed_gap_size => [ $gap ] }, | ||
| 76 | ) | ||
| 77 | ); | ||
| 78 | |||
| 79 | if ($seq_fh) { | ||
| 80 | my $seq = substr $full_seq, $last_pos, $-[0] - $last_pos; | ||
| 81 | # output everything before it | ||
| 82 | faspew( | ||
| 83 | $seq_fh, | ||
| 84 | Bio::Gonzales::Seq->new( | ||
| 85 | id => $match_id, | ||
| 86 | seq => $seq | ||
| 87 | ) | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | if ( $opt{include_junk} ) { | ||
| 92 | $match_cnt++; | ||
| 93 | my $match_id = sprintf( "%s|match:%d|%d..%d", $id, $match_cnt, $-[0] + 1, $+[0] ); | ||
| 94 | $gffo->write_feat( | ||
| 95 | Bio::Gonzales::Feat->new( | ||
| 96 | seq_id => $id, | ||
| 97 | source => 'fasplit', | ||
| 98 | type => 'gap', | ||
| 99 | start => $-[0] + 1, | ||
| 100 | end => $+[0], | ||
| 101 | strand => 0, | ||
| 102 | attributes => { ID => [$match_id] }, | ||
| 103 | ) | ||
| 104 | ); | ||
| 105 | if ($seq_fh) { | ||
| 106 | faspew( | ||
| 107 | $seq_fh, | ||
| 108 | Bio::Gonzales::Seq->new( | ||
| 109 | id => $match_id, | ||
| 110 | seq => substr( $full_seq, $-[0], $+[0] - $-[0] ), | ||
| 111 | ) | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | $last_pos = $+[0]; | ||
| 116 | } | ||
| 117 | if ( $last_pos < length($full_seq) ) { | ||
| 118 | |||
| 119 | my $match_id | ||
| 120 | = sprintf( "%s|nomatch:%d|%d..%d+%d", $id, $match_cnt + 1, $last_pos + 1, length($full_seq), 0 ); | ||
| 121 | $gffo->write_feat( | ||
| 122 | Bio::Gonzales::Feat->new( | ||
| 123 | seq_id => $id, | ||
| 124 | source => 'fasplit', | ||
| 125 | type => 'match', | ||
| 126 | start => $last_pos + 1, | ||
| 127 | end => length($full_seq), | ||
| 128 | strand => 0, | ||
| 129 | attributes => { ID => [$match_id], followed_gap_size => [0] }, | ||
| 130 | ) | ||
| 131 | ); | ||
| 132 | |||
| 133 | if ($seq_fh) { | ||
| 134 | my $seq = substr $full_seq, $last_pos, length($full_seq) - $last_pos; | ||
| 135 | faspew( | ||
| 136 | $seq_fh, | ||
| 137 | Bio::Gonzales::Seq->new( | ||
| 138 | id => $match_id, | ||
| 139 | seq => $seq | ||
| 140 | ) | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | =head1 NAME | ||
| 147 | |||
| 148 | bg_fasplit.pl - split sequences in fasta files by given regular expression | ||
| 149 | |||
| 150 | =head1 SYNOPSIS | ||
| 151 | |||
| 152 | bg_fasplit.pl [options] <regex> <file> | ||
| 153 | |||
| 154 | <regex> the split pattern in form of a perl regular expression | ||
| 155 | <file> input sequence file in fata format | ||
| 156 | |||
| 157 | Options: | ||
| 158 | |||
| 159 | --help brief help message | ||
| 160 | --clean_seq clean DNA sequence (mask unknown characters with N) | ||
| 161 | --include_junk sequence matched by <regex> will be included in the output | ||
| 162 | --seq_f <file> write the split sequences into <file> |