420e1d1809ca866aa018c03216356fe9fb531a73
[jalview.git] / src / jalview / bin / Commands.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import jalview.analysis.AlignmentUtils;
9 import jalview.bin.ArgParser.Arg;
10 import jalview.bin.ArgParser.ArgValues;
11 import jalview.gui.AlignFrame;
12 import jalview.gui.Desktop;
13 import jalview.io.AppletFormatAdapter;
14 import jalview.io.DataSourceType;
15 import jalview.io.FileFormatException;
16 import jalview.io.FileFormatI;
17 import jalview.io.FileLoader;
18 import jalview.io.IdentifyFile;
19 import jalview.util.HttpUtils;
20 import jalview.util.MessageManager;
21 import jalview.util.Platform;
22
23 public class Commands
24 {
25   Desktop desktop;
26
27   private static boolean headless;
28
29   private static ArgParser argParser;
30
31   private Map<String, AlignFrame> afMap;
32
33   public static void processArgs(ArgParser ap, boolean h)
34   {
35     argParser = ap;
36     headless = h;
37     for (String id : argParser.linkedIds())
38     {
39       Commands cmds = new Commands();
40       if (id == null)
41       {
42         cmds.processUnlinked(id);
43       }
44       else
45       {
46         cmds.processLinked(id);
47       }
48     }
49   }
50
51   public Commands()
52   {
53     this(Desktop.instance);
54   }
55
56   public Commands(Desktop d)
57   {
58     this.desktop = d;
59     afMap = new HashMap<String, AlignFrame>();
60   }
61
62   protected void processUnlinked(String id)
63   {
64     processLinked(id);
65   }
66
67   protected void processLinked(String id)
68   {
69     Map<Arg, ArgValues> m = argParser.linkedArgs(id);
70     FileLoader fileLoader = new FileLoader(!headless);
71
72     /*
73     // script to execute after all loading is completed one way or another
74     String groovyscript = m.get(Arg.GROOVY) == null ? null
75             : m.get(Arg.GROOVY).getValue();
76     String file = m.get(Arg.OPEN) == null ? null
77             : m.get(Arg.OPEN).getValue();
78     String data = null;
79     FileFormatI format = null;
80     DataSourceType protocol = null;
81     */
82
83     if (m.get(Arg.OPEN) != null)
84     {
85       long progress = -1;
86
87       boolean first = true;
88       AlignFrame af;
89       OPEN: for (String openFile : m.get(Arg.OPEN).getValues())
90       {
91         if (openFile == null)
92           continue OPEN;
93         Console.debug("********** id = " + id + ", openFile = " + openFile);
94
95         if (first)
96         {
97           first = false;
98           if (!headless)
99           {
100             desktop.setProgressBar(
101                     MessageManager.getString(
102                             "status.processing_commandline_args"),
103                     progress = System.currentTimeMillis());
104           }
105         }
106
107         if (!Platform.isJS())
108         /**
109          * ignore in JavaScript -- can't just file existence - could load it?
110          * 
111          * @j2sIgnore
112          */
113         {
114           if (!HttpUtils.startsWithHttpOrHttps(openFile))
115           {
116             if (!(new File(openFile)).exists())
117             {
118               Console.warn("Can't find file '" + openFile + "'");
119               continue OPEN;
120             }
121           }
122         }
123
124         DataSourceType protocol = AppletFormatAdapter
125                 .checkProtocol(openFile);
126
127         FileFormatI format = null;
128         try
129         {
130           format = new IdentifyFile().identify(openFile, protocol);
131         } catch (FileFormatException e1)
132         {
133           Console.error("Unknown file format for '" + openFile + "'");
134         }
135
136         af = afMap.get(id);
137         if (af == null)
138         {
139           Console.debug(
140                   "Opening '" + openFile + "' in new alignment frame");
141           af = fileLoader.LoadFileWaitTillLoaded(openFile, protocol,
142                   format);
143           if (m.get(Arg.TITLE) != null)
144             af.setTitle(m.get(Arg.TITLE).getValue());
145           if (m.get(Arg.SSANNOTATION) != null
146                   && !m.get(Arg.SSANNOTATION).getBoolean())
147           {
148             // do this better (annotation types?)
149             AlignmentUtils.showOrHideSequenceAnnotations(
150                     af.getCurrentView().getAlignment(),
151                     Collections.singleton("Secondary Structure"), null,
152                     false, false);
153           }
154           if (m.get(Arg.NOTEMPFAC) != null
155                   && m.get(Arg.NOTEMPFAC).getBoolean())
156           {
157             // do this better (annotation types?)
158             AlignmentUtils.showOrHideSequenceAnnotations(
159                     af.getCurrentView().getAlignment(),
160                     Collections.singleton("Temperature Factor"), null,
161                     false, false);
162             AlignmentUtils.showOrHideSequenceAnnotations(
163                     af.getCurrentView().getAlignment(),
164                     Collections.singleton("Alphafold Reliability"), null,
165                     false, false);
166           }
167
168           // store the AlignFrame for this id
169           afMap.put(id, af);
170         }
171         else
172         {
173           Console.debug(
174                   "Opening '" + openFile + "' in existing alignment frame");
175           af.getCurrentView().addFile(new File(openFile), format);
176         }
177
178         System.out
179                 .println("Command " + Arg.OPEN + " executed successfully!");
180
181       }
182       if (first) // first=true means nothing opened
183       {
184         if (headless)
185         {
186           Console.error("Could not open any files in headless mode");
187           System.exit(1);
188         }
189       }
190       else
191       {
192         Console.warn("No more files to open");
193         if (desktop != null)
194           desktop.setProgressBar(null, progress);
195       }
196
197     }
198   }
199
200 }