-
Notifications
You must be signed in to change notification settings - Fork 2
/
tester.py
executable file
·861 lines (760 loc) · 32 KB
/
tester.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
"""
Data tester for the GA4GH streaming API.
"""
from __future__ import print_function
from __future__ import division
import argparse
import collections
import logging
import os
import os.path
import random
import subprocess
import sys
import tempfile
import time
import requests
import json
import htsget
import pysam
import humanize
from six.moves import zip
from six.moves.urllib.parse import urlencode
__version__ = "0.1.1"
FORMAT_BAM = "BAM"
FORMAT_CRAM = "CRAM"
def retry_command(cmd, filename=None, max_retries=5, retry_wait=5):
"""
Runs the specified command and writes the stdout to the specified file (if
supplied). If an exception occurs, retry a maximumum of max_retries times,
waiting retry_wait seconds after every failure. If the maximum number of
retries has been exceeded, raise the original exception.
"""
done = False
num_retries = 0
while not done:
try:
outfile = None
if filename is not None:
outfile = open(filename, "w")
subprocess.check_call(cmd, stdout=outfile)
done = True
except subprocess.CalledProcessError as cse:
if num_retries == max_retries:
raise cse
logging.warn("Command failed, retrying: '{}'".format(" ".join(cmd)))
time.sleep(retry_wait)
num_retries += 1
finally:
if outfile is not None:
outfile.close()
def htsget_api(
url, filename, reference_name=None, reference_md5=None, start=None, end=None,
data_format=None):
logging.info("htsget-api: request (name='{}', md5='{}', start={}, end={})".format(
reference_name, reference_md5, start, end))
with open(filename, "wb") as tmp:
htsget.get(
url, tmp, reference_name=reference_name, reference_md5=reference_md5,
start=start, end=end, data_format=data_format, timeout=120)
def htsget_cli(
url, filename, reference_name=None, reference_md5=None, start=None, end=None,
data_format=None):
"""
Runs the htsget CLI program. Assumes that htsget has been installed and the
CLI program is in PATH.
"""
cmd = ["htsget", url, "-O", filename, "--timeout", "120"]
if args.bearer_token: cmd.extend(["--bearer-token", args.bearer_token])
if args.ca_bundle: os.environ["CURL_CA_BUNDLE"] = args.ca_bundle
if reference_name is not None:
cmd.extend(["-r", str(reference_name)])
if start is not None:
cmd.extend(["-s", str(start)])
if end is not None:
cmd.extend(["-e", str(end)])
if data_format is not None:
cmd.extend(["-f", str(data_format)])
logging.info("htsget-cli run: {}".format(" ".join(cmd)))
# We don't need to retry here because htsget automatically retries on errors.
subprocess.check_call( cmd )
def dnanexus_cli(
url, filename, reference_name=None, start=None, end=None, data_format=None):
"""
Runs the htsnexus CLI program. Assumes that the script has been downloaded
into the current working directory, and is executable.
See https://github.com/dnanexus-rnd/htsnexus for details on how to download
the script and use it.
"""
url_segments = url.split('/')
namespace = url_segments[-2]
accession = url_segments[-1]
server = "/".join(url_segments[:-2])
cmd = ["./htsnexus.py", "-s", server]
if reference_name is not None:
ref = str(reference_name)
# htsnexus doesn't support specifying start and end on their own,
# so we have to work around this with boundary values.
s = 0 if start is None else start
# This should be a safe upper bound according to the SAM spec.
e = 2**31 - 1 if end is None else end
if start is not None or end is not None:
ref += ":{}-{}".format(s, e)
cmd.extend(["-r", ref])
if args.bearer_token: cmd.extend(["--token", args.bearer_token])
if args.ca_bundle: cmd.extend(["--insecure"])
cmd.extend([namespace, accession])
if data_format is not None:
cmd.append(data_format)
logging.info("htsnexus run: {}".format(" ".join(cmd)))
retry_command(cmd, filename)
def sanger_cli(
url, filename, reference_name=None, start=None, end=None, data_format=None):
"""
Runs the Sanger Javascript client. Make sure you run these commands in the
same directory as the current script, i.e.
$ npm install npg_ranger
$ python tester.py --client=sanger-cli <other args>
"""
params = {}
if reference_name is not None:
params["referenceName"] = reference_name
if start is not None:
params["start"] = str(start)
if end is not None:
params["end"] = str(end)
if data_format is not None:
params["format"] = data_format
if len(params) > 0:
url += "?{}".format(urlencode(params))
cmd = ["node_modules/.bin/npg_ranger_client", url, filename]
if args.ca_bundle: cmd.extend(["--with_ca", args.ca_bundle])
if args.bearer_token:
with open('./sanger_token.json', 'w') as outfile:
json.dump({'token': ""+str(args.bearer_token)}, outfile)
cmd.extend(["--token_config",'./sanger_token.json'])
logging.info("sanger client run: {}".format(" ".join(cmd)))
retry_command(cmd)
def ena_cli(url, filename, reference_name=None, start=None, end=None, data_format=None):
"""
Runs the ENA client ( https://github.com/enasequence/ena-ga4gh-read-client )
"""
url_segments = url.split('/')
dataset_id = url_segments[-1]
endpoint_url = "/".join(url_segments[:-1]) + "/"
cmd = ["ga4gh-dapi-client"]
cmd.extend(["--endpoint-url", endpoint_url])
cmd.extend(["--dataset-id", dataset_id])
cmd.extend(["--output-file", filename])
if data_format is not None:
cmd.extend(["--format", data_format])
if reference_name is not None:
cmd.extend(["--reference-name", reference_name])
if start is not None:
cmd.extend(["--alignment-start", str(start)])
if end is not None:
cmd.extend(["--alignment-stop", str(end)])
logging.info("ENA run: {}".format(" ".join(cmd)))
retry_command(cmd)
def ega_cli(url, filename, reference_name=None, start=None, end=None, data_format=None):
"""
Runs the EGA client by Alexander Senf ( https://github.com/EbiEga/ega-htsget-client )
"""
url_segments = url.split('/')
dataset_id = url_segments[-1]
endpoint_url = "/".join(url_segments[:-1]) + "/"
cmd = ["java"]
cmd.extend(["-jar", "EgaHtsgetClient.jar"])
if args.bearer_token: cmd.extend(["--oauth-token", args.bearer_token])
cmd.extend(["--endpoint-url", endpoint_url])
cmd.extend(["--dataset-id", dataset_id])
cmd.extend(["--output-file", filename])
cmd.extend(["--debug"])
if data_format is not None:
cmd.extend(["--format", data_format])
if reference_name is not None:
cmd.extend(["--reference-name", reference_name])
if start is not None:
cmd.extend(["--alignment-start", str(start)])
if end is not None:
cmd.extend(["--alignment-stop", str(end)])
logging.info("EGA run: {}".format(" ".join(cmd)))
retry_command(cmd)
def samtools_cli( url, filename, reference_name=None, start=None, end=None, data_format=None):
"""
Runs the samtools CLI program.
https://github.com/samtools/samtools
"""
url += '?format=' + ('BAM' if data_format is None else str(data_format))
format_flag = '-b' if data_format is None or data_format == FORMAT_BAM else '-C'
if reference_name is not None:
url += '&referenceName='+str(reference_name)
if start is not None:
url += '&start='+ str(start)
if end is not None:
url += '&end='+ str(end)
cmd = ["samtools", "view", format_flag, url]
#--- uncomment these 2 lines to debug samtools output
#format_flag = "-c"
#cmd = ["htsfile", "-vvvvvvvvv", format_flag, url]
if args.bearer_token:
with open('samtools_token', 'w') as outfile:
outfile.write( args.bearer_token )
os.environ["HTS_AUTH_LOCATION"] = "samtools_token"
if args.ca_bundle: os.environ["CURL_CA_BUNDLE"] = args.ca_bundle;
logging.info("samtools run: {}".format(" ".join(cmd)))
logging.info("tmp file : "+str(filename) )
retry_command(cmd, filename)
# All clients must be registered in this map to be available for use.
client_map = {
"htsget-api": htsget_api,
"htsget-cli": htsget_cli,
"dnanexus-cli": dnanexus_cli,
"sanger-cli": sanger_cli,
"ena-cli": ena_cli,
"ega-cli": ega_cli,
"samtools-cli": samtools_cli
}
class TestFailedException(Exception):
"""
Exception raised when we know we've failed.
"""
class DownloadFailedException(Exception):
"""
Exception raised when we downloading the data failed for some reason.
"""
def hashkey(read):
"""
Returns a unique hash key for the specified read. We cannot use the qname
as mates and secondary alignments have the same qname.
# (first of pair, second of pair, not primary alignment, supplementary alignment).
"""
key = "{}_{}_{}_{}_{}_{}_{}".format(
read.query_name, read.pos, read.cigar,
int(read.is_read1), int(read.is_read2),
int(read.is_secondary), int(read.is_supplementary))
return key
def filter_unmapped(iterator):
"""
Returns an iterator over the specified iterator of reads with
all unmapped reads removed.
"""
filtered = 0
for read in iterator:
if read.is_unmapped:
filtered += 1
else:
yield read
logging.info("Filtered {} reads".format(filtered))
class TestReport(object):
"""
A report on a single test run.
"""
def __init__(
self, num_references=None, total_queries=None, total_downloaded_data=None,
total_download_time=None, mismatch_counts=None):
self.num_references = num_references
self.total_queries = total_queries
self.total_downloaded_data = total_downloaded_data
self.total_download_time = total_download_time
self.mismatch_counts = mismatch_counts
class Contig(object):
"""
Represents a single contig within the BAM file.
"""
def __init__(
self, reference_name, reference_md5, length, start_positions, end_positions):
self.reference_name = reference_name
self.reference_md5 = reference_md5
self.length = length
self.start_positions = start_positions
self.end_positions = end_positions
class Field(object):
"""
A class to simplify comparing a single field in two different Pysam Read
records.
"""
def __init__(self, name, equals):
self.name = name
self.equals = equals
def simple_equality(v1, v2, read):
"""
Equality for simple fields where equality can be tested directly.
"""
return v1 == v2
def sorted_equality(v1, v2, read):
"""
Equality for fields where the values must be sorted before equality tested.
"""
return sorted(v1) == sorted(v2)
class ServerTester(object):
"""
Class to run systematic tests on a server based on a local indexed
BAM file.
"""
def __init__(
self, source_file_name, server_url, filter_unmapped=False, tmpdir=None,
num_boundary_reads=10, max_references=100, max_random_query_length=10**6,
client=None):
self.source_file_name = source_file_name
self.server_url = server_url
self.filter_unmapped = filter_unmapped
self.tmpdir = tmpdir
self.max_random_query_length = max_random_query_length
self.alignment_file = pysam.AlignmentFile(self.source_file_name)
extension = os.path.splitext(self.source_file_name)[1].lower()
if extension == ".cram":
self.data_format = FORMAT_CRAM
elif extension == ".bam":
self.data_format = FORMAT_BAM
else:
raise ValueError("Unknown file format: {}. Please use .bam or .cram".format(
extension))
self.temp_file_name = None
self.subset_alignment_file = None
self.num_boundary_reads = num_boundary_reads
self.max_references = max_references
self.client = client
self.contigs = []
self.mismatch_counts = collections.Counter()
self.mismatch_report_threshold = 20 # Avoid flooding log.
self.total_queries = 0
self.total_downloaded_data = 0
self.total_download_time = 1e-8 # Avoid zero division problems
self.fields = [
Field("pos", simple_equality),
Field("query_name", simple_equality),
Field("reference_name", simple_equality),
Field("cigarstring", simple_equality),
Field("query_alignment_sequence", simple_equality),
Field("query_alignment_qualities", simple_equality),
Field("template_length", simple_equality),
Field("mapping_quality", simple_equality),
Field("tags", sorted_equality),
Field("next_reference_id", self.next_reference_equality),
Field("next_reference_start", self.next_reference_start_equality),
Field("flag", self.flags_equality),
# TODO fill in remaining BAM fields.
]
def next_reference_equality(self, local_rid, remote_rid, local_read):
"""
Compares the two specified references.
"""
# We need to convert the reference IDs back into names so that
# we can compare them.
r1 = self.alignment_file.references[local_rid]
r2 = self.subset_alignment_file.references[remote_rid]
if self.filter_unmapped and local_read.mate_is_unmapped:
ret = True
# We allow the remote reference ID to be unset if we are filtering
# out unmapped reads and the mate is unmapped
if remote_rid != -1:
ret = r1 == r2
else:
# If both are unset, they are equal. We need to check this as the
# order of the references is abitrary, and references[-1] is just
# the last element in the list.
if local_rid == -1 and remote_rid == -1:
ret = True
else:
ret = r1 == r2
return ret
def next_reference_start_equality(self, v1, v2, local_read):
"""
Compares the two specified values.
"""
if self.filter_unmapped and local_read.mate_is_unmapped:
ret = True
# We allow the remote value to be unset if we are filtering out unmapped
# reads.
if v2 != -1:
ret = v1 == v2
else:
ret = v1 == v2
return ret
def flags_equality(self, local_flags, remote_flags, local_read):
"""
Compares the specified flags values.
"""
condition = (
self.filter_unmapped and
local_read.mate_is_unmapped and
local_read.mate_is_reverse)
if condition:
# If we are filtering out unmapped reads and the mate is unmapped, then
# the mate_is_reverse flag can't be set on the remote flags. This is
# what the offset by 32 effectively means.
ret = local_flags == remote_flags + 32
else:
ret = local_flags == remote_flags
return ret
def get_start_positions(self, reference_name):
"""
Returns the positions of the first num_boundary_reads on the specified reference.
"""
start_positions = []
for read in self.alignment_file.fetch(reference_name):
start_positions.append(read.pos)
if len(start_positions) == self.num_boundary_reads:
break
return start_positions
def get_end_positions(self, reference_name, length):
"""
Returns the positions of the last num_boundary_reads on the specified reference.
"""
found = False
x = length
while not found:
for read in self.alignment_file.fetch(reference_name, x):
found = True
break
if not found:
# Skip back 1% of the length of the chromosome
x = x - length / 100
positions = collections.deque([], maxlen=self.num_boundary_reads)
for read in self.alignment_file.fetch(reference_name, x):
positions.append(read.pos)
return list(positions)
def initialise(self):
"""
Scans the input file and initialises the data structures we need for the
tests.
"""
fd, self.temp_file_name = tempfile.mkstemp(
prefix="gastream_test_", dir=self.tmpdir, suffix="." + self.data_format)
os.close(fd)
# Determine the bounds of the individual contigs.
total_references = len(self.alignment_file.lengths)
num_references = min(self.max_references, total_references)
logging.info("Reading file {}".format(self.source_file_name))
for j in range(num_references):
reference_name = self.alignment_file.references[j]
sq = self.alignment_file.header['SQ'][j]
reference_md5 = sq.get('M5', None)
length = self.alignment_file.lengths[j]
assert sq['LN'] == length
assert sq['SN'] == reference_name
start_positions = self.get_start_positions(reference_name)
if len(start_positions) > 0:
end_positions = self.get_end_positions(reference_name, length)
contig = Contig(
reference_name, reference_md5, length, start_positions,
end_positions)
self.contigs.append(contig)
msg = (
"Read contig {}: got {} start positions and {} end "
"positions".format(
contig.reference_name, len(start_positions), len(end_positions)))
logging.info(msg)
else:
logging.info("Skipping empty contig {}".format(reference_name))
def verify_reads_equal(self, r1, r2):
for field in self.fields:
v1 = getattr(r1, field.name)
v2 = getattr(r2, field.name)
if not field.equals(v1, v2, r1):
self.mismatch_counts[field.name] += 1
if self.mismatch_counts[field.name] < self.mismatch_report_threshold:
logging.warning("Mismatch at {}:{}.{}:: {} != {}".format(
r1.reference_name, r1.pos, field.name, v1, v2))
if type(v1) is list and type(v2) is list:
v1 = set(v1)
v2 = set(v2)
logging.warning(
"Extra elements in 1st set (v1-v2) : {}".format(
sorted(v1 - v2)))
logging.warning(
"Extra elements in 2nd set (v2-v1) : {}".format(
sorted(v2 - v1)))
elif self.mismatch_counts[field.name] == self.mismatch_report_threshold:
logging.warning(
"More than {} mismatches logged. Not reporting any more".format(
self.mismatch_report_threshold))
def verify_reads(self, iter1, iter2):
"""
Verifies that the specified iterators contain the same set of
reads. Returns the number of reads in the iterators.
"""
def check_reads_for_position(d1, d2):
num_checks = 0
if len(d1) != len(d2):
raise TestFailedException("different numbers of reads returned")
for k in d1.keys():
if k not in d2:
raise TestFailedException("{} not found".format(k))
self.verify_reads_equal(d1[k], d2[k])
num_checks += 1
return num_checks
# Because the order of reads for a given position is unspecified,
# we gather the reads for each iterator into dictionaries indexed
# by a (hopefully) unique combination of fields.
d1 = {}
d2 = {}
last_pos = -1
num_reads = 0
total_checks = 0
for r1, r2 in zip(iter1, iter2):
num_reads += 1
if r1.pos != r2.pos:
raise TestFailedException("Non-matching read positions(local != remote) : ({}){}!={}({})".format( getattr(r1,"query_name"), r1.pos, r2.pos, getattr(r2,"query_name")) )
if r1.pos != last_pos:
total_checks += check_reads_for_position(d1, d2)
d1.clear()
d2.clear()
last_pos = r1.pos
k = hashkey(r1)
if k in d1:
raise TestFailedException("Duplicate read: {}".format(k))
d1[k] = r1
k = hashkey(r2)
if k in d2:
raise TestFailedException("Duplicate read: {}".format(k))
d2[k] = r2
# Check the last set of reads in the dictionaries.
total_checks += check_reads_for_position(d1, d2)
# make sure both iterators are empty
r1 = next(iter1, None)
r2 = next(iter2, None)
if r1 is not None or r2 is not None:
extra_reads = 0;
while r1 is not None:
extra_reads += 1
r1 = next(iter1, None)
while r2 is not None:
extra_reads += 1
r2 = next(iter2, None)
raise TestFailedException("Total number of reads not matching {} vs {}".format(num_reads, num_reads+extra_reads+1))
assert total_checks == num_reads
return num_reads
def verify_query(self, reference_name=None, start=None, end=None):
"""
Runs the specified query and verifies the result.
"""
self.total_queries += 1
# We use the wall-clock time here
before = time.time()
self.client(
self.server_url, self.temp_file_name, reference_name=reference_name,
start=start, end=end, data_format=self.data_format)
duration = time.time() - before
size = os.path.getsize(self.temp_file_name)
self.total_downloaded_data += size
self.total_download_time += duration
logging.info("Downloaded {} in {:.3f} seconds".format(
humanize.naturalsize(size, binary=True), duration))
# Index the downloaded file and compare the reads.
before = time.clock()
pysam.index(self.temp_file_name)
duration = time.clock() - before
logging.debug("Indexed {} file in {:.3f} CPU seconds".format(
self.data_format, duration))
# Analyse the reads
before = time.clock()
iter1 = self.alignment_file.fetch(reference_name, start, end)
if self.data_format == FORMAT_CRAM:
# Due to a bug in htslib, we cannot use the standard code path below
# for CRAM files that have no records. The workaround is to open the
# CRAM file without an index and see if it is emtpy. If it is, we
# skip the rest of the tests. Once the upstream bug in htslib has
# been fixed and incorporated into pysam, we should remove this
# special case.
# See https://github.com/pysam-developers/pysam/issues/483
tmp = pysam.AlignmentFile(
self.temp_file_name, filepath_index="/no/such/index/exists.crai")
empty = True
for _ in tmp.head(1):
empty = False
tmp.close()
if empty:
# Make sure the original iterator is also empty...
count = 0
for _ in iter1:
count += 1
if count != 0:
raise TestFailedException(
"Downloaded CRAM empty, but original contains {} reads".format(
count))
return
try:
self.subset_alignment_file = pysam.AlignmentFile(self.temp_file_name)
iter2 = self.subset_alignment_file.fetch(reference_name, start, end)
except ValueError as ve:
raise DownloadFailedException("Reading downloaded data: {}".format(ve))
if self.filter_unmapped:
iter1 = filter_unmapped(iter1)
num_reads = self.verify_reads(iter1, iter2)
duration = time.clock() - before
logging.debug("Checked reads in {:.3f} CPU seconds".format(duration))
if self.data_format == FORMAT_BAM:
# Count the reads outside the original region. This only works for
# BAM files, so we don't bother for CRAM.
self.subset_alignment_file.reset()
all_reads = 0
for read in self.subset_alignment_file:
all_reads += 1
extra = all_reads - num_reads
logging.info("Downloaded {} reads with {} extra".format(num_reads, extra))
else:
logging.info("Downloaded {} reads".format(num_reads))
def run_random_reads(self, num_tests):
"""
Runs tests for valid ranges chosen randomly across the available
regions.
"""
logging.info("Starting random queries")
for _ in range(num_tests):
contig = random.choice(self.contigs)
start = random.randint(0, contig.length)
end = random.randint(
start, min(start + self.max_random_query_length, contig.length))
self.verify_query(contig.reference_name, start=start, end=end)
def run_full_contig_fetch(self):
"""
Gets all reads for contigs < 1Mb
"""
logging.info("Starting full contig queries")
for contig in self.contigs:
if contig.length < 10**6:
self.verify_query(contig.reference_name)
else:
logging.info("Skipping full fetch for {}; too long".format(
contig.reference_name))
def run_start_reads(self):
"""
Gets the first few reads from each contig.
"""
logging.info("Starting contig start queries")
for contig in self.contigs:
values = [
(contig.start_positions[0], contig.start_positions[-1] + 1),
(None, contig.start_positions[-1] + 1),
(max(0, contig.start_positions[0] - 100), contig.start_positions[0] + 1),
(contig.start_positions[-1], contig.start_positions[-1] + 1)
]
for start, end in values:
self.verify_query(
reference_name=contig.reference_name, start=start, end=end)
# self.verify_query(
# reference_md5=contig.reference_md5, start=start, end=end)
def run_end_reads(self):
"""
Gets the first few reads from each contig.
"""
logging.info("Starting contig end queries")
for contig in self.contigs:
if len(contig.end_positions) > 0:
values = [
(contig.end_positions[0], contig.end_positions[-1] + 1),
(contig.end_positions[0], None),
(max(0, contig.end_positions[0] - 100), contig.end_positions[0] + 1),
]
for start, end in values:
self.verify_query(
reference_name=contig.reference_name, start=start, end=end)
else:
logging.info("Skipping end reads for {}".format(contig.reference_name))
def cleanup(self):
self.alignment_file.close()
if os.path.exists(self.temp_file_name):
os.unlink(self.temp_file_name)
index_file = self.temp_file_name + ".bai"
if os.path.exists(index_file):
os.unlink(index_file)
index_file = self.temp_file_name + ".crai"
if os.path.exists(index_file):
os.unlink(index_file)
def report(self):
return TestReport(
num_references=len(self.contigs),
total_queries=self.total_queries,
total_downloaded_data=self.total_downloaded_data,
total_download_time=self.total_download_time,
mismatch_counts=self.mismatch_counts)
def print_report(self):
# TODO provide options to make this machine readable.
megabytes = self.total_downloaded_data / (1024**2)
average_bandwidth = megabytes / self.total_download_time
print("{} queries downloaded {} of data at an average of {:.2f} Mib/s".format(
humanize.intword(self.total_queries),
humanize.naturalsize(self.total_downloaded_data, binary=True),
average_bandwidth))
print("Total mismatches = ", sum(self.mismatch_counts.values()))
for name in sorted(self.mismatch_counts.keys()):
print("", name, self.mismatch_counts[name], sep="\t")
if __name__ == "__main__":
version_report = "%(prog)s {} (htsget {}; Python {})".format(
__version__, htsget.__version__, ".".join(map(str, sys.version_info[:3])))
parser = argparse.ArgumentParser(
description="A simple tester application for the GA4GH streaming API.")
parser.add_argument(
"-V", "--version", action='version',
version=version_report)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument(
"source_file", type=str, help="The local BAM/CRAM file to compare to")
parser.add_argument(
"url", type=str, help="The server url corresponding to the input source file.")
parser.add_argument(
"--filter-unmapped", action='store_true',
help="Filter out unmapped reads before comparing reads.")
parser.add_argument(
"--tmpdir", type=str,
help=(
"Directory in which to store temporary files. "
"Defaults to platform default"))
parser.add_argument(
"--max-references", type=int, default=100,
help="The maximum number of references to consider")
parser.add_argument(
"--num-random-reads", type=int, default=20,
help="The number of random queries to send")
parser.add_argument(
"--random-seed", type=int, default=1,
help=(
"The random seed. Use this to ensure that the same set of queries is "
"run across different client/server combinations."))
parser.add_argument(
"--max-random-query-length", type=int, default=10**7,
help="The maximum length of a random query in bases")
parser.add_argument(
"--client", choices=list(client_map.keys()), default="htsget-api",
help="The client to use for running the transfer")
parser.add_argument(
"--bearer-token", type=str, help="Bearer token")
parser.add_argument(
"--ca-bundle", type=str, help="CA bundle")
args = parser.parse_args()
log_level = logging.WARNING
if args.verbose == 1:
log_level = logging.INFO
if args.verbose >= 2:
log_level = logging.DEBUG
logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)
random.seed(args.random_seed)
tester = ServerTester(
args.source_file, args.url, filter_unmapped=args.filter_unmapped,
tmpdir=args.tmpdir, max_references=args.max_references,
client=client_map[args.client])
exit_status = 1
try:
tester.initialise()
tester.run_full_contig_fetch()
tester.run_start_reads()
tester.run_end_reads()
tester.run_random_reads(args.num_random_reads)
exit_status = 0
except DownloadFailedException as dfe:
print("Download failed: ", dfe, file=sys.stderr)
exit_status = 1
except TestFailedException as tfe:
print("Test failed:", tfe, ". Is the file correct?", file=sys.stderr)
exit_status = 1
except KeyboardInterrupt:
logging.warn("Interrupted!")
exit_status = 0
finally:
tester.cleanup()
# If everything went OK, write out a report.
if exit_status == 0:
tester.print_report()
sys.exit(exit_status)