4c1748b3aa2ca383a886f66875f122add3fa64b0
[jalview.git] / src / jalview / hmmer / HMMERCommands.java
1 package jalview.hmmer;
2
3 import jalview.datamodel.Alignment;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.AnnotatedCollectionI;
7 import jalview.datamodel.HiddenMarkovModel;
8 import jalview.datamodel.SequenceI;
9 import jalview.gui.AlignFrame;
10 import jalview.io.HMMFile;
11 import jalview.io.StockholmFile;
12
13 import java.io.BufferedReader;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.PrintWriter;
18 import java.util.Hashtable;
19 import java.util.List;
20
21 /**
22  * Contains multiple commands and methods frequently used to run hmmbuild,
23  * hmmalign and hmmsearch
24  * 
25  * @author TZVanaalten
26  *
27  */
28 public class HMMERCommands
29 {
30   // Path of hmmer binaries directory
31   String HMMERFOLDER = "/Documents/";
32
33   public String JALVIEWDIRECTORY = System.getProperty("user.dir")
34           + "/";
35
36   public final String HMMALIGN = "/hmmalign ";
37
38   public final String HMMBUILD = "/hmmbuild ";
39
40   public final String HMMSEARCH = "/hmmsearch ";
41
42   public String OUTPUTALIGNMENT;
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           AnnotatedCollectionI al)
135           throws IOException
136   {
137     if (seqs != null)
138     {
139       AlignmentI newAl = new Alignment(seqs);
140       if (stoLocation != null)
141       {
142         for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
143         {
144           if (annot.label.contains("Reference") || "RF".equals(annot.label))
145           {
146             newAl.addAnnotation(annot);
147           }
148         }
149       }
150       StockholmFile file = new StockholmFile(newAl);
151       String output = file.print(seqs, false);
152       PrintWriter writer = new PrintWriter(stoLocation);
153       writer.println(output);
154       writer.close();
155     }
156
157     if (hmm != null)
158     {
159       HMMFile file = new HMMFile(hmm);
160       PrintWriter writer = new PrintWriter(hmmLocation);
161       writer.print(file.print());
162       writer.close();
163     }
164   }
165
166   /**
167    * Adds any HMM sequences removed before submitting the alignment as a job
168    * back into the alignment.
169    * 
170    * @param af
171    */
172   public void addHMMConsensusSequences(AlignFrame af)
173   {
174     AlignmentI al = af.getViewport().getAlignment();
175     if (hmmSeqs == null || hmmSeqs.size() < 1)
176     {
177       return;
178     }
179     for (SequenceI seq : hmmSeqs)
180     {
181       Integer position = seq.getPreviousPosition();
182       al.getSequences().add(position, seq);
183     }
184     af.getViewport().setAlignment(al);
185     af.alignPanel.adjustAnnotationHeight();
186     af.getViewport().updateSequenceIdColours();
187     af.buildSortByAnnotationScoresMenu();
188   }
189
190   /**
191    * Returns the list of HMM sequences removed
192    * 
193    * @return
194    */
195   public List<SequenceI> getHmmSeqs()
196   {
197     return hmmSeqs;
198   }
199
200   /**
201    * Sets the list of removed HMM sequences
202    * 
203    * @param hmmSeqs
204    */
205   public void setHmmSeqs(List<SequenceI> hmmSeqs)
206   {
207     this.hmmSeqs = hmmSeqs;
208   }
209 }