eu.popgen.canephora.parsers.plink.BIMReader.java Source code

Java tutorial

Introduction

Here is the source code for eu.popgen.canephora.parsers.plink.BIMReader.java

Source

/*
 * Copyright (C) 2012 Tiago Antao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.popgen.canephora.parsers.plink;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author tiago
 */

public class BIMReader implements Iterator {
    private BufferedReader in;
    private BIMRecord next = null;

    BIMReader(BufferedReader in) {
        this.in = in;
    }

    /**
     *
     * @param fileName
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public BIMReader read(String fileName) throws FileNotFoundException, IOException {
        return read(new BufferedReader(new FileReader(fileName)));
    }

    /**
     *
     * @param in
     * @return
     * @throws IOException
     */
    public BIMReader read(BufferedReader in) throws IOException {
        BIMReader fr = new BIMReader(in);
        return fr;
    }

    @Override
    public boolean hasNext() {
        if (this.next != null) {
            return true;
        } else {

        }
        throw new UnsupportedOperationException("Not supported yet.");
    }

    BIMRecord readRecord() throws IOException {
        String line = this.in.readLine();
        if (line == null) {
            return null;
        }
        String[] toks = StringUtils.split(line);
        BIMRecord rec = new BIMRecord(Integer.parseInt(toks[0]), toks[1], Float.parseFloat(toks[2]),
                Integer.parseInt(toks[3]), toks[4].charAt(0), toks[5].charAt(0));
        return rec;
    }

    /**
     *
     * @return
     */
    @Override
    public BIMRecord next() {
        BIMRecord n;
        if (this.next == null) {
            try {
                n = readRecord();
                if (n == null) {
                    throw new NoSuchElementException();
                }
            } catch (IOException ex) {
                throw new RuntimeException("Could not parse FAM line");
            }
            return n;
        } else {
            n = this.next;
            this.next = null;
            return n;
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported.");
    }

}