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