public static void main(String[] args) throws IOException {
		Params params = new Params();
		JCommander jc = new JCommander(params);
		jc.parse(args);

		Log.setGlobalLogLevel(params.logLevel);

		if (args.length == 0 || params.help) {
			usage(jc);
			System.exit(1);
		}

		if (params.reference == null) {
			System.out.println("Reference file not found, will try downloading...");
		}

		ReferenceSource referenceSource = null;
		if (params.reference != null) {
			System.setProperty("reference", params.reference.getAbsolutePath());
			referenceSource = new ReferenceSource(params.reference);
		} else {
			String prop = System.getProperty("reference");
			if (prop != null) {
				referenceSource = new ReferenceSource(new File(prop));
			}
		}

		SamReaderFactory factory = SamReaderFactory.make().validationStringency(params.validationLevel);
		SamInputResource r;
		if ("file".equalsIgnoreCase(params.url.getProtocol()))
			r = SamInputResource.of(params.url.getPath());
		else
			r = SamInputResource.of(params.url);
		SamReader reader = factory.open(r);
		SAMRecordIterator iterator = reader.iterator();

		SAMFileWriterFactory writerFactory = new SAMFileWriterFactory();
		SAMFileWriter writer = null;
		OutputStream os = new BufferedOutputStream(new FileOutputStream(params.outputFile));
		switch (params.outputFormat) {
		case BAM:
			writer = writerFactory.makeBAMWriter(reader.getFileHeader(),
					reader.getFileHeader().getSortOrder() == SortOrder.coordinate, os);
			break;
		case CRAM:
			writer = writerFactory.makeCRAMWriter(reader.getFileHeader(), os, params.reference);
			break;

		default:
			System.out.println("Unknown output format: " + params.outputFormat);
			System.exit(1);
		}

		while (iterator.hasNext()) {
			writer.addAlignment(iterator.next());
		}
		writer.close();
		reader.close();
	}