JAL-2629 current hmmer commands are now executed in threads
[jalview.git] / src / jalview / hmmer / HMMBuildThread.java
1 package jalview.hmmer;
2
3 import jalview.datamodel.AlignmentI;
4 import jalview.datamodel.HiddenMarkovModel;
5 import jalview.datamodel.SequenceI;
6 import jalview.gui.AlignFrame;
7 import jalview.io.DataSourceType;
8 import jalview.io.FileFormat;
9 import jalview.util.MessageManager;
10
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.util.Map;
14
15 public class HMMBuildThread implements Runnable
16 {
17
18   AlignFrame af;
19   AlignmentI alignment;
20
21   long barID;
22
23   Map<Integer, SequenceI> hmmSeqs;
24   
25   public HMMBuildThread(AlignFrame af)
26   {
27     this.af = af;
28     alignment = af.getViewport().getAlignment();
29   }
30
31   @Override
32   public void run()
33   {
34     barID = System.currentTimeMillis();
35     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
36             barID);
37
38     try
39     {
40     try
41     {
42       exportAlignment();
43     } catch (FileNotFoundException e)
44     {
45       // TODO Auto-generated catch block
46       e.printStackTrace();
47
48     }
49     try
50     {
51       runCommand();
52     } catch (IOException | InterruptedException e)
53     {
54       // TODO Auto-generated catch block
55       e.printStackTrace();
56     }
57     try
58     {
59       importData();
60     } catch (IOException | InterruptedException e)
61     {
62       // TODO Auto-generated catch block
63       e.printStackTrace();
64     }
65     } catch (Exception e)
66     {
67
68     } finally
69     {
70       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
71               barID);
72     }
73   }
74
75   
76   private void exportAlignment() throws IOException
77   {
78     hmmSeqs = alignment.getHMMConsensusSequences(true);
79     if (!alignment.isAligned())
80     {
81       alignment.padGaps();
82     }
83     HMMERCommands.exportData(alignment, true, false,
84             new HiddenMarkovModel());
85   }
86   
87   private void runCommand() throws IOException, InterruptedException
88   {
89     final String command = HMMERCommands.HMMBUILD + HMMERCommands.NAME
90             + af.getName() + HMMERCommands.SPACE
91             + HMMERCommands.JALVIEWDIRECTORY + HMMERCommands.HMMBUFFER
92             + HMMERCommands.JALVIEWDIRECTORY + HMMERCommands.ALIGNMENTBUFFER;
93     HMMERCommands.runCommand(command);
94   }
95   
96   private void importData() throws IOException, InterruptedException
97   {
98     af.loadJalviewDataFile(HMMERCommands.HMMBUFFER, DataSourceType.FILE,
99             FileFormat.HMMER3, null);
100     for (Map.Entry<Integer, SequenceI> entry : hmmSeqs.entrySet())
101     {
102       SequenceI seq = entry.getValue();
103       Integer pos = entry.getKey();
104       HMMERCommands.addHMMConsensusSequence(af, seq, pos);
105     }
106     af.alignPanel.alignmentChanged();
107   }
108   
109   
110   
111  
112 }