JAL-1807 test
[jalviewjs.git] / bin / jalview / io / IdentifyFile.js
1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (null, "jalview.io.IdentifyFile", ["jalview.io.FileParse"], function () {
3 c$ = Clazz.declareType (jalview.io, "IdentifyFile");
4 Clazz.defineMethod (c$, "Identify", 
5 function (file, protocol) {
6 var emessage = "UNIDENTIFIED FILE PARSING ERROR";
7 var parser = null;
8 try {
9 parser =  new jalview.io.FileParse (file, protocol);
10 if (parser.isValid ()) {
11 return this.Identify (parser);
12 }} catch (e) {
13 if (Clazz.exceptionOf (e, Exception)) {
14 System.err.println ("Error whilst identifying");
15 e.printStackTrace (System.err);
16 emessage = e.getMessage ();
17 } else {
18 throw e;
19 }
20 }
21 if (parser != null) {
22 return parser.errormessage;
23 }return emessage;
24 }, "~S,~S");
25 Clazz.defineMethod (c$, "Identify", 
26 function (source) {
27 return this.Identify (source, true);
28 }, "jalview.io.FileParse");
29 Clazz.defineMethod (c$, "Identify", 
30 function (source, closeSource) {
31 var reply = "PFAM";
32 var data;
33 var length = 0;
34 var lineswereskipped = false;
35 var isBinary = false;
36 try {
37 if (!closeSource) {
38 source.mark ();
39 }while ((data = source.nextLine ()) != null) {
40 length += data.trim ().length;
41 if (!lineswereskipped) {
42 for (var i = 0; !isBinary && i < data.length; i++) {
43 var c = data.charAt (i);
44 isBinary = (c.charCodeAt (0) < 32 && c != '\t' && c != '\n' && c != '\r' && c.charCodeAt (0) != 5 && c.charCodeAt (0) != 27);
45 }
46 }if (isBinary) {
47 if (source.inFile != null) {
48 var fileStr = source.inFile.getName ();
49 if (fileStr.lastIndexOf (".jar") > -1 || fileStr.lastIndexOf (".zip") > -1) {
50 reply = "Jalview";
51 }}if (!lineswereskipped && data.startsWith ("PK")) {
52 reply = "Jalview";
53 break;
54 }}data = data.toUpperCase ();
55 if (data.startsWith ("##GFF-VERSION")) {
56 reply = "GFF v2 or v3";
57 break;
58 }if (data.indexOf ("# STOCKHOLM") > -1) {
59 reply = "STH";
60 break;
61 }if (data.matches ("<HTML(\"[^\"]*\"|'[^']*'|[^'\">])*>")) {
62 reply = "HTML";
63 break;
64 }if (data.matches ("<RNAML (\"[^\"]*\"|'[^']*'|[^'\">])*>")) {
65 reply = "RNAML";
66 break;
67 }if (data.indexOf ("{\"") > -1) {
68 reply = "JSON";
69 break;
70 }if ((data.length < 1) || (data.indexOf ("#") == 0)) {
71 lineswereskipped = true;
72 continue;
73 }if (data.indexOf ("PILEUP") > -1) {
74 reply = "PileUp";
75 break;
76 }if ((data.indexOf ("//") == 0) || ((data.indexOf ("!!") > -1) && (data.indexOf ("!!") < data.indexOf ("_MULTIPLE_ALIGNMENT ")))) {
77 reply = "MSF";
78 break;
79 } else if (data.indexOf ("CLUSTAL") > -1) {
80 reply = "CLUSTAL";
81 break;
82 } else if (data.indexOf (">") > -1) {
83 var checkPIR = false;
84 var starterm = false;
85 if ((data.indexOf (">P1;") > -1) || (data.indexOf (">DL;") > -1)) {
86 checkPIR = true;
87 reply = "PIR";
88 }data = source.nextLine ();
89 if (data.indexOf (">") > -1) {
90 reply = "BLC";
91 } else {
92 var data1 = source.nextLine ();
93 var data2 = source.nextLine ();
94 var c1;
95 if (checkPIR) {
96 starterm = (data1 != null && data1.indexOf ("*") > -1) || (data2 != null && data2.indexOf ("*") > -1);
97 }if (data2 != null && (c1 = data.indexOf ("*")) > -1) {
98 if (c1 == 0 && c1 == data2.indexOf ("*")) {
99 reply = "BLC";
100 } else {
101 reply = "FASTA";
102 }} else {
103 reply = "FASTA";
104 if (!checkPIR) {
105 break;
106 }}}if (checkPIR) {
107 var dta = null;
108 if (!starterm) {
109 do {
110 try {
111 dta = source.nextLine ();
112 } catch (ex) {
113 if (Clazz.exceptionOf (ex, java.io.IOException)) {
114 } else {
115 throw ex;
116 }
117 }
118 ;if (dta != null && dta.indexOf ("*") > -1) {
119 starterm = true;
120 }} while (dta != null && !starterm);
121 }if (starterm) {
122 reply = "PIR";
123 break;
124 } else {
125 reply = "FASTA";
126 }}break;
127 } else if (data.indexOf ("HEADER") == 0 || data.indexOf ("ATOM") == 0) {
128 reply = "PDB";
129 break;
130 } else if (data.matches ("\\s*\\d+\\s+\\d+\\s*")) {
131 reply = "PHYLIP";
132 break;
133 } else if (!lineswereskipped && data.charAt (0) != '*' && data.charAt (0) != ' ' && data.indexOf (":") < data.indexOf (",")) {
134 reply = "JnetFile";
135 break;
136 }lineswereskipped = true;
137 }
138 if (closeSource) {
139 source.close ();
140 } else {
141 source.reset ();
142 }} catch (ex) {
143 if (Clazz.exceptionOf (ex, Exception)) {
144 System.err.println ("File Identification failed!\n" + ex);
145 return source.errormessage;
146 } else {
147 throw ex;
148 }
149 }
150 if (length == 0) {
151 System.err.println ("File Identification failed! - Empty file was read.");
152 return "EMPTY DATA FILE";
153 }return reply;
154 }, "jalview.io.FileParse,~B");
155 Clazz.defineStatics (c$,
156 "GFF3File", "GFF v2 or v3");
157 });