JAL-629 implementation of --tempfac options
[jalview.git] / src / jalview / bin / Commands.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Locale;
11 import java.util.Map;
12
13 import jalview.analysis.AlignmentUtils;
14 import jalview.bin.ArgParser.Arg;
15 import jalview.bin.ArgParser.ArgValues;
16 import jalview.datamodel.AlignmentAnnotation;
17 import jalview.gui.AlignFrame;
18 import jalview.gui.Desktop;
19 import jalview.io.AppletFormatAdapter;
20 import jalview.io.DataSourceType;
21 import jalview.io.FileFormatException;
22 import jalview.io.FileFormatI;
23 import jalview.io.FileLoader;
24 import jalview.io.IdentifyFile;
25 import jalview.util.HttpUtils;
26 import jalview.util.MessageManager;
27 import jalview.util.Platform;
28 import jalview.ws.dbsources.EBIAlfaFold;
29
30 public class Commands
31 {
32   Desktop desktop;
33
34   private static boolean headless;
35
36   private static ArgParser argParser;
37
38   private Map<String, AlignFrame> afMap;
39
40   public static void processArgs(ArgParser ap, boolean h)
41   {
42     argParser = ap;
43     headless = h;
44     for (String id : argParser.linkedIds())
45     {
46       Commands cmds = new Commands();
47       if (id == null)
48       {
49         cmds.processUnlinked(id);
50       }
51       else
52       {
53         cmds.processLinked(id);
54       }
55     }
56   }
57
58   public Commands()
59   {
60     this(Desktop.instance);
61   }
62
63   public Commands(Desktop d)
64   {
65     this.desktop = d;
66     afMap = new HashMap<String, AlignFrame>();
67   }
68
69   protected void processUnlinked(String id)
70   {
71     processLinked(id);
72   }
73
74   protected void processLinked(String id)
75   {
76     Map<Arg, ArgValues> m = argParser.linkedArgs(id);
77
78     /*
79     // script to execute after all loading is completed one way or another
80     String groovyscript = m.get(Arg.GROOVY) == null ? null
81             : m.get(Arg.GROOVY).getValue();
82     String file = m.get(Arg.OPEN) == null ? null
83             : m.get(Arg.OPEN).getValue();
84     String data = null;
85     FileFormatI format = null;
86     DataSourceType protocol = null;
87     */
88
89     if (m.get(Arg.OPEN) != null)
90     {
91       long progress = -1;
92
93       boolean first = true;
94       AlignFrame af;
95       OPEN: for (String openFile : m.get(Arg.OPEN).getValues())
96       {
97         if (openFile == null)
98           continue OPEN;
99
100         if (first)
101         {
102           first = false;
103           if (!headless)
104           {
105             desktop.setProgressBar(
106                     MessageManager.getString(
107                             "status.processing_commandline_args"),
108                     progress = System.currentTimeMillis());
109           }
110         }
111
112         if (!Platform.isJS())
113         /**
114          * ignore in JavaScript -- can't just file existence - could load it?
115          * 
116          * @j2sIgnore
117          */
118         {
119           if (!HttpUtils.startsWithHttpOrHttps(openFile))
120           {
121             if (!(new File(openFile)).exists())
122             {
123               Console.warn("Can't find file '" + openFile + "'");
124               continue OPEN;
125             }
126           }
127         }
128
129         DataSourceType protocol = AppletFormatAdapter
130                 .checkProtocol(openFile);
131
132         FileFormatI format = null;
133         try
134         {
135           format = new IdentifyFile().identify(openFile, protocol);
136         } catch (FileFormatException e1)
137         {
138           Console.error("Unknown file format for '" + openFile + "'");
139         }
140
141         af = afMap.get(id);
142         if (af == null)
143         {
144           // get kind of temperature factor annotation
145           AlignmentAnnotation.TFType tempfacType = null;
146           if ((m.get(Arg.NOTEMPFAC) == null
147                   || !m.get(Arg.NOTEMPFAC).getBoolean())
148                   && m.get(Arg.TEMPFAC) != null)
149           {
150             try
151             {
152               tempfacType = AlignmentAnnotation.TFType
153                       .valueOf(m.get(Arg.TEMPFAC).getValue()
154                               .toUpperCase(Locale.ROOT));
155               Console.debug("Obtained Temperature Factor type of '"
156                       + tempfacType + "'");
157             } catch (IllegalArgumentException e)
158             {
159               StringBuilder sb = new StringBuilder().append("Cannot set --")
160                       .append(Arg.TEMPFAC.getName()).append(" to '")
161                       .append(tempfacType)
162                       .append("', ignoring.  Valid values are: ");
163               Iterator<AlignmentAnnotation.TFType> it = Arrays
164                       .stream(AlignmentAnnotation.TFType.values())
165                       .iterator();
166               while (it.hasNext())
167               {
168                 sb.append(it.next().toString().toLowerCase(Locale.ROOT));
169                 if (it.hasNext())
170                   sb.append(", ");
171               }
172               Console.warn(sb.toString());
173             }
174           }
175
176           Console.debug(
177                   "Opening '" + openFile + "' in new alignment frame");
178           FileLoader fileLoader = new FileLoader(!headless);
179           af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol, format,
180                   tempfacType);
181
182           // wrap alignment?
183           if (m.get(Arg.WRAP) != null && m.get(Arg.WRAP).getBoolean())
184           {
185             af.getCurrentView().setWrapAlignment(true);
186           }
187
188           // change alignment frame title
189           if (m.get(Arg.TITLE) != null)
190             af.setTitle(m.get(Arg.TITLE).getValue());
191
192           // show secondary structure annotations?
193           if (m.get(Arg.SSANNOTATION) != null
194                   && !m.get(Arg.SSANNOTATION).getBoolean())
195           {
196             // do this better (annotation types?)
197             AlignmentUtils.showOrHideSequenceAnnotations(
198                     af.getCurrentView().getAlignment(),
199                     Collections.singleton("Secondary Structure"), null,
200                     false, false);
201           }
202
203           // show temperature factor annotations?
204           if (m.get(Arg.NOTEMPFAC) != null
205                   && m.get(Arg.NOTEMPFAC).getBoolean())
206           {
207             // do this better (annotation types?)
208             List<String> hideThese = new ArrayList<>();
209             hideThese.add("Temperature Factor");
210             hideThese.add(MessageManager
211                     .getString("label.alphafold_reliability"));
212             AlignmentUtils.showOrHideSequenceAnnotations(
213                     af.getCurrentView().getAlignment(), hideThese, null,
214                     false, false);
215           }
216           else
217           {
218             if (m.get(Arg.TEMPFAC_LABEL) != null)
219             {
220               AlignmentAnnotation aa = AlignmentUtils
221                       .getFirstSequenceAnnotationOfType(
222                               af.getCurrentView().getAlignment(),
223                               AlignmentAnnotation.LINE_GRAPH);
224               Console.debug("***** Trying to change label from '"
225                       + (aa == null ? aa : aa.label) + "' to '"
226                       + m.get(Arg.TEMPFAC_LABEL).getValue() + "'");
227               if (aa != null)
228               {
229                 aa.label = m.get(Arg.TEMPFAC_LABEL).getValue();
230               }
231             }
232           }
233
234           //
235
236           // load a pAE file?
237           if (m.get(Arg.PAEMATRIX) != null)
238           {
239             File paeFile = new File(m.get(Arg.PAEMATRIX).getValue());
240             EBIAlfaFold.addAlphaFoldPAE(af.getCurrentView().getAlignment(),
241                     paeFile);
242           }
243
244           // store the AlignFrame for this id
245           afMap.put(id, af);
246         }
247         else
248         {
249           Console.debug(
250                   "Opening '" + openFile + "' in existing alignment frame");
251           af.getCurrentView().addFile(new File(openFile), format);
252         }
253
254         System.out
255                 .println("Command " + Arg.OPEN + " executed successfully!");
256
257       }
258       if (first) // first=true means nothing opened
259       {
260         if (headless)
261         {
262           Console.error("Could not open any files in headless mode");
263           System.exit(1);
264         }
265       }
266       else
267       {
268         Console.warn("No more files to open");
269         if (desktop != null)
270           desktop.setProgressBar(null, progress);
271       }
272
273     }
274   }
275
276 }