2fad4265ea7e2c51eb72f8576a5393651c9a6062
[jalview.git] / src / jalview / hmmer / Search.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.SequenceI;
7 import jalview.gui.AlignFrame;
8 import jalview.util.FileUtils;
9 import jalview.util.MessageManager;
10 import jalview.ws.params.ArgumentI;
11
12 import java.io.BufferedReader;
13 import java.io.File;
14 import java.io.FileReader;
15 import java.io.IOException;
16 import java.util.Hashtable;
17 import java.util.List;
18 import java.util.Scanner;
19
20 public abstract class Search extends HmmerCommand
21 {
22
23   static final String JACKHMMER = "jackhmmer";
24
25   static final String HMMSEARCH = "hmmsearch";
26
27   boolean realign = false;
28
29   boolean trim = false;
30
31   SequenceI[] seqs;
32
33   String databaseName;
34
35   boolean searchAlignment = true;
36
37   Hashtable sequencesHash;
38
39   public Search(AlignFrame alignFrame, List<ArgumentI> args)
40   {
41     super(alignFrame, args);
42   }
43
44   @Override
45   public void run()
46   {
47   }
48
49   /**
50    * Reads in the scores table output by jackhmmer and adds annotation to
51    * sequences for E-value and bit score
52    * 
53    * @param inputTableTemp
54    * @throws IOException
55    */
56   void readTable(File inputTableTemp) throws IOException
57   {
58     BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
59     String line = "";
60     while (!line.startsWith("Query:"))
61     {
62       line = br.readLine();
63     }
64     while (!line.contains("-------"))
65     {
66       line = br.readLine();
67     }
68     line = br.readLine();
69
70     int index = 0;
71
72     while (!"".equals(line) && line != null)
73     {
74       if ("  ------ inclusion threshold ------".equals(line))
75       {
76         line = br.readLine();
77         continue;
78       }
79
80       Scanner scanner = new Scanner(line);
81       String evalue = scanner.next();
82
83       if (evalue.equals("+") || evalue.equals("-"))
84       {
85         evalue = scanner.next();
86       }
87
88
89       String score = scanner.next();
90       checkSequenceOrder(index, scanner);
91       SequenceI seq = seqs[index];
92       addScoreAnnotations(evalue, score, seq);
93       scanner.close();
94       line = br.readLine();
95       index++;
96     }
97
98     br.close();
99   }
100
101   void checkSequenceOrder(int index, Scanner scanner)
102   {
103     String seqName = null;
104
105     for (int i = 0; i < 7; i++)
106     {
107       seqName = scanner.next();
108     }
109
110     if (!seqs[index].getName().equals(seqName))
111     {
112       SequenceI temp = seqs[index];
113
114       for (int j = 0; j < seqs.length; j++)
115       {
116         if (seqs[j].getName().equals(seqName))
117         {
118           seqs[index] = seqs[j];
119           seqs[j] = temp;
120           break;
121         }
122       }
123     }
124   }
125
126   void addScoreAnnotations(String eValue, String bitScore, SequenceI seq)
127   {
128     String label = "Search Scores";
129     String description = "Full sequence bit score and E-Value";
130
131     try
132     {
133       AlignmentAnnotation annot = new AlignmentAnnotation(label,
134               description, null);
135
136       annot.label = label;
137       annot.description = description;
138
139       annot.setCalcId(JACKHMMER);
140
141       double dEValue = Double.parseDouble(eValue);
142       annot.setEValue(dEValue);
143
144       double dBitScore = Double.parseDouble(bitScore);
145       annot.setBitScore(dBitScore);
146
147       annot.setSequenceRef(seq);
148       seq.addAlignmentAnnotation(annot);
149
150     } catch (NumberFormatException e)
151     {
152       System.err.println("Error parsing " + label + " from " + eValue
153               + " & " + bitScore);
154     }
155   }
156
157   void buildArguments(List<String> args, File searchOutputFile,
158           File hitsAlignmentFile, File queryFile) throws IOException
159   {
160     args.add("-o");
161     args.add(getFilePath(searchOutputFile, true));
162     args.add("-A");
163     args.add(getFilePath(hitsAlignmentFile, true));
164
165     File databaseFile = null;
166
167     boolean useEvalueCutoff = false;
168     boolean useScoreCutoff = false;
169     String seqReportingEvalueCutoff = null;
170     String domReportingEvalueCutoff = null;
171     String seqReportingScoreCutoff = null;
172     String domReportingScoreCutoff = null;
173     String seqInclusionEvalueCutoff = null;
174     String domInclusionEvalueCutoff = null;
175     String seqInclusionScoreCutoff = null;
176     String domInclusionScoreCutoff = null;
177     databaseName = "Alignment";
178
179     if (params != null)
180     {
181       for (ArgumentI arg : params)
182       {
183         String name = arg.getName();
184
185         if (MessageManager.getString(REPORTING_CUTOFF_KEY).equals(name))
186         {
187           if (MessageManager.getString(CUTOFF_EVALUE)
188                   .equals(arg.getValue()))
189           {
190             useEvalueCutoff = true;
191           }
192           else if (MessageManager.getString(CUTOFF_SCORE)
193                   .equals(arg.getValue()))
194           {
195             useScoreCutoff = true;
196           }
197         }
198         else if (MessageManager.getString(REPORTING_SEQ_EVALUE_KEY)
199                 .equals(name))
200         {
201           seqReportingEvalueCutoff = arg.getValue();
202         }
203         else if (MessageManager.getString(REPORTING_SEQ_SCORE_KEY)
204                 .equals(name))
205         {
206           seqReportingScoreCutoff = arg.getValue();
207         }
208         else if (MessageManager.getString(REPORTING_DOM_EVALUE_KEY)
209                 .equals(name))
210         {
211           domReportingEvalueCutoff = arg.getValue();
212         }
213         else if (MessageManager.getString(REPORTING_DOM_SCORE_KEY)
214                 .equals(name))
215         {
216           domReportingScoreCutoff = arg.getValue();
217         }
218         else if (MessageManager.getString(INCLUSION_SEQ_EVALUE_KEY)
219                 .equals(name))
220         {
221           seqInclusionEvalueCutoff = arg.getValue();
222         }
223         else if (MessageManager.getString(INCLUSION_SEQ_SCORE_KEY)
224                 .equals(name))
225         {
226           seqInclusionScoreCutoff = arg.getValue();
227         }
228         else if (MessageManager.getString(INCLUSION_DOM_EVALUE_KEY)
229                 .equals(name))
230         {
231           domInclusionEvalueCutoff = arg.getValue();
232         }
233         else if (MessageManager.getString(INCLUSION_DOM_SCORE_KEY)
234                 .equals(name))
235         {
236           domInclusionScoreCutoff = arg.getValue();
237         }
238         else if (MessageManager.getString(DATABASE_KEY).equals(name))
239         {
240           databaseFile = new File(arg.getValue());
241           if (!arg.getValue().isEmpty())
242           {
243             searchAlignment = false;
244           }
245         }
246       }
247     }
248
249     if (useEvalueCutoff)
250     {
251       args.add("-E");
252       args.add(seqReportingEvalueCutoff);
253       args.add("--domE");
254       args.add(domReportingEvalueCutoff);
255
256       args.add("--incE");
257       args.add(seqInclusionEvalueCutoff);
258       args.add("--incdomE");
259       args.add(domInclusionEvalueCutoff);
260     }
261     else if (useScoreCutoff)
262     {
263       args.add("-T");
264       args.add(seqReportingScoreCutoff);
265       args.add("--domT");
266       args.add(domReportingScoreCutoff);
267
268       args.add("--incT");
269       args.add(seqInclusionEvalueCutoff);
270       args.add("--incdomT");
271       args.add(domInclusionEvalueCutoff);
272     }
273
274     // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
275     // .equals(dbPath))
276     if (searchAlignment)
277     {
278       /*
279        * no external database specified for search, so
280        * export current alignment as 'database' to search
281        */
282       databaseFile = FileUtils.createTempFile("database", ".sto");
283       AlignmentI al = af.getViewport().getAlignment();
284       AlignmentI copy = new Alignment(al);
285
286       deleteHmmSequences(copy);
287
288       if (searchAlignment)
289       {
290         sequencesHash = stashSequences(copy.getSequencesArray());
291       }
292
293       exportStockholm(copy.getSequencesArray(), databaseFile, null);
294     }
295
296     args.add(getFilePath(queryFile, true));
297     args.add(getFilePath(databaseFile, true));
298   }
299 }