1 # This is -*-Perl-*- code#
2 # Bioperl Test Harness Script for Modules
9 test_begin(-tests => 171,
10 -requires_module => 'Graph',
11 -requires_module => 'XML::Twig' );
13 use_ok('Bio::Network::ProteinNet');
14 use_ok('Bio::Network::IO');
17 my $verbose = test_debug();
22 my $io = Bio::Network::IO->new(
24 -file => test_input_file("tab1part.tab"),
27 ok my $g1 = $io->next_network();
28 ok my $node = $g1->get_nodes_by_id('PIR:A64696');
29 my @proteins = $node->proteins;
30 ok $proteins[0]->accession_number eq 'PIR:A64696';
31 ok $node = $g1->nodes_by_id('PIR:A64696');
32 @proteins = $node->proteins;
33 ok $proteins[0]->accession_number eq 'PIR:A64696';
34 my %ids = $g1->get_ids_by_node($node);
36 my @ids = qw(A64696 2314583 3053N);
37 for my $k (keys %ids) {
38 ok ( $ids{$k} eq $ids[$x++] );
44 ok $g1->vertices == 76;
45 # now remove 2 nodes: this removes 4 edges
46 my $g2 = $g1->delete_vertices($g1->get_nodes_by_id('DIP:3082N'),
47 $g1->get_nodes_by_id('DIP:3083N') );
49 ok $g2->vertices == 74;
51 # test for identifiers and Annotations
53 ok $node = $g1->get_nodes_by_id('PIR:A64696');
54 @proteins = $node->proteins;
55 ok $proteins[0]->accession_number eq 'PIR:A64696';
56 my $ac = $proteins[0]->annotation;
57 @ids = $ac->get_Annotations('dblink');
58 ok $ids[0]->primary_id eq "A64696";
59 ok $ids[1]->primary_id eq "3053N";
60 ok $ids[2]->primary_id eq "2314583";
62 # test some basic graph properties from Graph
64 ok sprintf("%.3f",$g2->density), "0.028";
65 ok $g2->is_connected eq "";
66 ok $g2->is_forest eq "";
67 ok $g2->is_tree eq "";
68 ok $g2->is_empty eq "";
69 ok $g2->is_cyclic == 1;
70 ok $g2->expect_undirected;
72 # get connected subgraphs
74 my @components = $g2->connected_components();
75 ok scalar @components == 7;
77 # before deleting 3048N, 3047N has 2 neighbours
79 my @n1 = $g2->neighbors($g2->get_nodes_by_id('DIP:3047N'));
82 ok $g2->delete_vertices($g2->get_nodes_by_id('DIP:3048N'));
84 # after deleting there is only 1 interactor
86 @n1 = $g2->neighbors($g2->get_nodes_by_id('DIP:3047N'));
88 my $ncount = $g2->neighbor_count($g2->get_nodes_by_id('DIP:3047N'));
91 # check that no undefs are left after node removal
93 my @edges = $g2->edges;
94 for my $edgeref (@edges) {
95 my %interactions = $g2->get_interactions(@$edgeref);
96 for my $interaction (values %interactions) {
97 ok $interaction->primary_id;
101 # get an Interaction by its id
103 ok my $interx = $g2->get_interaction_by_id('DIP:4368E');
104 ok $interx->primary_id eq 'DIP:4368E';
111 my @n = $g2->neighbors($g2->get_nodes_by_id('DIP:3075N'));
114 ok $g2->remove_nodes($g2->get_nodes_by_id('DIP:3075N'));
115 ok scalar $g2->edges == 62;
117 # test connected_components
119 @components = $g2->connected_components();
120 ok scalar @components == 17;
122 # test isolated_vertices
124 my @ucnodes = $g2->isolated_vertices;
125 ok scalar @ucnodes == 10;
127 # test clustering_coefficent
129 ok sprintf("%.3f", $g2->clustering_coefficient($g2->get_nodes_by_id('PIR:B64525'))) == 0.022;
131 # test get_nodes_by_id() method
133 ok defined $g2->get_nodes_by_id('PIR:B64525');
134 is $g2->get_nodes_by_id('B64'), undef;
138 $io = Bio::Network::IO->new
140 -file => test_input_file("bovin_small_intact.xml"));
141 my $g = $io->next_network();
145 @ids = qw(EBI-354674 EBI-444335 EBI-349968 EBI-354657
146 EBI-302230 EBI-640775 EBI-640793 EBI-79764);
147 my @nodes = $g->get_nodes_by_id(@ids);
148 ok scalar @nodes == 8;
149 my $sg = $g->subgraph(@nodes);
153 @nodes = $g->get_nodes_by_id($ids[0]);
154 $sg = $g->subgraph(@nodes);
158 # test internal method _all_pairs()
160 my @pairs = $g->_all_pairs(@ids);
162 @pairs = $g->_all_pairs("A","B");
163 ok scalar @pairs == 1;
165 # test the add_interactions_from() method
167 $io = Bio::Network::IO->new
168 (-format => 'dip_tab',
169 -file => test_input_file("tab4part.tab"));
170 $g1 = $io->next_network();
172 my $io2 = Bio::Network::IO->new
173 (-format => 'dip_tab',
174 -file => test_input_file("tab3part.tab"));
175 $g2 = $io2->next_network();
182 ok my $node1 = $g1->get_nodes_by_id("UniProt:Q09472");
183 ok my $node2 = $g1->get_nodes_by_id("UniProt:P04637");
184 my %interx = $g1->get_interactions($node1,$node2);
185 ok scalar keys %interx == 1;
187 ok $node1 = $g1->get_nodes_by_id("UniProt:P10243");
188 ok $node2 = $g1->get_nodes_by_id("GenBank:2134877");
189 %interx = $g1->get_interactions($node1,$node2);
190 ok scalar keys %interx == 1;
192 is my $ix = $g1->get_interaction_by_id("DIP:16E"), undef;
193 is $ix = $g1->get_interaction_by_id("DIP:19E"), undef;
195 $g1->add_interactions_from($g2);
197 # $g1 should have 2 more Interactions with new interaction ids and
198 # the same number of nodes and edges, $g2 should be unaffected
205 ok $node1 = $g1->get_nodes_by_id("UniProt:Q09472");
206 ok $node2 = $g1->get_nodes_by_id("UniProt:P04637");
207 %interx = $g1->get_interactions($node1,$node2);
208 ok scalar keys %interx == 2;
210 ok $node1 = $g1->get_nodes_by_id("UniProt:P10243");
211 ok $node2 = $g1->get_nodes_by_id("GenBank:2134877");
212 %interx = $g1->get_interactions($node1,$node2);
213 ok scalar keys %interx == 2;
215 ok $ix = $g1->get_interaction_by_id("DIP:16E");
217 ok $ix = $g1->get_interaction_by_id("DIP:19E");
218 ok $ix->weight == 12;
220 # test that removing a node removes its edges correctly
222 ok $io = Bio::Network::IO->new
224 -file => test_input_file("sv40_small.xml"));
225 ok $g1 = $io->next_network();
226 ok $g1->edge_count == 3;
227 ok $g1->node_count == 5;
228 ok $g1->is_connected == 0;
229 @components = $g1->connected_components;
230 ok scalar @components == 2;
232 my $n = $g1->get_nodes_by_id("EBI-617321");
233 my @ns = $g1->edges_at($n);
235 $g1->remove_nodes($n);
236 ok $g1->edge_count == 1;
237 ok $g1->node_count == 4;
238 @components = $g1->connected_components;
239 ok scalar @components == 3;
240 @ns = $g1->isolated_vertices;
242 @ns = $g1->unconnected_nodes;
244 @ns = $g1->self_loop_vertices;
249 @components = $g1->components;
250 ok scalar @components == 3;
254 $n = $g1->get_random_node;
255 isa_ok( $n, 'Bio::Network::Node');
256 my $e = $g1->get_random_edge;
257 isa_ok( $e->[0], 'Bio::Network::Node');