71a7d10f06f15d7bb9cba918dc98b16db05c086d
[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 = "/hmmalign ";
28
29   public final String HMMBUILD = "/hmmbuild ";
30
31   public final String HMMSEARCH = "/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 void runCommand(String command)
77           throws IOException, InterruptedException
78   {
79     final Process p = Runtime.getRuntime().exec(command);
80
81     new Thread(new Runnable()
82     {
83       @Override
84       public void run()
85       {
86         BufferedReader input = new BufferedReader(
87                 new InputStreamReader(p.getInputStream()));
88         String line = null;
89
90         try
91         {
92           while ((line = input.readLine()) != null)
93           {
94             System.out.println(line);
95           }
96         } catch (IOException e)
97         {
98           e.printStackTrace();
99         }
100       }
101     }).start();
102
103     p.waitFor();
104   }
105
106   /**
107    * Exports an alignment to the buffer location in Jalview.
108    * 
109    * @param alignment
110    * @throws IOException
111    */
112   public void exportData(SequenceI[] seqs,
113           File stoLocation, HiddenMarkovModel hmm, File hmmLocation)
114           throws IOException
115   {
116     if (seqs != null)
117     {
118       StockholmFile file = new StockholmFile(new Alignment(seqs));
119       file.setSeqs(seqs);
120       String output = file.print();
121       PrintWriter writer = new PrintWriter(stoLocation);
122       writer.println(output);
123       writer.close();
124     }
125
126     if (hmm != null)
127     {
128       HMMFile file = new HMMFile(hmm);
129       file.exportFile(hmmLocation);
130     }
131   }
132
133   public void addHMMConsensusSequences(AlignFrame af)
134   {
135     AlignmentI al = af.getViewport().getAlignment();
136     for (SequenceI seq : hmmSeqs)
137     {
138       Integer position = seq.getPreviousPosition();
139       al.getSequences().add(position, seq);
140     }
141     af.getViewport().setAlignment(al);
142     af.alignPanel.adjustAnnotationHeight();
143     af.getViewport().updateSequenceIdColours();
144     af.buildSortByAnnotationScoresMenu();
145   }
146
147   public List<SequenceI> getHmmSeqs()
148   {
149     return hmmSeqs;
150   }
151
152   public void setHmmSeqs(List<SequenceI> hmmSeqs)
153   {
154     this.hmmSeqs = hmmSeqs;
155   }
156 }