Wiki

Clone wiki

Tassel 5 Source / code / Panzea Journal Club March 28 2014

This is java code written in Netbeans using Tassel v.5 source code.

#!java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MiscFilter;

import java.io.DataOutputStream;
import java.util.ArrayList;
import net.maizegenetics.dna.snp.ExportUtils;
import net.maizegenetics.dna.snp.FilterGenotypeTable;
import net.maizegenetics.dna.snp.GenotypeTable;
import net.maizegenetics.dna.snp.ImportUtils;
import net.maizegenetics.util.Utils;

/**
 *
 * @author kls283
 */
public class FilterGenotypes {

    public static void sampleSitesByMAF(String inFile, double minMAF, double maxMAF) {
        GenotypeTable genos= ImportUtils.readGuessFormat(inFile);
        System.out.println("Read in genotypeTable file"+inFile);
        ArrayList<String> keepSites= new ArrayList<>();
        ArrayList<Integer> removeSites= new ArrayList<>();

        for (int site = 0; site < genos.numberOfSites(); site++) {
            double currMAF= genos.minorAlleleFrequency(site);
            if (currMAF>=minMAF && currMAF<=maxMAF) keepSites.add(genos.siteName(site));
            else removeSites.add(site);
        }
        System.out.println("Out of "+genos.numberOfSites()+" keep "+keepSites.size()+" sites");
        FilterGenotypeTable filterKeep= FilterGenotypeTable.getInstance(genos, keepSites.toArray(new String[keepSites.size()]));
        String outFile= inFile.substring(0, inFile.indexOf(".hmp"))+"InclusiveByMAF_"+minMAF+"-"+maxMAF+".vcf";
        ExportUtils.writeToHapmap(genos, outFile);

        String outputToText= inFile.substring(0, inFile.indexOf(".hmp"))+"ExcludedSites_"+minMAF+"-"+maxMAF+".txt";
        writeExcludedSitesToFile(genos, removeSites, outputToText);
    }

    public static void writeExcludedSitesToFile(GenotypeTable genos, ArrayList<Integer> removeSites, String outFile) {
        try{
            DataOutputStream outStream= Utils.getDataOutputStream(outFile, 1000);
            outStream.writeBytes("Chromosome\tPhysicalPosition\tMAF\n");
            for (int site:removeSites) {
                outStream.writeBytes(genos.chromosomeName(site)+"\t"+genos.physicalPositions()[site]+"\t"+genos.minorAlleleFrequency(site)+"\n");
            }
            outStream.close();
        }
        catch (Exception e) {
            System.out.println("Problem writing output: "+e);
        }
    }

    public static void main(String[] args) {
        String dir="/Users/usr/Desktop/";
        String inFile= dir+"yourFileName";
        sampleSitesByMAF(inFile, .1, .4);
    }

}

Updated