JAL-2629 fix for hmmbuild not loading a HMM onto the current frame
[jalview.git] / src / jalview / hmmer / HMMERCommands.java
1 package jalview.hmmer;
2
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.HiddenMarkovModel;
6 import jalview.datamodel.SequenceI;
7 import jalview.gui.AlignFrame;
8 import jalview.io.HMMFile;
9 import jalview.io.StockholmFile;
10
11 import java.io.BufferedReader;
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.PrintWriter;
16 import java.util.Hashtable;
17 import java.util.List;
18
19 /**
20  * Contains multiple commands and methods frequently used to run hmmbuild,
21  * hmmalign and hmmsearch
22  * 
23  * @author TZVanaalten
24  *
25  */
26 public class HMMERCommands
27 {
28   // Path of hmmer binaries directory
29   String HMMERFOLDER = "/Documents/";
30
31   public String JALVIEWDIRECTORY = System.getProperty("user.dir")
32           + "/";
33
34   public final String HMMALIGN = "/hmmalign ";
35
36   public final String HMMBUILD = "/hmmbuild ";
37
38   public final String HMMSEARCH = "/hmmsearch ";
39
40   public String OUTPUTALIGNMENT;
41
42   public final String NAME = "-n ";
43
44   public final String SPACE = " ";
45
46   public final String ALLCOL = "--allcol ";
47
48   public final String TRIM = "--trim ";
49
50   public final String FORCEAMINO = "--amino ";
51
52   public final String FORCEDNA = "--dna ";
53
54   public final String FORCERNA = "--rna ";
55
56   Hashtable hash = new Hashtable();
57
58   List<SequenceI> hmmSeqs;
59
60   /**
61    * Uniquifies the sequences when exporting and stores their details in a
62    * hashtable.
63    * 
64    * @param seqs
65    */
66   public void uniquifySequences(SequenceI[] seqs)
67   {
68     hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
69   }
70
71   /**
72    * Recover the sequence data lost by uniquifying.
73    * 
74    * @param seqs
75    */
76   public void recoverSequenceNames(SequenceI[] seqs)
77   {
78     jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
79   }
80
81   /**
82    * Runs a command in the command line.
83    * 
84    * @param command
85    * @throws IOException
86    * @throws InterruptedException
87    */
88   public boolean runCommand(String command)
89           throws IOException, InterruptedException
90   {
91     try
92     {
93     final Process p = Runtime.getRuntime().exec(command);
94
95     new Thread(new Runnable()
96     {
97       @Override
98       public void run()
99       {
100         BufferedReader input = new BufferedReader(
101                 new InputStreamReader(p.getInputStream()));
102         String line = null;
103
104         try
105         {
106           while ((line = input.readLine()) != null)
107           {
108             System.out.println(line);
109           }
110         } catch (IOException e)
111         {
112           e.printStackTrace();
113         }
114       }
115     }).start();
116
117     p.waitFor();
118     } catch (Exception e)
119     {
120       e.printStackTrace();
121       return false;
122     }
123     return true;
124   }
125
126   /**
127    * Exports an alignment and/or HMM to the specified file.
128    * 
129    * @param alignment
130    * @throws IOException
131    */
132   public void exportData(SequenceI[] seqs,
133           File stoLocation, HiddenMarkovModel hmm, File hmmLocation)
134           throws IOException
135   {
136     if (seqs != null)
137     {
138       StockholmFile file = new StockholmFile(new Alignment(seqs));
139       boolean removeAnnots = false;
140       if (hmm == null)
141       {
142         removeAnnots = true;
143       }
144       String output = file.print(seqs, false, removeAnnots);
145       PrintWriter writer = new PrintWriter(stoLocation);
146       writer.println(output);
147       writer.close();
148     }
149
150     if (hmm != null)
151     {
152       HMMFile file = new HMMFile(hmm);
153       file.exportFile(hmmLocation);
154     }
155   }
156
157   /**
158    * Adds any HMM sequences removed before submitting the alignment as a job
159    * back into the alignment.
160    * 
161    * @param af
162    */
163   public void addHMMConsensusSequences(AlignFrame af)
164   {
165     AlignmentI al = af.getViewport().getAlignment();
166     for (SequenceI seq : hmmSeqs)
167     {
168       Integer position = seq.getPreviousPosition();
169       al.getSequences().add(position, seq);
170     }
171     af.getViewport().setAlignment(al);
172     af.alignPanel.adjustAnnotationHeight();
173     af.getViewport().updateSequenceIdColours();
174     af.buildSortByAnnotationScoresMenu();
175   }
176
177   /**
178    * Returns the list of HMM sequences removed
179    * 
180    * @return
181    */
182   public List<SequenceI> getHmmSeqs()
183   {
184     return hmmSeqs;
185   }
186
187   /**
188    * Sets the list of removed HMM sequences
189    * 
190    * @param hmmSeqs
191    */
192   public void setHmmSeqs(List<SequenceI> hmmSeqs)
193   {
194     this.hmmSeqs = hmmSeqs;
195   }
196 }