JAL-2629 fix files permission issue 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.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         cmds.exportData(alignment, stoTemp, null, null);
62     } catch (FileNotFoundException e)
63     {
64       // TODO Auto-generated catch block
65       e.printStackTrace();
66
67     }
68     try
69     {
70       runCommand();
71     } catch (IOException | InterruptedException e)
72     {
73       // TODO Auto-generated catch block
74       e.printStackTrace();
75     }
76     try
77     {
78
79       importData();
80     } catch (IOException | InterruptedException e)
81     {
82       // TODO Auto-generated catch block
83       e.printStackTrace();
84     }
85     } catch (Exception e)
86     {
87
88     } finally
89     {
90       af.setProgressBar(MessageManager.getString("status.running_hmmbuild"),
91               barID);
92     }
93   }
94
95   
96
97   
98   private void runCommand() throws IOException, InterruptedException
99   {
100     final String command = cmds.HMMERFOLDER + cmds.HMMBUILD + cmds.NAME
101             + af.getName() + cmds.SPACE + hmmTemp.getAbsolutePath()
102             + cmds.SPACE + stoTemp.getAbsolutePath() + cmds.SPACE;
103     cmds.runCommand(command);
104   }
105   
106   private void importData() throws IOException, InterruptedException
107   {
108     af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE,
109             FileFormat.HMMER3, null);
110     for (Map.Entry<Integer, SequenceI> entry : hmmSeqs.entrySet())
111     {
112       SequenceI seq = entry.getValue();
113       Integer pos = entry.getKey();
114       cmds.addHMMConsensusSequence(af, seq, pos);
115     }
116     af.alignPanel.alignmentChanged();
117     hmmTemp.delete();
118     stoTemp.delete();
119   }
120   
121   
122   
123  
124 }