JAL-2629 add ability to build a HMM from a group
[jalview.git] / src / jalview / hmmer / HMMBuildThread.java
1 package jalview.hmmer;
2
3 import jalview.bin.Cache;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.SequenceGroup;
6 import jalview.datamodel.SequenceI;
7 import jalview.gui.AlignFrame;
8 import jalview.gui.Preferences;
9 import jalview.io.DataSourceType;
10 import jalview.io.FileFormat;
11 import jalview.util.MessageManager;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.util.List;
17
18 import javax.swing.JOptionPane;
19
20 public class HMMBuildThread implements Runnable
21 {
22   HMMERCommands cmds = new HMMERCommands();
23   AlignFrame af;
24   AlignmentI alignment;
25
26   SequenceGroup group;
27
28   boolean canRun = true;
29
30   File hmmTemp = null;
31
32   File stoTemp = null;
33
34   long barID;
35   
36   public HMMBuildThread(AlignFrame af)
37   {
38     this.af = af;
39     if (af.getViewport().getSelectionGroup() != null)
40     {
41       group = af.getViewport().getSelectionGroup();
42     }
43     alignment = af.getViewport().getAlignment();
44
45   }
46
47   @Override
48   public void run()
49   {
50     barID = System.currentTimeMillis();
51     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
52             barID);
53     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
54     if (alignment == null && group == null)
55     {
56       JOptionPane.showMessageDialog(af,
57               MessageManager.getString("label.no_sequence_data"));
58       return;
59     }
60     try
61     {
62       hmmTemp = File.createTempFile("hmm", ".hmm");
63       hmmTemp.deleteOnExit();
64       stoTemp = File.createTempFile("output", ".sto");
65       stoTemp.deleteOnExit();
66     } catch (IOException e1)
67     {
68       e1.printStackTrace();
69     }
70
71     try
72     {
73     try
74     {
75         List<SequenceI> seqs = alignment
76                 .getHMMConsensusSequences(true);
77         cmds.setHmmSeqs(seqs);
78         if (group != null)
79         {
80           SequenceI[] array = group.getSelectionAsNewSequences(alignment);
81
82           cmds.exportData(array, stoTemp, null, null);
83         }
84         else
85         {
86           if (!alignment.isAligned())
87           {
88             alignment.padGaps();
89           }
90           cmds.exportData(alignment.getSequencesArray(), stoTemp, null,
91                   null);
92         }
93     } catch (FileNotFoundException e)
94     {
95       // TODO Auto-generated catch block
96       e.printStackTrace();
97
98     }
99     try
100     {
101       runCommand();
102     } catch (IOException | InterruptedException e)
103     {
104       // TODO Auto-generated catch block
105       e.printStackTrace();
106     }
107     try
108     {
109
110       importData();
111     } catch (IOException | InterruptedException e)
112     {
113       // TODO Auto-generated catch block
114       e.printStackTrace();
115     }
116     } catch (Exception e)
117     {
118       e.printStackTrace();
119     } finally
120     {
121       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
122               barID);
123     }
124   }
125
126   
127
128   
129   private void runCommand() throws IOException, InterruptedException
130   {
131     final String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME
132             + af.getName() + cmds.SPACE + hmmTemp.getAbsolutePath()
133             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
134     cmds.runCommand(command);
135   }
136   
137   private void importData() throws IOException, InterruptedException
138   {
139     cmds.addHMMConsensusSequences(af);
140     af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE,
141             FileFormat.HMMER3, null);
142     hmmTemp.delete();
143     stoTemp.delete();
144   }
145   
146   public boolean canRun()
147   {
148     return canRun;
149   }
150   
151   
152  
153 }