X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=site%2Fj2s%2Fjalview%2Fio%2FIdentifyFile.js;fp=site%2Fj2s%2Fjalview%2Fio%2FIdentifyFile.js;h=cf352415eb256ec0bf0b0b70e45b4d10ca312991;hb=4231d40261fef32ce4570a6bbad0d35df44e8baf;hp=0000000000000000000000000000000000000000;hpb=c07eb2c5794833bd0241000d6844d783fe10cb82;p=jalview.git diff --git a/site/j2s/jalview/io/IdentifyFile.js b/site/j2s/jalview/io/IdentifyFile.js new file mode 100644 index 0000000..cf35241 --- /dev/null +++ b/site/j2s/jalview/io/IdentifyFile.js @@ -0,0 +1,157 @@ +Clazz.declarePackage ("jalview.io"); +Clazz.load (null, "jalview.io.IdentifyFile", ["jalview.io.FileParse"], function () { +c$ = Clazz.declareType (jalview.io, "IdentifyFile"); +Clazz.defineMethod (c$, "Identify", +function (file, protocol) { +var emessage = "UNIDENTIFIED FILE PARSING ERROR"; +var parser = null; +try { +parser = new jalview.io.FileParse (file, protocol); +if (parser.isValid ()) { +return this.Identify (parser); +}} catch (e) { +if (Clazz.exceptionOf (e, Exception)) { +System.err.println ("Error whilst identifying"); +e.printStackTrace (System.err); +emessage = e.getMessage (); +} else { +throw e; +} +} +if (parser != null) { +return parser.errormessage; +}return emessage; +}, "~S,~S"); +Clazz.defineMethod (c$, "Identify", +function (source) { +return this.Identify (source, true); +}, "jalview.io.FileParse"); +Clazz.defineMethod (c$, "Identify", +function (source, closeSource) { +var reply = "PFAM"; +var data; +var length = 0; +var lineswereskipped = false; +var isBinary = false; +try { +if (!closeSource) { +source.mark (); +}while ((data = source.nextLine ()) != null) { +length += data.trim ().length; +if (!lineswereskipped) { +for (var i = 0; !isBinary && i < data.length; i++) { +var c = data.charAt (i); +isBinary = (c.charCodeAt (0) < 32 && c != '\t' && c != '\n' && c != '\r' && c.charCodeAt (0) != 5 && c.charCodeAt (0) != 27); +} +}if (isBinary) { +if (source.inFile != null) { +var fileStr = source.inFile.getName (); +if (fileStr.lastIndexOf (".jar") > -1 || fileStr.lastIndexOf (".zip") > -1) { +reply = "Jalview"; +}}if (!lineswereskipped && data.startsWith ("PK")) { +reply = "Jalview"; +break; +}}data = data.toUpperCase (); +if (data.startsWith ("##GFF-VERSION")) { +reply = "GFF v2 or v3"; +break; +}if (data.indexOf ("# STOCKHOLM") > -1) { +reply = "STH"; +break; +}if (data.matches ("])*>")) { +reply = "HTML"; +break; +}if (data.matches ("])*>")) { +reply = "RNAML"; +break; +}if (data.indexOf ("{\"") > -1) { +reply = "JSON"; +break; +}if ((data.length < 1) || (data.indexOf ("#") == 0)) { +lineswereskipped = true; +continue; +}if (data.indexOf ("PILEUP") > -1) { +reply = "PileUp"; +break; +}if ((data.indexOf ("//") == 0) || ((data.indexOf ("!!") > -1) && (data.indexOf ("!!") < data.indexOf ("_MULTIPLE_ALIGNMENT ")))) { +reply = "MSF"; +break; +} else if (data.indexOf ("CLUSTAL") > -1) { +reply = "CLUSTAL"; +break; +} else if (data.indexOf (">") > -1) { +var checkPIR = false; +var starterm = false; +if ((data.indexOf (">P1;") > -1) || (data.indexOf (">DL;") > -1)) { +checkPIR = true; +reply = "PIR"; +}data = source.nextLine (); +if (data.indexOf (">") > -1) { +reply = "BLC"; +} else { +var data1 = source.nextLine (); +var data2 = source.nextLine (); +var c1; +if (checkPIR) { +starterm = (data1 != null && data1.indexOf ("*") > -1) || (data2 != null && data2.indexOf ("*") > -1); +}if (data2 != null && (c1 = data.indexOf ("*")) > -1) { +if (c1 == 0 && c1 == data2.indexOf ("*")) { +reply = "BLC"; +} else { +reply = "FASTA"; +}} else { +reply = "FASTA"; +if (!checkPIR) { +break; +}}}if (checkPIR) { +var dta = null; +if (!starterm) { +do { +try { +dta = source.nextLine (); +} catch (ex) { +if (Clazz.exceptionOf (ex, java.io.IOException)) { +} else { +throw ex; +} +} +;if (dta != null && dta.indexOf ("*") > -1) { +starterm = true; +}} while (dta != null && !starterm); +}if (starterm) { +reply = "PIR"; +break; +} else { +reply = "FASTA"; +}}break; +} else if (data.indexOf ("HEADER") == 0 || data.indexOf ("ATOM") == 0) { +reply = "PDB"; +break; +} else if (data.matches ("\\s*\\d+\\s+\\d+\\s*")) { +reply = "PHYLIP"; +break; +} else if (!lineswereskipped && data.charAt (0) != '*' && data.charAt (0) != ' ' && data.indexOf (":") < data.indexOf (",")) { +reply = "JnetFile"; +break; +}lineswereskipped = true; +} +if (closeSource) { +source.close (); +} else { +source.reset (); +}} catch (ex) { +if (Clazz.exceptionOf (ex, Exception)) { +System.err.println ("File Identification failed!\n" + ex); +return source.errormessage; +} else { +throw ex; +} +} +if (length == 0) { +System.err.println ("File Identification failed! - Empty file was read."); +return "EMPTY DATA FILE"; +}return reply; +}, "jalview.io.FileParse,~B"); +Clazz.defineStatics (c$, +"GFF3File", "GFF v2 or v3"); +});