JAL-2629 hmmalign now correctly creates a new frame
[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.SequenceI;
6 import jalview.gui.AlignFrame;
7 import jalview.gui.Preferences;
8 import jalview.io.DataSourceType;
9 import jalview.io.FileFormat;
10 import jalview.util.MessageManager;
11
12 import java.io.File;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.util.Map;
16
17 public class HMMBuildThread implements Runnable
18 {
19   HMMERCommands cmds = new HMMERCommands();
20   AlignFrame af;
21   AlignmentI alignment;
22
23   File hmmTemp = null;
24
25   File stoTemp = null;
26
27   long barID;
28
29   Map<Integer, SequenceI> hmmSeqs;
30   
31   public HMMBuildThread(AlignFrame af)
32   {
33     this.af = af;
34     alignment = af.getViewport().getAlignment();
35   }
36
37   @Override
38   public void run()
39   {
40     barID = System.currentTimeMillis();
41     System.out.println(System.getProperty("java.io.tmpdir"));
42     af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
43             barID);
44     cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH);
45     try
46     {
47       hmmTemp = File.createTempFile("hmm", ".hmm");
48       hmmTemp.deleteOnExit();
49       stoTemp = File.createTempFile("output", ".sto");
50       stoTemp.deleteOnExit();
51     } catch (IOException e1)
52     {
53       e1.printStackTrace();
54     }
55
56     try
57     {
58     try
59     {
60         hmmSeqs = alignment.getHMMConsensusSequences(true);
61         if (!alignment.isAligned())
62         {
63           alignment.padGaps();
64         }
65         cmds.exportData(alignment.getSequencesArray(), stoTemp, null, null);
66     } catch (FileNotFoundException e)
67     {
68       // TODO Auto-generated catch block
69       e.printStackTrace();
70
71     }
72     try
73     {
74       runCommand();
75     } catch (IOException | InterruptedException e)
76     {
77       // TODO Auto-generated catch block
78       e.printStackTrace();
79     }
80     try
81     {
82
83       importData();
84     } catch (IOException | InterruptedException e)
85     {
86       // TODO Auto-generated catch block
87       e.printStackTrace();
88     }
89     } catch (Exception e)
90     {
91
92     } finally
93     {
94       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
95               barID);
96     }
97   }
98
99   
100
101   
102   private void runCommand() throws IOException, InterruptedException
103   {
104     final String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME
105             + af.getName() + cmds.SPACE + hmmTemp.getAbsolutePath()
106             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
107     cmds.runCommand(command);
108   }
109   
110   private void importData() throws IOException, InterruptedException
111   {
112     af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE,
113             FileFormat.HMMER3, null);
114     for (Map.Entry<Integer, SequenceI> entry : hmmSeqs.entrySet())
115     {
116       SequenceI seq = entry.getValue();
117       Integer pos = entry.getKey();
118       cmds.addHMMConsensusSequence(af, seq, pos);
119     }
120     af.alignPanel.alignmentChanged();
121     hmmTemp.delete();
122     stoTemp.delete();
123   }
124   
125   
126   
127  
128 }