<commit_before>/*--------------------------------------------------------------------------
 *  Copyright 2010 utgenome.org
 *
 *  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.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// utgb-core Project
//
// FastqToBAM.java
// Since: Jul 5, 2010
//
//--------------------------------------
package org.utgenome.format.fastq;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.zip.GZIPInputStream;

import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMFileWriter;
import net.sf.samtools.SAMFileWriterFactory;
import net.sf.samtools.SAMReadGroupRecord;
import net.sf.samtools.SAMRecord;

import org.utgenome.UTGBErrorCode;
import org.utgenome.UTGBException;
import org.xerial.util.log.Logger;
import org.xerial.util.opt.Argument;
import org.xerial.util.opt.Option;
import org.xerial.util.opt.OptionParser;
import org.xerial.util.opt.OptionParserException;

/**
 * Converting Illumina's FASTQ read data (single or paired-end) into BAM format, which can be used BLOAD's GATK
 * pipeline.
 * 
 * This code is migrated from
 * 
 * @author leo
 * 
 */
public class FastqToBAM {

	private static Logger _logger = Logger.getLogger(FastqToBAM.class);

	@Option(longName = "readGroup", description = "read group name")
	private String readGroupName;

	@Option(longName = "sample", description = "sample name")
	private String sampleName;

	@Option(longName = "prefix", description = "prefix of the read")
	private String readPrefix;

	@Argument(index = 0, name = "input fastq (.fastq, .fastq.gz)", required = true)
	private File input1;
	@Argument(index = 1, name = "input fastq (.fastq, .fastq.gz, when paried-end read)")
	private File input2;

	@Option(symbol = "o", longName = "output", description = "output file name (.sam or .bam)")
	private File outputFile;

	@Option(longName = "tmpdir", description = "temporary directory.for storing merge sort runs. default = System.getProperty(\"java.io.tmpdir\"));")
	private File tmpDir = new File(System.getProperty("java.io.tmpdir"));

	public static int execute(String[] args) throws Exception {

		FastqToBAM main = new FastqToBAM();
		OptionParser parser = new OptionParser(main);
		try {
			parser.parse(args);

			main.convert();
		}
		catch (OptionParserException e) {
			_logger.error(e);
			return 1;
		}

		return 0;

	}

	public int convert() throws UTGBException, IOException {
		if (input1 == null) {
			throw new UTGBException(UTGBErrorCode.MISSING_OPTION, "missing fastq file");
		}

		Reader in1;
		if (input1.getName().endsWith(".gz")) {
			in1 = new InputStreamReader(new GZIPInputStream(new FileInputStream(input1)));
		}
		else
			in1 = new FileReader(input1);

		Reader in2 = null;
		if (input2 != null) {
			if (input2.getName().endsWith(".gz"))
				in2 = new InputStreamReader(new GZIPInputStream(new FileInputStream(input2)));
			else
				in2 = new FileReader(input2);
		}

		return convert(new BufferedReader(in1), in2 == null ? null : new BufferedReader(in2));
	}

	public int convert(Reader input1, Reader input2) throws UTGBException, IOException {

		if (!tmpDir.exists()) {
			_logger.info("create dir: " + tmpDir.getPath());
			tmpDir.mkdirs();
			// set the temporary directory for storing merge sort runs. 
			// Picard library will look up this java.io.tmpdir system property value. 
			System.setProperty("java.io.tmpdir", tmpDir.getAbsolutePath());
		}

		FastqReader end1 = new FastqReader(input1);
		FastqReader end2 = (input2 == null) ? null : new FastqReader(input2);

		SAMReadGroupRecord readGroupRecord = new SAMReadGroupRecord(readGroupName);
		SAMFileHeader samHeader = new SAMFileHeader();
		if (readGroupName != null) {
			samHeader.addReadGroup(readGroupRecord);
		}
		readGroupRecord.setSample(sampleName);<commit_msg>fixed a bug which failed to set java.io.tmp dir<commit_after>/*--------------------------------------------------------------------------
 *  Copyright 2010 utgenome.org
 *
 *  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.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// utgb-core Project
//
// FastqToBAM.java
// Since: Jul 5, 2010
//
//--------------------------------------
package org.utgenome.format.fastq;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.zip.GZIPInputStream;

import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMFileWriter;
import net.sf.samtools.SAMFileWriterFactory;
import net.sf.samtools.SAMReadGroupRecord;
import net.sf.samtools.SAMRecord;

import org.utgenome.UTGBErrorCode;
import org.utgenome.UTGBException;
import org.xerial.util.log.Logger;
import org.xerial.util.opt.Argument;
import org.xerial.util.opt.Option;
import org.xerial.util.opt.OptionParser;
import org.xerial.util.opt.OptionParserException;

/**
 * Converting Illumina's FASTQ read data (single or paired-end) into BAM format, which can be used BLOAD's GATK
 * pipeline.
 * 
 * This code is migrated from
 * 
 * @author leo
 * 
 */
public class FastqToBAM {

	private static Logger _logger = Logger.getLogger(FastqToBAM.class);

	@Option(longName = "readGroup", description = "read group name")
	private String readGroupName;

	@Option(longName = "sample", description = "sample name")
	private String sampleName;

	@Option(longName = "prefix", description = "prefix of the read")
	private String readPrefix;

	@Argument(index = 0, name = "input fastq (.fastq, .fastq.gz)", required = true)
	private File input1;
	@Argument(index = 1, name = "input fastq (.fastq, .fastq.gz, when paried-end read)")
	private File input2;

	@Option(symbol = "o", longName = "output", description = "output file name (.sam or .bam)")
	private File outputFile;

	@Option(longName = "tmpdir", description = "temporary directory.for storing merge sort runs. default = System.getProperty(\"java.io.tmpdir\"));")
	private File tmpDir = new File(System.getProperty("java.io.tmpdir"));

	public static int execute(String[] args) throws Exception {

		FastqToBAM main = new FastqToBAM();
		OptionParser parser = new OptionParser(main);
		try {
			parser.parse(args);

			main.convert();
		}
		catch (OptionParserException e) {
			_logger.error(e);
			return 1;
		}

		return 0;

	}

	public int convert() throws UTGBException, IOException {
		if (input1 == null) {
			throw new UTGBException(UTGBErrorCode.MISSING_OPTION, "missing fastq file");
		}

		Reader in1;
		if (input1.getName().endsWith(".gz")) {
			in1 = new InputStreamReader(new GZIPInputStream(new FileInputStream(input1)));
		}
		else
			in1 = new FileReader(input1);

		Reader in2 = null;
		if (input2 != null) {
			if (input2.getName().endsWith(".gz"))
				in2 = new InputStreamReader(new GZIPInputStream(new FileInputStream(input2)));
			else
				in2 = new FileReader(input2);
		}

		return convert(new BufferedReader(in1), in2 == null ? null : new BufferedReader(in2));
	}

	public int convert(Reader input1, Reader input2) throws UTGBException, IOException {

		if (!tmpDir.exists()) {
			_logger.info("create dir: " + tmpDir.getPath());
			tmpDir.mkdirs();
		}
		// set the temporary directory for storing merge sort runs. 
		// Picard library will look up this java.io.tmpdir system property value. 
		System.setProperty("java.io.tmpdir", tmpDir.getAbsolutePath());

		FastqReader end1 = new FastqReader(input1);
		FastqReader end2 = (input2 == null) ? null : new FastqReader(input2);

		SAMReadGroupRecord readGroupRecord = new SAMReadGroupRecord(readGroupName);
		SAMFileHeader samHeader = new SAMFileHeader();
		if (readGroupName != null) {
			samHeader.addReadGroup(readGroupRecord);
		}
		readGroupRecord.setSample(sampleName);
{'fraction_non_alphanumeric': 0.15964621337755666, 'fraction_numerical': 0.009508015478164732, 'mean_word_length': 3.3616200578592093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059473', 'n_tokens_mistral': 2916, 'n_tokens_neox': 2672, 'n_words': 777}