File: Fasta_retriever.pm

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (79 lines) | stat: -rwxr-xr-x 1,255 bytes parent folder | download | duplicates (3)
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
package Fasta_retriever;

use strict;
use warnings;
use Carp;

sub new {
    my ($packagename) = shift;
    my $filename = shift;
    
    unless ($filename) {
        confess "Error, need filename as param";
    }

    my $self = { filename => $filename,
                 acc_to_pos_index => {},
                 fh => undef,
    };

    
    
    bless ($self, $packagename);

    $self->_init();


    return($self);
}


sub _init {
    my $self = shift;
    
    my $filename = $self->{filename};
        
    open (my $fh, $filename) or die $!;
    $self->{fh} = $fh;
    while (<$fh>) {
        if (/>(\S+)/) {
            my $acc = $1;
            my $file_pos = tell($fh);
            $self->{acc_to_pos_index}->{$acc} = $file_pos;
        }
    }
    
    return;
}

sub get_seq {
    my $self = shift;
    my $acc = shift;

    unless ($acc) {
        confess "Error, need acc as param";
    }

    my $file_pos = $self->{acc_to_pos_index}->{$acc} or confess "Error, no seek pos for acc: $acc";
    
    my $fh = $self->{fh};
    seek($fh, $file_pos, 0);
    
    my $seq = "";
    while (<$fh>) {
        if (/^>/) {
            last;
        }
        $seq .= $_;
    }

    $seq =~ s/\s+//g;

    return($seq);
}
    
    
    

1; #EOM