Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import javax.swing.*;
- import java.io.*;
- import java.util.*;
- import javax.swing.border.*;
- import java.text.StringCharacterIterator;
- import java.text.CharacterIterator;
- import org.biojava.bio.*;
- import org.biojava.bio.seq.*;
- import org.biojava.bio.seq.db.*;
- import org.biojava.bio.seq.io.*;
- import org.biojava.bio.symbol.*;
- public class orffinder{
- public static void main (String args[]){
- View view = new View();
- view.showMain();
- }
- }
- class View extends JFrame implements ActionListener{
- JPanel root = new JPanel();
- Box topbox = Box.createVerticalBox();
- JPanel input_panel = new JPanel();
- JPanel output_panel = new JPanel();
- JTextArea input = new JTextArea(10,30);
- JTextArea output = new JTextArea(10,30);
- JMenuBar menubar = new JMenuBar();
- JMenu filemenu = new JMenu("file");
- JMenu helpmenu = new JMenu("help");
- JMenuItem item_open = new JMenuItem("open");
- JMenuItem item_run = new JMenuItem("run");
- JMenuItem item_about = new JMenuItem("about");
- TitledBorder title_input = BorderFactory.createTitledBorder("input sequence(DNA)");
- TitledBorder title_output = BorderFactory.createTitledBorder("output sequence(protein)");
- //variables
- Sequence input_sequence = null;
- public void showMain(){
- output.setEditable(false);
- input.setEditable(true);
- JScrollPane input_scrollpane = new JScrollPane(input);
- JScrollPane output_scrollpane = new JScrollPane(output);
- this.setName("ORF finder");
- item_open.addActionListener(this);
- item_run.addActionListener(this);
- item_about.addActionListener(this);
- filemenu.add(item_open);
- filemenu.add(item_run);
- helpmenu.add(item_about);
- menubar.add(filemenu);
- menubar.add(helpmenu);
- this.setJMenuBar(menubar);
- input_panel.add(input_scrollpane);
- input_panel.setBorder(title_input);
- output_panel.add(output_scrollpane);
- output_panel.setBorder(title_output);
- topbox.add(input_panel);
- topbox.add(output_panel);
- root.add(topbox);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setContentPane(root);
- this.pack();
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent e){
- if(e.getSource().equals(item_open)){
- JFileChooser filechooser = new JFileChooser();
- int returnVal = filechooser.showOpenDialog(this);
- FastaParser file = new FastaParser();
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- file.setFile(filechooser.getSelectedFile());
- }
- Object[] options = file.getIDs().toArray();
- Object dialog = null;
- dialog = JOptionPane.showInputDialog(this,
- "wich sequence do u want to use?",
- "choose sequence",
- JOptionPane.QUESTION_MESSAGE,
- null,
- options,
- null);
- input_sequence = file.getSequence(dialog);
- CharacterIterator it = new StringCharacterIterator(input_sequence.seqString());
- String temp = "";
- int i = 0;
- for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
- temp += ch;
- i += 1;
- if (i == 37){
- temp += '\n';
- i = 0;
- }
- }
- input.setText(temp);
- }
- if(e.getSource().equals(item_run)){
- Translator translator = new Translator();
- translator.setSequence(input_sequence);
- Sequence output_sequence = translator.run();
- output.setText(output_sequence.getName());
- output.append("\n");
- String temp = null;
- int i = 0;
- CharacterIterator it = new StringCharacterIterator(output_sequence.seqString());
- for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
- temp += ch;
- i += 1;
- if (i == 37){
- temp += '\n';
- i = 0;
- }
- }
- output.append(temp);
- }
- }
- }
- class FastaParser{
- static SequenceDB sequences;
- public static void setFile(File fastafile){
- try{
- BufferedInputStream input = new BufferedInputStream(new FileInputStream(fastafile));
- Alphabet alpha = AlphabetManager.alphabetForName("PROTEIN");
- sequences = SeqIOTools.readFasta(input, alpha);
- }catch (BioException ex){
- //not in fasta format or wrong alphabet
- ex.printStackTrace();
- }catch (NoSuchElementException ex){
- //no fasta sequences in the file
- ex.printStackTrace();
- }catch (FileNotFoundException ex){
- //problem reading file
- ex.printStackTrace();
- }
- }
- public Set getIDs(){
- return sequences.ids();
- }
- public Sequence getSequence(Object id){
- Object[] ids = sequences.ids().toArray();
- int x = 0;
- Sequence sequence = null;
- SequenceIterator iterator = sequences.sequenceIterator();
- for(int i = 0;i < ids.length;i++){
- try{
- sequence = iterator.nextSequence();
- }catch(BioException ex){
- ex.printStackTrace();
- }
- if(ids[i] == id){
- break;
- }
- }
- return sequence;
- }
- }
- class Translator{
- Sequence seq;
- ArrayList<Sequence> output = new ArrayList<Sequence>();
- Sequence temp;
- public void setSequence(Sequence input){
- seq = input;
- }
- public Sequence run(){
- // for each frame
- for (int i = 0; i < 3; i++){
- try{
- SymbolList prot;
- // take the reading frame
- // remember that in a SymbolList the first element has
- // index= 1
- // remember that if the length of the list evenly divisible
- // by three an IllegalArgumentException will be thrown
- SymbolList syms = seq.subList(i + 1, seq.length()
- - (seq.length() - i) % 3);
- // if it is DNA transcribe it to RNA
- if(syms.getAlphabet() == DNATools.getDNA()){
- syms = DNATools.toRNA(syms);
- }
- SymbolList codons = SymbolListViews.windowedSymbolList(syms, 3);
- prot = SymbolListViews.translate(codons,RNATools.getGeneticCode("UNIVERSAL"));
- // System.out.println(syms.seqString());
- output.add(SequenceTools.createSequence(prot, "", seq.getName()
- + "TranslationFrame: +" + i,Annotation.EMPTY_ANNOTATION));
- syms = RNATools.reverseComplement(syms);
- prot = SymbolListViews.translate(codons,RNATools.getGeneticCode("UNIVERSAL"));
- output.add(SequenceTools.createSequence(prot, "", seq.getName()
- + " TranslationFrame: -" + i,Annotation.EMPTY_ANNOTATION));
- }catch(IllegalAlphabetException ex){
- ex.printStackTrace();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- for(int i = 0;i < output.size();i++){
- for(int x = 0;x < output.size();x++){
- if(output.get(i).seqString().length() > output.get(x).seqString().length()){
- temp = output.get(i);
- }
- }
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement