1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#!/usr/bin/perl
#sequence_to_multi_fasta.pl
#version 1.1
#
#This script requires bioperl-1.4 or newer.
#
#Written by Paul Stothard
#stothard@ualberta.ca
use strict;
use warnings;
use Getopt::Long;
use Bio::SeqIO;
my %options = (
input => undef,
output => undef,
size => undef,
overlap => undef,
title => undef,
type => undef
);
Getopt::Long::Configure('bundling');
GetOptions(
'i=s' => \$options{'input'},
'o=s' => \$options{'output'},
's=i' => \$options{'size'},
'v=i' => \$options{'overlap'}
);
if ( !( defined( $options{'input'} ) ) ) {
_usage();
}
if ( !( defined( $options{'output'} ) ) ) {
_usage();
}
my $seqObject = _getSeqObject( \%options );
if ( ( $options{"type"} eq "embl" ) || ( $options{"type"} eq "genbank" ) ) {
$options{"accession"} = $seqObject->accession_number;
if ( !( defined( $options{"title"} ) ) ) {
$options{"title"} = $seqObject->description();
}
}
if ( $options{"type"} eq "fasta" ) {
if ( !( defined( $options{"title"} ) ) ) {
$options{"title"} = $seqObject->description();
}
}
if ( !( defined( $options{"title"} ) ) ) {
$options{"title"} = "split";
}
my $dna = $seqObject->seq();
my $length = length($dna);
$options{'title'} =~ s/\s+/_/g;
if ( !( defined( $options{'size'} ) ) ) {
$options{'size'} = $length;
}
open( OUTFILE, ">" . $options{"output"} ) or die("Cannot open file : $!");
for ( my $i = 0; $i < $length; $i = $i + $options{'size'} ) {
#if using overlap adjust $i
if ( ( defined( $options{'overlap'} ) ) && ( $i > $options{'overlap'} ) )
{
$i = $i - $options{'overlap'};
}
my $start = $i + 1;
my $subseq = substr( $dna, $i, $options{'size'} );
my $subseq_length = length($subseq);
my $end = $start + $subseq_length - 1;
print( OUTFILE ">"
. $options{'title'}
. "_start=$start;end=$end;length=$subseq_length;source_length=$length\n$subseq\n"
);
}
close(OUTFILE) or die("Cannot close file : $!");
sub _getSeqObject {
my $options = shift;
open( INFILE, $options->{'input'} ) or die("Cannot open input file: $!");
while ( my $line = <INFILE> ) {
if ( !( $line =~ m/\S/ ) ) {
next;
}
#guess file type from first line
if ( $line =~ m/^LOCUS\s+/ ) {
$options->{'type'} = "genbank";
}
elsif ( $line =~ m/^ID\s+/ ) {
$options->{'type'} = "embl";
}
elsif ( $line =~ m/^>/ ) {
$options->{'type'} = "fasta";
}
else {
$options->{'type'} = "raw";
}
last;
}
close(INFILE) or die("Cannot close input file: $!");
#get seqobj
my $in = Bio::SeqIO->new(
-format => $options->{'type'},
-file => $options->{'input'}
);
my $seq = $in->next_seq();
return $seq;
}
sub _usage {
die('Usage: sequence_to_multi_fasta.pl -i <input file> -o <output file> -s <size of new sequences>'
);
}
|