Package versusSNP.io

Source Code of versusSNP.io.ORFFile

package versusSNP.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

import versusSNP.Document;
import versusSNP.genome.Genome;
import versusSNP.genome.ORF;
import versusSNP.gui.UICaption;
import versusSNP.gui.widgets.ProgressBar;
import versusSNP.util.Utils;

public class ORFFile implements FileIO, Runnable {
  public static final byte TAB = 1;
  public static final byte SPACE = 2;
  public static final byte COMMA = 3;
  public static final byte EXCEL = 4;
  private byte delimiter;
  private int lineNum = 0;
  private String path, path2;
  private Genome genome;
  private Document document;

  public ORFFile() {
    super();
    this.delimiter = TAB;
  }
 
  private ORFFile(byte delimiter) {
    this();
    this.delimiter = delimiter;
  }
 
  public ORFFile(String path, Genome genome, Document document) {
    this();
    this.path = path;
    this.genome = genome;
    this.document = document;
  }
 
  public ORFFile(String path, Genome genome, Document document, byte delimiter) {
    this(delimiter);
    this.path = path;
    this.genome = genome;
    this.document = document;
  }

  public ORFFile(String path, String path2, Genome genome, Document document, byte delimiter) {
    this(path, genome, document);
    this.path2 = path2;
  }

  @Override
  public boolean readFile(String path, Genome genome) {
    try {
      FileInputStream fis = new FileInputStream(new File(path));
      BufferedReader br = new BufferedReader(new InputStreamReader(fis));
      switch (delimiter) {
      case TAB:
      case SPACE:
        tabbed(br, genome);
        break;
      case COMMA:
        csv(br, genome);
        break;
      case EXCEL:
        excel(br, genome);
        break;
      default:
        break;
      }
      br.close();
      fis.close();
      return true;
    } catch (IOException e) {
      JOptionPane.showMessageDialog(null, UICaption.dialog_exception_file_io_orf, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
      return false;
    } catch (IndexOutOfBoundsException e) {
      JOptionPane.showMessageDialog(null, UICaption.dialog_exception_orf_index_out_of_bounds, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
      return false;
    } catch (NumberFormatException e) {
      JOptionPane.showMessageDialog(null, UICaption.dialog_exception_parse_int_number_format, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
      return false;
    } catch (BadCharException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
      return false;
    }
  }
  private void tabbed(BufferedReader br, Genome genome) throws IOException, IndexOutOfBoundsException, NumberFormatException, BadCharException {
    String line = null;
    while ((line = br.readLine())!=null) {
      if (!checkLine(line))
        continue;
      String[] v;
      if (line.indexOf('\t') == -1)
        delimiter = SPACE;
      if (delimiter == TAB)
        v = line.split("\\t+");
      else
        v = line.split("\\s+");
      // remove "###,###" format
      if (v[1].indexOf('"') != -1 && v[1].indexOf(',') != -1 &&
        v[2].indexOf('"') != -1 && v[2].indexOf('"') != -1)
        genome.addORF(new ORF(v[0], Utils.toIntegar(v[1]), Utils.toIntegar(v[2]), Utils.toStrand(v[3])));
      else
        genome.addORF(new ORF(v[0], Integer.parseInt(v[1]), Integer.parseInt(v[2]), Utils.toStrand(v[3])));
    }
  }
  private void csv(BufferedReader br, Genome genome) throws IOException, IndexOutOfBoundsException, NumberFormatException, BadCharException {
    String line = null;
    while ((line = br.readLine())!=null) {
      if (!checkLine(line))
        continue;
      String[] v = line.split(",");
      genome.addORF(new ORF(v[0], Integer.parseInt(v[1]), Integer.parseInt(v[2]), Utils.toStrand(v[3])));
    }
  }
  private void excel(BufferedReader br, Genome genome) throws IOException, IndexOutOfBoundsException {
    // TODO add implementation to excel files
  }
  private boolean checkLine(String line) {
    lineNum++;
    if (line.equals(""))
      return false;
    else
      return true;
  }

  @Override
  public void run() {
    ProgressBar progressBar = new ProgressBar(UICaption.progress_caption_load_orf, UICaption.progress_label_load_orf, true);
    progressBar.setVisible(true);
    if (readFile(path, genome)) {
      if (path2 != null) {
        new Thread(new FastaFile(path2, genome)).start();
      }
      document.addGenome(genome);
    }
    progressBar.dispose();
  }
}
TOP

Related Classes of versusSNP.io.ORFFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.