3b5472a620dda7ea3b3ef3b2897d04378e7e9757
[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 String HMMALIGN = "/hmmalign ";
28
29   public String HMMBUILD = "/hmmbuild ";
30
31   public 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 String NAME = "-n ";
42
43   public String SPACE = " ";
44
45   public String ALLCOL = "--allcol ";
46
47   public String TRIM = "--trim ";
48
49   Hashtable hash = new Hashtable();
50
51   List<SequenceI> hmmSeqs;
52
53   public void uniquifySequences(SequenceI[] seqs)
54   {
55     hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true);
56   }
57
58   public void recoverSequenceNames(SequenceI[] seqs)
59   {
60     jalview.analysis.SeqsetUtils.deuniquify(hash, seqs);
61   }
62
63   /**
64    * Runs a command in the terminal.
65    * 
66    * @param command
67    * @throws IOException
68    * @throws InterruptedException
69    */
70   public void runCommand(String command)
71           throws IOException, InterruptedException
72   {
73     final Process p = Runtime.getRuntime().exec(command);
74
75     new Thread(new Runnable()
76     {
77       @Override
78       public void run()
79       {
80         BufferedReader input = new BufferedReader(
81                 new InputStreamReader(p.getInputStream()));
82         String line = null;
83
84         try
85         {
86           while ((line = input.readLine()) != null)
87           {
88             System.out.println(line);
89           }
90         } catch (IOException e)
91         {
92           e.printStackTrace();
93         }
94       }
95     }).start();
96
97     p.waitFor();
98   }
99
100   /**
101    * Exports an alignment to the buffer location in Jalview.
102    * 
103    * @param alignment
104    * @throws IOException
105    */
106   public void exportData(SequenceI[] seqs,
107           File stoLocation, HiddenMarkovModel hmm, File hmmLocation)
108           throws IOException
109   {
110     if (seqs != null)
111     {
112       StockholmFile file = new StockholmFile(new Alignment(seqs));
113       file.setSeqs(seqs);
114       String output = file.print();
115       PrintWriter writer = new PrintWriter(stoLocation);
116       writer.println(output);
117       writer.close();
118     }
119
120     if (hmm != null)
121     {
122       HMMFile file = new HMMFile(hmm);
123       file.exportFile(hmmLocation);
124     }
125   }
126
127   public void addHMMConsensusSequences(AlignFrame af)
128   {
129     AlignmentI al = af.getViewport().getAlignment();
130     for (SequenceI seq : hmmSeqs)
131     {
132       Integer position = seq.getPreviousPosition();
133       al.getSequences().add(position, seq);
134     }
135     af.getViewport().setAlignment(al);
136     af.alignPanel.adjustAnnotationHeight();
137     af.getViewport().updateSequenceIdColours();
138     af.buildSortByAnnotationScoresMenu();
139   }
140
141   public List<SequenceI> getHmmSeqs()
142   {
143     return hmmSeqs;
144   }
145
146   public void setHmmSeqs(List<SequenceI> hmmSeqs)
147   {
148     this.hmmSeqs = hmmSeqs;
149   }
150 }