This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPaginator.java
1134 lines (998 loc) · 40 KB
/
Paginator.java
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
/*
* Copyright (C) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.cloud.genomics.utils;
import com.google.api.services.genomics.Genomics;
import com.google.api.services.genomics.Genomics.Annotationsets;
import com.google.api.services.genomics.GenomicsRequest;
import com.google.api.services.genomics.model.Annotation;
import com.google.api.services.genomics.model.AnnotationSet;
import com.google.api.services.genomics.model.CallSet;
import com.google.api.services.genomics.model.CoverageBucket;
import com.google.api.services.genomics.model.Dataset;
import com.google.api.services.genomics.model.ListBasesResponse;
import com.google.api.services.genomics.model.ListCoverageBucketsResponse;
import com.google.api.services.genomics.model.ListDatasetsResponse;
import com.google.api.services.genomics.model.ListOperationsResponse;
import com.google.api.services.genomics.model.Operation;
import com.google.api.services.genomics.model.Read;
import com.google.api.services.genomics.model.ReadGroupSet;
import com.google.api.services.genomics.model.Reference;
import com.google.api.services.genomics.model.ReferenceSet;
import com.google.api.services.genomics.model.SearchAnnotationSetsRequest;
import com.google.api.services.genomics.model.SearchAnnotationSetsResponse;
import com.google.api.services.genomics.model.SearchAnnotationsRequest;
import com.google.api.services.genomics.model.SearchAnnotationsResponse;
import com.google.api.services.genomics.model.SearchCallSetsRequest;
import com.google.api.services.genomics.model.SearchCallSetsResponse;
import com.google.api.services.genomics.model.SearchReadGroupSetsRequest;
import com.google.api.services.genomics.model.SearchReadGroupSetsResponse;
import com.google.api.services.genomics.model.SearchReadsRequest;
import com.google.api.services.genomics.model.SearchReadsResponse;
import com.google.api.services.genomics.model.SearchReferenceSetsRequest;
import com.google.api.services.genomics.model.SearchReferenceSetsResponse;
import com.google.api.services.genomics.model.SearchReferencesRequest;
import com.google.api.services.genomics.model.SearchReferencesResponse;
import com.google.api.services.genomics.model.SearchVariantSetsRequest;
import com.google.api.services.genomics.model.SearchVariantSetsResponse;
import com.google.api.services.genomics.model.SearchVariantsRequest;
import com.google.api.services.genomics.model.SearchVariantsResponse;
import com.google.api.services.genomics.model.Variant;
import com.google.api.services.genomics.model.VariantSet;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.AbstractSequentialIterator;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
/**
* An abstraction that understands the {@code pageToken} / {@code nextPageToken} protocol for paging
* results back to the user.
*
* <p>The {@link #search(Object, GenomicsRequestInitializer, RetryPolicy)} method can obtain an
* {@link Iterable} to the objects returned from a search request. Although it is possible to
* invoke this method directly, the {@code Iterable} that is returned may throw
* {@link SearchException} during iteration. Client code is responsible for catching this exception
* where the {@code Iterable} is consumed, unwrapping the underlying {@link IOException}, and
* handling or rethrowing it.
* </p>
*
* <p>A safer alternative for consuming the results of a search is
* {@link #search(Object, GenomicsRequestInitializer, Callback, RetryPolicy)}.
* This method requires the client code to pass in a {@link Callback} object that consumes the
* search results and will unwrap and rethrow {@link IOException}s that occur during iteration
* for you.
* </p>
*
* <p>Example usage: Fetching all {@link ReadGroupSet}s in a {@link Dataset}:</p>
* <pre>
*{@code
*Genomics stub = ...;
*String datasetId = ...;
*Paginator.Readsets searchReadsets = Paginator.Readsets.create(stub);
*for (Readset readset =
* searchReadsets.search(new SearchReadsetsRequest().setDatasetId(datasetId))) {
* // do something with readset
*}
*}
*</pre>
*
* @param <ApiT> The API type.
* @param <RequestT> The request type.
* @param <RequestSubT> The {@link GenomicsRequest} subtype.
* @param <ResponseT> The response type.
* @param <ItemT> The type of object being streamed back to the user.
*/
public abstract class Paginator<ApiT, RequestT, RequestSubT extends GenomicsRequest<ResponseT>, ResponseT, ItemT> {
/**
* A callback object for
* {@link #search(Object, GenomicsRequestInitializer, Callback, RetryPolicy)} that can
* consume all or part of the results from a search request and accumulate a value, which becomes
* the value returned from the
* {@link #search(Object, GenomicsRequestInitializer, Callback, RetryPolicy)} method.
*
* @param <EntityT> The type of objects returned from a search
* @param <AccumulatedT> The type of object to accumulate when consuming search results.
*/
public interface Callback<EntityT, AccumulatedT> {
/**
* Consume the search results and accumulate an object to return.
*
* @param responses The search results to consume.
* @return the accumulated summary value.
* @throws IOException
*/
AccumulatedT consumeResponses(Iterable<EntityT> responses) throws IOException;
}
/**
* A {@link Paginator} for the {@code searchCallsets()} API.
*/
public static class Callsets extends Paginator<
Genomics.Callsets,
SearchCallSetsRequest,
Genomics.Callsets.Search,
SearchCallSetsResponse,
CallSet> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Callsets create(Genomics genomics) {
return new Callsets(genomics);
}
private Callsets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Callsets.Search createSearch(Genomics.Callsets api,
final SearchCallSetsRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchCallSetsRequest>() {
@Override public SearchCallSetsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Genomics.Callsets getApi(Genomics genomics) {
return genomics.callsets();
}
@Override String getNextPageToken(SearchCallSetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<CallSet> getResponses(SearchCallSetsResponse response) {
return response.getCallSets();
}
}
/**
* A {@link Paginator} for the {@code searchDatasets()} API.
*/
public static class Datasets extends Paginator<
Genomics.Datasets,
String,
Genomics.Datasets.List,
ListDatasetsResponse,
Dataset> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Datasets create(Genomics genomics) {
return new Datasets(genomics);
}
private Datasets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Datasets.List createSearch(Genomics.Datasets api, String projectId,
Optional<String> pageToken) throws IOException {
final Genomics.Datasets.List list = api.list().setProjectId(projectId);
return pageToken
.transform(
new Function<String, Genomics.Datasets.List>() {
@Override public Genomics.Datasets.List apply(String token) {
return list.setPageToken(token);
}
})
.or(list);
}
@Override Genomics.Datasets getApi(Genomics genomics) {
return genomics.datasets();
}
@Override String getNextPageToken(ListDatasetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Dataset> getResponses(ListDatasetsResponse response) {
return response.getDatasets();
}
}
public interface Factory<P extends Paginator<?, ?, C, D, ?>, C extends GenomicsRequest<D>, D>
extends Serializable {
P createPaginator(Genomics genomics);
}
/**
* A hook for customizing {@link GenomicsRequest} request objects before they are sent to the
* server. Typically, users will use this to call {@link GenomicsRequest#setFields} to specify
* which fields of the response object should be set.
*
* @param <C> The type of {@code GenomicsRequest} to initialize.
*/
public interface GenomicsRequestInitializer<C extends GenomicsRequest<?>> {
/**
* Initialize the given {@link GenomicsRequest} object.
*
* @param search The object to initialize.
*/
void initialize(C search);
}
/**
* A {@link Paginator} for the {@code Genomics.Operations.List()} API.
*/
public static class Operations extends Paginator<
Genomics.Operations,
String,
Genomics.Operations.List,
ListOperationsResponse,
Operation> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Operations create(Genomics genomics) {
return new Operations(genomics);
}
private Operations(Genomics genomics) {
super(genomics);
}
@Override
Genomics.Operations.List createSearch(Genomics.Operations api, final String filter,
Optional<String> pageToken) throws IOException {
Genomics.Operations.List genomicsRequest = api.list("unused argument");
if (!Strings.isNullOrEmpty(filter)) {
genomicsRequest.setFilter(filter);
}
if (pageToken.isPresent()) {
genomicsRequest.setPageToken(pageToken.get());
}
return genomicsRequest;
}
@Override Genomics.Operations getApi(Genomics genomics) {
return genomics.operations();
}
@Override String getNextPageToken(ListOperationsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Operation> getResponses(ListOperationsResponse response) {
return response.getOperations();
}
}
private class Pair {
final RequestSubT request;
final ResponseT response;
Pair(RequestSubT request, ResponseT response) {
this.request = request;
this.response = response;
}
}
/**
* A {@link Paginator} for the {@code searchReads()} API.
*/
public static class Reads extends Paginator<
Genomics.Reads,
SearchReadsRequest,
Genomics.Reads.Search,
SearchReadsResponse,
Read> {
// TODO: When this is supported server-side, these additional fields are no longer required
private static ImmutableMap<String,String> REQUIRED_STRICT_SHARD_FIELDS =
ImmutableMap.<String, String>builder()
.putAll(REQUIRED_FIELDS)
.put("alignment", ".*\\p{Punct}alignment\\p{Punct}.*")
.put("position", ".*\\p{Punct}position\\p{Punct}.*")
.build();
private final ShardBoundary.Requirement shardBoundary;
private Predicate<Read> shardPredicate = null;
/**
* Static factory method.
*
* The reads search API by default returns all reads that overlap the specified range.
* Ranges are half-open 0-based, so [start,end)
*
* Cluster compute jobs attempting to shard the data will see any records that span a shard
* boundary in both shards. In some cases this might be okay, in others it is not.
*
* @param genomics The {@link Genomics} stub.
* @param shardBoundary Use ShardBoundary.OVERLAPS for the default behavior or use
* ShardBoundary.STRICT to ensure that a record does not appear in more than one shard.
* @return the new paginator.
*/
public static Reads create(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
return new Reads(genomics, shardBoundary);
}
private Reads(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
super(genomics);
this.shardBoundary = shardBoundary;
}
@Override Genomics.Reads.Search createSearch(Genomics.Reads api, final SearchReadsRequest request,
Optional<String> pageToken) throws IOException {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
// TODO: When this is supported server-side, instead verify that request.getIntersectionType
// will yield a strict shard.
shardPredicate = new Predicate<Read>() {
@Override
public boolean apply(Read read) {
return read.getAlignment().getPosition().getPosition() >= request.getStart();
}
};
}
return api.search(pageToken
.transform(
new Function<String, SearchReadsRequest>() {
@Override public SearchReadsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override
public GenomicsRequestInitializer<GenomicsRequest<?>> setFieldsInitializer(final String fields) {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_STRICT_SHARD_FIELDS);
}
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_FIELDS);
}
@Override Genomics.Reads getApi(Genomics genomics) {
return genomics.reads();
}
@Override String getNextPageToken(SearchReadsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Read> getResponses(SearchReadsResponse response) {
Iterable<Read> responses = response.getAlignments();
if(shardPredicate != null && responses != null) {
return Iterables.filter(responses, shardPredicate);
}
return responses;
}
}
/**
* A {@link Paginator} for the {@code searchAnnotations()} API.
*/
public static class Annotations extends Paginator<
Genomics.Annotations,
SearchAnnotationsRequest,
Genomics.Annotations.Search,
SearchAnnotationsResponse,
Annotation> {
// TODO: When this is supported server-side, these additional fields are no longer required
private static ImmutableMap<String,String> REQUIRED_STRICT_SHARD_FIELDS =
ImmutableMap.<String, String>builder()
.putAll(REQUIRED_FIELDS)
.put("position", ".*\\p{Punct}position\\p{Punct}.*")
.put("start", ".*\\p{Punct}start\\p{Punct}.*")
.build();
private final ShardBoundary.Requirement shardBoundary;
private Predicate<Annotation> shardPredicate = null;
/**
* Static factory method.
*
* The annotations search API by default returns all annotations that overlap the specified
* range. Ranges are half-open 0-based, so [start,end)
*
* Cluster compute jobs attempting to shard the data will see any records that span a shard
* boundary in both shards. In some cases this might be okay, in others it is not.
*
* @param genomics The {@link Genomics} stub.
* @param shardBoundary Use ShardBoundary.OVERLAPS for the default behavior or use
* ShardBoundary.STRICT to ensure that a record does not appear in more than one shard.
* @return the new paginator.
*/
public static Annotations create(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
return new Annotations(genomics, shardBoundary);
}
private Annotations(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
super(genomics);
this.shardBoundary = shardBoundary;
}
@Override Genomics.Annotations.Search createSearch(Genomics.Annotations api,
final SearchAnnotationsRequest request, Optional<String> pageToken) throws IOException {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
// TODO: When this is supported server-side, instead verify that request.getIntersectionType
// will yield a strict shard.
shardPredicate = new Predicate<Annotation>() {
@Override
public boolean apply(Annotation anno) {
return anno.getStart() >= request.getStart();
}
};
}
return api.search(pageToken
.transform(
new Function<String, SearchAnnotationsRequest>() {
@Override public SearchAnnotationsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override
public GenomicsRequestInitializer<GenomicsRequest<?>> setFieldsInitializer(final String fields) {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_STRICT_SHARD_FIELDS);
}
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_FIELDS);
}
@Override Genomics.Annotations getApi(Genomics genomics) {
return genomics.annotations();
}
@Override String getNextPageToken(SearchAnnotationsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Annotation> getResponses(SearchAnnotationsResponse response) {
Iterable<Annotation> responses = response.getAnnotations();
if(shardPredicate != null && responses != null) {
return Iterables.filter(responses, shardPredicate);
}
return responses;
}
}
/**
* A {@link Paginator} for the {@code searchAnnotationSets()} API.
*/
public static class AnnotationSets extends Paginator<
Annotationsets,
SearchAnnotationSetsRequest,
Annotationsets.Search,
SearchAnnotationSetsResponse,
AnnotationSet> {
/**
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static AnnotationSets create(Genomics genomics) {
return new AnnotationSets(genomics);
}
private AnnotationSets(Genomics genomics) {
super(genomics);
}
@Override Annotationsets.Search createSearch(Annotationsets api,
final SearchAnnotationSetsRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchAnnotationSetsRequest>() {
@Override public SearchAnnotationSetsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Annotationsets getApi(Genomics genomics) {
return genomics.annotationsets();
}
@Override String getNextPageToken(SearchAnnotationSetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<AnnotationSet> getResponses(SearchAnnotationSetsResponse response) {
return response.getAnnotationSets();
}
}
/**
* A {@link Paginator} for the {@code searchReadGroupSets()} API.
*/
public static class ReadGroupSets extends Paginator<Genomics.Readgroupsets,
SearchReadGroupSetsRequest, Genomics.Readgroupsets.Search, SearchReadGroupSetsResponse,
ReadGroupSet> {
/**
* A {@link Paginator} for the {@code coveragebuckets()} API.
*/
public static class Coveragebuckets extends Paginator<
Genomics.Readgroupsets.Coveragebuckets,
String,
Genomics.Readgroupsets.Coveragebuckets.List,
ListCoverageBucketsResponse,
CoverageBucket> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Coveragebuckets create(Genomics genomics) {
return new Coveragebuckets(genomics);
}
private Coveragebuckets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Readgroupsets.Coveragebuckets.List createSearch(
Genomics.Readgroupsets.Coveragebuckets api,
String request, Optional<String> pageToken) throws IOException {
final Genomics.Readgroupsets.Coveragebuckets.List list = api.list(request);
return pageToken
.transform(
new Function<String, Genomics.Readgroupsets.Coveragebuckets.List>() {
@Override public Genomics.Readgroupsets.Coveragebuckets.List apply(
String token) {
return list.setPageToken(token);
}
})
.or(list);
}
@Override Genomics.Readgroupsets.Coveragebuckets getApi(Genomics genomics) {
return genomics.readgroupsets().coveragebuckets();
}
@Override String getNextPageToken(ListCoverageBucketsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<CoverageBucket> getResponses(ListCoverageBucketsResponse response) {
return response.getCoverageBuckets();
}
}
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static ReadGroupSets create(Genomics genomics) {
return new ReadGroupSets(genomics);
}
private ReadGroupSets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Readgroupsets.Search createSearch(Genomics.Readgroupsets api,
final SearchReadGroupSetsRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchReadGroupSetsRequest>() {
@Override public SearchReadGroupSetsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Genomics.Readgroupsets getApi(Genomics genomics) {
return genomics.readgroupsets();
}
@Override String getNextPageToken(SearchReadGroupSetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<ReadGroupSet> getResponses(SearchReadGroupSetsResponse response) {
return response.getReadGroupSets();
}
}
/**
* A {@link Paginator} for the {@code searchReferenceSets()} API.
*/
public static class ReferenceSets extends Paginator<
Genomics.Referencesets,
SearchReferenceSetsRequest,
Genomics.Referencesets.Search,
SearchReferenceSetsResponse,
ReferenceSet> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static ReferenceSets create(Genomics genomics) {
return new ReferenceSets(genomics);
}
private ReferenceSets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Referencesets.Search createSearch(Genomics.Referencesets api,
final SearchReferenceSetsRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchReferenceSetsRequest>() {
@Override public SearchReferenceSetsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Genomics.Referencesets getApi(Genomics genomics) {
return genomics.referencesets();
}
@Override String getNextPageToken(SearchReferenceSetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<ReferenceSet> getResponses(SearchReferenceSetsResponse response) {
return response.getReferenceSets();
}
}
/**
* A {@link Paginator} for the {@code searchReferences()} API.
*/
public static class References extends Paginator<
Genomics.References,
SearchReferencesRequest,
Genomics.References.Search,
SearchReferencesResponse,
Reference> {
/**
* A {@link Paginator} for the {@code bases()} API.
*/
public static class Bases extends Paginator<
Genomics.References.Bases,
String,
Genomics.References.Bases.List,
ListBasesResponse,
String> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Bases create(Genomics genomics) {
return new Bases(genomics);
}
private Bases(Genomics genomics) {
super(genomics);
}
@Override Genomics.References.Bases.List createSearch(
Genomics.References.Bases api,
String request, Optional<String> pageToken) throws IOException {
final Genomics.References.Bases.List list = api.list(request);
return pageToken
.transform(
new Function<String, Genomics.References.Bases.List>() {
@Override public Genomics.References.Bases.List apply(
String token) {
return list.setPageToken(token);
}
})
.or(list);
}
@Override Genomics.References.Bases getApi(Genomics genomics) {
return genomics.references().bases();
}
@Override String getNextPageToken(ListBasesResponse response) {
return response.getNextPageToken();
}
@Override Iterable<String> getResponses(ListBasesResponse response) {
return Lists.newArrayList(response.getSequence());
}
}
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static References create(Genomics genomics) {
return new References(genomics);
}
private References(Genomics genomics) {
super(genomics);
}
@Override Genomics.References.Search createSearch(Genomics.References api,
final SearchReferencesRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchReferencesRequest>() {
@Override public SearchReferencesRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Genomics.References getApi(Genomics genomics) {
return genomics.references();
}
@Override String getNextPageToken(SearchReferencesResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Reference> getResponses(SearchReferencesResponse response) {
return response.getReferences();
}
}
/**
* A {@link RuntimeException} for wrapping {@link IOException}s that occur during lazy consumption
* of search results.
*/
public static class SearchException extends RuntimeException {
private static final long serialVersionUID = -72047056528032423L;
SearchException(IOException cause) {
super(cause);
}
@Override public synchronized IOException getCause() {
return (IOException) super.getCause();
}
}
/**
* A {@link Paginator} for the {@code searchVariants()} API.
*/
public static class Variants extends Paginator<
Genomics.Variants,
SearchVariantsRequest,
Genomics.Variants.Search,
SearchVariantsResponse,
Variant> {
// TODO: When this is supported server-side, these additional fields are no longer required
private static ImmutableMap<String,String> REQUIRED_STRICT_SHARD_FIELDS =
ImmutableMap.<String, String>builder()
.putAll(REQUIRED_FIELDS)
.put("start", ".*\\p{Punct}start\\p{Punct}.*")
.build();
private final ShardBoundary.Requirement shardBoundary;
private Predicate<Variant> shardPredicate = null;
/**
* Static factory method.
*
* The variants search API by default returns all variants that overlap the specified range.
* Ranges are half-open 0-based, so [start,end)
*
* Cluster compute jobs attempting to shard the data will see any records that span a shard
* boundary in both shards. In some cases this might be okay, in others it is not.
*
* @param genomics The {@link Genomics} stub.
* @param shardBoundary Use ShardBoundary.OVERLAPS for the default behavior or use
* ShardBoundary.STRICT to ensure that a record does not appear in more than one shard.
* @return the new paginator
*/
public static Variants create(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
return new Variants(genomics, shardBoundary);
}
private Variants(Genomics genomics, ShardBoundary.Requirement shardBoundary) {
super(genomics);
this.shardBoundary = shardBoundary;
}
@Override Genomics.Variants.Search createSearch(Genomics.Variants api,
final SearchVariantsRequest request, Optional<String> pageToken) throws IOException {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
// TODO: When this is supported server-side, instead verify that request.getIntersectionType
// will yield a strict shard.
shardPredicate = new Predicate<Variant>() {
@Override
public boolean apply(Variant variant) {
return variant.getStart() >= request.getStart();
}
};
}
return api.search(pageToken
.transform(
new Function<String, SearchVariantsRequest>() {
@Override public SearchVariantsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override
public GenomicsRequestInitializer<GenomicsRequest<?>> setFieldsInitializer(final String fields) {
if(shardBoundary == ShardBoundary.Requirement.STRICT) {
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_STRICT_SHARD_FIELDS);
}
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_FIELDS);
}
@Override Genomics.Variants getApi(Genomics genomics) {
return genomics.variants();
}
@Override String getNextPageToken(SearchVariantsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<Variant> getResponses(SearchVariantsResponse response) {
Iterable<Variant> responses = response.getVariants();
if(shardPredicate != null && responses != null) {
return Iterables.filter(responses, shardPredicate);
}
return responses;
}
}
/**
* A {@link Paginator} for the {@code searchVariantsets()} API.
*/
public static class Variantsets extends Paginator<
Genomics.Variantsets,
SearchVariantSetsRequest,
Genomics.Variantsets.Search,
SearchVariantSetsResponse,
VariantSet> {
/**
* Static factory method.
*
* @param genomics The {@link Genomics} stub.
* @return the new paginator.
*/
public static Variantsets create(Genomics genomics) {
return new Variantsets(genomics);
}
private Variantsets(Genomics genomics) {
super(genomics);
}
@Override Genomics.Variantsets.Search createSearch(Genomics.Variantsets api,
final SearchVariantSetsRequest request, Optional<String> pageToken) throws IOException {
return api.search(pageToken
.transform(
new Function<String, SearchVariantSetsRequest>() {
@Override public SearchVariantSetsRequest apply(String token) {
return request.setPageToken(token);
}
})
.or(request));
}
@Override Genomics.Variantsets getApi(Genomics genomics) {
return genomics.variantsets();
}
@Override String getNextPageToken(SearchVariantSetsResponse response) {
return response.getNextPageToken();
}
@Override Iterable<VariantSet> getResponses(SearchVariantSetsResponse response) {
return response.getVariantSets();
}
}
public static final GenomicsRequestInitializer<GenomicsRequest<?>> DEFAULT_INITIALIZER =
new GenomicsRequestInitializer<GenomicsRequest<?>>() {
@Override public void initialize(GenomicsRequest<?> search) {}
};
protected static final ImmutableMap<String, String> REQUIRED_FIELDS = ImmutableMap
.<String, String>builder()
.put("nextPageToken", "(.+\\p{Punct})?nextPageToken(\\p{Punct}.+)?").
build();
public GenomicsRequestInitializer<GenomicsRequest<?>> setFieldsInitializer(final String fields) {
return new GenomicsSearchFieldRequestInitializer(fields, REQUIRED_FIELDS);
}
public static class GenomicsSearchFieldRequestInitializer implements
GenomicsRequestInitializer<GenomicsRequest<?>> {
private final String fields;
private final ImmutableMap<String, String> requiredFields;
public GenomicsSearchFieldRequestInitializer(String fields,
ImmutableMap<String, String> requiredFields) {
this.fields = fields;
this.requiredFields = requiredFields;
}
@Override
public void initialize(GenomicsRequest<?> search) {
if (null != fields) {
Map<String, String> missingFields =
Maps.filterValues(requiredFields, new Predicate<String>() {
@Override
public boolean apply(String input) {
return !fields.matches(input);
}
});
Preconditions
.checkArgument(
missingFields.isEmpty(),
"Required field(s) missing: '%s' Add this field to the list of fields to be returned in the partial response.",
Joiner.on(",").join(missingFields.keySet()));
search.setFields(fields);
}
}
}
private final Genomics genomics;
public Paginator(Genomics genomics) {
this.genomics = genomics;