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

net.maizegenetics.dna.map.PositionHDF5List Maven / Gradle / Ivy

Go to download

TASSEL is a software package to evaluate traits associations, evolutionary patterns, and linkage disequilibrium.

The newest version!
package net.maizegenetics.dna.map;

import ch.systemsx.cisd.hdf5.IHDF5Reader;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import net.maizegenetics.dna.WHICH_ALLELE;
import net.maizegenetics.dna.snp.GenotypeTable;
import net.maizegenetics.util.HDF5Utils;
import net.maizegenetics.util.Tassel5HDF5Constants;

import java.lang.reflect.Array;
import java.nio.IntBuffer;
import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * HDF5 immutable instance of {@link PositionList}.  Use the {@link PositionListBuilder}
 * to create the list.
 *
 * @author Ed Buckler
 */
final class PositionHDF5List implements PositionList {
    private final IHDF5Reader reader;
    private final int numPositions;
    private final Map myChrOffPosTree;
    private final Map myChrNameHash;
    private final int[] chrOffsets;  //starting site for each chromosome
    private final Chromosome[] chrIndex;
    private final byte[][] alleles;  //store reference, major, etc. alleles only fully initialized if requested once.
    private final String genomeVersion;

    /*Byte representations of DNA sequences are stored in blocks of 65536 sites*/
    public static final int BLOCKSIZE=1<<16;
    public static final int blockMask=BLOCKSIZE-1;
    public static final int siteMask=~(BLOCKSIZE-1);

    private LoadingCache mySiteList; //key site > AnnoPos
    private CacheLoader annoPosLoader = new CacheLoader()  {
        @Override
        public Position load(Integer key) {
            List toFill=new ArrayList<>();
            toFill.add(key);
            try {
                mySiteList.putAll(loadAll(toFill));
                return get(key);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        public Map loadAll(Iterable keys) throws Exception {
            int key=keys.iterator().next();
            HashMap result=new HashMap(BLOCKSIZE);
            byte[][] afOrder=new byte[2][];
            byte[] ref, anc;
            float[] maf;
            float[] paf;
            String[] snpIDs;
            int startSite=key&siteMask;
            int length=((numPositions-startSite) chrs=new ArrayList<>();
        for (String ls : lociStrings) {
            chrs.add(new Chromosome(ls));
        }

        int[] locusIndices = reader.readIntArray(Tassel5HDF5Constants.CHROMOSOME_INDICES);
        myChrOffPosTree=new TreeMap<>();
        myChrNameHash=new HashMap<>();
        int currStart=0;
        int currLocusIndex=locusIndices[0];
        chrOffsets=new int[chrs.size()];
        chrIndex=new Chromosome[chrs.size()];
        int cI=0;
        for (int i=0; i0)&&(physicalPosition==cop.position[i-1])) {i--;} //backup to the first position if there are duplicates
        i+=(i<0)?-cop.startSiteOff:cop.startSiteOff;
        return i;
    }

    @Override
    public int siteOfPhysicalPosition(int physicalPosition, Chromosome chromosome, String snpName) {
        int result=siteOfPhysicalPosition(physicalPosition, chromosome);
        if (result < 0) {return result;}
        else {
            if (snpName.equals(siteName(result))) {return result;
            } else {
                int index=result;
                while ((index < numPositions) && (chromosomalPosition(index) == physicalPosition)) {
                    if (snpName.equals(siteName(index))) {return index;}
                    result++;
                }
                return -result - 1;
            }
        }
    }

    @Override
    public int[] physicalPositions() {
        int[] result=new int[numPositions];
        IntBuffer ib=IntBuffer.wrap(result);
        for (ChrOffPos cop: myChrOffPosTree.values()) {
            ib.put(cop.position);
        }
        return result;
    }

    @Override
    public String chromosomeName(int site) {
        return chromosome(site).getName();
       // return rangeMap.get(site).getName();
    }

    @Override
    public Chromosome chromosome(int site) {
        int i=Arrays.binarySearch(chrOffsets,site);
        if(i<0) i=-(i+1)-1;
        Chromosome chr=chrIndex[i];
        return chr;
       // return rangeMap.get(site);
    }

    @Override
    public Chromosome chromosome(String name) {
        return myChrNameHash.get(name);
    }

    @Override
    public Chromosome[] chromosomes() {
        return myChrOffPosTree.keySet().toArray(new Chromosome[0]);
    }

    @Override
    public int numChromosomes() {
        return myChrOffPosTree.size();
    }

    @Override
    public int[] chromosomesOffsets() {
        int[] result=new int[myChrOffPosTree.size()];
        int index=0;
        for (ChrOffPos cop: myChrOffPosTree.values()) {
            result[index++]=cop.startSiteOff;
        }
        return result;
    }

    @Override
    public int indelSize(int site) {
        try{return mySiteList.get(site).getKnownVariants()[1].length();}
    catch (ExecutionException e) {
        e.printStackTrace();
        return -1;
    }
    }

    @Override
    public boolean isIndel(int site) {
        try{return mySiteList.get(site).isIndel();}
        catch (ExecutionException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String genomeVersion() {
        return genomeVersion;
    }

    @Override
    public boolean isPositiveStrand(int site) {
        return true;
    }

    // List methods

    @Override
    public int size() {
        return numPositions;
    }

    @Override
    public boolean isEmpty() {
        return (numPositions==0);
    }

    @Override
    public boolean contains(Object o) {
        if(o instanceof Position) {
            Position p=(Position)o;
            int site=siteOfPhysicalPosition(p.getPosition(),p.getChromosome());
            //test for SNP ID also?
            if(site>=0) return true;
        }
        return false;
    }

    @Override
    public Iterator iterator() {
        Iterator it = new Iterator() {
            private int currentIndex = 0;
            @Override
            public boolean hasNext() {
                return currentIndex < numPositions;
            }
            @Override
            public Position next() {
                return get(currentIndex++);
            }
            @Override
            public void remove() {
                throw new UnsupportedOperationException("This Class is Immutable.");
            }
        };
        return it;
       // return mySiteList.iterator();
    }

    @Override
    public Object[] toArray() {
        Position[] aps=new Position[numPositions];
        for (int i=0; i Position[] toArray(Position[] a) {
        if (a.length < numPositions) {
            // If array is too small, allocate the new one with the same component type
            a = (Position[])Array.newInstance(a.getClass().getComponentType(), numPositions);
        } else if (a.length > numPositions) {
            // If array is to large, set the first unassigned element to null
            a[numPositions] = null;
        }
        for (int i=0; i c) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public boolean addAll(Collection c) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public boolean addAll(int index, Collection c) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public boolean removeAll(Collection c) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public boolean retainAll(Collection c) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public void clear() {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    @Override
    public Position get(int index) {
        try {
            return mySiteList.get(index);
        } catch (ExecutionException e) {
            return null;
        }
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public Position set(int index, Position element) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public void add(int index, Position element) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    /**Not supported immutable class*/
    @Override@Deprecated
    public Position remove(int index) {
        throw new UnsupportedOperationException("This Class is Immutable.");
    }

    @Override
    public int indexOf(Object o) {
        if(o instanceof Position) {
            Position p=(Position)o;
            int site=siteOfPhysicalPosition(p.getPosition(),p.getChromosome());
            if(site>=0) return site;
        }
        return -1;
    }

    @Override
    public int lastIndexOf(Object o) {
        throw new UnsupportedOperationException("Not implemented yet.");
       // return mySiteList.lastIndexOf(o);
    }

    @Override
    public ListIterator listIterator() {
        throw new UnsupportedOperationException("Not implemented yet.");
     //   return mySiteList.listIterator();
    }

    @Override
    public ListIterator listIterator(int index) {
        throw new UnsupportedOperationException("Not implemented yet.");
       // return mySiteList.listIterator(index);
    }

    @Override
    public List subList(int fromIndex, int toIndex) {
        throw new UnsupportedOperationException("Not implemented yet.");
        //return mySiteList.subList(fromIndex, toIndex);
    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy