Advertisement
Guest User

trying biojava 3

a guest
Feb 13th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.79 KB | None | 0 0
  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.border.*;
  6. import java.text.StringCharacterIterator;
  7. import java.text.CharacterIterator;
  8.  
  9. import org.biojava3.core.sequence.*;
  10. import org.biojava3.core.sequence.io.*;
  11. import org.biojava3.core.sequence.compound.*;
  12. import org.biojava3.core.sequence.transcription.*;
  13.  
  14. public class orffinder{
  15.     public static void main (String args[]){
  16.         View view = new View();
  17.         view.showMain();
  18.     }
  19. }
  20.  
  21. class View extends JFrame implements ActionListener{
  22.     JPanel root = new JPanel();
  23.     Box topbox = Box.createVerticalBox();
  24.     JPanel input_panel = new JPanel();
  25.     JPanel output_panel = new JPanel();
  26.     JTextArea input = new JTextArea(10,30);
  27.     JTextArea output = new JTextArea(10,30);
  28.  
  29.     JMenuBar menubar = new JMenuBar();
  30.     JMenu filemenu = new JMenu("file");
  31.     JMenu helpmenu = new JMenu("help");
  32.     JMenuItem item_open = new JMenuItem("open");
  33.     JMenuItem item_run = new JMenuItem("run");
  34.     JMenuItem item_about = new JMenuItem("about");
  35.  
  36.     TitledBorder title_input = BorderFactory.createTitledBorder("input sequence(DNA)");
  37.     TitledBorder title_output = BorderFactory.createTitledBorder("output sequence(protein)");
  38.  
  39.     //variables
  40.     DNASequence input_sequence;
  41.    
  42.     public void showMain(){
  43.         output.setEditable(false);
  44.         input.setEditable(true);
  45.         JScrollPane input_scrollpane = new JScrollPane(input);
  46.         JScrollPane output_scrollpane = new JScrollPane(output);
  47.         this.setName("ORF finder");
  48.  
  49.         item_open.addActionListener(this);
  50.         item_run.addActionListener(this);
  51.         item_about.addActionListener(this);
  52.  
  53.         filemenu.add(item_open);
  54.         filemenu.add(item_run);
  55.         helpmenu.add(item_about);
  56.         menubar.add(filemenu);
  57.         menubar.add(helpmenu);
  58.         this.setJMenuBar(menubar);
  59.  
  60.         input_panel.add(input_scrollpane);
  61.         input_panel.setBorder(title_input);
  62.         output_panel.add(output_scrollpane);
  63.         output_panel.setBorder(title_output);
  64.  
  65.         topbox.add(input_panel);
  66.         topbox.add(output_panel);
  67.         root.add(topbox);
  68.         this.setResizable(false);
  69.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         this.setContentPane(root);
  71.         this.pack();
  72.         this.setVisible(true);
  73.        
  74.     }
  75.     public void actionPerformed(ActionEvent e){
  76.         if(e.getSource().equals(item_open)){
  77.             LinkedHashMap<String, DNASequence> inputSequences = null;
  78.             JFileChooser filechooser = new JFileChooser();
  79.             int returnVal = filechooser.showOpenDialog(this);
  80.             FastaParser file = new FastaParser();
  81.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  82.                 try{
  83.                         inputSequences = file.readFastaDNASequence(filechooser.getSelectedFile());
  84.                 }catch(Exception ex){}
  85.             }
  86.             Object[] options = inputSequences.keySet().toArray();
  87.             Object dialog = null;
  88.             dialog = JOptionPane.showInputDialog(this,
  89.             "wich sequence do you want to use?",
  90.             "choose sequence",
  91.             JOptionPane.QUESTION_MESSAGE,
  92.             null,
  93.             options,
  94.             null);
  95.             input_sequence = file.getSequence(dialog);
  96.             CharacterIterator it = new StringCharacterIterator(input_sequence.getSequenceAsString());
  97.             String temp = "";
  98.             int i = 0;
  99.             for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
  100.                 temp += ch;
  101.                 i += 1;
  102.                 if (i == 37){
  103.                     temp += '\n';
  104.                     i = 0;
  105.                 }
  106.             }
  107.             input.setText(temp);
  108.         }
  109.         if(e.getSource().equals(item_run)){
  110.             Translator translator = new Translator();
  111.             translator.setSequence(input_sequence);
  112.             ProteinSequence output_sequence = translator.run();
  113.             output.setText(output_sequence.getOriginalHeader());
  114.             output.append("\n");
  115.             String temp = null;
  116.             int i = 0;
  117.             CharacterIterator it = new StringCharacterIterator(output_sequence.getSequenceAsString());
  118.             for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
  119.                 temp += ch;
  120.                 i += 1;
  121.                 if (i == 37){
  122.                     temp += '\n';
  123.                     i = 0;
  124.                 }
  125.             }
  126.             output.append(temp);
  127.         }
  128.     }
  129. }
  130.  
  131. class FastaParser{
  132.     LinkedHashMap<String, DNASequence> dnaSequences = null;
  133.     public LinkedHashMap<String, DNASequence> readFastaDNASequence(InputStream inStream) throws Exception{
  134.         FastaReader<DNASequence, NucleotideCompound> fastaReader = new FastaReader<DNASequence, NucleotideCompound>(
  135.         inStream,new GenericFastaHeaderParser<DNASequence, NucleotideCompound>(),
  136.         new DNASequenceCreator(DNACompoundSet.getDNACompoundSet()));
  137.         return fastaReader.process();
  138.     }
  139.     public LinkedHashMap<String, DNASequence> readFastaDNASequence(File file) throws Exception{
  140.         FileInputStream inStream = new FileInputStream(file);
  141.         dnaSequences = readFastaDNASequence(inStream);
  142.         inStream.close();
  143.         return dnaSequences;
  144.     }
  145.     public DNASequence getSequence(Object sequenceID){
  146.         return dnaSequences.get(sequenceID);
  147.     }
  148. }
  149.  
  150. class Translator{
  151.     DNASequence seq;
  152.     ArrayList<ProteinSequence> output = new ArrayList<ProteinSequence>();
  153.     ProteinSequence temp;
  154.     Frame frame;
  155.     public void setSequence(DNASequence input){
  156.         seq = input;
  157.     }
  158.     public ProteinSequence run(){
  159.         for (int i = 0; i < 3; i++){
  160.             if(i == 0){
  161.                 frame = frame.ONE;
  162.             }if(i == 1){
  163.                 frame = frame.TWO;
  164.             }if(i == 2){
  165.                 frame = frame.THREE;
  166.             }
  167.             ProteinSequence prot_forward = seq.getRNASequence(frame).getProteinSequence();
  168.             prot_forward.setOriginalHeader(seq.getOriginalHeader() + "TranslationFrame: +" + i);
  169.             //ProteinSequence prot_reversecomplement = seq.getRNASequence(frame).getReverseComplement().getProteinSequence();
  170.             //prot_reversecomplement.setOriginalHeader(seq.getOriginalHeader() + "TranslationFrame: +" + i);
  171.             output.add(prot_forward);
  172.             //output.add(prot_reversecomplement);
  173.         }
  174.         for(int i = 0;i < output.size();i++){
  175.             for(int x = 0;x < output.size();x++){
  176.                 if(output.get(i).getSequenceAsString().length() > output.get(x).getSequenceAsString().length()){
  177.                     temp = output.get(i);
  178.                 }
  179.             }
  180.         }
  181.     return temp;
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement