<commit_before>package net.maizegenetics.gwas.NAM;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;


public class ModelFitter extends Thread {
	double[][] genotypes;
	int firstMarker;
	static AGPMap theAGPMap = null;
	double[][] residuals;
	String[] residualSamples;
	HashMap<String, Integer> sampleNameMap;
	int traitnumber;
	String thisTraitName;
	int totalSnps;
	int snpcount;
	FileNames files;
	
	SnpData snpdata;
	int maxmarker;
	ArrayList<String> pops;
	int[] popIndex;

	
	public static final int[] chromosomeLength = new int[]{300239041,
		234752839,
		230558137,
		247095508,
		216915529,
		169254300,
		170974187,
		174515299,
		152350485,
		149686046};

	public ModelFitter(int chromosome, FileNames files) {
		getAGPMap(files.agpmap);
		this.files = files;
	}
	
	public ModelFitter(FileNames parameters) {
		files = parameters;
		getAGPMap(files.agpmap);
	}
	
	public void run() {
		init();
		long start = System.currentTimeMillis();
		testSnps();
		long elapsed = System.currentTimeMillis() - start;
		System.out.println("Elapsed time for namgwas = " + elapsed);
	}
	
	public void init() {
		importResiduals(files.residuals);
		importNamMarkersforMap(files.namMarkersByChr);
		
		if (files.randomizeSnpOrder) snpdata = new SnpDataRandomOrder(files);
		else snpdata = new SnpData(files);
		maxmarker = genotypes[0].length - 1;
		int nSamples = residualSamples.length;
		popIndex = new int[nSamples];
		pops = new ArrayList<String>();
		int count = 0;
		for (String sample : residualSamples) {
			int ipop = getPopulation(sample) - 1;
			pops.add(SnpData.popnames[ipop]);
			popIndex[count++] = ipop;
		}

	}
	
	public synchronized void getAGPMap(File mapfile) {
		if (theAGPMap == null) {
			theAGPMap = new AGPMap(mapfile);
		}
	}
	
	//reads the combined NAM + IBM data file for a chromosome
	//residuals must have been imported first so that the genotypes are properly matched to them
	protected void importNamMarkersforMap(File file) {
		Pattern tab = Pattern.compile("\t");
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			//the first line contains headers
			String input = br.readLine();
			String[] data = tab.split(input);
			firstMarker = Integer.parseInt(data[2].substring(1));

			int nMarkers = data.length - 1;
			int nTaxa = sampleNameMap.size();
			genotypes = new double[nTaxa][nMarkers];
			
			while ((input = br.readLine()) != null) {
				data = tab.split(input);
				Integer sampleNumber = sampleNameMap.get(data[0]);
				if (sampleNumber != null) {
					int n = sampleNumber.intValue();
					for (int j = 0; j < nMarkers; j++) {
						genotypes[n][j] = Double.parseDouble(data[j + 1]);
					}
				}
			}
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	protected void importResiduals(File residualfile) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(residualfile));
			int nRows = 0;
			br.readLine();
			while (br.readLine() != null) nRows++;
			br.close();<commit_msg>change snp storage to float<commit_after>package net.maizegenetics.gwas.NAM;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;


public class ModelFitter extends Thread {
	float[][] genotypes;
	int firstMarker;
	static AGPMap theAGPMap = null;
	double[][] residuals;
	String[] residualSamples;
	HashMap<String, Integer> sampleNameMap;
	int traitnumber;
	String thisTraitName;
	int totalSnps;
	int snpcount;
	FileNames files;
	
	SnpData snpdata;
	int maxmarker;
	ArrayList<String> pops;
	int[] popIndex;

	
	public static final int[] chromosomeLength = new int[]{300239041,
		234752839,
		230558137,
		247095508,
		216915529,
		169254300,
		170974187,
		174515299,
		152350485,
		149686046};

	public ModelFitter(int chromosome, FileNames files) {
		getAGPMap(files.agpmap);
		this.files = files;
	}
	
	public ModelFitter(FileNames parameters) {
		files = parameters;
		getAGPMap(files.agpmap);
	}
	
	public void run() {
		init();
		long start = System.currentTimeMillis();
		testSnps();
		long elapsed = System.currentTimeMillis() - start;
		System.out.println("Elapsed time for namgwas = " + elapsed);
	}
	
	public void init() {
		importResiduals(files.residuals);
		importNamMarkersforMap(files.namMarkersByChr);
		
		if (files.randomizeSnpOrder) snpdata = new SnpDataRandomOrder(files);
		else snpdata = new SnpData(files);
		maxmarker = genotypes[0].length - 1;
		int nSamples = residualSamples.length;
		popIndex = new int[nSamples];
		pops = new ArrayList<String>();
		int count = 0;
		for (String sample : residualSamples) {
			int ipop = getPopulation(sample) - 1;
			pops.add(SnpData.popnames[ipop]);
			popIndex[count++] = ipop;
		}

	}
	
	public synchronized void getAGPMap(File mapfile) {
		if (theAGPMap == null) {
			theAGPMap = new AGPMap(mapfile);
		}
	}
	
	//reads the combined NAM + IBM data file for a chromosome
	//residuals must have been imported first so that the genotypes are properly matched to them
	protected void importNamMarkersforMap(File file) {
		Pattern tab = Pattern.compile("\t");
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			//the first line contains headers
			String input = br.readLine();
			String[] data = tab.split(input);
			firstMarker = Integer.parseInt(data[2].substring(1));

			int nMarkers = data.length - 1;
			int nTaxa = sampleNameMap.size();
			genotypes = new float[nTaxa][nMarkers];
			
			while ((input = br.readLine()) != null) {
				data = tab.split(input);
				Integer sampleNumber = sampleNameMap.get(data[0]);
				if (sampleNumber != null) {
					int n = sampleNumber.intValue();
					for (int j = 0; j < nMarkers; j++) {
						genotypes[n][j] = Float.parseFloat(data[j + 1]);
					}
				}
			}
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	protected void importResiduals(File residualfile) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(residualfile));
			int nRows = 0;
			br.readLine();
			while (br.readLine() != null) nRows++;
			br.close();
{'fraction_non_alphanumeric': 0.11903341288782816, 'fraction_numerical': 0.030429594272076373, 'mean_word_length': 4.030007501875469, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '2643661', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2249, 'n_words': 553}