47
47
import picard .cmdline .programgroups .DiagnosticsAndQCProgramGroup ;
48
48
49
49
import java .io .File ;
50
+ import java .io .IOException ;
51
+ import java .nio .file .Path ;
50
52
import java .util .Collections ;
51
53
import java .util .List ;
52
54
@@ -169,7 +171,7 @@ public class CheckFingerprint extends CommandLineProgram {
169
171
@ Argument (shortName = StandardOptionDefinitions .INPUT_SHORT_NAME , doc = "Input file SAM/BAM or VCF. If a VCF is used, " +
170
172
"it must have at least one sample. If there are more than one samples in the VCF, the parameter OBSERVED_SAMPLE_ALIAS must " +
171
173
"be provided in order to indicate which sample's data to use. If there are no samples in the VCF, an exception will be thrown." )
172
- public File INPUT ;
174
+ public String INPUT ;
173
175
174
176
@ Argument (optional = true , doc = "If the input is a VCF, this parameters used to select which sample's data in the VCF to use." )
175
177
public String OBSERVED_SAMPLE_ALIAS ;
@@ -187,7 +189,7 @@ public class CheckFingerprint extends CommandLineProgram {
187
189
188
190
@ Argument (shortName = "G" , doc = "File of genotypes (VCF) to be used in comparison. May contain " +
189
191
"any number of genotypes; CheckFingerprint will use only those that are usable for fingerprinting." )
190
- public File GENOTYPES ;
192
+ public String GENOTYPES ;
191
193
192
194
@ Argument (shortName = "SAMPLE_ALIAS" , optional = true , doc = "This parameter can be used to specify which sample's genotypes to use from the " +
193
195
"expected VCF file (the GENOTYPES file). If it is not supplied, the sample name from the input " +
@@ -212,10 +214,8 @@ public class CheckFingerprint extends CommandLineProgram {
212
214
public static final String FINGERPRINT_SUMMARY_FILE_SUFFIX = "fingerprinting_summary_metrics" ;
213
215
public static final String FINGERPRINT_DETAIL_FILE_SUFFIX = "fingerprinting_detail_metrics" ;
214
216
215
- // Stock main method
216
- public static void main (final String [] args ) {
217
- new CheckFingerprint ().instanceMainWithExit (args );
218
- }
217
+ private Path inputPath ;
218
+ private Path genotypesPath ;
219
219
220
220
@ Override
221
221
protected int doWork () {
@@ -229,28 +229,34 @@ protected int doWork() {
229
229
outputSummaryMetricsFile = new File (OUTPUT + FINGERPRINT_SUMMARY_FILE_SUFFIX );
230
230
}
231
231
232
- IOUtil .assertFileIsReadable (INPUT );
232
+ try {
233
+ inputPath = IOUtil .getPath (INPUT );
234
+ genotypesPath = IOUtil .getPath (GENOTYPES );
235
+ } catch (IOException e ) {
236
+ throw new IllegalArgumentException (e );
237
+ }
238
+ IOUtil .assertFileIsReadable (inputPath );
233
239
IOUtil .assertFileIsReadable (HAPLOTYPE_MAP );
234
- IOUtil .assertFileIsReadable (GENOTYPES );
240
+ IOUtil .assertFileIsReadable (genotypesPath );
235
241
IOUtil .assertFileIsWritable (outputDetailMetricsFile );
236
242
IOUtil .assertFileIsWritable (outputSummaryMetricsFile );
237
243
238
244
final FingerprintChecker checker = new FingerprintChecker (HAPLOTYPE_MAP );
239
245
List <FingerprintResults > results ;
240
246
241
247
String observedSampleAlias = null ;
242
- final boolean isBamOrSamFile = isBamOrSamFile ( INPUT );
248
+ final boolean isBamOrSamFile = isBamOrSam ( inputPath );
243
249
if (isBamOrSamFile ) {
244
- SequenceUtil .assertSequenceDictionariesEqual (SAMSequenceDictionaryExtractor .extractDictionary (INPUT . toPath ()) , SAMSequenceDictionaryExtractor .extractDictionary (GENOTYPES . toPath () ), true );
245
- SequenceUtil .assertSequenceDictionariesEqual (SAMSequenceDictionaryExtractor .extractDictionary (INPUT . toPath () ), checker .getHeader ().getSequenceDictionary (), true );
250
+ SequenceUtil .assertSequenceDictionariesEqual (SAMSequenceDictionaryExtractor .extractDictionary (inputPath ) , SAMSequenceDictionaryExtractor .extractDictionary (genotypesPath ), true );
251
+ SequenceUtil .assertSequenceDictionariesEqual (SAMSequenceDictionaryExtractor .extractDictionary (inputPath ), checker .getHeader ().getSequenceDictionary (), true );
246
252
247
253
// Verify that there's only one sample in the SAM/BAM.
248
- final SamReader in = SamReaderFactory .makeDefault ().referenceSequence (REFERENCE_SEQUENCE ).open (INPUT . toPath () );
254
+ final SamReader in = SamReaderFactory .makeDefault ().referenceSequence (REFERENCE_SEQUENCE ).open (inputPath );
249
255
for (final SAMReadGroupRecord rec : in .getFileHeader ().getReadGroups ()) {
250
256
if (observedSampleAlias == null ) {
251
257
observedSampleAlias = rec .getSample ();
252
258
} else if (!observedSampleAlias .equals (rec .getSample ())) {
253
- throw new PicardException ("INPUT SAM/BAM file must not contain data from multiple samples." );
259
+ throw new PicardException ("inputPath SAM/BAM file must not contain data from multiple samples." );
254
260
}
255
261
}
256
262
CloserUtil .close (in );
@@ -261,28 +267,28 @@ protected int doWork() {
261
267
}
262
268
263
269
results = checker .checkFingerprints (
264
- Collections .singletonList (INPUT ),
265
- Collections .singletonList (GENOTYPES ),
270
+ Collections .singletonList (inputPath ),
271
+ Collections .singletonList (genotypesPath ),
266
272
EXPECTED_SAMPLE_ALIAS ,
267
273
IGNORE_READ_GROUPS );
268
274
} else { // Input is a VCF
269
275
// Note that FingerprintChecker.loadFingerprints() verifies that the VCF's Sequence Dictionaries agree with that of the Haplotye Map File
270
276
271
277
// Verify that there is only one sample in the VCF
272
- final VCFFileReader fileReader = new VCFFileReader (INPUT , false );
278
+ final VCFFileReader fileReader = new VCFFileReader (inputPath , false );
273
279
final VCFHeader fileHeader = fileReader .getFileHeader ();
274
280
if (fileHeader .getNGenotypeSamples () < 1 ) {
275
- throw new PicardException ("INPUT VCF file must contain at least one sample." );
281
+ throw new PicardException ("inputPath VCF file must contain at least one sample." );
276
282
}
277
283
if ((fileHeader .getNGenotypeSamples () > 1 ) && (OBSERVED_SAMPLE_ALIAS == null )) {
278
- throw new PicardException ("INPUT VCF file contains multiple samples and yet the OBSERVED_SAMPLE_ALIAS parameter is not set." );
284
+ throw new PicardException ("inputPath VCF file contains multiple samples and yet the OBSERVED_SAMPLE_ALIAS parameter is not set." );
279
285
}
280
286
// set observedSampleAlias to the parameter, if set. Otherwise, if here, this must be a single sample VCF, get it's sample
281
287
observedSampleAlias = (OBSERVED_SAMPLE_ALIAS != null ) ? OBSERVED_SAMPLE_ALIAS : fileHeader .getGenotypeSamples ().get (0 );
282
288
283
289
// Now verify that observedSampleAlias is, in fact, in the VCF
284
290
if (!fileHeader .getGenotypeSamples ().contains (observedSampleAlias )) {
285
- throw new PicardException ("INPUT VCF file does not contain OBSERVED_SAMPLE_ALIAS: " + observedSampleAlias );
291
+ throw new PicardException ("inputPath VCF file does not contain OBSERVED_SAMPLE_ALIAS: " + observedSampleAlias );
286
292
}
287
293
288
294
if (OBSERVED_SAMPLE_ALIAS == null ) {
@@ -295,9 +301,9 @@ protected int doWork() {
295
301
EXPECTED_SAMPLE_ALIAS = observedSampleAlias ;
296
302
}
297
303
298
- results = checker .checkFingerprints (
299
- Collections .singletonList (INPUT ),
300
- Collections .singletonList (GENOTYPES ),
304
+ results = checker .checkFingerprintsFromPaths (
305
+ Collections .singletonList (inputPath ),
306
+ Collections .singletonList (genotypesPath ),
301
307
observedSampleAlias ,
302
308
EXPECTED_SAMPLE_ALIAS );
303
309
}
@@ -368,19 +374,26 @@ protected int doWork() {
368
374
}
369
375
370
376
protected String [] customCommandLineValidation () {
371
- IOUtil .assertFileIsReadable (INPUT );
372
377
373
- boolean isBamOrSamFile = isBamOrSamFile (INPUT );
374
- if (!isBamOrSamFile && IGNORE_READ_GROUPS ) {
375
- return new String []{"The parameter IGNORE_READ_GROUPS can only be used with BAM/SAM inputs." };
376
- }
377
- if (isBamOrSamFile && OBSERVED_SAMPLE_ALIAS != null ) {
378
- return new String []{"The parameter OBSERVED_SAMPLE_ALIAS can only be used with a VCF input." };
378
+ try {
379
+ final boolean isBamOrSamFile = isBamOrSam (IOUtil .getPath (INPUT ));
380
+ if (!isBamOrSamFile && IGNORE_READ_GROUPS ) {
381
+ return new String []{"The parameter IGNORE_READ_GROUPS can only be used with BAM/SAM inputs." };
382
+ }
383
+ if (isBamOrSamFile && OBSERVED_SAMPLE_ALIAS != null ) {
384
+ return new String []{"The parameter OBSERVED_SAMPLE_ALIAS can only be used with a VCF input." };
385
+ }
386
+ } catch (IOException e ) {
387
+ e .printStackTrace ();
379
388
}
380
389
return super .customCommandLineValidation ();
381
390
}
382
391
383
- static boolean isBamOrSamFile (final File f ) {
384
- return (BamFileIoUtils .isBamFile (f ) || f .getName ().endsWith (IOUtil .SAM_FILE_EXTENSION ));
392
+ static boolean isBamOrSam (final File f ) {
393
+ return isBamOrSam (f .toPath ());
394
+ }
395
+
396
+ static boolean isBamOrSam (final Path p ) {
397
+ return (p .toUri ().getRawPath ().endsWith (BamFileIoUtils .BAM_FILE_EXTENSION ) || p .toUri ().getRawPath ().endsWith (IOUtil .SAM_FILE_EXTENSION ));
385
398
}
386
399
}
0 commit comments