-
Notifications
You must be signed in to change notification settings - Fork 5
/
import_genenames.php
executable file
·117 lines (100 loc) · 2.51 KB
/
import_genenames.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
#!/usr/bin/php
<?php
;
// Copyright: see COPYING
// Authors: see git-blame(1)
if ($_SERVER["argc"] != 2)
{
die ("Usage: ".$_SERVER["argv"][0]." genenames.txt\n");
}
$fh = fopen ($_SERVER["argv"][1], "r");
chdir ('public_html');
require_once 'lib/setup.php';
ini_set ("memory_limit", pow(2,27));
print "Creating/updating get-evidence tables...";
evidence_create_tables ();
print "\n";
$official = array();
$donottranslate = array();
print "Reading input...";
$in = 0;
while (($line = fgets ($fh)) !== FALSE) {
if (ereg ("^HGNC ID", $line)) // header
continue;
++$in;
$f = explode ("\t", rtrim($line, "\n"));
$canonical = $f[1];
$donottranslate[$canonical] = 1;
if ($f[4] == "") // no aliases listed
continue;
foreach (explode (",", $f[4]) as $aka) {
$aka = trim($aka);
if ($aka == "")
continue;
$official[$aka] = $canonical;
}
}
print "$in inputs\n";
print "Removing mappings for {official name} > {another official name}...";
$did = 0;
foreach ($donottranslate as $gene => $x) {
if (isset ($official[$gene])) {
unset ($official[$gene]);
$did++;
}
}
print "$did\n";
$compressed = 1;
print "Compressing paths...";
while ($compressed > 0) {
$compressed = 0;
foreach ($official as $aka => &$canonical) {
if ($canonical == $aka) {
unset ($official[$aka]);
$compressed++;
}
else if (isset ($official[$canonical])) {
if ($official[$canonical] == $aka) {
if (strcmp($aka,$canonical) < 0) {
$a = $aka;
$b = $canonical;
} else {
$a = $canonical;
$b = $aka;
}
print "\n$aka -> $canonical -> $aka -- choosing $a...";
unset ($official[$canonical]);
unset ($official[$aka]);
$official[$b] = $a;
} else {
$canonical = $official[$canonical];
$compressed ++;
}
}
}
if ($compressed > 0)
print "removed $compressed\nCompressing paths...";
}
print "done\n";
$g_sql = "";
$g_sql_param = array();
$out = 0;
foreach ($official as $aka => $canonical) {
$g_sql .= "(?, ?), ";
array_push ($g_sql_param, $aka, $canonical);
++$out;
}
print "Strung together $out outputs\n";
if (!$out)
exit;
print "Importing to database...";
theDb()->query ("LOCK TABLES gene_canonical_name WRITE");
theDb()->query ("DELETE FROM gene_canonical_name");
$q = theDb()->query ("INSERT IGNORE INTO gene_canonical_name (aka, official) VALUES "
.ereg_replace(', $', '', $g_sql),
$g_sql_param);
if (theDb()->isError($q)) die($q->getMessage());
print theDb()->affectedRows();
print "\n";
theDb()->query ("UNLOCK TABLES");
?>