-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkfiles.py
1564 lines (1427 loc) · 71.1 KB
/
checkfiles.py
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""\
Run sanity checks on files.
Example.
%(prog)s --username ACCESS_KEY_ID --password SECRET_ACCESS_KEY \\
--output check_files.log https://www.encodeproject.org
"""
import datetime
import time
import os.path
import json
import sys
from shlex import quote
import subprocess
import re
from urllib.parse import urljoin
import requests
import copy
from slackclient import SlackClient
EPILOG = __doc__
PYTHON_PATH = "/opt/encoded/checkfiles/venv/bin/python"
# For submitters, bam files should not be submitted as .gz
GZIP_TYPES = [
"CEL",
"bam",
"bed",
"bedpe",
"csfasta",
"csqual",
"fasta",
"fastq",
"gff",
"gtf",
"tagAlign",
"tar",
"txt",
"sam",
"wig",
"vcf",
"pairs",
]
read_name_prefix = re.compile(
'^(@[a-zA-Z\d]+[a-zA-Z\d_-]*:[a-zA-Z\d-]+:[a-zA-Z\d_-]' +
'+:\d+:\d+:\d+:\d+)$')
read_name_pattern = re.compile(
'^(@[a-zA-Z\d]+[a-zA-Z\d_-]*:[a-zA-Z\d-]+:[a-zA-Z\d_-]' +
'+:\d+:\d+:\d+:\d+[\s_][123]:[YXN]:[0-9]+:([ACNTG\+]*|[0-9]*))$'
)
special_read_name_pattern = re.compile(
'^(@[a-zA-Z\d]+[a-zA-Z\d_-]*:[a-zA-Z\d-]+:[a-zA-Z\d_-]' +
'+:\d+:\d+:\d+:\d+[/1|/2]*[\s_][123]:[YXN]:[0-9]+:([ACNTG\+]*|[0-9]*))$'
)
srr_read_name_pattern = re.compile(
'^(@SRR[\d.]+)$'
)
pacbio_read_name_pattern = re.compile(
'^(@m\d{6}_\d{6}_\d+_[a-zA-Z\d_-]+\/.*)$|^(@m\d+U?_\d{6}_\d{6}\/.*)$|^(@c.+)$'
)
def is_path_gzipped(path):
with open(path, 'rb') as f:
magic_number = f.read(2)
return magic_number == b'\x1f\x8b'
def update_content_error(errors, error_message):
if 'content_error' not in errors:
errors['content_error'] = error_message
else:
errors['content_error'] += ', ' + error_message
def check_format(encValData, job, path):
""" Local validation
"""
ASSEMBLY_MAP = {
'GRCh38-minimal': 'GRCh38',
'mm10-minimal': 'mm10'
}
errors = job['errors']
item = job['item']
result = job['result']
subreads = False
# if assembly is in the map, use the mapping, otherwise just use the string in assembly
assembly = ASSEMBLY_MAP.get(item.get('assembly'), item.get('assembly'))
file_output_type = item.get('output_type')
if (item.get('file_format') == 'bam' and
file_output_type in ['transcriptome alignments',
'gene alignments',
'redacted transcriptome alignments']):
if 'assembly' not in item:
errors['assembly'] = 'missing assembly'
update_content_error(errors, 'File metadata lacks assembly information')
if 'genome_annotation' not in item:
errors['genome_annotation'] = 'missing genome_annotation'
update_content_error(errors, 'File metadata lacks genome annotation information')
if errors:
return errors
if file_output_type in ['transcriptome alignments', 'redacted transcriptome alignments']:
chromInfo = '-chromInfo={}/{}/{}/chrom.sizes'.format(
encValData, assembly, item['genome_annotation'])
else:
chromInfo = '-chromInfo={}/{}/{}/gene.sizes'.format(
encValData, assembly, item['genome_annotation'])
elif (item.get('file_format') == 'bam' and
file_output_type in ['subreads']):
subreads = True
chromInfo = ''
else:
chromInfo = '-chromInfo={}/{}/chrom.sizes'.format(encValData, assembly)
validate_map = {
('fasta', None): ['-type=fasta'],
('fastq', None): ['-type=fastq'],
('bam', None): ['-type=bam', chromInfo],
('bigWig', None): ['-type=bigWig', chromInfo],
('bigInteract', None): ['-type=bigBed5+13', chromInfo, '-as=%s/as/interact.as' % encValData],
# standard bed formats
('bed', 'bed3'): ['-type=bed3', chromInfo],
('bigBed', 'bed3'): ['-type=bigBed3', chromInfo],
('bed', 'bed5'): ['-type=bed5', chromInfo],
('bigBed', 'bed5'): ['-type=bigBed5', chromInfo],
('bed', 'bed6'): ['-type=bed6', chromInfo],
('bigBed', 'bed6'): ['-type=bigBed6', chromInfo],
('bed', 'bed9'): ['-type=bed9', chromInfo],
('bigBed', 'bed9'): ['-type=bigBed9', chromInfo],
('bedGraph', None): ['-type=bedGraph', chromInfo],
# extended "bed+" formats, -tab is required to allow for text fields to contain spaces
('bed', 'bed3+'): ['-tab', '-type=bed3+', chromInfo],
('bigBed', 'bed3+'): ['-tab', '-type=bigBed3+', chromInfo],
('bed', 'bed6+'): ['-tab', '-type=bed6+', chromInfo],
('bigBed', 'bed6+'): ['-tab', '-type=bigBed6+', chromInfo],
('bed', 'bed9+'): ['-tab', '-type=bed9+', chromInfo],
('bigBed', 'bed9+'): ['-tab', '-type=bigBed9+', chromInfo],
# a catch-all shoe-horn (as long as it's tab-delimited)
('bed', 'unknown'): ['-tab', '-type=bed3+', chromInfo],
('bigBed', 'unknown'): ['-tab', '-type=bigBed3+', chromInfo],
# special bed types
('bed', 'bedLogR'): ['-type=bed9+1', chromInfo, '-as=%s/as/bedLogR.as' % encValData],
('bigBed', 'bedLogR'): ['-type=bigBed9+1', chromInfo, '-as=%s/as/bedLogR.as' % encValData],
('bed', 'bedMethyl'): ['-type=bed9+2', chromInfo, '-as=%s/as/bedMethyl.as' % encValData],
('bigBed', 'bedMethyl'): ['-type=bigBed9+2', chromInfo, '-as=%s/as/bedMethyl.as' % encValData],
('bed', 'broadPeak'): ['-type=bed6+3', chromInfo, '-as=%s/as/broadPeak.as' % encValData],
('bigBed', 'broadPeak'): ['-type=bigBed6+3', chromInfo, '-as=%s/as/broadPeak.as' % encValData],
('bed', 'gappedPeak'): ['-type=bed12+3', chromInfo, '-as=%s/as/gappedPeak.as' % encValData],
('bigBed', 'gappedPeak'): ['-type=bigBed12+3', chromInfo, '-as=%s/as/gappedPeak.as' % encValData],
('bed', 'narrowPeak'): ['-type=bed6+4', chromInfo, '-as=%s/as/narrowPeak.as' % encValData],
('bigBed', 'narrowPeak'): ['-type=bigBed6+4', chromInfo, '-as=%s/as/narrowPeak.as' % encValData],
('bed', 'bedRnaElements'): ['-type=bed6+3', chromInfo, '-as=%s/as/bedRnaElements.as' % encValData],
('bigBed', 'bedRnaElements'): ['-type=bed6+3', chromInfo, '-as=%s/as/bedRnaElements.as' % encValData],
('bed', 'bedExonScore'): ['-type=bed6+3', chromInfo, '-as=%s/as/bedExonScore.as' % encValData],
('bigBed', 'bedExonScore'): ['-type=bigBed6+3', chromInfo, '-as=%s/as/bedExonScore.as' % encValData],
('bed', 'bedRrbs'): ['-type=bed9+2', chromInfo, '-as=%s/as/bedRrbs.as' % encValData],
('bigBed', 'bedRrbs'): ['-type=bigBed9+2', chromInfo, '-as=%s/as/bedRrbs.as' % encValData],
('bed', 'enhancerAssay'): ['-type=bed9+1', chromInfo, '-as=%s/as/enhancerAssay.as' % encValData],
('bigBed', 'enhancerAssay'): ['-type=bigBed9+1', chromInfo, '-as=%s/as/enhancerAssay.as' % encValData],
('bed', 'modPepMap'): ['-type=bed9+7', chromInfo, '-as=%s/as/modPepMap.as' % encValData],
('bigBed', 'modPepMap'): ['-type=bigBed9+7', chromInfo, '-as=%s/as/modPepMap.as' % encValData],
('bed', 'pepMap'): ['-type=bed9+7', chromInfo, '-as=%s/as/pepMap.as' % encValData],
('bigBed', 'pepMap'): ['-type=bigBed9+7', chromInfo, '-as=%s/as/pepMap.as' % encValData],
('bed', 'openChromCombinedPeaks'): ['-type=bed9+12', chromInfo, '-as=%s/as/openChromCombinedPeaks.as' % encValData],
('bigBed', 'openChromCombinedPeaks'): ['-type=bigBed9+12', chromInfo, '-as=%s/as/openChromCombinedPeaks.as' % encValData],
('bed', 'peptideMapping'): ['-type=bed6+4', chromInfo, '-as=%s/as/peptideMapping.as' % encValData],
('bigBed', 'peptideMapping'): ['-type=bigBed6+4', chromInfo, '-as=%s/as/peptideMapping.as' % encValData],
('bed', 'shortFrags'): ['-type=bed6+21', chromInfo, '-as=%s/as/shortFrags.as' % encValData],
('bigBed', 'shortFrags'): ['-type=bigBed6+21', chromInfo, '-as=%s/as/shortFrags.as' % encValData],
('bed', 'encode_elements_H3K27ac'): ['-tab', '-type=bed9+1', chromInfo, '-as=%s/as/encode_elements_H3K27ac.as' % encValData],
('bigBed', 'encode_elements_H3K27ac'): ['-tab', '-type=bigBed9+1', chromInfo, '-as=%s/as/encode_elements_H3K27ac.as' % encValData],
('bed', 'encode_elements_H3K9ac'): ['-tab', '-type=bed9+1', chromInfo, '-as=%s/as/encode_elements_H3K9ac.as' % encValData],
('bigBed', 'encode_elements_H3K9ac'): ['-tab', '-type=bigBed9+1', chromInfo, '-as=%s/as/encode_elements_H3K9ac.as' % encValData],
('bed', 'encode_elements_H3K4me1'): ['-tab', '-type=bed9+1', chromInfo, '-as=%s/as/encode_elements_H3K4me1.as' % encValData],
('bigBed', 'encode_elements_H3K4me1'): ['-tab', '-type=bigBed9+1', chromInfo, '-as=%s/as/encode_elements_H3K4me1.as' % encValData],
('bed', 'encode_elements_H3K4me3'): ['-tab', '-type=bed9+1', chromInfo, '-as=%s/as/encode_elements_H3K4me3.as' % encValData],
('bigBed', 'encode_elements_H3K4me3'): ['-tab', '-type=bigBed9+1', chromInfo, '-as=%s/as/encode_elements_H3K4me3.as' % encValData],
('bed', 'dnase_master_peaks'): ['-tab', '-type=bed9+1', chromInfo, '-as=%s/as/dnase_master_peaks.as' % encValData],
('bigBed', 'dnase_master_peaks'): ['-tab', '-type=bigBed9+1', chromInfo, '-as=%s/as/dnase_master_peaks.as' % encValData],
('bed', 'encode_elements_dnase_tf'): ['-tab', '-type=bed5+1', chromInfo, '-as=%s/as/encode_elements_dnase_tf.as' % encValData],
('bigBed', 'encode_elements_dnase_tf'): ['-tab', '-type=bigBed5+1', chromInfo, '-as=%s/as/encode_elements_dnase_tf.as' % encValData],
('bed', 'candidate enhancer predictions'): ['-type=bed3+', chromInfo, '-as=%s/as/candidate_enhancer_prediction.as' % encValData],
('bigBed', 'candidate enhancer predictions'): ['-type=bigBed3+', chromInfo, '-as=%s/as/candidate_enhancer_prediction.as' % encValData],
('bed', 'enhancer predictions'): ['-type=bed3+', chromInfo, '-as=%s/as/enhancer_prediction.as' % encValData],
('bigBed', 'enhancer predictions'): ['-type=bigBed3+', chromInfo, '-as=%s/as/enhancer_prediction.as' % encValData],
('bed', 'idr_peak'): ['-type=bed6+', chromInfo, '-as=%s/as/idr_peak.as' % encValData],
('bigBed', 'idr_peak'): ['-type=bigBed6+', chromInfo, '-as=%s/as/idr_peak.as' % encValData],
('bed', 'tss_peak'): ['-type=bed6+', chromInfo, '-as=%s/as/tss_peak.as' % encValData],
('bigBed', 'tss_peak'): ['-type=bigBed6+', chromInfo, '-as=%s/as/tss_peak.as' % encValData],
('bed', 'idr_ranked_peak'): ['-type=bed6+14', chromInfo, '-as=%s/as/idr_ranked_peak.as' % encValData],
('bed', 'element enrichments'): ['-type=bed6+5', chromInfo, '-as=%s/as/mpra_starr.as' % encValData],
('bigBed', 'element enrichments'): ['-type=bigBed6+5', chromInfo, '-as=%s/as/mpra_starr.as' % encValData],
('bed', 'CRISPR element quantifications'): ['-type=bed3+22', chromInfo, '-as=%s/as/element_quant_format.as' % encValData],
('bedpe', None): ['-type=bed3+', chromInfo],
('bedpe', 'mango'): ['-type=bed3+', chromInfo],
# non-bed types
('rcc', None): ['-type=rcc'],
('idat', None): ['-type=idat'],
('gtf', None): None,
('tagAlign', None): ['-type=tagAlign', chromInfo],
('tar', None): None,
('tsv', None): None,
('csv', None): None,
('2bit', None): None,
('csfasta', None): ['-type=csfasta'],
('csqual', None): ['-type=csqual'],
('CEL', None): None,
('sam', None): None,
('wig', None): None,
('hdf5', None): None,
('hic', None): None,
('gff', None): None,
('vcf', None): None,
('btr', None): None
}
if not subreads:
# samtools quickcheck
if item.get('file_format') == 'bam':
try:
output = subprocess.check_output(
['samtools', 'quickcheck', path], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
errors['bamValidation'] = e.output.decode(errors='replace').rstrip('\n')
update_content_error(errors, 'File failed bam validation ' +
'(samtools quickcheck). ' + errors['bamValidation'])
else:
result['bamValidation'] = output.decode(errors='replace').rstrip('\n')
# validateFiles
validate_args = validate_map.get((item['file_format'], item.get('file_format_type')))
if validate_args is None:
return
if chromInfo in validate_args and 'assembly' not in item:
errors['assembly'] = 'missing assembly'
update_content_error(errors, 'File metadata lacks assembly information')
return
result['validateFiles_args'] = ' '.join(validate_args)
try:
output = subprocess.check_output(
['validateFiles'] + validate_args + [path], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
errors['validateFiles'] = e.output.decode(errors='replace').rstrip('\n')
update_content_error(errors, 'File failed file format specific ' +
'validation (encValData) ' + errors['validateFiles'])
else:
result['validateFiles'] = output.decode(errors='replace').rstrip('\n')
def validate_crispr(job, filePath):
'''
ENCODE CRISPR Group provided scripts for guide quantification validation
which can be found here: https://github.com/oh-jinwoo94/ENCODE by Jin Woo Oh
'''
errors = job['errors']
item = job['item']
result = job['result']
guide_validationScript_path = '/opt/ENCODE_CRISPR_Validation/check_guide_quant_format.py'
pam_validationScript_path = '/opt/ENCODE_CRISPR_Validation/check_PAM.py'
guide_format_path = '/opt/ENCODE_CRISPR_Validation/guide_quant_format.txt'
genome_reference_path = '/opt/GRCh38_no_alt_analysis_set_GCA_000001405.15.fasta'
try:
output = subprocess.Popen(
[PYTHON_PATH, guide_validationScript_path,
guide_format_path, filePath],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
checkPAM = False
for line in output.stdout.readlines():
line = line.strip()
try:
assert('passed' in line)
checkPAM = True
except AssertionError:
errors['CRISPR_guide_quant_validation'] = line
update_content_error(errors, 'File failed CRISPR guide quantification format validation ' +
'(check_guide_quant_format.py). ' + errors['CRISPR_guide_quant_validation'])
else:
result['CRISPR_guide_quant_validation'] = line
if checkPAM:
try:
output = subprocess.Popen(
[PYTHON_PATH, pam_validationScript_path,
filePath,
genome_reference_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
count = 0
for line in output.stdout.readlines():
line = line.strip()
if count == 3:
try:
assert('More than 80% of the PAMs are NGG. The coordinates are likely to be correct' in line)
except AssertionError:
errors['CRISPR_PAM_validation'] = line
update_content_error(errors, 'File failed CRISPR PAM validation ' +
'(check_PAM.py). ' + errors['CRISPR_PAM_validation'])
else:
result['CRISPR_PAM_validation'] = line
count+=1
except subprocess.CalledProcessError as e:
errors['CRISPR_PAM_info_extraction'] = 'Failed to extract information from ' + \
filePath
except subprocess.CalledProcessError as e:
errors['CRISPR_guide_info_extraction'] = 'Failed to extract information from ' + \
filePath
def process_illumina_read_name_pattern(read_name,
read_numbers_set,
signatures_set,
signatures_no_barcode_set,
srr_flag):
read_name_array = re.split(r'[:\s_]', read_name)
flowcell = read_name_array[2]
lane_number = read_name_array[3]
if srr_flag:
read_number = list(read_numbers_set)[0]
else:
read_number = read_name_array[-4]
read_numbers_set.add(read_number)
barcode_index = read_name_array[-1]
signatures_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':' + barcode_index + ':')
signatures_no_barcode_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':')
def process_special_read_name_pattern(read_name,
words_array,
signatures_set,
signatures_no_barcode_set,
read_numbers_set,
srr_flag):
if srr_flag:
read_number = list(read_numbers_set)[0]
else:
read_number = 'not initialized'
if len(words_array[0]) > 3 and \
words_array[0][-2:] in ['/1', '/2']:
read_number = words_array[0][-1]
read_numbers_set.add(read_number)
read_name_array = re.split(r'[:\s_]', read_name)
flowcell = read_name_array[2]
lane_number = read_name_array[3]
barcode_index = read_name_array[-1]
signatures_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':' + barcode_index + ':')
signatures_no_barcode_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':')
def process_new_illumina_prefix(read_name,
signatures_set,
old_illumina_current_prefix,
read_numbers_set,
srr_flag):
if srr_flag:
read_number = list(read_numbers_set)[0]
else:
read_number = '1'
read_numbers_set.add(read_number)
read_name_array = re.split(r':', read_name)
if len(read_name_array) > 3:
flowcell = read_name_array[2]
lane_number = read_name_array[3]
prefix = flowcell + ':' + lane_number
if prefix != old_illumina_current_prefix:
old_illumina_current_prefix = prefix
signatures_set.add(
flowcell + ':' + lane_number + ':' +
read_number + '::' + read_name)
return old_illumina_current_prefix
def process_pacbio_read_name_pattern(
read_name,
signatures_set,
movie_identifier
):
arr = re.split(r'/', read_name)
if len(arr) > 1:
movie_identifier = arr[0]
signatures_set.add(
'pacbio:0:1::' + movie_identifier)
return movie_identifier
def process_old_illumina_read_name_pattern(read_name,
read_numbers_set,
signatures_set,
old_illumina_current_prefix,
srr_flag):
if srr_flag:
read_number = list(read_numbers_set)[0]
else:
read_number = '1'
if read_name[-2:] in ['/1', '/2']:
read_numbers_set.add(read_name[-1])
read_number = read_name[-1]
arr = re.split(r':', read_name)
if len(arr) > 1:
prefix = arr[0] + ':' + arr[1]
if prefix != old_illumina_current_prefix:
old_illumina_current_prefix = prefix
flowcell = arr[0][1:]
if (flowcell.find('-') != -1 or
flowcell.find('_') != -1):
flowcell = 'TEMP'
# at this point we assume the read name is following old illumina format template
# however sometimes the read names are following some different template
# in case the lane variable is different from number (i.e contains letters)
# we will default it to 0, the information is not lost, since the whole read name is
# at the end of the signature string
lane_number = '0'
if arr[1].isdigit():
lane_number = arr[1]
signatures_set.add(
flowcell + ':' + lane_number + ':' +
read_number + '::' + read_name)
return old_illumina_current_prefix
def process_read_name_line(read_name_line,
old_illumina_current_prefix,
read_numbers_set,
signatures_no_barcode_set,
signatures_set,
read_lengths_dictionary,
errors, srr_flag, read_name_details):
read_name = read_name_line.strip()
if read_name_details:
#extract fastq signature parts using read_name_detail
read_name_array = re.split(r'[:\s]', read_name)
flowcell = read_name_array[read_name_details['flowcell_id_location']]
lane_number = read_name_array[read_name_details['lane_id_location']]
if not read_name_details.get('read_number_location'):
read_number = "1"
else:
read_number = read_name_array[read_name_details['read_number_location']]
read_numbers_set.add(read_number)
if not read_name_details.get('barcode_location'):
barcode_index = ''
else:
barcode_index = read_name_array[read_name_details['barcode_location']]
signatures_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':' + barcode_index + ':')
signatures_no_barcode_set.add(
flowcell + ':' + lane_number + ':' +
read_number + ':')
else:
words_array = re.split(r'\s', read_name)
if read_name_pattern.match(read_name) is None:
if special_read_name_pattern.match(read_name) is not None:
process_special_read_name_pattern(read_name,
words_array,
signatures_set,
signatures_no_barcode_set,
read_numbers_set,
srr_flag)
elif srr_read_name_pattern.match(read_name.split(' ')[0]) is not None:
# in case the readname is following SRR format, read number will be
# defined using SRR format specifications, and not by the illumina portion of the read name
# srr_flag is used to distinguish between srr and "regular" readname formats
srr_portion = read_name.split(' ')[0]
if srr_portion.count('.') == 2:
read_numbers_set.add(srr_portion[-1])
else:
read_numbers_set.add('1')
illumina_portion = read_name.split(' ')[1]
old_illumina_current_prefix = process_read_name_line('@'+illumina_portion,
old_illumina_current_prefix,
read_numbers_set,
signatures_no_barcode_set,
signatures_set,
read_lengths_dictionary,
errors, True, read_name_details)
elif pacbio_read_name_pattern.match(read_name):
# pacbio reads include:
# movie identifier that includes the time of run start (m140415_143853)
# instrment serial number (42175)
# SMRT cell barcode (c100635972550000001823121909121417)
# set number
# part number
# m140415_143853_42175_c100635972550000001823121909121417_s1_p0/....
# alternatively the names would include:
# instrment serial number (42175)
# time of run start (140415_143853)
# m42175_140415_143853/
movie_identifier = read_name.split('/')[0]
if len(movie_identifier) > 0:
process_pacbio_read_name_pattern(
read_name,
signatures_set,
movie_identifier
)
else:
errors['fastq_format_readname'] = read_name
else:
# unrecognized read_name_format
# current convention is to include WHOLE
# readname at the end of the signature
if len(words_array) == 1:
if read_name_prefix.match(read_name) is not None:
# new illumina without second part
old_illumina_current_prefix = process_new_illumina_prefix(
read_name,
signatures_set,
old_illumina_current_prefix,
read_numbers_set,
srr_flag)
elif len(read_name) > 3 and read_name.count(':') > 2:
# assuming old illumina format
old_illumina_current_prefix = process_old_illumina_read_name_pattern(
read_name,
read_numbers_set,
signatures_set,
old_illumina_current_prefix,
srr_flag)
else:
errors['fastq_format_readname'] = read_name
# the only case to skip update content error - due to the changing
# nature of read names
else:
errors['fastq_format_readname'] = read_name
# found a match to the regex of "almost" illumina read_name
else:
process_illumina_read_name_pattern(
read_name,
read_numbers_set,
signatures_set,
signatures_no_barcode_set,
srr_flag)
return old_illumina_current_prefix
def process_sequence_line(sequence_line, read_lengths_dictionary):
length = len(sequence_line.strip())
if length not in read_lengths_dictionary:
read_lengths_dictionary[length] = 0
read_lengths_dictionary[length] += 1
def process_fastq_file(job, fastq_data_stream, session, url):
item = job['item']
errors = job['errors']
result = job['result']
platform_uuid = get_platform_uuid(job.get('@id'), errors, session, url)
read_name_details = get_read_name_details(job.get('@id'), errors, session, url)
read_numbers_set = set()
signatures_set = set()
signatures_no_barcode_set = set()
read_lengths_dictionary = {}
read_count = 0
old_illumina_current_prefix = 'empty'
try:
line_index = 0
for encoded_line in fastq_data_stream.stdout:
try:
line = encoded_line.decode('utf-8')
except UnicodeDecodeError:
errors['readname_encoding'] = 'Error occured, while decoding the readname string.'
else:
line_index += 1
if line_index == 1:
# may be from here deliver a flag about the presence/absence of the readnamedetails
if platform_uuid not in ['ced61406-dcc6-43c4-bddd-4c977cc676e8', #pacbio sequel
'c7564b38-ab4f-4c42-a401-3de48689a998', #pacbio rs2
'e2be5728-5744-4da4-8881-cb9526d0389e', #pacbio sequel 2
'7cc06b8c-5535-4a77-b719-4c23644e767d', #ON SmidgION
'8f1a9a8c-3392-4032-92a8-5d196c9d7810', #ON promethion
'6c275b37-018d-4bf8-85f6-6e3b830524a9', #ON minion
'6ce511d5-eeb3-41fc-bea7-8c38301e88c1', #ON gridionx5
'25acccbd-cb36-463b-ac96-adbac11227e6', #Ultima
]:
old_illumina_current_prefix = \
process_read_name_line(
line,
old_illumina_current_prefix,
read_numbers_set,
signatures_no_barcode_set,
signatures_set,
read_lengths_dictionary,
errors, False,
read_name_details)
if line_index == 2:
read_count += 1
process_sequence_line(line, read_lengths_dictionary)
line_index = line_index % 4
except IOError:
errors['unzipped_fastq_streaming'] = 'Error occured, while streaming unzipped fastq.'
else:
# read_count update
result['read_count'] = read_count
# read1/read2
# Ultima FASTQs should be excluded from read pairing checks
if platform_uuid not in ['25acccbd-cb36-463b-ac96-adbac11227e6']:
if len(read_numbers_set) > 1:
errors['inconsistent_read_numbers'] = \
'fastq file contains mixed read numbers ' + \
'{}.'.format(', '.join(sorted(list(read_numbers_set))))
update_content_error(errors,
'Fastq file contains a mixture of read1 and read2 sequences')
# read_length
read_lengths_list = []
for k in sorted(read_lengths_dictionary.keys()):
read_lengths_list.append((k, read_lengths_dictionary[k]))
#excluding Pacbio, Nanopore, and Ultima from read_length verification
if platform_uuid not in ['ced61406-dcc6-43c4-bddd-4c977cc676e8',
'c7564b38-ab4f-4c42-a401-3de48689a998',
'e2be5728-5744-4da4-8881-cb9526d0389e',
'7cc06b8c-5535-4a77-b719-4c23644e767d',
'8f1a9a8c-3392-4032-92a8-5d196c9d7810',
'6c275b37-018d-4bf8-85f6-6e3b830524a9',
'6ce511d5-eeb3-41fc-bea7-8c38301e88c1',
'25acccbd-cb36-463b-ac96-adbac11227e6'
]:
if 'read_length' in item and item['read_length'] > 2:
process_read_lengths(read_lengths_dictionary,
read_lengths_list,
item['read_length'],
read_count,
0.9,
errors,
result)
else:
errors['read_length'] = 'no specified read length in the uploaded fastq file, ' + \
'while read length(s) found in the file were {}. '.format(
', '.join(map(str, read_lengths_list)))
update_content_error(errors,
'Fastq file metadata lacks read length information, ' +
'but the file contains read length(s) {}'.format(
', '.join(map(str, read_lengths_list))))
# signatures
# Ultima FASTQs should be excluded from signature checks
if platform_uuid in ['25acccbd-cb36-463b-ac96-adbac11227e6']:
return
signatures_for_comparison = set()
is_UMI = False
if 'flowcell_details' in item and len(item['flowcell_details']) > 0:
for entry in item['flowcell_details']:
if 'barcode' in entry and entry['barcode'] == 'UMI':
is_UMI = True
break
if old_illumina_current_prefix == 'empty' and is_UMI:
for entry in signatures_no_barcode_set:
signatures_for_comparison.add(entry + 'UMI:')
else:
if old_illumina_current_prefix == 'empty' and len(signatures_set) > 100:
signatures_for_comparison = process_barcodes(signatures_set)
if len(signatures_for_comparison) == 0:
for entry in signatures_no_barcode_set:
signatures_for_comparison.add(entry + 'mixed:')
else:
signatures_for_comparison = signatures_set
result['fastq_signature'] = sorted(list(signatures_for_comparison))
check_for_fastq_signature_conflicts(
session,
url,
errors,
item,
signatures_for_comparison)
def process_barcodes(signatures_set):
set_to_return = set()
flowcells_dict = {}
for entry in signatures_set:
(f, l, r, b, rest) = entry.split(':')
if (f, l, r) not in flowcells_dict:
flowcells_dict[(f, l, r)] = {}
if b not in flowcells_dict[(f, l, r)]:
flowcells_dict[(f, l, r)][b] = 0
flowcells_dict[(f, l, r)][b] += 1
for key in flowcells_dict.keys():
barcodes_dict = flowcells_dict[key]
total = 0
for b in barcodes_dict.keys():
total += barcodes_dict[b]
for b in barcodes_dict.keys():
if ((float(total)/float(barcodes_dict[b])) < 100):
set_to_return.add(key[0] + ':' +
key[1] + ':' +
key[2] + ':' +
b + ':')
return set_to_return
def process_read_lengths(read_lengths_dict,
lengths_list,
submitted_read_length,
read_count,
threshold_percentage,
errors_to_report,
result):
reads_quantity = sum([count for length, count in read_lengths_dict.items()
if (submitted_read_length - 2) <= length <= (submitted_read_length + 2)])
informative_length_list = []
for readLength in lengths_list:
informative_length_list.append('bp, '.join(map(str,readLength)))
if ((threshold_percentage * read_count) > reads_quantity):
errors_to_report['read_length'] = \
'in file metadata the read_length is {}bp, '.format(submitted_read_length) + \
'however the uploaded fastq file contains reads of following length(s) ' + \
'{}. '.format(', '.join(map(str, ['(%s)' % item for item in informative_length_list])))
update_content_error(errors_to_report,
'Fastq file metadata specified read length was {}bp, '.format(
submitted_read_length) +
'but the file contains read length(s) {}'.format(
', '.join(map(str, ['(%s)' %item for item in informative_length_list]))))
def create_a_list_of_barcodes(details):
barcodes = set()
for entry in details:
barcode = entry.get('barcode')
lane = entry.get('lane')
if lane and barcode:
barcodes.add((lane, barcode))
return barcodes
def compare_flowcell_details(flowcell_details_1, flowcell_details_2):
barcodes_1 = create_a_list_of_barcodes(flowcell_details_1)
barcodes_2 = create_a_list_of_barcodes(flowcell_details_1)
if barcodes_1 & barcodes_2:
# intersection found
return True
# no intersection
return False
def get_mapped_run_type_bam(job, bam_data_stream):
"""
obtain mapped run type from all bams by using samtools stats
"""
errors = job['errors']
result = job['result']
numPairedReads = None
for line in bam_data_stream.stdout.readlines():
line = line.strip()
try:
assert('Failure' not in line)
except AssertionError:
errors['samtools_stats_decoding_failure'] = line
update_content_error(errors, 'File failed samtools stats extraction. ' +
errors['samtools_stats_decoding_failure'])
else:
if 'SN' in line and 'reads paired' in line.strip():
line = line.split('\t')
numPairedReads = line[2]
result['samtools_stats_mapped_run_type_extraction'] = line
runType = None
if numPairedReads:
if int(numPairedReads) > 0:
runType = 'paired-ended'
else:
runType = 'single-ended'
return runType
def get_mapped_read_length_bam(job, bam_data_stream):
"""
obtain mapped read length from all bams by using samtools stats
"""
errors = job['errors']
result = job['result']
readLength = None
for line in bam_data_stream.stdout.readlines():
line = line.strip()
try:
assert('Failure' not in line)
except AssertionError:
errors['samtools_stats_decoding_failure'] = line
update_content_error(errors, 'File failed samtools stats extraction. ' +
errors['samtools_stats_decoding_failure'])
else:
line = line.split('\t')
readLength = int(line[0])
result['samtools_stats_mapped_read_length_extraction'] = line
return readLength
def get_read_name_details(job_id, errors, session, url):
query = job_id +'?datastore=database&frame=object&format=json'
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_read_name_detaisl'] = ('Network error occured, while looking for '
'file read_name details on the portal. {}').format(str(e))
else:
details = r.json().get('read_name_details')
if details:
return details
def get_platform_uuid(job_id, errors, session, url):
query = job_id +'?datastore=database&frame=object&format=json'
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_platform'] = ('Network error occured, while looking for '
'platform on the portal. {}').format(str(e))
else:
platform_id = r.json().get('platform')
if platform_id:
query = platform_id +'?datastore=database&frame=object&format=json'
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_platform'] = ('Network error occured, while looking for '
'platform on the portal. {}').format(str(e))
else:
platform_uuid = r.json().get('uuid')
return platform_uuid
return platform_id
def get_all_derived_from(job_id, errors, session, url):
'''
following logic from property_closure function from encoded
'''
derived_from_list = set()
remaining = {str(job_id)}
while remaining:
derived_from_list.update(remaining)
next_remaining = set()
for file in remaining:
query = file +'?datastore=database&frame=object&format=json'
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_file_derived_from'] = ('Network error occured, while looking for '
'derived_from on the portal. {}').format(str(e))
else:
try:
next_remaining.update(r.json().get('derived_from'))
except TypeError:
pass
remaining = next_remaining - derived_from_list
return derived_from_list
def get_platform_from_bams(job_id, errors, session, url):
query = job_id +'?datastore=database&frame=object&format=json'
platform_list = []
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_derived_from'] = ('Network error occured, while looking for '
'derived_from on the portal. {}').format(str(e))
else:
derived_from_list = get_all_derived_from(r.json().get('@id'), errors, session, url)
if derived_from_list:
for file in derived_from_list:
query = file +'?datastore=database&frame=object&format=json'
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_file'] = ('Network error occured, while looking for '
'file_format on the portal. {}').format(str(e))
else:
file_format = r.json().get('file_format')
if file_format == 'fastq':
platform_uuid = get_platform_uuid(file, errors, session, url)
if platform_uuid:
platform_list.append(platform_uuid)
return set(platform_list)
def check_for_fastq_signature_conflicts(session,
url,
errors,
item,
signatures_to_check):
conflicts = []
for signature in sorted(list(signatures_to_check)):
if not signature.endswith('mixed:'):
query = '/search/?type=File&status!=replaced&file_format=fastq&' + \
'datastore=database&fastq_signature=' + signature
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_fastq_signature'] = 'Network error occured, while looking for ' + \
'fastq signature conflict on the portal. ' + \
str(e)
else:
r_graph = r.json().get('@graph')
# found a conflict
if len(r_graph) > 0:
# the conflict in case of missing barcode in read names could be resolved with metadata flowcell details
for entry in r_graph:
if (not signature.endswith('::') or
(signature.endswith('::') and entry.get('flowcell_details') and
item.get('flowcell_details') and
compare_flowcell_details(entry.get('flowcell_details'),
item.get('flowcell_details')))):
if 'accession' in entry and 'accession' in item and \
entry['accession'] != item['accession']:
conflicts.append(
'%s in file %s ' % (
signature,
entry['accession']))
elif 'accession' in entry and 'accession' not in item:
conflicts.append(
'%s in file %s ' % (
signature,
entry['accession']))
elif 'accession' not in entry and 'accession' not in item:
conflicts.append(
'%s ' % (
signature) +
'file on the portal.')
# "Fastq file contains read name signatures that conflict with signatures from file X”]
if len(conflicts) > 0:
errors['not_unique_flowcell_details'] = 'Fastq file contains read name signature ' + \
'that conflict with signature of existing ' + \
'file(s): {}'.format(
', '.join(map(str, conflicts)))
update_content_error(errors, 'Fastq file contains read name signature ' +
'that conflict with signature of existing ' +
'file(s): {}'.format(
', '.join(map(str, conflicts))))
return False
return True
def check_for_contentmd5sum_conflicts(item, result, output, errors, session, url):
result['content_md5sum'] = output[:32].decode(errors='replace')
try:
int(result['content_md5sum'], 16)
except ValueError:
errors['content_md5sum'] = output.decode(errors='replace').rstrip('\n')
update_content_error(errors, 'File content md5sum format error')
else:
query = '/search/?type=File&status!=replaced&datastore=database&content_md5sum=' + result[
'content_md5sum']
try:
r = session.get(urljoin(url, query))
except requests.exceptions.RequestException as e:
errors['lookup_for_content_md5sum'] = 'Network error occured, while looking for ' + \
'content md5sum conflict on the portal. ' + str(e)
else:
try:
r_graph = r.json().get('@graph')
except ValueError:
errors['content_md5sum_lookup_json_error'] = str(r)
else:
if len(r_graph) > 0: