-
Notifications
You must be signed in to change notification settings - Fork 1
/
relations.php
208 lines (168 loc) · 5.6 KB
/
relations.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
$currentDir = getcwd();
chdir( $currentDir );
if ( php_sapi_name() == 'cli' ) {
$argsMsg = "Available options :
Note: Parameters are Case sensitive
a)Update => method=update
Params: [type]=[path to score file], Seperate multiple entries by space
Example: php relations.php method=update xref=xref.txt label=label.txt
b)Get Relations => method=relation
Params: [param-name]=[param-value], Separate multiple entries by &
Available Params: pwId_1, pwId_2, minScore, type(xref/label/ontology), species(replace the space with a -)
Example: php relations.php method=relation pwId_1=WP500 pwId_2=WP520 minscore=5 type=xref species=Homo-sapiens\n
";
if ( $argv[0] == 'relations.php' ) {
if ( $argc >= 2 ) {
parse_str( $argv[1], $args );
switch ( $args['method'] ) {
case 'update':
$relationType = [];
for ( $i = 2; $i < count( $argv ); $i++ ) {
parse_str( $argv[$i], $params );
if ( count( $params ) == 0 ) {
echo "Error: Please enter proper parameters\n\n";
echo $argsMsg;
} else {
foreach ( $params as $type => $file ) {
if ( $type == '' || $file == '' ) {
echo "Error in parameters";
exit();
}
$relationType[] = [ 'type' => $type, 'file' => $file ];
}
}
}
Relations::updateDb( $relationType );
break;
case 'relation':
$type = "";
$pwId_1 = "";
$pwId_2 = "";
$minscore = "";
$species = "";
for ( $i = 2; $i < count( $argv ); $i++ ) {
parse_str( $argv[$i] );
}
if ( $species != "" ) {
$species = str_replace( "-", " ", $species );
}
// echo $type . " " . $pwId_1 . " " . $pwId_2 . " " . $minscore;
$results = Relations::fetchRelations( $type, $pwId_1, $pwId_2, $minscore, $species );
echo "Pathway Id 1\tPathway Id 2\tScore\tRelation type\tSpecies\n";
if ( count( $results ) > 0 ) {
foreach ( $results as $relation ) {
echo "{$relation->pwId_1}\t{$relation->pwId_2}\t{$relation->score}\t{$relation->type}\t{$relation->species}\n";
}
}
break;
default:
echo "Error in parameters\n\n";
echo $argsMsg;
exit();
}
} else {
echo $argsMsg;
exit();
}
}
}
class Relations {
public static $_relationsTable = "relations";
private static $_pathwaySpecies = [];
public static function lastUpdated() {
$result['xref'] = date( "F d, Y H:i:s", filemtime( self::$_xrefScoreFile ) );
$result['label'] = date( "F d, Y H:i:s", filemtime( self::$_labelScoreFile ) );
return $result;
}
public static function checkFile( $file ) {
if ( !is_file( $file ) ) {
return false;
} else { return true;
}
}
private static function refreshTable( $relationType ) {
$dbw =& wfGetDB( DB_MASTER );
$sql = "CREATE TABLE IF NOT EXISTS `" . self::$_relationsTable . "` (
`pwId_1` VARCHAR( 10 ) NOT NULL ,
`pwId_2` VARCHAR( 10 ) NOT NULL ,
`type` VARCHAR( 10 ) NOT NULL ,
`score` FLOAT UNSIGNED NOT NULL,
`species` VARCHAR( 50 ) NOT NULL
);";
$create = $dbw->query( $sql );
$sql = "Delete FROM " . self::$_relationsTable . " WHERE type ='" . $relationType . "'";
$truncate = $dbw->query( $sql );
}
public static function updateDb( $relationType ) {
$dbw =& wfGetDB( DB_MASTER );
foreach ( $relationType as $method ) {
if ( !self::checkFile( $method['file'] ) ) {
echo "Error: File not found! {$method['file']}\n";
exit();
}
// Remove only those type of relations which are being updated.
self::refreshTable( $method['type'] );
$file = file( $method['file'] );
for ( $i = 1; $i < count( $file ); $i++ ) {
$relation = explode( "\t", trim( $file[$i] ) );
if ( $relation[2] > 0 ) {
$species = self::getSpecies( $relation[0] );
$score = self::normalizeScore( $relation[2], $relation[3], $relation[4] );
$dbw->insert( self::$_relationsTable, [
'pwId_1' => $relation[0],
'pwId_2' => $relation[1],
'type' => $method['type'],
'score' => $score,
'species' => $species
] );
}
}
unset( $file );
}
}
public static function fetchRelations( $type = '', $pwId_1 = '', $pwId_2 = '', $minScore = 0, $species = '' ) {
$dbr =& wfGetDB( DB_SLAVE );
$query = "SELECT * FROM " . self::$_relationsTable;
$minScore = (float)$minScore;
$conditions = [];
if ( $type != '' ) {
$conditions[] = "type = '$type'";
}
if ( $pwId_1 != '' && $pwId_2 != '' ) {
$conditions[] = "((pwId_1 = '$pwId_1' AND pwId_2 = '$pwId_2') OR (pwId_1 = '$pwId_2' AND pwId_2 = '$pwId_1'))";
} elseif ( $pwId_1 != '' && $pwId_2 == '' || $pwId_1 == '' && $pwId_2 != '' ) {
$pwId = ( $pwId_1 == "" ) ? $pwId_2 : $pwId_1;
$conditions[] = "(pwId_1 = '$pwId' OR pwId_2 = '$pwId')";
}
if ( $minScore > 0 ) {
$conditions[] = "score > $minScore";
}
if ( $species != '' ) {
$conditions[] = "species = '$species'";
}
if ( count( $conditions ) > 0 ) {
$cons = implode( " AND ", $conditions );
$query .= " Where $cons";
}
$res = $dbr->query( $query );
while ( $row = $dbr->fetchObject( $res ) ) {
$result[] = $row;
}
$dbr->freeResult( $res );
return $result;
}
private static function getSpecies( $pathwayId ) {
if ( array_key_exists( $pathwayId, self::$_pathwaySpecies ) ) {
return self::$_pathwaySpecies[$pathwayId];
} else {
$pathway = Pathway::newFromTitle( $pathwayId );
$species = $pathway->getSpecies();
self::$_pathwaySpecies[$pathwayId] = $species;
return $species;
}
}
private static function normalizeScore( $score, $pwId_1Count, $pwId_2Count ) {
return $normalizedScore = $score / min( $pwId_1Count, $pwId_2Count );
}
}