<commit_before>/* Copyright 2013 University of North Carolina at Chapel Hill.  All rights reserved. */
package abra;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.samtools.SAMFileReader;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.SAMFileReader.ValidationStringency;

public class NativeAssembler implements Assembler {
	
	private boolean truncateOnRepeat;
	private int maxContigs;
	private int maxPathsFromRoot;
	private int readLength;
	private int kmer;
	private int minKmerFrequency;
	private Set<String> readIds;

	private native int assemble(String input, String output, String prefix, int truncateOnRepeat, int maxContigs, int maxPathsFromRoot, int readLength, int kmerSize, int minKmerFreq);
	
//	static{
//        System.loadLibrary("Abra");
//	}
	
	private String getIdentifier(SAMRecord read) {
		String id = read.getReadName();
		
		if (read.getReadPairedFlag() && read.getSecondOfPairFlag()) {
			id += "_2";
		}
		
		return id;
	}
	
//	public boolean assembleContigs(String input, String output, String prefix, boolean checkForDupes) {
	
	public boolean assembleContigs(List<String> inputFiles, String output, String tempDir, Feature region, String prefix, boolean checkForDupes, ReAligner realigner) {
		
		long start = System.currentTimeMillis();
		
		int count = 0;
		
		readIds = new HashSet<String>();
		
		try {
			
			String readFile;
			if (region != null) {
				readFile = tempDir + "/" + region.getDescriptor() + ".reads";
			} else {
				readFile = tempDir + "/" + "unaligned.reads";
			}
			
			BufferedWriter writer = new BufferedWriter(new FileWriter(readFile, false));

			for (String input : inputFiles) {
				SAMFileReader reader = new SAMFileReader(new File(input));
				reader.setValidationStringency(ValidationStringency.SILENT);
	
				Iterator<SAMRecord> iter;
				if (region != null) {
					iter = reader.queryOverlapping(region.getSeqname(), (int) region.getStart(), (int) region.getEnd());
				} else {
					iter = reader.iterator();
				}
				
				while (iter.hasNext()) {
					
					SAMRecord read = iter.next();
					
					if (read.getReadLength() > readLength) {
						reader.close();
						throw new IllegalArgumentException(
								"Read length exceeds expected value of: " + readLength + " for read [" +
								read.getSAMString() + "]");
					}
					
					// Don't allow same read to be counted twice.
					if ( (!realigner.isFiltered(read)) && (!read.getDuplicateReadFlag()) && (!read.getReadFailsVendorQualityCheckFlag()) && ((!checkForDupes) || (!readIds.contains(getIdentifier(read))))) {
	//					boolean hasAmbiguousBases = read.getReadString().contains("N");
						Integer numBestHits = (Integer) read.getIntegerAttribute("X0");
						boolean hasAmbiguousInitialAlignment = numBestHits != null && numBestHits > 1;
						//TODO: Stampy ambiguous read (mapq < 4)
						
	//					if (!hasAmbiguousBases && !hasAmbiguousInitialAlignment && !hasLowQualityBase(read)) {
						if (!hasAmbiguousInitialAlignment) {
							if (!checkForDupes) {
								readIds.add(getIdentifier(read));
							}
							
							if (read.getReadLength() == readLength) {
								writer.write(read.getReadString() + "\n");
								writer.write(read.getBaseQualityString() + "\n");
							} else {
								StringBuffer basePadding = new StringBuffer();
								StringBuffer qualPadding = new StringBuffer();
								
								for (int i=0; i<readLength-read.getReadLength(); i++) {
									basePadding.append('N');
									qualPadding.append('!');
								}
								
								writer.write(read.getReadNegativeStrandFlag() ? "1\n" : "0\n");
								writer.write(read.getReadString() + basePadding.toString() + "\n");
								writer.write(read.getBaseQualityString() + qualPadding.toString() + "\n");							
							}
						}
					}
				}
				
				reader.close();
			}
			<commit_msg>Corrected assembly strand reporting bug.<commit_after>/* Copyright 2013 University of North Carolina at Chapel Hill.  All rights reserved. */
package abra;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.samtools.SAMFileReader;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.SAMFileReader.ValidationStringency;

public class NativeAssembler implements Assembler {
	
	private boolean truncateOnRepeat;
	private int maxContigs;
	private int maxPathsFromRoot;
	private int readLength;
	private int kmer;
	private int minKmerFrequency;
	private Set<String> readIds;

	private native int assemble(String input, String output, String prefix, int truncateOnRepeat, int maxContigs, int maxPathsFromRoot, int readLength, int kmerSize, int minKmerFreq);
	
//	static{
//        System.loadLibrary("Abra");
//	}
	
	private String getIdentifier(SAMRecord read) {
		String id = read.getReadName();
		
		if (read.getReadPairedFlag() && read.getSecondOfPairFlag()) {
			id += "_2";
		}
		
		return id;
	}
	
//	public boolean assembleContigs(String input, String output, String prefix, boolean checkForDupes) {
	
	public boolean assembleContigs(List<String> inputFiles, String output, String tempDir, Feature region, String prefix, boolean checkForDupes, ReAligner realigner) {
		
		long start = System.currentTimeMillis();
		
		int count = 0;
		
		readIds = new HashSet<String>();
		
		try {
			
			String readFile;
			if (region != null) {
				readFile = tempDir + "/" + region.getDescriptor() + ".reads";
			} else {
				readFile = tempDir + "/" + "unaligned.reads";
			}
			
			BufferedWriter writer = new BufferedWriter(new FileWriter(readFile, false));

			for (String input : inputFiles) {
				SAMFileReader reader = new SAMFileReader(new File(input));
				reader.setValidationStringency(ValidationStringency.SILENT);
	
				Iterator<SAMRecord> iter;
				if (region != null) {
					iter = reader.queryOverlapping(region.getSeqname(), (int) region.getStart(), (int) region.getEnd());
				} else {
					iter = reader.iterator();
				}
				
				while (iter.hasNext()) {
					
					SAMRecord read = iter.next();
					
					if (read.getReadLength() > readLength) {
						reader.close();
						throw new IllegalArgumentException(
								"Read length exceeds expected value of: " + readLength + " for read [" +
								read.getSAMString() + "]");
					}
					
					// Don't allow same read to be counted twice.
					if ( (!realigner.isFiltered(read)) && (!read.getDuplicateReadFlag()) && (!read.getReadFailsVendorQualityCheckFlag()) && ((!checkForDupes) || (!readIds.contains(getIdentifier(read))))) {
	//					boolean hasAmbiguousBases = read.getReadString().contains("N");
						Integer numBestHits = (Integer) read.getIntegerAttribute("X0");
						boolean hasAmbiguousInitialAlignment = numBestHits != null && numBestHits > 1;
						//TODO: Stampy ambiguous read (mapq < 4)
						
	//					if (!hasAmbiguousBases && !hasAmbiguousInitialAlignment && !hasLowQualityBase(read)) {
						if (!hasAmbiguousInitialAlignment) {
							if (!checkForDupes) {
								readIds.add(getIdentifier(read));
							}
							
							writer.write(read.getReadNegativeStrandFlag() ? "1\n" : "0\n");
							
							if (read.getReadLength() == readLength) {
								writer.write(read.getReadString() + "\n");
								writer.write(read.getBaseQualityString() + "\n");
							} else {
								StringBuffer basePadding = new StringBuffer();
								StringBuffer qualPadding = new StringBuffer();
								
								for (int i=0; i<readLength-read.getReadLength(); i++) {
									basePadding.append('N');
									qualPadding.append('!');
								}
								
//								writer.write(read.getReadNegativeStrandFlag() ? "1\n" : "0\n");
								writer.write(read.getReadString() + basePadding.toString() + "\n");
								writer.write(read.getBaseQualityString() + qualPadding.toString() + "\n");							
							}
						}
					}
				}
				
				reader.close();
			}
			
{'fraction_non_alphanumeric': 0.12700765999505806, 'fraction_numerical': 0.0032122559920929085, 'mean_word_length': 3.251575630252101, 'pattern_counts': {'":': 0, '<': 15, '<?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': '5775098', 'n_tokens_mistral': 3199, 'n_tokens_neox': 2401, 'n_words': 559}