All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.umcg.westrah.binarymetaanalyzer.MetaQTL4TraitAnnotation Maven / Gradle / Ivy

The newest version!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.umcg.westrah.binarymetaanalyzer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import umcg.genetica.io.text.TextFile;

/**
 *
 * @author harmjan
 */
public class MetaQTL4TraitAnnotation {

    private final String[] platforms;
    private final ArrayList> traitHashPerPlatform;
    private final HashMap platformToId;
    private final MetaQTL4MetaTraitTreeSet metatraits;
    private final HashMap metaTraitNameToObj;

    public MetaQTL4TraitAnnotation(File probeAnnotationFile, Set platformsToInclude) throws IOException {
        TextFile tf = new TextFile(probeAnnotationFile, TextFile.R);
        int nrLines = tf.countLines();
        String[] header = tf.readLineElems(TextFile.tab);
        boolean[] colsToInclude = new boolean[header.length];
        int nrPlatforms = 0;
        HashSet visitedPlatforms = new HashSet();
        for (int i = 0; i < header.length; i++) {
            if (platformsToInclude.contains(header[i])) {
                if (!visitedPlatforms.contains(header[i])) {
                    colsToInclude[i] = true;
                    visitedPlatforms.add(header[i]);
                    nrPlatforms++;
                } else {
                    System.err.println("ERROR: your probe annotation file contains duplicate platform identifiers!");
                }

            }
        }

        metatraits = new MetaQTL4MetaTraitTreeSet();
        metaTraitNameToObj = new HashMap();
        platformToId = new HashMap();
        platforms = new String[nrPlatforms];
        traitHashPerPlatform = new ArrayList>();

        int platformNr = 0;
        for (int i = 0; i < header.length; i++) {
            if (colsToInclude[i]) {
                platforms[platformNr] = header[i];
                platformToId.put(header[i], platformNr);
                HashMap probeToId = new HashMap();
                traitHashPerPlatform.add(probeToId);
                platformNr++;
            }
        }

        for (String platform : platformsToInclude) {
            if (!visitedPlatforms.contains(platform)) {
                System.err.println("WARNING: no annotation will be loaded for platform: " + platform);
            }
        }

        int probeCounter = 0;

        // parse lines
        for (String[] elems : tf.readLineElemsIterable(TextFile.tab)) {

            String metaTraitName = elems[0];
            String chr = elems[2].intern();
            String chrpos = elems[3];
            String[] chrposElems = chrpos.split("-");
            int chrstartpos = -1;
            int chrendpos = -1;
            if (chrposElems.length >= 1) {
                try {
                    chrstartpos = Integer.parseInt(chrposElems[0]);
                } catch (NumberFormatException e) {
                }
                try {
                    chrendpos = Integer.parseInt(chrposElems[chrposElems.length - 1]);
                } catch (NumberFormatException e) {
                }
            }

            String hugo = elems[4];

            String[] platformIds = new String[nrPlatforms];
            // int metaTraitId, String metaTraitName, String chr, int chrStart, int chrEnd, String annotation, String[] platformIds

            MetaQTL4MetaTrait metaTraitObj = new MetaQTL4MetaTrait(probeCounter, metaTraitName, chr, chrstartpos, chrendpos, hugo, platformIds);

            platformNr = 0;
            for (int i = 5; i < elems.length; i++) {
                if (colsToInclude[i]) {
                    platformIds[platformNr] = elems[i];
                    HashMap probeToId = traitHashPerPlatform.get(platformNr);
                    probeToId.put(elems[i], metaTraitObj);
                    platformNr++;
                }
            }
            
            probeCounter++;
            metatraits.add(metaTraitObj);
            metaTraitNameToObj.put(metaTraitName, metaTraitObj);
        }
        System.out.println(tf.getFileName() + " has annotation for " + visitedPlatforms.size() + " platforms and " + metatraits.size() + " traits.");
        tf.close();
    }

    public Integer getPlatformId(String platform) {
        return platformToId.get(platform);
    }

    public MetaQTL4MetaTrait getTraitForPlatformId(Integer platformId, String platformTrait) {
        return traitHashPerPlatform.get(platformId).get(platformTrait);
    }

    public HashMap getTraitHashForPlatform(Integer platformId) {
        return traitHashPerPlatform.get(platformId);
    }

    public String[] getPlatforms() {
        return platforms;
    }

    public ArrayList> getTraitHashPerPlatform() {
        return traitHashPerPlatform;
    }

    public HashMap getPlatformToId() {
        return platformToId;
    }

    public MetaQTL4MetaTraitTreeSet getMetatraits() {
        return metatraits;
    }

    public HashMap getMetaTraitNameToObj() {
        return metaTraitNameToObj;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy