1 # This is -*-Perl-*- code#
2 # Bioperl Test Harness Script for Modules#
3 # $Id: protgraph.t,v 1.1 2004/03/13 23:45:32 radams Exp
5 use vars qw($NUMTESTS $DEBUG $XML_TESTS);
7 $DEBUG = $ENV{'BIOPERLDEBUG'} || 0;
11 eval { require Test::More;};
17 eval { require Class::AutoClass;
20 plan skip_all => "Class::AutoClass or Clone not installed. This means that the module is not usable. Skipping tests";
22 plan tests => $NUMTESTS;
24 eval {require XML::Twig;};
30 use_ok('Bio::Graph::ProteinGraph');
31 use_ok('Bio::Graph::IO');
32 use_ok('Bio::Graph::Edge');
36 unlink Bio::Root::IO->catfile("t","data","out.mif");
41 ################1st of all let's test the io.....
42 ############### test dip tab delimited format ###################
47 ok my $io = Bio::Graph::IO->new(
49 -file => Bio::Root::IO->catfile("t","data","tab1part.mif"),
52 ok $gr = $io->next_network();
54 ok my $node = $gr->nodes_by_id('A64696');
55 is $node->accession_number, 'A64696';
57 ##test write. to filehandle...
59 ok my $out = Bio::Graph::IO->new(
61 -file =>">". Bio::Root::IO->catfile("t","data","out.mif"));
62 ok $out->write_network($gr);
64 ## get articulation_points.
65 my @nodes = $gr->articulation_points();
67 ##now remove 2 nodes: this removes 4 edges and 3087 should be a new artic point
68 is $gr->edge_count, 72;
69 $gr->remove_nodes($gr->nodes_by_id('3082N'), $gr->nodes_by_id('3083N'));
70 is $gr->edge_count, 68;
71 my $nodes = $gr->articulation_points();
72 ok grep {$_->object_id eq 'H64521'} @$nodes;
73 is scalar @$nodes, 13;
74 @nodes = @{$gr->articulation_points()};
76 # these were failing, I don't understand the module enough to know if
77 # this is a bug. Richard needs to look at it
79 skip ('TODO:Possible bug; skipping two tests',2);
80 ok grep {$_->object_id eq 'B64701'} @nodes;
84 ok grep {$_->object_id eq 'B64528'} @nodes;
88 ok $node = $gr->nodes_by_id('A64696');
89 is $node->accession_number, 'A64696';
92 ## can we round trip, is out format same as original format?
93 ok my $io2 = Bio::Graph::IO->new(
95 -file => Bio::Root::IO->catfile("t","data","out.mif"));
96 ok my $g2 = $io2->next_network();
97 ok $node = $g2->nodes_by_id('A64696');
98 is $node->accession_number, 'A64696';
100 ##### now lets test some graph properties.....##
101 ## basic properties from SImpleGraph.
103 is sprintf("%.3f",$g2->density), "0.027";
104 is $g2->is_connected, '';
105 is $g2->is_forest, undef;
107 is $g2->is_empty, '';
108 is $g2->is_cyclic, 1;
110 ## get connected subgraphs
111 my @components = $g2->components();
112 is scalar @components, 5;
114 ## get nodes connected to parameter
115 my $t = $g2->traversal($g2->nodes_by_id('3079N'));
116 my @dfnodes = $t->get_all;
119 ##before deleting 3048N, 3047N has 2 neighbours
120 my @n1 = $g2->neighbors($g2->nodes_by_id('3047N'));
123 ok $g2->remove_nodes($g2->nodes_by_id('3048N'));
125 ## after deleting there is only 1 interactor
126 @n1 = $g2->neighbors($g2->nodes_by_id('3047N'));
129 ##check no undefs left after node removal ##
131 ok map {$_->object_id}$g2->edges;
133 ## get an edge by its id
135 ok my $edge = $g2->edge_by_id('4368E');
136 is $edge->object_id, '4368E';
140 is $g2->edge_count, 71;
142 my @n = $g2->neighbors($g2->nodes_by_id('3075N'));
145 ok $g2->remove_nodes($g2->nodes_by_id('3075N'));
147 ## should be 13 less interactions in graph.
148 is scalar $g2->edge_count, 58;
150 ## many more subgraphs now
151 @components = $g2->components();
152 #there were 5 subgraphs, now there are 10 unconnected nodes, total 15
153 is scalar @components, 15;
155 ## how many unconnected nodes?
156 my @ucnodes = $g2->unconnected_nodes;
157 is scalar @ucnodes, 10;
159 ##get CC using protein object..
160 is sprintf("%.3f", $g2->clustering_coefficient($g2->nodes_by_id('B64525'))), 0.022;
162 #.. and using id string (same as previous, for convenience )
163 is sprintf("%.3f", $g2->clustering_coefficient('B64525')), 0.022;
165 ## test has_node() method
166 is $g2->has_node('B64525'), 1;
167 is $g2->has_node('B64'), 0;
169 ## remove a single duplicate edge
170 ok $g2->remove_dup_edges($g2->nodes_by_id('3103N'));
172 ## remove all duplicate edges
173 ok $g2->remove_dup_edges();
175 ## should now be no duplicates
176 my @dupids = map{$_->object_id()} $g2->dup_edges();
177 is $dupids[0], undef;
179 ########### now we test the 'union()' method to see it conforms to
180 ## the rules described in its documentation:
182 $io = Bio::Graph::IO->new(
184 -file => Bio::Root::IO->catfile("t","data","tab1part.mif"));
185 $gr = $io->next_network();
186 $io2 = Bio::Graph::IO->new(
188 -file => Bio::Root::IO->catfile("t","data","tab1part.mif"));
190 $g2 = $io2->next_network();
192 # First of all we put the same graph into both variables. After a union
193 # graph 1 should be unaffected. Because all edge ids are the same,
194 # all duplicates will be redundant.
195 # therea re 3 duplicates in dataset.
196 my @dups = $gr->dup_edges();
199 @dups = $gr->dup_edges();
201 my @redundant = $gr->redundant_edge();
202 is scalar @redundant, 72;
204 ## now lets do a union with a graph that has some new edges,
205 ## using existing nodes
210 $io = Bio::Graph::IO->new(
212 -file => Bio::Root::IO->catfile("t","data","tab1part.mif"));
213 $gr = $io->next_network();
214 $io2 = Bio::Graph::IO->new(
216 -file => Bio::Root::IO->catfile("t","data","tab2part.mif"));
217 $g2 = $io2->next_network();
218 is $gr->edge_count, 72;
219 is $gr->node_count, 74;
221 #there should be 1 more edge in the graph $gr now, with no new nodes.
223 is $gr->edge_count, 73;
224 is $gr->node_count, 74;
226 ## now lets test a union that has new nodes in $g2
229 $io = Bio::Graph::IO->new
231 -file => Bio::Root::IO->catfile("t","data","tab1part.mif"));
232 $gr = $io->next_network();
233 $io2 = Bio::Graph::IO->new
235 -file => Bio::Root::IO->catfile("t","data","tab3part.mif"));
237 $g2 = $io2->next_network();
238 is $gr->edge_count, 72;
239 is $gr->node_count, 74;
241 # there should be 2 more edge in the graph $gr now and 2 more nodes.
243 is $gr->edge_count, 74;
244 is $gr->node_count, 76;
246 # test IO/psi_xml if the required modules are present
248 skip ('XML::Twig required for psi_xml-based tests. Skipping...',10) if !$XML_TESTS;
250 ok $io = Bio::Graph::IO->new
251 (-format => 'psi_xml',
252 -file => Bio::Root::IO->catfile("t", "data", "psi_xml.dat"));
253 ok my $g = $io->next_network();
254 is $g->edge_count, 3;
255 is $g->node_count, 4;
256 #my @rts =$g->articulation_points();
257 my $n = $g->nodes_by_id(207153);
258 is $n->species->node_name,"Helicobacter pylori 26695";
259 is $n->primary_seq->desc,"bogus-binding membrane protein (lepA) HP0355";
261 # PSI XML from IntAct
262 ok my $io2 = Bio::Graph::IO->new
263 (-format => 'psi_xml',
264 -file => Bio::Root::IO->catfile("t", "data", "sv40_small.xml"));
265 ok my $g3 = $io2->next_network();
266 is $g3->edge_count, 3;
267 is $g3->node_count, 5;
269 my @rts =$g->articulation_points();
270 $n = $g->nodes_by_id(207153);
272 skip('TODO:Possible bug in binomial output',1);
273 is $n->species->binomial(),"Helicobacter pylori 26695";
275 is $n->primary_seq->desc,"bogus-binding membrane protein (lepA) HP0355";