Java BufferedReader Read readFileGdeHash(File gdeFile, ArrayList name, ArrayList seq)

Here you can find the source of readFileGdeHash(File gdeFile, ArrayList name, ArrayList seq)

Description

read File Gde Hash

License

Open Source License

Parameter

Parameter Description
gdeFile a GDE formated file
name empty ArrayList - will be filled with names
seq empty ArrayList - will be filled with sequences

Return

true if everything ran OK

Declaration

public static boolean readFileGdeHash(File gdeFile, ArrayList name,
        ArrayList seq) 

Method Source Code

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Main {
    /**//from  w ww.  ja  v  a2  s  .  c  om
     * 
     * @param gdeFile a GDE formated file
     * @param name empty ArrayList - will be filled with names
     * @param seq empty ArrayList - will be filled with sequences
     * @return true if everything ran OK
     */
    public static boolean readFileGdeHash(File gdeFile, ArrayList name,
            ArrayList seq) {
        return readFastaFileToArray(gdeFile, "#", name, seq);
    }

    /** reads in a fasta file to two arrays:- one for header and one for sequence
     The filled ArrayLists will have an equal number of elements
     @param name empty ArrayList - will be filled with names
     @param seq empty ArrayList - will be filled with sequences
     @return true if file was read successfully
     */
    public static boolean readFastaFileToArray(File fastaFile,
            String nameIndicatorChar, ArrayList name, ArrayList seq) {
        boolean debug = false;
        boolean success = false;
        int seqCount = -1;
        StringBuilder sequence = new StringBuilder();

        // empty ArrayLists
        name.clear();
        seq.clear();

        // construct two arrays - one of names, one of sequences
        try {
            if (fastaFile.canRead()) {
                BufferedReader bf = new BufferedReader(new FileReader(
                        fastaFile));
                // loop through all the lines in the file
                // does NOT return end of line chars
                int lineCount = 0;
                for (String s = bf.readLine(); s != null; s = bf.readLine(), lineCount++) {
                    if (s.startsWith(nameIndicatorChar)) {
                        if (seqCount > -1) {
                            // write the sequence to the array
                            seq.add(sequence.toString());
                            sequence.delete(0, sequence.length());
                        }
                        seqCount++;
                        // get the name
                        name.add(s.substring(1, s.length()));
                    } else if (seqCount > -1) {
                        sequence.append(s);
                    }

                    if (lineCount % 6000 == 0) {
                        //System.out.println("BioUtil.readFastaFileToArray: loaded seq: "+seqCount+" lines read: "+lineCount);
                    }
                }
                // write the last sequence to the file
                seq.add(sequence.toString());
                sequence.delete(0, sequence.length());

                // if the resulting arraylists ahve the same number of elements
                if (name.size() > 0 && name.size() == seq.size()) {
                    success = true;
                    // remove all spaces from seq strings
                    for (int i = 0; i < seq.size(); i++) {
                        String str = (String) seq.remove(i);
                        seq.add(i, removeAllxFromString(str, " "));
                    }
                }

                if (debug) {
                    System.out.println("name.size: " + name.size()
                            + " seq.size: " + seq.size());
                    for (int i = 0; i < name.size(); i++) {
                        System.out.println(name.get(i));
                    }
                }
            }
        }

        catch (IOException e) {
            System.out
                    .println("BioUtil.readFastaFileToArray: IOException caught");
            System.out.println(e.getMessage());
        }
        return success;
    }

    /**
     * 
     * @param orig the string to work on
     * @param charToBeRemoved the character to remove from string
     * @return the formated string
     */
    public static String removeAllxFromString(String orig,
            String charToBeRemoved) {
        StringBuilder str = new StringBuilder();
        String ch = "";
        for (int i = 0; i < orig.length(); i++) {
            ch = orig.substring(i, i + 1);
            if (ch.compareTo(charToBeRemoved) != 0) {
                str.append(ch);
            }
        }
        return str.toString();
    }
}

Related

  1. readFileAsListOfStrings(String filename)
  2. readFileAsText(String path)
  3. readFileByChar(String path)
  4. readFileContext(File fileToRead)
  5. readFileEncoding(String file)
  6. readFilePathToString(String filePath)
  7. readFileToHashMap(String filePath, String separator, boolean valueFirst)
  8. readFileToHashMap(String filePath, String separator, boolean valueFirst)
  9. readFileToList(String fileName, List list)