File: Ktree.pm

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 468,004 kB
  • sloc: perl: 49,905; cpp: 17,993; java: 12,489; python: 3,282; sh: 1,989; ansic: 985; makefile: 717; xml: 62
file content (156 lines) | stat: -rw-r--r-- 2,516 bytes parent folder | download | duplicates (5)
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
package Ktree;

use strict;
use warnings;
use Carp;

sub new {
    my $packagename = shift;
    
    my $self = { _root => KtreeNode->new("", 0) };

    bless ($self, $packagename);

    return($self);
}

sub add_kmer {
    my $self = shift;
    my ($kmer) = @_;


    unless (defined $kmer) {
        confess "error, require param kmer";
    }

    my $root_node = $self->{_root};

    my @seq = split(//, $kmer);

    my $node = $root_node; 
    do {
        my $char = shift @seq;
        $node = $node->get_child($char);
    } while (@seq);
    
    $node->set_val( $node->get_val() + 1 );
    
    return;
}

sub report_kmer_counts {
    my $self = shift;
    
    my $root_node = $self->{_root};
    
    &_recurse_through_kmer_counts("", $root_node);
    
    return;
}

sub _recurse_through_kmer_counts {
    my ($prefix, $node) = @_;

    my $char = $node->get_char();
    
    my @children_chars = $node->get_children_chars();
    
    if (@children_chars) {
        foreach my $child_char (@children_chars) {
            my $child_node = $node->get_child($child_char);
            &_recurse_through_kmer_counts($prefix . $char, $child_node);
        }
    }
    else {
        # base case
        my $val = $node->get_val();
        print join("\t", $prefix . $char, $val) . "\n";
    }

    return;
}



package KtreeNode;

use strict;
use warnings;
use Carp;


sub new {
    my $packagename = shift;
    my ($char, $val) = @_;

    unless (defined $char && defined $val) {
        confess "Error, require (character, val) as parameter";
    }

    my $self = { char => $char,
                 val => $val,
                 children => {},
    };
    
    bless ($self, $packagename);
    
    return($self);
}




####
sub get_child {
    my $self = shift;
    my ($char) = @_;

    unless (defined $char) {
        confess "error, parameter 'char' required";
    }

    my $child = $self->{children}->{$char};
    unless (ref $child) {
        
        $child = $self->{children}->{$char} = new KtreeNode($char, 0);
    }

    return($child);
}


sub get_children_chars {
    my $self = shift;
    
    my @chars = keys %{$self->{children}};
    return(@chars);
}


sub get_char {
    my $self = shift;
    return($self->{char});
}


####
sub get_val {
    my $self = shift;
    return($self->{val});
}

####
sub set_val {
    my $self = shift;
    my $val = shift;
    unless (defined $val) {
        confess "error, require val as param";
    }

    $self->{val} = $val;
    
    return;
}


1; #EOM