Java Code Examples for gnu.trove.list.array.TDoubleArrayList#get()

The following examples show how to use gnu.trove.list.array.TDoubleArrayList#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InteractionAnalysisDetermineDirection.java    From systemsgenetics with GNU General Public License v3.0 4 votes vote down vote up
public EffectDiffResult calculateEffectDifference(String snpId, String geneName, String covariateName, Allele assessedAllele, double fractionOfSamplesPerGroup) {

		if (!variantIdMap.containsKey(snpId)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (!expressionData.containsRow(geneName)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (!covariatesData.containsRow(covariateName)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (fractionOfSamplesPerGroup <= 0 || fractionOfSamplesPerGroup >= 1) {
			throw new RuntimeException("Fraction must be between 0 and 1");
		}

		GeneticVariant variant = variantIdMap.get(snpId);
		Alleles variantAlleles = variant.getVariantAlleles();

		if (!variantAlleles.contains(assessedAllele)) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		if (variantAlleles.getAlleleCount() != 2) {
			return new EffectDiffResult(Double.NaN, Double.NaN);
		}

		float[] dosagesAll = variant.getSampleDosages();
		String[] genotypedSamples = genotypeData.getSampleNames();

		LinkedHashSet<String> includedGenotypedSamples = new LinkedHashSet<>();
		TDoubleArrayList dosages = new TDoubleArrayList(dosagesAll.length);

		for (int i = 0; i < dosagesAll.length; ++i) {
			if (dosagesAll[i] >= 0 && gte.containsKey(genotypedSamples[i])) {
				includedGenotypedSamples.add(genotypedSamples[i]);
				dosages.add(dosagesAll[i]);
			}
		}

		System.out.println("Included samples: " + includedGenotypedSamples.size());

		double[] expressionLevels = new double[includedGenotypedSamples.size()];
		double[] covariateLevels = new double[includedGenotypedSamples.size()];

		int s = 0;
		for (String genotypeSample : includedGenotypedSamples) {
			expressionLevels[s] = expressionData.getElement(geneName, gte.get(genotypeSample));
			covariateLevels[s] = covariatesData.getElement(covariateName, gte.get(genotypeSample));
			++s;
		}

		if (assessedAllele != variantAlleles.get(0)) {
			for (int i = 0; i < dosages.size(); ++i) {
				dosages.setQuick(i, dosages.getQuick(i) * -1);
			}
		}

		double[] covariateRanks = COV_RANKER.rank(covariateLevels);

		int samplesPerGroup = (int) Math.floor(covariateRanks.length * fractionOfSamplesPerGroup);

		System.out.println("Samples per group: " + samplesPerGroup);

		double[] dosagesLow = new double[samplesPerGroup];
		double[] expressionLow = new double[samplesPerGroup];

		double[] dosagesHigh = new double[samplesPerGroup];
		double[] expressionHigh = new double[samplesPerGroup];

		for (int i = 0; i < samplesPerGroup; ++i) {
			dosagesLow[i] = dosages.get((int) covariateRanks[i]);
			expressionLow[i] = expressionLevels[(int) covariateRanks[i]];
			dosagesHigh[i] = dosages.get((int) covariateRanks[covariateRanks.length - 1 - i]);
			expressionHigh[i] = expressionLevels[(int) covariateRanks[covariateRanks.length - 1 - i]];
		}

		double rhoLow = spearmanCalculator.correlation(dosagesLow, expressionLow);
		double rhoHigh = spearmanCalculator.correlation(dosagesHigh, expressionHigh);

		System.out.println("rho low:" + rhoLow);
		System.out.println("rho high:" + rhoHigh);

		return new EffectDiffResult(rhoLow, rhoHigh);

	}