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

Java tutorial

Introduction

Here is the source code for eu.popgen.canephora.parsers.plink.FAMReader.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.*;
import java.util.*;
import org.apache.commons.lang3.*;

/**
 *
 * @author tiago
 */
public class FAMReader implements Iterator {
    private BufferedReader in;
    private FAMRecord next = null;

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

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

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

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

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

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

    /**
     *
     * @return
     */
    @Override
    public FAMRecord next() {
        FAMRecord 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.");
    }

}