JAL-2629 fix for addHMMConsensus failing if no HMM sequences found
[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       String output = file.print(seqs, false);
140       PrintWriter writer = new PrintWriter(stoLocation);
141       writer.println(output);
142       writer.close();
143     }
144
145     if (hmm != null)
146     {
147       HMMFile file = new HMMFile(hmm);
148       PrintWriter writer = new PrintWriter(hmmLocation);
149       writer.print(file.print());
150       writer.close();
151     }
152   }
153
154   /**
155    * Adds any HMM sequences removed before submitting the alignment as a job
156    * back into the alignment.
157    * 
158    * @param af
159    */
160   public void addHMMConsensusSequences(AlignFrame af)
161   {
162     AlignmentI al = af.getViewport().getAlignment();
163     if (hmmSeqs == null || hmmSeqs.size() < 1)
164     {
165       return;
166     }
167     for (SequenceI seq : hmmSeqs)
168     {
169       Integer position = seq.getPreviousPosition();
170       al.getSequences().add(position, seq);
171     }
172     af.getViewport().setAlignment(al);
173     af.alignPanel.adjustAnnotationHeight();
174     af.getViewport().updateSequenceIdColours();
175     af.buildSortByAnnotationScoresMenu();
176     af.getViewport().initInformation();
177   }
178
179   /**
180    * Returns the list of HMM sequences removed
181    * 
182    * @return
183    */
184   public List<SequenceI> getHmmSeqs()
185   {
186     return hmmSeqs;
187   }
188
189   /**
190    * Sets the list of removed HMM sequences
191    * 
192    * @param hmmSeqs
193    */
194   public void setHmmSeqs(List<SequenceI> hmmSeqs)
195   {
196     this.hmmSeqs = hmmSeqs;
197   }
198 }