J'ai un objet Bio::Tree::Node que j'aimerais parcourir de façon récursive.
Code non récursif
A chaque fois qu'on s'enfonce plus en profondeur, on décale l'affichage d'une tabulation.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 foreach my $node (@nodes){ if (defined $node->id()){ print "\tid : ".$node->id()."\n"; } if (defined $node->branch_length()){ print "\tbranche length : ".$node->branch_length()."\n"; } if (defined $node->ancestor()){ print "\tancestor : ".$node->ancestor()."\n"; } if (defined $node->_creation_id()){ # a private method signifying the internal creation order print "\t_creation_id : ".$node->_creation_id()."\n"; } my @nodes = $node->get_all_Descendents; foreach my $n (@nodes){ if (defined $n->id()){ print "\tid : ".$n->id()."\n"; } if (defined $n->branch_length()){ print "\tbranche length : ".$n->branch_length()."\n"; } if (defined $n->ancestor()){ print "\tancestor : ".$n->ancestor()."\n"; } if (defined $n->_creation_id()){ # a private method signifying the internal creation order print "\t_creation_id : ".$n->_creation_id()."\n"; } if (defined $n->get_all_Descendents){ my @nodes = $n->get_all_Descendents; # ... } } print "\n\n"; }
Voici le code que j'ai essayé :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 tree_reader(\@nodes, qq{}); sub tree_reader { my $ref_nodes = $_[0]; my $tree_shift = $_[1]; $tree_shift .= "\t"; my $next_branches; foreach my $node (@{$ref_nodes}){ if (defined $node->id()){ print $tree_shift."\tid : ".$node->id()."\n"; } if (defined $node->branch_length()){ print $tree_shift."branche length : ".$node->branch_length()."\n"; } if (defined $node->ancestor()){ print $tree_shift."ancestor : ".$node->ancestor()."\n"; } if (defined $node->_creation_id()){ # a private method signifying the internal creation order print $tree_shift."_creation_id : ".$node->_creation_id()."\n"; } if (defined $node->get_all_Descendents){ my @nodes = $node->get_all_Descendents; $next_branches = \@nodes; } print "\n\n"; } if (defined $next_branches){ tree_reader($next_branches, $tree_shift); } }
Voici le contenu du @node de départ
Merci pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 $VAR1 = bless( { '_adding_descendent' => 0, '_root_cleanup_methods' => [ sub { "DUMMY" } ], '_creation_id' => 7, '_id' => 'TRICHOTOMY', '_root_verbose' => 0, '_desc' => { '6' => bless( { '_ancestor' => $VAR1, '_id' => 'P11c', '_desc' => {}, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_branch_length' => '0.88041', '_creation_id' => 6, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ), '0' => bless( { '_ancestor' => $VAR1, '_id' => 'P3A', '_desc' => {}, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_branch_length' => '0.62174', '_creation_id' => 0, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ), '5' => bless( { '_ancestor' => $VAR1, '_id' => '573', '_desc' => { '4' => bless( { '_ancestor' => $VAR1->{'_desc'}{'5'}, '_id' => 'P9e', '_desc' => {}, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_branch_length' => '0.62480', '_creation_id' => 4, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ), '3' => bless( { '_ancestor' => $VAR1->{'_desc'}{'5'}, '_id' => '779', '_desc' => { '1' => bless( { '_ancestor' => $VAR1->{'_desc'}{'5'}{'_desc'}{'3'}, '_id' => 'P4a', '_desc' => {}, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_branch_length' => '0.51661', '_creation_id' => 1, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ), '2' => bless( { '_ancestor' => $VAR1->{'_desc'}{'5'}{'_desc'}{'3'}, '_id' => 'P10f', '_desc' => {}, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_branch_length' => '0.60976', '_creation_id' => 2, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ) }, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_adding_descendent' => 0, '_branch_length' => '0.10722', '_creation_id' => 3, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ) }, '_root_cleanup_methods' => [ $VAR1->{'_root_cleanup_methods'}[0] ], '_adding_descendent' => 0, '_branch_length' => '0.05625', '_creation_id' => 5, '_root_verbose' => 0, '_height' => undef }, 'Bio::Tree::Node' ) }, '_height' => undef }, 'Bio::Tree::Node' );
Partager