JAL-2629 can now choose number of jackhmmer iterations
[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   void readOutputFile(File inputTableTemp) throws IOException
51   {
52     BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
53   
54   
55     String line = "";
56     while (!line.startsWith("//"))
57     {
58   
59       while (!line.startsWith(">> ") && !line.startsWith("//"))
60       {
61         line = br.readLine();
62       }
63   
64       if (line.startsWith("//"))
65       {
66         break;
67       }
68   
69       Scanner scanner = new Scanner(line);
70       String name = scanner.next();
71       name = scanner.next();
72   
73       br.readLine();
74       br.readLine();
75   
76       List<SequenceI> domains = new ArrayList<>();
77   
78       for (SequenceI seq : seqs)
79       {
80         if (seq.getName().contains(name))
81         {
82           domains.add(seq);
83         }
84       }
85   
86       if (domains.contains(getSequence()))
87       {
88         domains.remove(getSequence());
89       }
90   
91       if (domains.size() > 0)
92       {
93         readOutputTable(br, domains);
94       }
95   
96       line = br.readLine();
97     }
98   
99   }
100   
101   
102   /**
103    * Reads in the scores table output by jackhmmer and adds annotation to
104    * sequences for E-value and bit score
105    * 
106    * @param inputTableTemp
107    * @throws IOException
108    */
109   /*
110   void readOutputTable(BufferedReader br, List<SequenceI> seqs) throws IOException
111   {
112     String line = br.readLine();
113   
114     while (!"".equals(line) && line != null)
115     {
116       if ("  ------ inclusion threshold ------".equals(line))
117       {
118         line = br.readLine();
119         continue;
120       }
121   
122       Scanner scanner = new Scanner(line);
123       scanner.next();
124       scanner.next();
125       String score = scanner.next();
126   
127       scanner.next();
128   
129       String evalue = scanner.next();
130   
131       scanner.next();
132       scanner.next();
133       scanner.next();
134       scanner.next();
135   
136       int start = scanner.nextInt();
137       int end = scanner.nextInt();
138   
139       SequenceI seq = null;
140       for (SequenceI sequence : seqs)
141       {
142         if (sequence.getStart() >= start && sequence.getEnd() <= end)
143         {
144           seq = sequence;
145           break;
146         }
147       }
148   
149       if (seq != null)
150       {
151         addScoreAnnotations(evalue, score, seq);
152       }
153   
154       scanner.close();
155       line = br.readLine();
156     }
157   }
158   */
159
160   void readDomainTable(File inputTableTemp, boolean includesQuery)
161           throws IOException
162   {
163     BufferedReader br = new BufferedReader(new FileReader(inputTableTemp));
164
165     String line = br.readLine();
166     br.readLine();
167     br.readLine();
168     line = br.readLine();
169
170     int index;
171
172     if (includesQuery)
173     {
174       index = 1;
175     }
176     else
177     {
178       index = 0;
179     }
180     while (!line.startsWith("#"))
181     {
182       if (line.contains("inclusion threshold"))
183       {
184         line = br.readLine();
185         continue;
186       }
187
188       Scanner scanner = new Scanner(line);
189       String name = scanner.next();
190
191       for (int i = 0; i < 10; i++)
192       {
193         scanner.next();
194       }
195
196       String evalue = scanner.next();
197       scanner.next();
198       String score = scanner.next();
199
200       addScoreAnnotations(evalue, score, seqs[index]);
201       index++;
202
203       scanner.close();
204       line = br.readLine();
205     }
206     br.close();
207   }
208
209
210
211
212   void addScoreAnnotations(String eValue, String bitScore, SequenceI seq)
213   {
214     String label = "Search Scores";
215     String description = "Full sequence bit score and E-Value";
216
217     try
218     {
219       AlignmentAnnotation annot = new AlignmentAnnotation(label,
220               description, null);
221
222       annot.label = label;
223       annot.description = description;
224
225       annot.setCalcId(JACKHMMER);
226
227       double dEValue = Double.parseDouble(eValue);
228       annot.setEValue(dEValue);
229
230       double dBitScore = Double.parseDouble(bitScore);
231       annot.setBitScore(dBitScore);
232
233       annot.setSequenceRef(seq);
234       seq.addAlignmentAnnotation(annot);
235
236     } catch (NumberFormatException e)
237     {
238       System.err.println("Error parsing " + label + " from " + eValue
239               + " & " + bitScore);
240     }
241   }
242
243   void buildArguments(List<String> args, File searchOutputFile,
244           File hitsAlignmentFile, File queryFile) throws IOException
245   {
246     args.add("--domtblout");
247     args.add(getFilePath(searchOutputFile, true));
248     args.add("-A");
249     args.add(getFilePath(hitsAlignmentFile, true));
250
251     File databaseFile = null;
252
253     boolean useEvalueCutoff = false;
254     boolean useScoreCutoff = false;
255     String seqReportingEvalueCutoff = null;
256     String domReportingEvalueCutoff = null;
257     String seqReportingScoreCutoff = null;
258     String domReportingScoreCutoff = null;
259     String seqInclusionEvalueCutoff = null;
260     String domInclusionEvalueCutoff = null;
261     String seqInclusionScoreCutoff = null;
262     String domInclusionScoreCutoff = null;
263     databaseName = "Alignment";
264
265     if (params != null)
266     {
267       for (ArgumentI arg : params)
268       {
269         String name = arg.getName();
270
271         if (MessageManager.getString(REPORTING_CUTOFF_KEY).equals(name))
272         {
273           if (MessageManager.getString(CUTOFF_EVALUE)
274                   .equals(arg.getValue()))
275           {
276             useEvalueCutoff = true;
277           }
278           else if (MessageManager.getString(CUTOFF_SCORE)
279                   .equals(arg.getValue()))
280           {
281             useScoreCutoff = true;
282           }
283         }
284         else if (MessageManager.getString(REPORTING_SEQ_EVALUE_KEY)
285                 .equals(name))
286         {
287           seqReportingEvalueCutoff = arg.getValue();
288         }
289         else if (MessageManager.getString(REPORTING_SEQ_SCORE_KEY)
290                 .equals(name))
291         {
292           seqReportingScoreCutoff = arg.getValue();
293         }
294         else if (MessageManager.getString(REPORTING_DOM_EVALUE_KEY)
295                 .equals(name))
296         {
297           domReportingEvalueCutoff = arg.getValue();
298         }
299         else if (MessageManager.getString(REPORTING_DOM_SCORE_KEY)
300                 .equals(name))
301         {
302           domReportingScoreCutoff = arg.getValue();
303         }
304         else if (MessageManager.getString(INCLUSION_SEQ_EVALUE_KEY)
305                 .equals(name))
306         {
307           seqInclusionEvalueCutoff = arg.getValue();
308         }
309         else if (MessageManager.getString(INCLUSION_SEQ_SCORE_KEY)
310                 .equals(name))
311         {
312           seqInclusionScoreCutoff = arg.getValue();
313         }
314         else if (MessageManager.getString(INCLUSION_DOM_EVALUE_KEY)
315                 .equals(name))
316         {
317           domInclusionEvalueCutoff = arg.getValue();
318         }
319         else if (MessageManager.getString(INCLUSION_DOM_SCORE_KEY)
320                 .equals(name))
321         {
322           domInclusionScoreCutoff = arg.getValue();
323         }
324         else if (MessageManager.getString(DATABASE_KEY).equals(name))
325         {
326           databaseFile = new File(arg.getValue());
327           if (!arg.getValue().isEmpty())
328           {
329             searchAlignment = false;
330           }
331         }
332         else if (MessageManager.getString(NUMBER_OF_ITERATIONS)
333                 .equals(name))
334         {
335           if (!arg.getValue().isEmpty())
336           {
337             args.add("-N");
338             args.add(arg.getValue());
339           }
340         }
341       }
342     }
343
344     if (useEvalueCutoff)
345     {
346       args.add("-E");
347       args.add(seqReportingEvalueCutoff);
348       args.add("--domE");
349       args.add(domReportingEvalueCutoff);
350
351       args.add("--incE");
352       args.add(seqInclusionEvalueCutoff);
353       args.add("--incdomE");
354       args.add(domInclusionEvalueCutoff);
355     }
356     else if (useScoreCutoff)
357     {
358       args.add("-T");
359       args.add(seqReportingScoreCutoff);
360       args.add("--domT");
361       args.add(domReportingScoreCutoff);
362
363       args.add("--incT");
364       args.add(seqInclusionEvalueCutoff);
365       args.add("--incdomT");
366       args.add(domInclusionEvalueCutoff);
367     }
368
369     // if (!dbFound || MessageManager.getString(THIS_ALIGNMENT_KEY)
370     // .equals(dbPath))
371     if (searchAlignment)
372     {
373       /*
374        * no external database specified for search, so
375        * export current alignment as 'database' to search
376        */
377       databaseFile = FileUtils.createTempFile("database", ".sto");
378       AlignmentI al = af.getViewport().getAlignment();
379       AlignmentI copy = new Alignment(al);
380
381       deleteHmmSequences(copy);
382
383       if (searchAlignment)
384       {
385         sequencesHash = stashSequences(copy.getSequencesArray());
386       }
387
388       exportStockholm(copy.getSequencesArray(), databaseFile, null);
389     }
390
391     args.add(getFilePath(queryFile, true));
392     args.add(getFilePath(databaseFile, true));
393   }
394 }