JAL-2629 fix file loading issues on Mac
[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 + "/binaries/hmmbuild");
153     if (!file.canExecute())
154     {
155       return false;
156     }
157     String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME;
158     if (forGroup)
159     {
160       command += group.getName();
161     }
162     else
163     {
164       String name = af.getName();
165       if (name == null)
166       {
167         name = "Alignment";
168       }
169       command += name;
170     }
171     command += cmds.SPACE;
172     if (!alignment.isNucleotide())
173     {
174       command += cmds.FORCEAMINO; // TODO check for rna
175     }
176     else
177     {
178       command += cmds.FORCEDNA;
179     }
180
181     command += hmmTemp.getAbsolutePath()
182             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
183     return cmds.runCommand(command);
184   }
185   
186   private void importData() throws IOException, InterruptedException
187   {
188     cmds.addHMMConsensusSequences(af);
189     FileLoader loader = new FileLoader();
190     loader.LoadFileOntoAlignmentWaitTillLoaded(af.getViewport(),
191             hmmTemp.getAbsolutePath(), DataSourceType.FILE,
192             FileFormat.HMMER3);
193     hmmTemp.delete();
194     stoTemp.delete();
195   }
196   
197   public boolean canRun()
198   {
199     return canRun;
200   }
201   
202   
203  
204 }