-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_affy_curation.pl
executable file
·551 lines (484 loc) · 20.4 KB
/
check_affy_curation.pl
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/env perl
# Perl core modules
use strict;
use warnings;
use diagnostics;
# Julien Roux, created 29/08/08
# USAGE: perl check_affy.pl before/after normalization
#
# TO DO: before normalization, check that the expression is
# annotated on organs that exist at the given stage
#############################################################
$| = 1; # stdout not put in memory buffer
use Getopt::Long;
use FindBin;
use lib "$FindBin::Bin/../.."; # Get lib path for Utils.pm
use Utils;
use lib '.';
require 'affy_utils.pl';
require 'bgee_utils.pl';
# Define arguments & their default value
my ($bgee_connector) = ('');
my ($normalizationType, $detectionType) = ('', '');
my ($chipType, $microarrayExperiment, $affymetrixChip) = ('', '', '');
my ($cel_data, $processed_mas5, $processed_schuster) = ('', '', '');
my ($affyChipInformation, $chipTypeQual) = ('', '');
my %opts = ('bgee=s' => \$bgee_connector, # Bgee connector string
'normalizationType=s' => \$normalizationType,
'detectionType=s' => \$detectionType,
'chipType=s' => \$chipType,
'microarrayExperiment=s' => \$microarrayExperiment,
'cel_data=s' => \$cel_data,
'processed_mas5=s' => \$processed_mas5,
'affyChipInformation=s' => \$affyChipInformation,
'chipTypeQual=s' => \$chipTypeQual,
'affymetrixChip=s' => \$affymetrixChip,
'processed_schuster=s' => \$processed_schuster,
);
# Check arguments
my $test_options = Getopt::Long::GetOptions(%opts);
if ( !$test_options || $bgee_connector eq '' ){
print "\n\tInvalid or missing argument:
\te.g. $0 -bgee=\$(BGEECMD) -normalizationType=\$(AFFY_NORMTYPE_FILEPATH) -detectionType=\$(AFFY_DETCTYPE_FILEPATH) -chipType=\$(AFFY_CHIPTYPE_FILEPATH) -microarrayExperiment=\$(MICROARRAY_EXPERIMENT_FILEPATH) -cel_data=\$(CELPATH) -processed_mas5=\$(MAS5PATH) -affyChipInformation=\$(AFFY_CHIPINFO_FILEPATH) -chipTypeQual=\$(AFFY_CHIPTYPEQUAL_FILEPATH) -affymetrixChip=\$(AFFY_CHIP_FILEPATH) -processed_schuster=\$(SCHUSTERPATH) -- before/after
\t-bgee Bgee connector string
\t-normalizationType normalizationType pipeline file
\t-detectionType detectionType pipeline file
\t-affyChipInformation affymetrixChipInformation pipeline file
\t-chipTypeQual chipTypeCorrespondencesAndQualityThresholds pipeline file
\t-chipType chipType annotation file
\t-microarrayExperiment microarrayExperiment annotation file
\t-affymetrixChip affymetrixChip annotation file
\t-cel_data cel_data directory
\t-processed_mas5 processed_mas5 directory
\t-processed_schuster processed_schuster directory
\n";
exit 1;
}
if ( $ARGV[0] !~ /^(before|after)$/ ){
print "\n\tInvalid or missing argument:
\te.g. $0 -bgee=\$(BGEECMD) -normalizationType=\$(AFFY_NORMTYPE_FILEPATH) -detectionType=\$(AFFY_DETCTYPE_FILEPATH) -chipType=\$(AFFY_CHIPTYPE_FILEPATH) -microarrayExperiment=\$(MICROARRAY_EXPERIMENT_FILEPATH) -cel_data=\$(CELPATH) -processed_mas5=\$(MAS5PATH) -affyChipInformation=\$(AFFY_CHIPINFO_FILEPATH) -chipTypeQual=\$(AFFY_CHIPTYPEQUAL_FILEPATH) -affymetrixChip=\$(AFFY_CHIP_FILEPATH) -processed_schuster=\$(SCHUSTERPATH) -- before/after\n";
exit 2;
}
# Bgee db connection
my $dbh = Utils::connect_bgee_db($bgee_connector);
##############################
# Retrieving species in BGee
##############################
my $selSpecies = $dbh->prepare('SELECT speciesId FROM species');
$selSpecies->execute() or die $selSpecies->errstr;
my %all_species;
while ( my @data = $selSpecies->fetchrow_array ){
$all_species{$data[0]}++;
}
$selSpecies->finish;
##########################
# Read normalizationType
##########################
open(my $IN0, '<', "$normalizationType") or die "Can't read file [$normalizationType]\n";
my %norms;
my $line = <$IN0>; #header
while ( defined ($line = <$IN0>) ){
chomp $line;
my @tmp = map { bgeeTrim($_) }
split(/\t/, $line);
$norms{$tmp[0]} = $tmp[1];
}
close $IN0;
######################
# Read detectionType
######################
open(my $IN1, '<', "$detectionType") or die "Can't read file [$detectionType]\n";
my %detects;
$line = <$IN1>; #header
while ( defined ($line = <$IN1>) ){
chomp $line;
my @tmp = map { bgeeTrim($_) }
split(/\t/, $line);
$detects{$tmp[0]} = $tmp[1];
}
close $IN1;
##################
# Check chipType
##################
open(my $IN2, '<', "$chipType") or die "Can't read file [$chipType]\n";
my %chip_id;
$line = <$IN2>; #header
while ( defined ($line = <$IN2>) ){
chomp $line;
my @tmp = map { bgeeTrim($_) }
split(/\t/, $line);
if ( $tmp[0] =~ /\s/ ){
die "Problem! Space inserted in chipTypeId for [$tmp[0]]\n";
}
else {
$chip_id{$tmp[0]}++;
}
if ( $tmp[2] =~ /\s/ && exists $all_species{$tmp[2]} ){
die "Problem! (Space or not in Bgee?) Species Id for [$tmp[0]]\n";
}
if ( $tmp[3] =~ /\s/ ){
die "Problem! Space inserted in xref for [$tmp[0]]\n";
}
}
close $IN2;
for my $chip ( keys %chip_id ){
die "Problem! Two chipTypeIds are identical [$chip_id{$chip} | $chip]\n" if ( $chip_id{$chip} > 1 );
}
##############################
# Check microarrayExperiment
##############################
my %experiments;
my %tsv = %{ Utils::read_spreadsheet("$microarrayExperiment", "\t", 'csv', '"', 1) };
map { warn "Problem! Space inserted for $_ Id\n" if ( $_ =~ /\s/ );
warn "Missing experimentId\n" if ( $_ eq '' );
$experiments{$_}++;
} @{ $tsv{'experimentId'} };
#NOTE Seb: replace this block with the read_spreadsheet function & the new microarrayExperiment sheet, in column
#open(my $IN3, '<', "$microarrayExperiment") or die "Can't read file [$microarrayExperiment]\n";
#while ( defined (my $line = <$IN3>) ){
# chomp $line;
#
# if ( $line =~ /^EXPERIMENT_ID\t(.+)/ ){
# my $exp_id = bgeeTrim($1);
# if ( $exp_id !~ /\s/ ){
# $experiments{$exp_id}++
# }
# else {
# print "Problem! Space inserted for $exp_id Id\n";
# }
# }
# if ( $line =~ /^EXPERIMENT_DESCRIPTION\:/ ){
# print "Problem! remove semi columns after EXPERIMENT_DESCRIPTION [$line]\n";
# }
#
# if ( $line =~ /^$/ ){
# die "Problem! Blank line in microarrayExperiment (instead of \"//\"?)\n";
# }
#}
#close $IN3;
map { warn "Missing EXPERIMENT_NAME\n" if ($_ eq ''); } @{ $tsv{'EXPERIMENT_NAME'} };
map { warn "Missing EXPERIMENT_DESCRIPTION\n" if ($_ eq ''); } @{ $tsv{'EXPERIMENT_DESCRIPTION'} };
map { warn "Missing EXPERIMENT_STATUS\n" if ($_ eq ''); } @{ $tsv{'EXPERIMENT_STATUS'} };
my %experimentSources;
map { warn "Missing EXPERIMENT_SOURCE\n" if ( $_ eq '' );
$experimentSources{$_}++;
} @{ $tsv{'EXPERIMENT_SOURCE'} };
#NOTE Seb: replace this block with the read_spreadsheet function & the new microarrayExperiment sheet, in column
#open(my $IN4, '<', "$microarrayExperiment") or die "Can't read file (2nd times) [$microarrayExperiment]\n";
#my $lineCount = 0;
#my $totalLineCount = 0;
#my $error = '';
#my %experimentSources = ();
#while ( defined ($line = <$IN4>) ){
# chomp $line;
# my @tmp = map { bgeeTrim($_) }
# split(/\t/, $line);
# # First line describes an experiment,
# # it can be either the experiment ID, or an URL linking to the experiment
# if ( $lineCount == 0 ){
# $error = 'missing EXPERIMENT_ID' if ( $tmp[0] ne 'EXPERIMENT_ID' );
# }
# elsif ( $lineCount == 1 ){
# $error = 'missing EXPERIMENT_NAME' if ( $tmp[0] ne 'EXPERIMENT_NAME' );
# }
# elsif ( $lineCount == 2 ){
# $error = 'missing EXPERIMENT_DESCRIPTION' if ( $tmp[0] ne 'EXPERIMENT_DESCRIPTION' );
# }
# elsif ( $lineCount == 3 ){
# if ( $tmp[0] eq 'EXPERIMENT_SOURCE' ){
# # everything's OK
# # store the soure to check that it exists in the database
# $experimentSources{$tmp[1]}++;
# }
# else {
# $error = 'missing EXPERIMENT_SOURCE';
# }
# }
# elsif ( $lineCount == 4 ){
# $error = 'missing EXPERIMENT_STATUS' if ( $tmp[0] ne 'EXPERIMENT_STATUS' );
# }
# elsif ( $lineCount == 5 ){
# if ( $tmp[0] eq 'COMMENT' ){
# # optionnal comment line, everything's OK
# # $line-- to match "//" during next iteration
# $line--;
# }
# elsif ( $line =~ /^\/\/\s*$/ ){
# # everything's OK
# }
# else {
# $error = 'missing COMMENT or end of experiment //';
# }
# }
# $totalLineCount++;
# if ( $error ne '' ){
# print "Badly formatted microarrayExperiment file at line $totalLineCount: $error\n";
# $error = '';
# }
# if ( $line =~ /^\/\/\s*$/ ){
# $lineCount = -1;
# $error = '';
# }
#
# $lineCount++;
#}
#close $IN4;
my $selDataSrc = $dbh->prepare('SELECT dataSourceId FROM dataSource WHERE dataSourceName = ?');
for my $source ( keys %experimentSources ){
$selDataSrc->execute($source) or die $selDataSrc->errstr;
if ( my @data = $selDataSrc->fetchrow_array ){
}
else {
die "Problem! [$source] in microarrayExperiment does not exist\n";
}
}
$selDataSrc->finish;
for my $exp ( keys %experiments ){
die "Problem! [$exp] inserted many times\n" if ( $experiments{$exp} > 1 );
}
# Checking experiment directories not present in the file
opendir(my $IMD0, "$cel_data") or die("Cannot open directory [$cel_data]\n");
for my $storedFile ( readdir($IMD0) ){
next if ( $storedFile =~ /^\./ );
if ( -d $cel_data.$storedFile ){
# Check that the experiment was present in the annotation file
warn sprintf("Warning: experiment %-17s present on the server in %s, not in the microarrayExperiment annotation file\n", "[$storedFile]", $cel_data)
if ( !exists $experiments{$storedFile} );
}
}
closedir($IMD0);
opendir(my $IMD1, "$processed_mas5") or die("Cannot open directory [$processed_mas5]\n");
for my $storedFile ( readdir($IMD1) ){
next if ( $storedFile =~ /^\./ );
if ( -d $processed_mas5.$storedFile ){
# Check that the experiment was present in the annotation file
warn sprintf("Warning: experiment %-17s present on the server in %s, not in the microarrayExperiment annotation file\n", "[$storedFile]", $processed_mas5)
if ( !exists $experiments{$storedFile} );
}
}
closedir($IMD1);
########################################
# retrieve all organs/stages from Bgee
########################################
# $organs{anatEntityId}->{'startStageId'} = startStageId
# $organs{anatEntityId}->{'endStageId'} = endStageId
my %organs = %{ Utils::getBgeedbOrgans($dbh) };
# $stages{stageId}->{'leftBound'} = leftBound
# $stages{stageId}->{'rightBound'} = rightBound
my %stages = %{ Utils::getBgeedbStages($dbh) };
########################
# Check affymetrixChip
########################
my %all_filenames;
# get chip info
my %affyChipsInfo = getAllChipsInfo($affyChipInformation);
# get the incompatible chip type
my %chipTypeInfo = getChipTypesInformation($chipTypeQual);
open(my $IN5, '<', "$affymetrixChip") or die "Can't read file [$affymetrixChip]\n";
my %affy_filename;
my %affy_chip;
my %affy_organ;
my %affy_stage;
my $affy_experiment;
my $start = 0;
# Store experiments including some chips incompatible or commented
my %commentedOrIncompatibleExperiments = ();
# Store experiments including some chips compatible and uncommented.
# Used with the previous var, it will allow to identify experiments that include
# ONLY chips incompatible or commented.
my %nonCommentedAndCompatibleExperiments = ();
$line = <$IN5>; #header
while ( defined ($line = <$IN5>) ){
chomp $line;
# ChipID ExperimentID chipTypeId normalizationTypeId detectionTypeId organId organName UberonId UberonName stageId stageName infoOrgan infoStage sampleTitle sampleSource SampleDescription SampleCharacteristics organAnnotationStatus organBiologicalStatus stageAnnotationStatus stageBiologicalStatus sex strain comment annotatorId lastModificationDate
# Check if it is a low quality or incompatible chip
my @tmp = map { bgeeTrim($_) }
split(/\t/, $line);
# Skip commentary line
# but store them to remove experiments with only commented or incompatible chips
if (( $line =~ /^#/ ) or ( $line =~ /^\"#/ )){
$commentedOrIncompatibleExperiments{$tmp[1]}++;
next;
}
if ( !defined $affyChipsInfo{$tmp[1]}{$tmp[0]} || isChipIncompatibleOrLowQuality($tmp[3], $affyChipsInfo{$tmp[1]}{$tmp[0]}, \%chipTypeInfo, $tmp[2]) ){
$commentedOrIncompatibleExperiments{$tmp[1]}++;
next;
}
# At this point, it is a chip compatible and non commented, store the experiment
$nonCommentedAndCompatibleExperiments{$tmp[1]}++;
my $countColumn = 0;
for my $element ( @tmp ){
# After the 7th column, it's commentary that can include spaces
# columns 5 and 6 are organ and stage ID with : in it
if ( ($countColumn <= 4 && $element !~ /^[\w\-\.]+$/) || (($countColumn == 7 || $countColumn == 9) && $element !~ /^[\w]+:[\w]+$/) ){
print "Problem! wrong char inserted in line: $line ==>$element<== column: $countColumn\n";
}
$countColumn++;
}
my $anatEntityId = $tmp[7];
my $stageId = $tmp[9];
# Check impossible annotation: organ not existing at the annotated stage
if ( exists $organs{$anatEntityId} && exists $stages{$stageId} ){
my $startStageId = $organs{$anatEntityId}->{'startStageId'};
my $endStageId = $organs{$anatEntityId}->{'endStageId'};
if ( $stages{$startStageId}->{'leftBound'} <= $stages{$stageId}->{'rightBound'} && $stages{$endStageId}->{'rightBound'} >= $stages{$stageId}->{'leftBound'} ){
#OK
}
else {
print "Warning, impossible annotation, $anatEntityId not existing at $stageId\n";
}
}
if ( $start ne 0 && $tmp[1] ne $affy_experiment ){
check_experiment($affy_experiment, \%affy_filename, \%affy_chip, \%affy_organ, \%affy_stage);
for my $name ( keys %affy_filename ){
$all_filenames{lc($name)}++;
}
$affy_experiment = $tmp[1];
%affy_filename = ();
$affy_filename{$tmp[0]}->{'count'}++;
%affy_chip = ();
$affy_chip{$tmp[2]}++;
if ( $tmp[3] eq 1 && $tmp[4] eq 1 ){
$affy_filename{$tmp[0]}->{'path'} = $processed_mas5;
$affy_filename{$tmp[0]}->{'out'} = '';
}
if ( $tmp[3] eq 2 && $tmp[4] eq 2 ){
$affy_filename{$tmp[0]}->{'path'} = $processed_schuster;
$affy_filename{$tmp[0]}->{'out'} = '.out';
# raw data
$affy_filename{$tmp[0]}->{'raw_path'} = $cel_data;
}
%affy_organ = ();
$affy_organ{$tmp[7]}++;
%affy_stage = ();
$affy_stage{$tmp[9]}++;
}
else {
$affy_experiment = $tmp[1];
$affy_filename{$tmp[0]}->{'count'}++;
$affy_chip{$tmp[2]}++;
if ( $tmp[3] eq 1 && $tmp[4] eq 1 ){
$affy_filename{$tmp[0]}->{'path'} = $processed_mas5;
$affy_filename{$tmp[0]}->{'out'} = '';
}
if ( $tmp[3] eq 2 && $tmp[4] eq 2 ){
$affy_filename{$tmp[0]}->{'path'} = $processed_schuster;
$affy_filename{$tmp[0]}->{'out'} = '.out';
# raw data
$affy_filename{$tmp[0]}->{'raw_path'} = $cel_data;
}
$affy_organ{$tmp[7]}++;
$affy_stage{$tmp[9]}++;
}
$start++;
}
close $IN5;
# Last experiment
check_experiment($affy_experiment, \%affy_filename, \%affy_chip, \%affy_organ, \%affy_stage);
# Check that experiments with incompatible or commented chips had actually
# ONLY incompatible or commented chips.
for my $expCompatible ( keys %nonCommentedAndCompatibleExperiments ){
delete $commentedOrIncompatibleExperiments{$expCompatible};
}
# Then we remove the experiences with only incompatible or commented chips
# from the list of experiments to check.
for my $expIncompatible ( keys %commentedOrIncompatibleExperiments ){
delete $experiments{$expIncompatible};
}
if ( (keys %experiments) > 0 ){
print "Problem! These experiments are in microarrayExperiment and not in affymetrixChip:\n";
for my $exp ( keys %experiments ){
print "$exp\n";
}
}
$dbh->disconnect;
exit 0;
sub check_experiment {
my ($exp, $files, $chips, $organs, $stages) = @_;
print "\t$exp\n";
if ( !exists $experiments{$exp} ){
print "Problem! Experiment not inserted in microarrayExperiment or experiment annotated twice in affymetrixChip [$exp]\n";
}
delete $experiments{$exp};
# not needed anymore, gcRMA is now used even when there is only one cel file
#if ( scalar(keys %{$files}) eq 1 ){
# for my $file ( keys %{$files} ){
# if ( $file =~ /\.cel$/i ){
# print "\tBe careful, this cannot be normalized by gcRMA, mas5 should be used... and affymetrixChip should be changed to 1 1\n";
# }
# }
#}
for my $file ( keys %{$files} ){
if ( length($file) >= 255 ){
print "Problem! File name is too long!\n";
}
if ( $$files{$file}->{'count'} > 1 ){
die "Problem! two or more filenames are identical in this experiment\n";
}
# We remove this check, cel files can actually have the same name in different experiments
#if ( exists $all_filenames{lc($file)} ){
# print "Problem! two or more filenames are identical between different experiments\n";
#}
my $temp = 0;
for my $file2 ( keys %{$files} ){
if ( lc $file eq lc $file2 ){
$temp++;
}
}
if ( $temp > 1 ){
die "Problem! two or more filenames are identical in this experiment.\n";
}
}
# Check normalized data
if ( $ARGV[0] eq 'after' ){
for my $file ( keys %{$files} ){
my $flag = 0;
#print $$files{$file}->{'path'}.$exp.'/', "\n";
opendir(my $DIRA, $$files{$file}->{'path'}.$exp.'/') or do {print "Problem! No directory for ", $$files{$file}->{'path'}.$exp.'/', "\n"; next;};
while ( defined(my $file_dir = readdir($DIRA)) ){
if ( $file_dir eq $file.$$files{$file}->{'out'} ){
$flag = 1;
}
#print $$files{$file}->{'path'}.$exp.'/'.$file_dir, "\n"
if ( -z $$files{$file}->{'path'}.$exp.'/'.$file_dir && $file_dir !~ /^\./ ){
print "\t$file_dir seems to be empty !\n";
}
}
closedir($DIRA);
if ( $flag eq 0 ){
print "\tProblem! Missing data file or problem in file name: ", $file.$$files{$file}->{'out'}, "\n";
}
}
}
# Check raw data (do nothing for MAS5 data)
if ( $ARGV[0] eq 'before' ){
for my $file ( keys %{$files} ){
if ( defined $$files{$file}->{'raw_path'} ){
my $flag = 0;
#print $$files{$file}->{'raw_path'}.$exp.'/', "\n";
opendir(my $DIRB, $$files{$file}->{'raw_path'}.$exp.'/') or print "Problem! No directory for data\n";
while ( defined(my $file_dir = readdir($DIRB)) ){
if ( $file_dir eq $file || $file_dir eq $file.'.gz' ){
$flag = 1;
}
}
closedir($DIRB);
if ( $flag eq 0 ){
print $file, "\n";
print "Problem! Missing raw data file or problem in file name\n";
}
}
}
}
for my $chip ( keys %{$chips} ){
print "Problem! A new Id has to be inserted in chipType ($chip)\n" if ( !exists $chip_id{$chip} );
}
# for my $organ ( keys %{$organs} ){
# print "Problem! An organ annotated is not in Bgee ($organ)\n" if ( !exists $organs{$organ} );
# }
#
# for my $stage ( keys %{$stages} ){
# print "Problem! A stage annotated is not in Bgee ($stage)\n" if ( !exists $stages{$stage} );
# }
return;
}