Repository navigation
Expand file tree
/
Copy pathloadUpdateJSON.pl
More file actions
executable file
·147 lines (119 loc) · 3.64 KB
/
Copy pathloadUpdateJSON.pl
File metadata and controls
executable file
·147 lines (119 loc) · 3.64 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use List::MoreUtils 'firstidx';
use Data::Dumper;
use FindBin '$Bin';
use lib "$Bin";
use Common;
my $usage = <<USAGE;
Usage: $0 <update json> [updateTool updateTool2 ...]
Each line of <update json> must contain all data encoded in JSON
necessary to update this sequence.
updateTool refers to a new release of this tool and thus indicates
that all results for this tool should be processed regardless.
USAGE
my $g_File = shift or die $usage;
my @g_UpdatedTools = @ARGV;
my @tools = @Common::tools;
my %updatedTools = ();
foreach my $updatedTool (@g_UpdatedTools) {
if (firstidx {$_ eq $updatedTool } @tools == -1) {
die qq($updatedTool is not a valid tool name\n);
}
$updatedTools{$updatedTool} = 1;
}
my %toolStatusPos = ();
my $i = 0;
foreach my $fieldName (@tools) {
$toolStatusPos{$fieldName} = $i;
++$i;
}
my $aseqs = &Common::aseqs();
my $fh = &Common::openFileOrGzFile($g_File);
&Common::startTicker();
while (my $json = <$fh>) {
chomp($json);
my $data = from_json($json);
my $aseqId = $data->{_id};
my $aseq = $aseqs->find_one({_id => $aseqId}, {_s => 1, x => 1});
if ($aseq) {
$Common::ticker{updated} += &updateAseq($aseq, $data);
}
else {
&createNewAseq($data);
&Common::removePadding($aseqs, $aseqId);
++$Common::ticker{new};
}
&Common::tick();
}
close ($fh);
# --------------------------------------------------------------------------------------------------------------------
sub updateAseq {
my $aseq = shift or die;
my $data = shift or die;
my %set = ();
my $newStatus = $aseq->{_s};
my $t = $data->{t};
if ($t) {
foreach my $fieldName (keys %$t) {
my $pos = $toolStatusPos{$fieldName};
my $isUpdate = $updatedTools{$fieldName};
if ($isUpdate || substr($aseq->{_s}, $pos, 1) ne 'T') {
$set{'t.' . $fieldName} = $t->{$fieldName};
substr($newStatus, $pos, 1, 'T');
}
}
if (scalar(keys %set) > 0) {
$set{_s} = $newStatus;
}
}
my $x = $data->{x};
if ($x) {
# Update the cross-references if there are new ones
foreach my $xrefType (keys %$x) {
my $xrefs = $x->{$xrefType};
if (!$aseq->{x}->{$xrefType} ||
&idsDiffer($xrefs, $aseq->{x}->{$xrefType})) {
$set{'x.' . $xrefType} = $xrefs;
}
}
}
if (scalar(keys %set) > 0) {
# $aseqs->update({_id => $aseq->{_id}}, {'$set' => \%set}, {safe => 1});
$aseqs->update({_id => $aseq->{_id}}, {'$set' => \%set});
return 1;
}
return 0;
}
sub createNewAseq {
my $data = shift or die;
die if (!exists $data->{s} || !exists $data->{l});
$data->{x} = {} if (!exists $data->{x});
$data->{t} = {} if (!exists $data->{t});
$data->{$Common::paddingKey} = $Common::padding{basic}->{buffer};
my $t = $data->{t};
my $status = $data->{_s};
foreach my $fieldName (keys %$t) {
my $pos = $toolStatusPos{$fieldName};
my $isUpdate = $updatedTools{$fieldName};
if ($isUpdate || substr($status, $pos, 1) ne 'T') {
substr($status, $pos, 1, 'T');
}
}
$data->{_s} = $status;
# $aseqs->insert(\%data, {safe => 1});
$aseqs->insert($data);
}
sub idsDiffer {
my $xrefs = shift or die;
my $dbXrefs = shift or die;
return 1 if (@$xrefs != @$dbXrefs);
my @alpha = sort @$xrefs;
my @beta = sort @$dbXrefs;
for (my $i = 0, my $z = @alpha; $i<$z; ++$i) {
return 1 if ($alpha[$i] ne $beta[$i]);
}
return 0;
}