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