ed074a8f54389788cd67934db291c3639bdb27a1
[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("label.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
96         SequenceI[] newArr = new SequenceI[array.length];
97         int index = 0;
98         for (SequenceI seq : array)
99         {
100           newArr[index] = new Sequence(seq);
101           index++;
102         }
103
104         cmds.uniquifySequences(newArr);
105         cmds.exportData(newArr, stoTemp, null, null);
106         jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
107
108     } catch (FileNotFoundException e)
109     {
110       // TODO Auto-generated catch block
111       e.printStackTrace();
112
113     }
114     try
115     {
116         boolean ran = runCommand();
117         if (!ran)
118         {
119           JvOptionPane.showInternalMessageDialog(af,
120                   MessageManager.getString("warn.hmmbuild_failed"));
121           return;
122         }
123     } catch (IOException | InterruptedException e)
124     {
125       // TODO Auto-generated catch block
126       e.printStackTrace();
127     }
128     try
129     {
130
131       importData();
132     } catch (IOException | InterruptedException e)
133     {
134       // TODO Auto-generated catch block
135       e.printStackTrace();
136     }
137     } catch (Exception e)
138     {
139       e.printStackTrace();
140     } finally
141     {
142       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
143               barID);
144     }
145   }
146
147   
148
149   
150   private boolean runCommand() throws IOException, InterruptedException
151   {
152     File file = new File(cmds.HMMERFOLDER + "/hmmbuild");
153     if (!file.canExecute())
154     {
155       file = new File(cmds.HMMERFOLDER + "/hmmbuild.exe");
156       {
157         if (!file.canExecute())
158         {
159           return false;
160         }
161       }
162     }
163     String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME;
164     if (forGroup)
165     {
166       command += group.getName();
167     }
168     else
169     {
170       String name = af.getName();
171       if (name == null)
172       {
173         name = "Alignment";
174       }
175       command += name;
176     }
177     command += cmds.SPACE;
178     if (!alignment.isNucleotide())
179     {
180       command += cmds.FORCEAMINO; // TODO check for rna
181     }
182     else
183     {
184       command += cmds.FORCEDNA;
185     }
186
187     command += hmmTemp.getAbsolutePath()
188             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
189     return cmds.runCommand(command);
190   }
191   
192   private void importData() throws IOException, InterruptedException
193   {
194     cmds.addHMMConsensusSequences(af);
195     FileLoader loader = new FileLoader();
196     loader.LoadFileOntoAlignmentWaitTillLoaded(af.getViewport(),
197             hmmTemp.getAbsolutePath(), DataSourceType.FILE,
198             FileFormat.HMMER3);
199     hmmTemp.delete();
200     stoTemp.delete();
201   }
202   
203   public boolean canRun()
204   {
205     return canRun;
206   }
207   
208   
209  
210 }