Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / jalview / io / AppletFormatAdapter.js
1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (null, "jalview.io.AppletFormatAdapter", ["MCview.PDBfile", "jalview.datamodel.Alignment", "jalview.io.FileParse", "$.IdentifyFile", "jalview.jsdev.GenericFileAdapter", "jalview.util.MessageManager", "java.io.IOException", "java.lang.Exception", "$.StringBuffer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.viewpanel = null;
5 this.annotFromStructure = false;
6 this.localSecondaryStruct = false;
7 this.serviceSecondaryStruct = false;
8 this.alignFile = null;
9 this.inFile = null;
10 this.newline = null;
11 this.exportSettings = null;
12 Clazz.instantialize (this, arguments);
13 }, jalview.io, "AppletFormatAdapter");
14 Clazz.prepareFields (c$, function () {
15 this.newline = System.getProperty ("line.separator");
16 });
17 Clazz.makeConstructor (c$, 
18 function () {
19 });
20 Clazz.makeConstructor (c$, 
21 function (viewpanel) {
22 this.viewpanel = viewpanel;
23 }, "jalview.api.AlignmentViewPanel");
24 Clazz.makeConstructor (c$, 
25 function (alignPanel, settings) {
26 this.viewpanel = alignPanel;
27 this.exportSettings = settings;
28 }, "jalview.api.AlignmentViewPanel,jalview.api.AlignExportSettingI");
29 c$.prettyPrint = Clazz.defineMethod (c$, "prettyPrint", 
30 function (els) {
31 var list =  new StringBuffer ();
32 for (var i = 0, iSize = els.length - 1; i < iSize; i++) {
33 list.append (els[i]);
34 list.append (", ");
35 }
36 list.append (" and " + els[els.length - 1] + ".");
37 return list.toString ();
38 }, "~A");
39 Clazz.defineMethod (c$, "setNewlineString", 
40 function (nl) {
41 this.newline = nl;
42 }, "~S");
43 Clazz.defineMethod (c$, "getNewlineString", 
44 function () {
45 return this.newline;
46 });
47 c$.isValidFormat = Clazz.defineMethod (c$, "isValidFormat", 
48 function (format) {
49 return jalview.io.AppletFormatAdapter.isValidFormat (format, false);
50 }, "~S");
51 c$.isValidFormat = Clazz.defineMethod (c$, "isValidFormat", 
52 function (format, forwriting) {
53 var valid = false;
54 var format_list = (forwriting) ? jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS : jalview.io.AppletFormatAdapter.READABLE_FORMATS;
55 for (var element, $element = 0, $$element = format_list; $element < $$element.length && ((element = $$element[$element]) || true); $element++) {
56 if (element.equalsIgnoreCase (format)) {
57 return true;
58 }}
59 return valid;
60 }, "~S,~B");
61 Clazz.defineMethod (c$, "readFile", 
62 function (inFile, type, format) {
63 this.inFile = inFile;
64 try {
65 if (format.equals ("FASTA")) {
66 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("FastaFile", [inFile, type]);
67 } else if (format.equals ("MSF")) {
68 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("MSFfile", [inFile, type]);
69 } else if (format.equals ("PileUp")) {
70 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PileUpfile", [inFile, type]);
71 } else if (format.equals ("CLUSTAL")) {
72 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("ClustalFile", [inFile, type]);
73 } else if (format.equals ("BLC")) {
74 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("BLCFile", [inFile, type]);
75 } else if (format.equals ("PIR")) {
76 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PIRFile", [inFile, type]);
77 } else if (format.equals ("PFAM")) {
78 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PfamFile", [inFile, type]);
79 } else if (format.equals ("JnetFile")) {
80 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("JPredFile", [inFile, type]);
81 (this.alignFile).removeNonSequences ();
82 } else if (format.equals ("PDB")) {
83 this.alignFile =  new MCview.PDBfile (this.annotFromStructure, this.localSecondaryStruct, this.serviceSecondaryStruct, inFile, type);
84 } else if (format.equals ("STH")) {
85 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("StockholmFile", [inFile, type]);
86 } else if (format.equals ("SimpleBLAST")) {
87 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("SimpleBlastFile", [inFile, type]);
88 } else if (format.equals ("PHYLIP")) {
89 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PhylipFile", [inFile, type]);
90 } else if (format.equals ("JSON")) {
91 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("JSONFile", [inFile, type]);
92 } else if (format.equals ("HTML")) {
93 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("HtmlFile", [inFile, type]);
94 } else if (format.equals ("RNAML")) {
95 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("RnamlFile", [inFile, type]);
96 } else if (format.equals ("GFF v2 or v3")) {
97 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("Gff3File", [inFile, type]);
98 }return this.buildAlignmentFrom (this.alignFile);
99 } catch (e) {
100 if (Clazz.exceptionOf (e, Exception)) {
101 e.printStackTrace ();
102 System.err.println ("Failed to read alignment using the '" + format + "' reader.\n" + e);
103 if (e.getMessage () != null && e.getMessage ().startsWith (jalview.io.AppletFormatAdapter.INVALID_CHARACTERS)) {
104 throw  new java.io.IOException (e.getMessage ());
105 }if (type.equalsIgnoreCase ("Paste")) {
106 try {
107 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("FastaFile", [">UNKNOWN\n" + inFile, "Paste"]);
108 return this.buildAlignmentFrom (this.alignFile);
109 } catch (ex) {
110 if (Clazz.exceptionOf (ex, Exception)) {
111 if (ex.toString ().startsWith (jalview.io.AppletFormatAdapter.INVALID_CHARACTERS)) {
112 throw  new java.io.IOException (e.getMessage ());
113 }ex.printStackTrace ();
114 } else {
115 throw ex;
116 }
117 }
118 }throw  new java.io.IOException (jalview.io.AppletFormatAdapter.SUPPORTED_FORMATS);
119 } else {
120 throw e;
121 }
122 }
123 }, "~S,~S,~S");
124 Clazz.defineMethod (c$, "readFromFile", 
125 function (source, format) {
126 this.inFile = source.getInFile ();
127 var type = source.type;
128 try {
129 if (format.equals ("FASTA")) {
130 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("FastaFile", [source]);
131 } else if (format.equals ("MSF")) {
132 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("MSFfile", [source]);
133 } else if (format.equals ("PileUp")) {
134 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PileUpfile", [source]);
135 } else if (format.equals ("CLUSTAL")) {
136 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("ClustalFile", [source]);
137 } else if (format.equals ("BLC")) {
138 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("BLCFile", [source]);
139 } else if (format.equals ("PIR")) {
140 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PIRFile", [source]);
141 } else if (format.equals ("PFAM")) {
142 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PfamFile", [source]);
143 } else if (format.equals ("JnetFile")) {
144 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("JPredFile", [source]);
145 (this.alignFile).removeNonSequences ();
146 } else if (format.equals ("PDB")) {
147 this.alignFile =  new MCview.PDBfile (this.annotFromStructure, this.localSecondaryStruct, this.serviceSecondaryStruct, source);
148 } else if (format.equals ("STH")) {
149 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("StockholmFile", [source]);
150 } else if (format.equals ("RNAML")) {
151 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("RnamlFile", [source]);
152 } else if (format.equals ("SimpleBLAST")) {
153 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("SimpleBlastFile", [source]);
154 } else if (format.equals ("PHYLIP")) {
155 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("PhylipFile", [source]);
156 } else if (format.equals ("GFF v2 or v3")) {
157 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("Gff3File", [this.inFile, type]);
158 } else if (format.equals ("JSON")) {
159 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("JSONFile", [source]);
160 } else if (format.equals ("HTML")) {
161 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("HtmlFile", [source]);
162 }return this.buildAlignmentFrom (this.alignFile);
163 } catch (e) {
164 if (Clazz.exceptionOf (e, Exception)) {
165 e.printStackTrace ();
166 System.err.println ("Failed to read alignment using the '" + format + "' reader.\n" + e);
167 if (e.getMessage () != null && e.getMessage ().startsWith (jalview.io.AppletFormatAdapter.INVALID_CHARACTERS)) {
168 throw  new java.io.IOException (e.getMessage ());
169 }if (type.equalsIgnoreCase ("Paste")) {
170 try {
171 this.alignFile = jalview.jsdev.GenericFileAdapter.getFile ("FastaFile", [">UNKNOWN\n" + this.inFile, "Paste"]);
172 return this.buildAlignmentFrom (this.alignFile);
173 } catch (ex) {
174 if (Clazz.exceptionOf (ex, Exception)) {
175 if (ex.toString ().startsWith (jalview.io.AppletFormatAdapter.INVALID_CHARACTERS)) {
176 throw  new java.io.IOException (e.getMessage ());
177 }ex.printStackTrace ();
178 } else {
179 throw ex;
180 }
181 }
182 }throw  new java.io.IOException (jalview.io.AppletFormatAdapter.SUPPORTED_FORMATS);
183 } else {
184 throw e;
185 }
186 }
187 }, "jalview.io.FileParse,~S");
188 Clazz.defineMethod (c$, "buildAlignmentFrom", 
189  function (alignFile2) {
190 var al =  new jalview.datamodel.Alignment (this.alignFile.getSeqsAsArray ());
191 this.alignFile.addAnnotations (al);
192 this.alignFile.addGroups (al);
193 return al;
194 }, "jalview.io.AlignFile");
195 Clazz.defineMethod (c$, "formatSequences", 
196 function (format, jvsuffix, ap, selectedOnly) {
197 var selvew = ap.getAlignViewport ().getAlignmentView (selectedOnly, false);
198 var aselview = selvew.getVisibleAlignment (ap.getAlignViewport ().getGapCharacter ());
199 var ala = (ap.getAlignViewport ().getVisibleAlignmentAnnotation (selectedOnly));
200 if (ala != null) {
201 for (var aa, $aa = ala.iterator (); $aa.hasNext () && ((aa = $aa.next ()) || true);) {
202 aselview.addAnnotation (aa);
203 }
204 }this.viewpanel = ap;
205 return this.formatSequences (format, aselview, jvsuffix);
206 }, "~S,~B,jalview.api.AlignmentViewPanel,~B");
207 Clazz.defineMethod (c$, "formatSequences", 
208 function (format, alignment, jvsuffix) {
209 try {
210 var afile = null;
211 if (format.equalsIgnoreCase ("FASTA")) {
212 afile = jalview.jsdev.GenericFileAdapter.getFile ("FastaFile", []);
213 } else if (format.equalsIgnoreCase ("MSF")) {
214 afile = jalview.jsdev.GenericFileAdapter.getFile ("MSFfile", []);
215 } else if (format.equalsIgnoreCase ("PileUp")) {
216 afile = jalview.jsdev.GenericFileAdapter.getFile ("PileUpfile", []);
217 } else if (format.equalsIgnoreCase ("CLUSTAL")) {
218 afile = jalview.jsdev.GenericFileAdapter.getFile ("ClustalFile", []);
219 } else if (format.equalsIgnoreCase ("BLC")) {
220 afile = jalview.jsdev.GenericFileAdapter.getFile ("BLCFile", []);
221 } else if (format.equalsIgnoreCase ("PIR")) {
222 afile = jalview.jsdev.GenericFileAdapter.getFile ("PIRFile", []);
223 } else if (format.equalsIgnoreCase ("PFAM")) {
224 afile = jalview.jsdev.GenericFileAdapter.getFile ("PfamFile", []);
225 } else if (format.equalsIgnoreCase ("STH")) {
226 afile = jalview.jsdev.GenericFileAdapter.getFile ("StockholmFile", [alignment]);
227 } else if (format.equalsIgnoreCase ("AMSA")) {
228 afile = jalview.jsdev.GenericFileAdapter.getFile ("AMSAFile", [alignment]);
229 } else if (format.equalsIgnoreCase ("PHYLIP")) {
230 afile = jalview.jsdev.GenericFileAdapter.getFile ("PhylipFile", []);
231 } else if (format.equalsIgnoreCase ("JSON")) {
232 afile = jalview.jsdev.GenericFileAdapter.getFile ("JSONFile", []);
233 } else if (format.equalsIgnoreCase ("RNAML")) {
234 afile = jalview.jsdev.GenericFileAdapter.getFile ("RnamlFile", []);
235 } else {
236 throw  new Exception (jalview.util.MessageManager.getString ("error.implementation_error_unknown_file_format_string"));
237 }afile.setNewlineString (this.newline);
238 afile.addJVSuffix (jvsuffix);
239 afile.setExportSettings (this.exportSettings);
240 afile.configureForView (this.viewpanel);
241 if (this.viewpanel == null || this.viewpanel.getAlignment () == null || this.viewpanel.getAlignment () !== alignment) {
242 afile.setSeqs (alignment.getSequencesArray ());
243 } else {
244 afile.setSeqs (this.viewpanel.getAlignment ().getSequencesArray ());
245 }var afileresp = afile.print ();
246 if (afile.hasWarningMessage ()) {
247 System.err.println ("Warning raised when writing as " + format + " : " + afile.getWarningMessage ());
248 }return afileresp;
249 } catch (e) {
250 if (Clazz.exceptionOf (e, Exception)) {
251 System.err.println ("Failed to write alignment as a '" + format + "' file\n");
252 e.printStackTrace ();
253 } else {
254 throw e;
255 }
256 }
257 return null;
258 }, "~S,jalview.datamodel.AlignmentI,~B");
259 c$.checkProtocol = Clazz.defineMethod (c$, "checkProtocol", 
260 function (file) {
261 var protocol = jalview.io.AppletFormatAdapter.FILE;
262 var ft = file.toLowerCase ().trim ();
263 if (ft.indexOf ("http:") == 0 || ft.indexOf ("https:") == 0 || ft.indexOf ("file:") == 0) {
264 protocol = jalview.io.AppletFormatAdapter.URL;
265 }return protocol;
266 }, "~S");
267 c$.resolveProtocol = Clazz.defineMethod (c$, "resolveProtocol", 
268 function (file, format) {
269 return jalview.io.AppletFormatAdapter.resolveProtocol (file, format, false);
270 }, "~S,~S");
271 c$.resolveProtocol = Clazz.defineMethod (c$, "resolveProtocol", 
272 function (file, format, debug) {
273 var protocol = null;
274 if (debug) {
275 System.out.println ("resolving datasource started with:\n>>file\n" + file + ">>endfile");
276 }try {
277 var rtn = false;
278 var is = System.getSecurityManager ().getClass ().getResourceAsStream ("/" + file);
279 if (is != null) {
280 rtn = true;
281 is.close ();
282 }if (debug) {
283 System.err.println ("Resource '" + file + "' was " + (rtn ? "" : "not") + " located by classloader.");
284 };if (rtn) {
285 protocol = jalview.io.AppletFormatAdapter.CLASSLOADER;
286 }} catch (ex) {
287 if (Clazz.exceptionOf (ex, Exception)) {
288 System.err.println ("Exception checking resources: " + file + " " + ex);
289 } else {
290 throw ex;
291 }
292 }
293 if (file.indexOf ("://") > -1) {
294 protocol = jalview.io.AppletFormatAdapter.URL;
295 } else {
296 protocol = jalview.io.AppletFormatAdapter.FILE;
297 }var fp = null;
298 try {
299 if (debug) {
300 System.out.println ("Trying to get contents of resource as " + protocol + ":");
301 }fp =  new jalview.io.FileParse (file, protocol);
302 if (!fp.isValid ()) {
303 fp = null;
304 } else {
305 if (debug) {
306 System.out.println ("Successful.");
307 }}} catch (e) {
308 if (Clazz.exceptionOf (e, Exception)) {
309 if (debug) {
310 System.err.println ("Exception when accessing content: " + e);
311 }fp = null;
312 } else {
313 throw e;
314 }
315 }
316 if (fp == null) {
317 if (debug) {
318 System.out.println ("Accessing as paste.");
319 }protocol = jalview.io.AppletFormatAdapter.PASTE;
320 fp = null;
321 try {
322 fp =  new jalview.io.FileParse (file, protocol);
323 if (!fp.isValid ()) {
324 fp = null;
325 }} catch (e) {
326 if (Clazz.exceptionOf (e, Exception)) {
327 System.err.println ("Failed to access content as paste!");
328 e.printStackTrace ();
329 fp = null;
330 } else {
331 throw e;
332 }
333 }
334 }if (fp == null) {
335 return null;
336 }if (format == null || format.length == 0) {
337 return protocol;
338 } else {
339 try {
340 var idformat =  new jalview.io.IdentifyFile ().Identify (file, protocol);
341 if (idformat == null) {
342 if (debug) {
343 System.out.println ("Format not identified. Inaccessible file.");
344 }return null;
345 }if (debug) {
346 System.out.println ("Format identified as " + idformat + "and expected as " + format);
347 }if (idformat.equals (format)) {
348 if (debug) {
349 System.out.println ("Protocol identified as " + protocol);
350 }return protocol;
351 } else {
352 if (debug) {
353 System.out.println ("File deemed not accessible via " + protocol);
354 }fp.close ();
355 return null;
356 }} catch (e) {
357 if (Clazz.exceptionOf (e, Exception)) {
358 if (debug) {
359 System.err.println ("File deemed not accessible via " + protocol);
360 e.printStackTrace ();
361 };} else {
362 throw e;
363 }
364 }
365 }return null;
366 }, "~S,~S,~B");
367 Clazz.defineMethod (c$, "getAlignFile", 
368 function () {
369 return this.alignFile;
370 });
371 Clazz.defineMethod (c$, "setAlignFile", 
372 function (alignFile) {
373 this.alignFile = alignFile;
374 }, "jalview.io.AlignFile");
375 Clazz.defineStatics (c$,
376 "FILE", "File",
377 "URL", "URL",
378 "PASTE", "Paste",
379 "CLASSLOADER", "ClassLoader");
380 c$.READABLE_FORMATS = c$.prototype.READABLE_FORMATS =  Clazz.newArray (-1, ["BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB", "JnetFile", "RNAML", "PHYLIP", "JSON", "GFF v2 or v3", "HTML"]);
381 c$.READABLE_EXTENSIONS = c$.prototype.READABLE_EXTENSIONS =  Clazz.newArray (-1, ["fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "sto,stk", "xml,rnaml", "phy", "json", ".gff2,gff3", "jar,jvp", "html"]);
382 c$.READABLE_FNAMES = c$.prototype.READABLE_FNAMES =  Clazz.newArray (-1, ["Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Stockholm", "RNAML", "PHYLIP", "JSON", "GFF v2 or v3", "Jalview", "HTML"]);
383 c$.WRITEABLE_FORMATS = c$.prototype.WRITEABLE_FORMATS =  Clazz.newArray (-1, ["BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA", "STH", "PHYLIP", "JSON"]);
384 c$.WRITABLE_EXTENSIONS = c$.prototype.WRITABLE_EXTENSIONS =  Clazz.newArray (-1, ["fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "sto,stk", "phy", "json", "jvp"]);
385 c$.WRITABLE_FNAMES = c$.prototype.WRITABLE_FNAMES =  Clazz.newArray (-1, ["Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH", "PHYLIP", "JSON", "Jalview"]);
386 Clazz.defineStatics (c$,
387 "INVALID_CHARACTERS", "Contains invalid characters");
388 c$.SUPPORTED_FORMATS = c$.prototype.SUPPORTED_FORMATS = "Formats currently supported are\n" + jalview.io.AppletFormatAdapter.prettyPrint (jalview.io.AppletFormatAdapter.READABLE_FORMATS);
389 });