JAL-2629 add adjustable parameters to hmmbuild
[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 SPACE = " ";
43
44   public final String ALLCOL = "--allcol ";
45
46   public final String TRIM = "--trim ";
47
48   public final String FORCEAMINO = "--amino ";
49
50   public final String FORCEDNA = "--dna ";
51
52   public final String FORCERNA = "--rna ";
53
54   Hashtable hash = new Hashtable();
55
56   List<SequenceI> hmmSeqs;
57
58   /**
59    * Uniquifies the sequences when exporting and stores their details in a
60    * hashtable.
61    * 
62    * @param seqs
63    */
64   public void uniquifySequences(SequenceI[] seqs)
65   {
66     hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
67   }
68
69   /**
70    * Recover the sequence data lost by uniquifying.
71    * 
72    * @param seqs
73    */
74   public void recoverSequenceNames(SequenceI[] seqs)
75   {
76     jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
77   }
78
79   /**
80    * Runs a command in the command line.
81    * 
82    * @param command
83    * @throws IOException
84    * @throws InterruptedException
85    */
86   public boolean runCommand(String command)
87           throws IOException, InterruptedException
88   {
89     try
90     {
91     final Process p = Runtime.getRuntime().exec(command);
92
93     new Thread(new Runnable()
94     {
95       @Override
96       public void run()
97       {
98         BufferedReader input = new BufferedReader(
99                 new InputStreamReader(p.getInputStream()));
100         String line = null;
101
102         try
103         {
104           while ((line = input.readLine()) != null)
105           {
106             System.out.println(line);
107           }
108         } catch (IOException e)
109         {
110           e.printStackTrace();
111         }
112       }
113     }).start();
114
115     p.waitFor();
116     } catch (Exception e)
117     {
118       e.printStackTrace();
119       return false;
120     }
121     return true;
122   }
123
124   /**
125    * Exports an alignment and/or HMM to the specified file.
126    * 
127    * @param alignment
128    * @throws IOException
129    */
130   public void exportData(SequenceI[] seqs,
131           File stoLocation, HiddenMarkovModel hmm, File hmmLocation)
132           throws IOException
133   {
134     if (seqs != null)
135     {
136       StockholmFile file = new StockholmFile(new Alignment(seqs));
137       String output = file.print(seqs, false);
138       PrintWriter writer = new PrintWriter(stoLocation);
139       writer.println(output);
140       writer.close();
141     }
142
143     if (hmm != null)
144     {
145       HMMFile file = new HMMFile(hmm);
146       PrintWriter writer = new PrintWriter(hmmLocation);
147       writer.print(file.print());
148       writer.close();
149     }
150   }
151
152   /**
153    * Adds any HMM sequences removed before submitting the alignment as a job
154    * back into the alignment.
155    * 
156    * @param af
157    */
158   public void addHMMConsensusSequences(AlignFrame af)
159   {
160     AlignmentI al = af.getViewport().getAlignment();
161     if (hmmSeqs == null || hmmSeqs.size() < 1)
162     {
163       return;
164     }
165     for (SequenceI seq : hmmSeqs)
166     {
167       Integer position = seq.getPreviousPosition();
168       al.getSequences().add(position, seq);
169     }
170     af.getViewport().setAlignment(al);
171     af.alignPanel.adjustAnnotationHeight();
172     af.getViewport().updateSequenceIdColours();
173     af.buildSortByAnnotationScoresMenu();
174   }
175
176   /**
177    * Returns the list of HMM sequences removed
178    * 
179    * @return
180    */
181   public List<SequenceI> getHmmSeqs()
182   {
183     return hmmSeqs;
184   }
185
186   /**
187    * Sets the list of removed HMM sequences
188    * 
189    * @param hmmSeqs
190    */
191   public void setHmmSeqs(List<SequenceI> hmmSeqs)
192   {
193     this.hmmSeqs = hmmSeqs;
194   }
195 }