<commit_before>package edu.yu.einstein.wasp.gatk.software;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.batch.core.explore.wasp.ParameterValueRetrievalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import edu.yu.einstein.wasp.gatk.service.GatkService;
import edu.yu.einstein.wasp.plugin.supplemental.organism.Build;
import edu.yu.einstein.wasp.service.GenomeService;
import edu.yu.einstein.wasp.service.SampleService;
import edu.yu.einstein.wasp.service.StrategyService;
import edu.yu.einstein.wasp.software.SoftwarePackage;

/**
 * @author asmclellan
 * @author jcai
 */
@Transactional("entityManager")
public class GATKSoftwareComponent extends SoftwarePackage {
	
	private static final long serialVersionUID = -7585834500378105920L;

	@Autowired
	private GatkService gatkService;
	
	@Autowired
	private StrategyService strategyService;
	
	@Autowired
	private SampleService sampleService;
	
	@Autowired
	private GenomeService genomeService;
	
	
	public GATKSoftwareComponent() {
	}
	
	public String getCreateTargetCmd(Build build, Set<String> inputFilenames, String intervalFilename, int memRequiredGb, int numProcessors) {
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar -nt " + numProcessors;
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + genomeService.getReferenceGenomeFastaFile(build) + 
				" -T RealignerTargetCreator -o " + intervalFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		logger.debug("Will conduct gatk create target for re-alignment with string: " + command);
		return command;
	}
	
	
	public String getLocalAlignCmd(Build build, Set<String> inputFilenames, String intervalFilename, String realnBamFilename, String realnBaiFilename, int memRequiredGb) {
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar";
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + 
				genomeService.getReferenceGenomeFastaFile(build) + " -T  IndelRealigner" + 
				" -targetIntervals " + intervalFilename + " -o " + realnBamFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		if (realnBaiFilename != null)
			command += " && mv " + realnBamFilename + ".bai " + realnBaiFilename;
		logger.debug("Will conduct gatk local re-alignment with string: " + command);
		
		return command;
	}
	
	/**
	 * assumes outputfiles are 1:1 with input files (matching order), or if output file list size is double input file list size, 
	 * assumes bai files present, thus bam files have odd number indexes and bai files have even number indexes:
	 * e.g. foo.bam (3rd input file) => foo.realn.bam (5th output file) and foo.realn.bai (6th output file)
	 * @param build
	 * @param inputFilenames
	 * @param intervalFilename
	 * @param memRequiredGb
	 * @return
	 */
	public String getLocalAlignCmd(Build build, Set<String> inputFilenames, String intervalFilename, Set<String> outputFilenames, int memRequiredGb) {
		boolean hasBaiFiles = false;
		if (outputFilenames.size() == inputFilenames.size() * 2)
			hasBaiFiles = true;
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar";
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + 
				genomeService.getReferenceGenomeFastaFile(build) + " -T  IndelRealigner --nWayOut .getLocalAlign" + 
				" -targetIntervals " + intervalFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		int i = -1;
		Iterator<String> outFileIterator = outputFilenames.iterator();
		for (String fileName : inputFilenames){
			if (outFileIterator.hasNext())
				command += " && temp[" + ++i + "]=" + fileName + ";mv ${temp[" + i + "]/.bam/.getLocalAlign} " + outFileIterator.next();
			if (hasBaiFiles && outFileIterator.hasNext())
				command += ";mv ${temp[" + i + "]/.bam/.getLocalAlign}.bai " + outFileIterator.next();
		}
			
		logger.debug("Will conduct gatk local re-alignment with string: " + command);<commit_msg>fixed bug in output file renaming<commit_after>package edu.yu.einstein.wasp.gatk.software;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.batch.core.explore.wasp.ParameterValueRetrievalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import edu.yu.einstein.wasp.gatk.service.GatkService;
import edu.yu.einstein.wasp.plugin.supplemental.organism.Build;
import edu.yu.einstein.wasp.service.GenomeService;
import edu.yu.einstein.wasp.service.SampleService;
import edu.yu.einstein.wasp.service.StrategyService;
import edu.yu.einstein.wasp.software.SoftwarePackage;

/**
 * @author asmclellan
 * @author jcai
 */
@Transactional("entityManager")
public class GATKSoftwareComponent extends SoftwarePackage {
	
	private static final long serialVersionUID = -7585834500378105920L;

	@Autowired
	private GatkService gatkService;
	
	@Autowired
	private StrategyService strategyService;
	
	@Autowired
	private SampleService sampleService;
	
	@Autowired
	private GenomeService genomeService;
	
	
	public GATKSoftwareComponent() {
	}
	
	public String getCreateTargetCmd(Build build, Set<String> inputFilenames, String intervalFilename, int memRequiredGb, int numProcessors) {
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar -nt " + numProcessors;
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + genomeService.getReferenceGenomeFastaFile(build) + 
				" -T RealignerTargetCreator -o " + intervalFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		logger.debug("Will conduct gatk create target for re-alignment with string: " + command);
		return command;
	}
	
	
	public String getLocalAlignCmd(Build build, Set<String> inputFilenames, String intervalFilename, String realnBamFilename, String realnBaiFilename, int memRequiredGb) {
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar";
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + 
				genomeService.getReferenceGenomeFastaFile(build) + " -T  IndelRealigner" + 
				" -targetIntervals " + intervalFilename + " -o " + realnBamFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		if (realnBaiFilename != null)
			command += " && mv " + realnBamFilename + ".bai " + realnBaiFilename;
		logger.debug("Will conduct gatk local re-alignment with string: " + command);
		
		return command;
	}
	
	/**
	 * assumes outputfiles are 1:1 with input files (matching order), or if output file list size is double input file list size, 
	 * assumes bai files present, thus bam files have odd number indexes and bai files have even number indexes:
	 * e.g. foo.bam (3rd input file) => foo.realn.bam (5th output file) and foo.realn.bai (6th output file)
	 * @param build
	 * @param inputFilenames
	 * @param intervalFilename
	 * @param memRequiredGb
	 * @return
	 */
	public String getLocalAlignCmd(Build build, Set<String> inputFilenames, String intervalFilename, Set<String> outputFilenames, int memRequiredGb) {
		boolean hasBaiFiles = false;
		if (outputFilenames.size() == inputFilenames.size() * 2)
			hasBaiFiles = true;
		String command = "java -Xmx" + memRequiredGb + "g -jar $GATK_ROOT/GenomeAnalysisTK.jar";
		for (String fileName : inputFilenames)
			command += " -I " + fileName;
		command += " -R " + 
				genomeService.getReferenceGenomeFastaFile(build) + " -T  IndelRealigner --nWayOut .getLocalAlign" + 
				" -targetIntervals " + intervalFilename + " -known " + gatkService.getReferenceIndelsVcfFile(build);
		Iterator<String> outFileIterator = outputFilenames.iterator();
		for (String fileName : inputFilenames){
			if (outFileIterator.hasNext())
				command += " && inFilePath=" + fileName + ";inFile=${inFilePath##*/};tempOutFile=${inFile/.bam/.getLocalAlign};mv $tempOutFile " + 
						outFileIterator.next();
			if (hasBaiFiles && outFileIterator.hasNext())
				command += ";mv ${tempOutFile}.bai " + outFileIterator.next();
		}
			
		logger.debug("Will conduct gatk local re-alignment with string: " + command);
{'fraction_non_alphanumeric': 0.11346588879628527, 'fraction_numerical': 0.0060721514466007855, 'mean_word_length': 4.626255860683188, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3755494', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2664, 'n_words': 626}