Advertisement
Guest User

orffinder, error in object translator

a guest
Feb 12th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.71 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.biojava.bio.*;
  10. import org.biojava.bio.seq.*;
  11. import org.biojava.bio.seq.db.*;
  12. import org.biojava.bio.seq.io.*;
  13. import org.biojava.bio.symbol.*;
  14.  
  15. public class orffinder{
  16.     public static void main (String args[]){
  17.         View view = new View();
  18.         view.showMain();
  19.     }
  20. }
  21.  
  22. class View extends JFrame implements ActionListener{
  23.     JPanel root = new JPanel();
  24.     Box topbox = Box.createVerticalBox();
  25.     JPanel input_panel = new JPanel();
  26.     JPanel output_panel = new JPanel();
  27.     JTextArea input = new JTextArea(10,30);
  28.     JTextArea output = new JTextArea(10,30);
  29.  
  30.     JMenuBar menubar = new JMenuBar();
  31.     JMenu filemenu = new JMenu("file");
  32.     JMenu helpmenu = new JMenu("help");
  33.     JMenuItem item_open = new JMenuItem("open");
  34.     JMenuItem item_run = new JMenuItem("run");
  35.     JMenuItem item_about = new JMenuItem("about");
  36.  
  37.     TitledBorder title_input = BorderFactory.createTitledBorder("input sequence(DNA)");
  38.     TitledBorder title_output = BorderFactory.createTitledBorder("output sequence(protein)");
  39.  
  40.     //variables
  41.     Sequence input_sequence = null;
  42.    
  43.     public void showMain(){
  44.         output.setEditable(false);
  45.         input.setEditable(true);
  46.         JScrollPane input_scrollpane = new JScrollPane(input);
  47.         JScrollPane output_scrollpane = new JScrollPane(output);
  48.         this.setName("ORF finder");
  49.  
  50.         item_open.addActionListener(this);
  51.         item_run.addActionListener(this);
  52.         item_about.addActionListener(this);
  53.  
  54.         filemenu.add(item_open);
  55.         filemenu.add(item_run);
  56.         helpmenu.add(item_about);
  57.         menubar.add(filemenu);
  58.         menubar.add(helpmenu);
  59.         this.setJMenuBar(menubar);
  60.  
  61.         input_panel.add(input_scrollpane);
  62.         input_panel.setBorder(title_input);
  63.         output_panel.add(output_scrollpane);
  64.         output_panel.setBorder(title_output);
  65.  
  66.         topbox.add(input_panel);
  67.         topbox.add(output_panel);
  68.         root.add(topbox);
  69.         this.setResizable(false);
  70.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  71.         this.setContentPane(root);
  72.         this.pack();
  73.         this.setVisible(true);
  74.        
  75.     }
  76.     public void actionPerformed(ActionEvent e){
  77.         if(e.getSource().equals(item_open)){
  78.             JFileChooser filechooser = new JFileChooser();
  79.             int returnVal = filechooser.showOpenDialog(this);
  80.             FastaParser file = new FastaParser();
  81.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  82.                 file.setFile(filechooser.getSelectedFile());
  83.             }
  84.             Object[] options = file.getIDs().toArray();
  85.             Object dialog = null;
  86.             dialog = JOptionPane.showInputDialog(this,
  87.             "wich sequence do u want to use?",
  88.             "choose sequence",
  89.             JOptionPane.QUESTION_MESSAGE,
  90.             null,
  91.             options,
  92.             null);
  93.             input_sequence = file.getSequence(dialog);
  94.             CharacterIterator it = new StringCharacterIterator(input_sequence.seqString());
  95.             String temp = "";
  96.             int i = 0;
  97.             for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
  98.                 temp += ch;
  99.                 i += 1;
  100.                 if (i == 37){
  101.                     temp += '\n';
  102.                     i = 0;
  103.                 }
  104.             }
  105.             input.setText(temp);
  106.         }
  107.         if(e.getSource().equals(item_run)){
  108.             Translator translator = new Translator();
  109.             translator.setSequence(input_sequence);
  110.             Sequence output_sequence = translator.run();
  111.             output.setText(output_sequence.getName());
  112.             output.append("\n");
  113.             String temp = null;
  114.             int i = 0;
  115.             CharacterIterator it = new StringCharacterIterator(output_sequence.seqString());
  116.             for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()){
  117.                 temp += ch;
  118.                 i += 1;
  119.                 if (i == 37){
  120.                     temp += '\n';
  121.                     i = 0;
  122.                 }
  123.             }
  124.             output.append(temp);
  125.         }
  126.     }
  127. }
  128.  
  129. class FastaParser{
  130.     static SequenceDB sequences;
  131.     public static void setFile(File fastafile){
  132.         try{
  133.             BufferedInputStream input = new BufferedInputStream(new FileInputStream(fastafile));
  134.             Alphabet alpha = AlphabetManager.alphabetForName("PROTEIN");
  135.             sequences = SeqIOTools.readFasta(input, alpha);
  136.         }catch (BioException ex){
  137.         //not in fasta format or wrong alphabet
  138.         ex.printStackTrace();
  139.         }catch (NoSuchElementException ex){
  140.         //no fasta sequences in the file
  141.         ex.printStackTrace();
  142.         }catch (FileNotFoundException ex){
  143.         //problem reading file
  144.         ex.printStackTrace();
  145.         }
  146.     }
  147.     public Set getIDs(){
  148.         return sequences.ids();
  149.     }
  150.     public Sequence getSequence(Object id){
  151.         Object[] ids = sequences.ids().toArray();
  152.         int x = 0;
  153.         Sequence sequence = null;
  154.         SequenceIterator iterator = sequences.sequenceIterator();
  155.         for(int i = 0;i < ids.length;i++){
  156.             try{
  157.                 sequence = iterator.nextSequence();
  158.             }catch(BioException ex){
  159.                 ex.printStackTrace();
  160.             }
  161.             if(ids[i] == id){
  162.                 break;
  163.             }
  164.         }
  165.         return sequence;
  166.     }
  167. }
  168.  
  169. class Translator{
  170.     Sequence seq;
  171.     ArrayList<Sequence> output = new ArrayList<Sequence>();
  172.     Sequence temp;
  173.     public void setSequence(Sequence input){
  174.         seq = input;
  175.     }
  176.     public Sequence run(){
  177.         // for each frame
  178.         for (int i = 0; i < 3; i++){
  179.             try{
  180.                 SymbolList prot;
  181.                 // take the reading frame
  182.                 // remember that in a SymbolList the first element has
  183.                 // index= 1
  184.                 // remember that if the length of the list evenly divisible
  185.                 // by three an IllegalArgumentException will be thrown
  186.                 SymbolList syms = seq.subList(i + 1, seq.length()
  187.                 - (seq.length() - i) % 3);
  188.                 // if it is DNA transcribe it to RNA
  189.                 if(syms.getAlphabet() == DNATools.getDNA()){
  190.                     syms = DNATools.toRNA(syms);
  191.                 }
  192.                 SymbolList codons = SymbolListViews.windowedSymbolList(syms, 3);
  193.                 prot = SymbolListViews.translate(codons,RNATools.getGeneticCode("UNIVERSAL"));
  194.                 // System.out.println(syms.seqString());
  195.                 output.add(SequenceTools.createSequence(prot, "", seq.getName()
  196.                 + "TranslationFrame: +" + i,Annotation.EMPTY_ANNOTATION));
  197.                 syms = RNATools.reverseComplement(syms);
  198.                 prot = SymbolListViews.translate(codons,RNATools.getGeneticCode("UNIVERSAL"));
  199.                 output.add(SequenceTools.createSequence(prot, "", seq.getName()
  200.                 + " TranslationFrame: -" + i,Annotation.EMPTY_ANNOTATION));
  201.             }catch(IllegalAlphabetException ex){
  202.                 ex.printStackTrace();
  203.             }catch(Exception ex){
  204.                 ex.printStackTrace();
  205.             }
  206.         }
  207.         for(int i = 0;i < output.size();i++){
  208.             for(int x = 0;x < output.size();x++){
  209.                 if(output.get(i).seqString().length() > output.get(x).seqString().length()){
  210.                     temp = output.get(i);
  211.                 }
  212.             }
  213.         }
  214.     return temp;
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement