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