63236ea0c157ef2cee1b9434b9f9bc86bc68d300
[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
28   SequenceGroup group;
29
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     }
45     alignment = af.getViewport().getAlignment();
46
47   }
48
49   @Override
50   public void run()
51   {
52     barID = System.currentTimeMillis();
53     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
54             barID);
55     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
56     if (alignment == null && group == null)
57     {
58       JOptionPane.showMessageDialog(af,
59               MessageManager.getString("label.no_sequence_data"));
60       return;
61     }
62     try
63     {
64       hmmTemp = File.createTempFile("hmm", ".hmm");
65       hmmTemp.deleteOnExit();
66       stoTemp = File.createTempFile("output", ".sto");
67       stoTemp.deleteOnExit();
68     } catch (IOException e1)
69     {
70       e1.printStackTrace();
71     }
72
73     try
74     {
75     try
76     {
77         SequenceI[] array;
78         List<SequenceI> seqs = alignment
79                 .getHMMConsensusSequences(true);
80         cmds.setHmmSeqs(seqs);
81         if (group != null)
82         {
83           array = group.getSelectionAsNewSequences(alignment);
84         }
85         else
86         {
87           if (!alignment.isAligned())
88           {
89             alignment.padGaps();
90           }
91           array = alignment.getSequencesArray();
92         }
93         SequenceI[] newArr = new SequenceI[array.length];
94         int index = 0;
95         for (SequenceI seq : array)
96         {
97           newArr[index] = new Sequence(seq);
98           index++;
99         }
100         cmds.uniquifySequences(newArr);
101         cmds.exportData(newArr, stoTemp, null, null);
102         jalview.analysis.SeqsetUtils.deuniquify(cmds.hash, array);
103
104     } catch (FileNotFoundException e)
105     {
106       // TODO Auto-generated catch block
107       e.printStackTrace();
108
109     }
110     try
111     {
112         boolean ran = runCommand();
113         if (!ran)
114         {
115           JvOptionPane.showInternalMessageDialog(af,
116                   MessageManager.getString("warn.hmmbuild_failed"));
117           return;
118         }
119     } catch (IOException | InterruptedException e)
120     {
121       // TODO Auto-generated catch block
122       e.printStackTrace();
123     }
124     try
125     {
126
127       importData();
128     } catch (IOException | InterruptedException e)
129     {
130       // TODO Auto-generated catch block
131       e.printStackTrace();
132     }
133     } catch (Exception e)
134     {
135       e.printStackTrace();
136     } finally
137     {
138       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
139               barID);
140     }
141   }
142
143   
144
145   
146   private boolean runCommand() throws IOException, InterruptedException
147   {
148     File file = new File(cmds.HMMERFOLDER + "/binaries/hmmbuild.exe");
149     if (!file.canExecute())
150     {
151       return false;
152     }
153     String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME
154             + af.getName() + cmds.SPACE;
155     if (!alignment.isNucleotide())
156     {
157       command += cmds.FORCEAMINO; // TODO check for rna
158     }
159     else
160     {
161       command += cmds.FORCEDNA;
162     }
163
164     command += hmmTemp.getAbsolutePath()
165             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
166     return cmds.runCommand(command);
167   }
168   
169   private void importData() throws IOException, InterruptedException
170   {
171     cmds.addHMMConsensusSequences(af);
172     af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE,
173             FileFormat.HMMER3, null);
174     hmmTemp.delete();
175     stoTemp.delete();
176   }
177   
178   public boolean canRun()
179   {
180     return canRun;
181   }
182   
183   
184  
185 }