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