4f1dbc6a7f6e1afe61215bb177211c0b5b93ccc7
[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 public class HMMERCommands
20 {
21   // Path of hmmer binaries directory
22   String HMMERFOLDER = "/Documents/";
23
24   public String JALVIEWDIRECTORY = System.getProperty("user.dir")
25           + "/";
26
27   public final String HMMALIGN = "/binaries/hmmalign ";
28
29   public final String HMMBUILD = "/binaries/hmmbuild ";
30
31   public final String HMMSEARCH = "/binaries/hmmsearch ";
32
33   public String HMMBUFFER;
34
35   public String ALIGNMENTBUFFER;
36
37   public String ALIGNMENTINPUT;
38
39   public String OUTPUTALIGNMENT;
40
41   public final String NAME = "-n ";
42
43   public final String SPACE = " ";
44
45   public final String ALLCOL = "--allcol ";
46
47   public final String TRIM = "--trim ";
48
49   public final String FORCEAMINO = "--amino ";
50
51   public final String FORCEDNA = "--dna ";
52
53   public final String FORCERNA = "--rna ";
54
55   Hashtable hash = new Hashtable();
56
57   List<SequenceI> hmmSeqs;
58
59   public void uniquifySequences(SequenceI[] seqs)
60   {
61     hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
62   }
63
64   public void recoverSequenceNames(SequenceI[] seqs)
65   {
66     jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
67   }
68
69   /**
70    * Runs a command in the terminal.
71    * 
72    * @param command
73    * @throws IOException
74    * @throws InterruptedException
75    */
76   public boolean runCommand(String command)
77           throws IOException, InterruptedException
78   {
79     try
80     {
81     final Process p = Runtime.getRuntime().exec(command);
82
83     new Thread(new Runnable()
84     {
85       @Override
86       public void run()
87       {
88         BufferedReader input = new BufferedReader(
89                 new InputStreamReader(p.getInputStream()));
90         String line = null;
91
92         try
93         {
94           while ((line = input.readLine()) != null)
95           {
96             System.out.println(line);
97           }
98         } catch (IOException e)
99         {
100           e.printStackTrace();
101         }
102       }
103     }).start();
104
105     p.waitFor();
106     } catch (Exception e)
107     {
108       e.printStackTrace();
109       return false;
110     }
111     return true;
112   }
113
114   /**
115    * Exports an alignment to the buffer location in Jalview.
116    * 
117    * @param alignment
118    * @throws IOException
119    */
120   public void exportData(SequenceI[] seqs,
121           File stoLocation, HiddenMarkovModel hmm, File hmmLocation)
122           throws IOException
123   {
124     if (seqs != null)
125     {
126       StockholmFile file = new StockholmFile(new Alignment(seqs));
127       boolean removeAnnots = false;
128       if (hmm == null)
129       {
130         removeAnnots = true;
131       }
132       String output = file.print(seqs, false, removeAnnots);
133       PrintWriter writer = new PrintWriter(stoLocation);
134       writer.println(output);
135       writer.close();
136     }
137
138     if (hmm != null)
139     {
140       HMMFile file = new HMMFile(hmm);
141       file.exportFile(hmmLocation);
142     }
143   }
144
145   public void addHMMConsensusSequences(AlignFrame af)
146   {
147     AlignmentI al = af.getViewport().getAlignment();
148     for (SequenceI seq : hmmSeqs)
149     {
150       Integer position = seq.getPreviousPosition();
151       al.getSequences().add(position, seq);
152     }
153     af.getViewport().setAlignment(al);
154     af.alignPanel.adjustAnnotationHeight();
155     af.getViewport().updateSequenceIdColours();
156     af.buildSortByAnnotationScoresMenu();
157   }
158
159   public List<SequenceI> getHmmSeqs()
160   {
161     return hmmSeqs;
162   }
163
164   public void setHmmSeqs(List<SequenceI> hmmSeqs)
165   {
166     this.hmmSeqs = hmmSeqs;
167   }
168 }