File: make_cvtermpath.pl

package info (click to toggle)
libchado-perl 1.22-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 24,024 kB
  • sloc: xml: 192,540; sql: 165,936; perl: 28,298; sh: 101; python: 73; makefile: 46
file content (170 lines) | stat: -rwxr-xr-x 4,877 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl

# assumes empty password--that should be fixed

use strict;

use DBI;
use Bio::OntologyIO;
use Bio::Ontology::TermFactory;

my ($user, $dbname, $cvname) = @ARGV;
die "USAGE: $0 <username> <dbname> <cvname>" unless $user and $dbname and $cvname;

my $db = DBI->connect("dbi:Pg:dbname=$dbname",$user,'');
my $sth_objects     = $db->prepare("select object_id from cvterm_relationship where subject_id = ? and type_id = ?");
my $sth_subjects    = $db->prepare("select subject_id from cvterm_relationship where object_id = ? and type_id = ?");
my $sth_allobjects  = $db->prepare("select object_id from cvterm_relationship where subject_id = ?");
my $sth_allsubjects = $db->prepare("select subject_id from cvterm_relationship where object_id = ?");

my %type;
my %subject;
my %object;
my %black;
my(%root,%leaf);
my %sot;

my $sth_type = $db->prepare("select cvterm_id from cvterm where cv_id = (select cv_id from cv where name = 'Relationship Ontology')");
$sth_type->execute;
while(my $type_id = $sth_type->fetchrow){
  $type{$type_id}++;
}

my %cvterm;
my $sth_cvterm = $db->prepare("select cvterm_id from cvterm");
$sth_cvterm->execute;
while(my $cvterm_id = $sth_cvterm->fetchrow_array){
  $cvterm{$cvterm_id}++;
}



my $cv_id;
warn "select cv_id from cv where name = '$cvname'";
my $sth_cv = $db->prepare("select cv_id from cv where name = '$cvname'");
$sth_cv->execute;
while(my $cv = $sth_cv->fetchrow_hashref){
  $cv_id = $cv->{cv_id};
}

die "no cv_id for '$cvname'" unless defined $cv_id;

#my $sth_cvterm_relationship = $db->prepare("select subject_id,type_id,object_id from cvterm_relationship");

my $sth_cvterm_relationship = $db->prepare("select subject_id,type_id,object_id from cvterm_relationship,cvterm where cvterm_relationship.subject_id = cvterm.cvterm_id and cvterm.cv_id = $cv_id");
$sth_cvterm_relationship->execute;
while(my $cvterm_relationship = $sth_cvterm_relationship->fetchrow_hashref){
  $subject{$cvterm_relationship->{subject_id}}++;
  $object{$cvterm_relationship->{object_id}}++;
  $sot{$cvterm_relationship->{subject_id}}{$cvterm_relationship->{object_id}}{$cvterm_relationship->{type_id}}++;
}

foreach my $cvterm (keys %cvterm){
  $root{$cvterm}++ if(!$subject{$cvterm} and  $object{$cvterm});
  $leaf{$cvterm}++ if( $subject{$cvterm} and !$object{$cvterm});
}

my %leafbak = %leaf;
%sot = ();

while(keys %leaf){
  foreach my $leaf (keys %leaf){
	foreach my $type (keys %type){
	  recurse([$leaf],$type,1);
	}
	delete $leaf{$leaf};
  }

  #  print "**************************************\n";
}

%leaf = %leafbak;

while(keys %leaf){
  foreach my $leaf (keys %leaf){
	recurse([$leaf],undef,1);
	delete $leaf{$leaf};
  }

  #  print "**************************************\n";
}


sub recurse {
  my($subjects,$type,$dist) = @_;

  my $subject = $subjects->[-1];
#  print $subject,"\n";

  my @objects = objects($subject,$type);
  if(!@objects){
	$leaf{$subject}++;
	return;
  }

  foreach my $object (@objects){
	my $tdist = $dist;
	foreach my $s (@$subjects){
	  next if $sot{$s}{$object}{$type}{$tdist};
	  $sot{$s}{$object}{$type}{$tdist}++;

#	  print $tdist,"\t"x$dist,"\t",$s,"\t",$object,"\t",$type||'transitive',"\n";
	  if(defined $type){
		print "insert into cvtermpath (subject_id,object_id,type_id,cv_id,pathdistance) values
                                      ($s,$object,$type,$cv_id,$tdist);\n";
		my $ttdist = -1 * $tdist;
		print "insert into cvtermpath (subject_id,object_id,type_id,cv_id,pathdistance) values
                                      ($object,$subject,$type,$cv_id,$ttdist);\n";
	  } else {
		print "insert into cvtermpath (subject_id,object_id,type_id,cv_id,pathdistance) values
                                      ($s,$object,(select cvterm_id from cvterm where name = 'OBO_REL:0001'),$cv_id,$tdist);\n";
		print "insert into cvtermpath (subject_id,object_id,type_id,cv_id,pathdistance) values
                                      ($object,$subject,(select cvterm_id from cvterm where name = 'OBO_REL:0001'),$cv_id,-$tdist);\n";
	  }
	  $tdist--;
	}
	$tdist = $dist;
	recurse([@$subjects,$object],$type,$dist+1);
  }

}

#-------------------


sub objects {
  my($subject,$type) = @_;
#warn $subject;
  my @objects;
  if(defined($type)){
	$sth_objects->execute($subject,$type);
	while(my $object = $sth_objects->fetchrow_array){
	  push @objects, $object;
	}
  } else {
	$sth_allobjects->execute($subject);
	while(my $object = $sth_allobjects->fetchrow_array){
	  push @objects, $object;
	}
  }

  return @objects;
}

sub subjects {
  my($object,$type) = @_;
  my @subjects;
  if(defined($type)){
	$sth_subjects->execute($object,$type);
	while(my $subject = $sth_subjects->fetchrow_array){
	  push @subjects, $subject;
	}
  } else {
	$sth_allsubjects->execute($object);
	while(my $subject = $sth_allsubjects->fetchrow_array){
	  push @subjects, $subject;
	}
  }

  return @subjects;
}