File: sequence_to_multi_fasta.pl

package info (click to toggle)
cct 1%3A1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 194,340 kB
  • sloc: perl: 8,484; sh: 1,693; makefile: 23
file content (168 lines) | stat: -rwxr-xr-x 3,898 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#FILE: sequence_to_multi_fasta.pl
#AUTH: Paul Stothard <stothard@ualberta.ca>
#DATE: June 22, 2020
#VERS: 1.2

use strict;
use warnings;

use Getopt::Long;
use Bio::SeqIO;
use Bio::SeqUtils;

my %options = (
    input   => undef,
    output  => undef,
    size    => undef,
    overlap => undef,
    title   => undef,
    type    => undef,
    help    => undef
);

Getopt::Long::Configure('bundling');
GetOptions(
    'i=s'    => \$options{'input'},
    'o=s'    => \$options{'output'},
    's=i'    => \$options{'size'},
    'v=i'    => \$options{'overlap'},
    'h|help' => \$options{'help'}
);

if ( defined( $options{'help'} ) ) {
    _usage();
    exit(0);
}
if ( !( defined( $options{'input'} ) ) ) {
    _usage();
    exit(1);
}
if ( !( defined( $options{'output'} ) ) ) {
    _usage();
    exit(1);
}

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'}
    );

    #merge multi-contig sequences
    my @seqs = ();

    while ( my $seq = $in->next_seq() ) {
        push( @seqs, $seq );
    }

    Bio::SeqUtils->cat(@seqs);

    return $seqs[0];
}

sub _usage {
    print <<BLOCK;
sequence_to_multi_fasta.pl - split a DNA sequence.

DISPLAY HELP AND EXIT:

usage:

  perl sequence_to_multi_fasta.pl -help

SPLIT DNA SEQUENCE:

usage:

  perl sequence_to_multi_fasta.pl -i <file> -o <file> [Options]

required arguments:

-i - Input file in FASTA, RAW, EMBL, or GenBank format.

-o - Output file in FASTA format of sequence fragments.

optional arguments:

-v - The overlap to include between sequences, in bases. [Integer].

-s - The size of the sequences to create, in bases. [Integer]. Default is to
return the entire sequence as a single FASTA record.

example usage:

  perl sequence_to_multi_fasta.pl -i input.gbk -o output.fasta -s 10000 -v 500
BLOCK
}