cleanup and convert to Test::More
[bioperl-live.git] / t / protgraph.t
blob33d455a5d7d1d601de77f93f488c8f40467c6a9e
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);
6 use strict;
7 $DEBUG = $ENV{'BIOPERLDEBUG'} || 0;
8 use Bio::Root::IO;
10 BEGIN {
11     eval { require Test::More;};
12     if ( $@ ) {
13                 use lib 't/lib';
14     }
15     use Test::More;
16     $NUMTESTS  = 72;
17     eval {      require Class::AutoClass;
18                 require Clone; };
19     if ( $@ ) {
20                 plan skip_all => "Class::AutoClass or Clone not installed. This means that the module is not usable. Skipping tests";
21     } else {
22                 plan tests => $NUMTESTS;
23         }
24         eval {require XML::Twig;};
25     if ($@) {
26                 $XML_TESTS = 0;
27         } else {
28                 $XML_TESTS = 1;
29         }
30         use_ok('Bio::Graph::ProteinGraph');
31         use_ok('Bio::Graph::IO');
32         use_ok('Bio::Graph::Edge');
35 END {
36     unlink Bio::Root::IO->catfile("t","data","out.mif");
39 my $verbose = $DEBUG;
41 ################1st of all let's test the io.....
42 ###############  test dip tab delimited  format  ###################
43 ## test read...
45 my %ids;
46 my $gr;
47 ok my $io = Bio::Graph::IO->new(
48   -format => 'dip',
49   -file   => Bio::Root::IO->catfile("t","data","tab1part.mif"),
50   -threshold => 0.6);
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(
60   -format => 'dip',
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()};
75 # <NOTE>
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
78 SKIP: {
79         skip ('TODO:Possible bug; skipping two tests',2);
80         ok grep {$_->object_id eq 'B64701'} @nodes;
81         is scalar @nodes, 14;
84 ok grep {$_->object_id eq 'B64528'} @nodes;
85 is scalar @nodes, 13;
86 # </NOTE>
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(
94   -format    => 'dip',
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;
106 is $g2->is_tree, '';
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'));
121 is scalar @n1,2;
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'));
127 is scalar @n1,1;
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';
138 ## count all edges
139 my $count = 0;
140 is $g2->edge_count, 71;
142 my @n = $g2->neighbors($g2->nodes_by_id('3075N'));
143 is scalar @n, 13;
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(
183    -format => 'dip',
184    -file   => Bio::Root::IO->catfile("t","data","tab1part.mif"));
185 $gr = $io->next_network();
186 $io2 = Bio::Graph::IO->new(
187    -format => 'dip',
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();
197 is scalar @dups, 3;
198 $gr->union($g2);
199 @dups = $gr->dup_edges();
200 is scalar @dups, 3;
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
207 ##read in graph data
208 $gr = undef;
209 $g2 = undef;
210 $io = Bio::Graph::IO->new(
211    -format => 'dip',
212    -file   => Bio::Root::IO->catfile("t","data","tab1part.mif"));
213 $gr = $io->next_network();
214 $io2 = Bio::Graph::IO->new(
215     -format => 'dip',
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;
220 $gr->union($g2);
221 #there should be 1 more edge in the graph $gr now, with no new nodes. 
222 #$g2 is unaffected.  
223 is $gr->edge_count, 73;
224 is $gr->node_count, 74;
226 ## now lets test a union that has new nodes in $g2 
227 $gr = undef;
228 $g2 = undef;
229 $io = Bio::Graph::IO->new
230     (-format => 'dip',
231      -file   => Bio::Root::IO->catfile("t","data","tab1part.mif"));
232 $gr = $io->next_network();
233 $io2 = Bio::Graph::IO->new
234     (-format => 'dip',
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;
240 $gr->union($g2);
241 # there should be 2 more edge in the graph $gr now and 2 more nodes. 
242 # $g2 is unaffected.  
243 is $gr->edge_count, 74;
244 is $gr->node_count, 76;
246 # test IO/psi_xml if the required modules are present
247 SKIP: {
248         skip ('XML::Twig required for psi_xml-based tests.  Skipping...',10) if !$XML_TESTS;
249         # PSI XML from DIP
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);
271         SKIP: {
272                 skip('TODO:Possible bug in binomial output',1);
273                 is $n->species->binomial(),"Helicobacter pylori 26695";
274         }
275         is $n->primary_seq->desc,"bogus-binding membrane protein (lepA) HP0355";