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