JAL-653 JAL-1968 FeaturesFile now handles Jalview or GFF2 or GFF3
[jalview.git] / src / jalview / io / AppletFormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * Jalview is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE.  See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.util.MessageManager;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.List;
35
36 /**
37  * A low level class for alignment and feature IO with alignment formatting
38  * methods used by both applet and application for generating flat alignment
39  * files. It also holds the lists of magic format names that the applet and
40  * application will allow the user to read or write files with.
41  *
42  * @author $author$
43  * @version $Revision$
44  */
45 public class AppletFormatAdapter
46 {
47   private AlignmentViewPanel viewpanel;
48
49   public static String FILE = "File";
50
51   public static String URL = "URL";
52
53   public static String PASTE = "Paste";
54
55   public static String CLASSLOADER = "ClassLoader";
56
57   /**
58    * add jalview-derived non-secondary structure annotation from PDB structure
59    */
60   boolean annotFromStructure = false;
61
62   /**
63    * add secondary structure from PDB data with built-in algorithms
64    */
65   boolean localSecondaryStruct = false;
66
67   /**
68    * process PDB data with web services
69    */
70   boolean serviceSecondaryStruct = false;
71
72   private AlignFile alignFile = null;
73
74   String inFile;
75
76   /**
77    * character used to write newlines
78    */
79   protected String newline = System.getProperty("line.separator");
80
81   private AlignExportSettingI exportSettings;
82
83   /**
84    * List of valid format strings used in the isValidFormat method
85    */
86   public static final String[] READABLE_FORMATS = new String[] { "BLC",
87       "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB",
88       "JnetFile", "RNAML", PhylipFile.FILE_DESC, JSONFile.FILE_DESC,
89       IdentifyFile.FeaturesFile, "HTML" };
90
91   /**
92    * List of readable format file extensions by application in order
93    * corresponding to READABLE_FNAMES
94    */
95   public static final String[] READABLE_EXTENSIONS = new String[] {
96       "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
97       "sto,stk", "xml,rnaml", PhylipFile.FILE_EXT, JSONFile.FILE_EXT,
98       ".gff2,gff3", "jar,jvp", HtmlFile.FILE_EXT };
99
100   /**
101    * List of readable formats by application in order corresponding to
102    * READABLE_EXTENSIONS
103    */
104   public static final String[] READABLE_FNAMES = new String[] { "Fasta",
105       "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Stockholm", "RNAML",
106       PhylipFile.FILE_DESC, JSONFile.FILE_DESC, IdentifyFile.FeaturesFile,
107       "Jalview", HtmlFile.FILE_DESC };
108
109   /**
110    * List of valid format strings for use by callers of the formatSequences
111    * method
112    */
113   public static final String[] WRITEABLE_FORMATS = new String[] { "BLC",
114       "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA", "STH",
115       PhylipFile.FILE_DESC, JSONFile.FILE_DESC };
116
117   /**
118    * List of extensions corresponding to file format types in WRITABLE_FNAMES
119    * that are writable by the application.
120    */
121   public static final String[] WRITABLE_EXTENSIONS = new String[] {
122       "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
123       "sto,stk", PhylipFile.FILE_EXT, JSONFile.FILE_EXT, "jvp" };
124
125   /**
126    * List of writable formats by the application. Order must correspond with the
127    * WRITABLE_EXTENSIONS list of formats.
128    */
129   public static final String[] WRITABLE_FNAMES = new String[] { "Fasta",
130       "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH",
131       PhylipFile.FILE_DESC, JSONFile.FILE_DESC, "Jalview" };
132
133   public static String INVALID_CHARACTERS = "Contains invalid characters";
134
135   // TODO: make these messages dynamic
136   public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
137           + prettyPrint(READABLE_FORMATS);
138
139   public AppletFormatAdapter()
140   {
141   }
142
143   public AppletFormatAdapter(AlignmentViewPanel viewpanel)
144   {
145     this.viewpanel = viewpanel;
146   }
147
148   public AppletFormatAdapter(AlignmentViewPanel alignPanel,
149           AlignExportSettingI settings)
150   {
151     viewpanel = alignPanel;
152     exportSettings = settings;
153   }
154
155   /**
156    *
157    * @param els
158    * @return grammatically correct(ish) list consisting of els elements.
159    */
160   public static String prettyPrint(String[] els)
161   {
162     StringBuffer list = new StringBuffer();
163     for (int i = 0, iSize = els.length - 1; i < iSize; i++)
164     {
165       list.append(els[i]);
166       list.append(", ");
167     }
168     list.append(" and " + els[els.length - 1] + ".");
169     return list.toString();
170   }
171
172   public void setNewlineString(String nl)
173   {
174     newline = nl;
175   }
176
177   public String getNewlineString()
178   {
179     return newline;
180   }
181
182   /**
183    * check that this format is valid for reading
184    *
185    * @param format
186    *          a format string to be compared with READABLE_FORMATS
187    * @return true if format is readable
188    */
189   public static final boolean isValidFormat(String format)
190   {
191     return isValidFormat(format, false);
192   }
193
194   /**
195    * validate format is valid for IO
196    *
197    * @param format
198    *          a format string to be compared with either READABLE_FORMATS or
199    *          WRITEABLE_FORMATS
200    * @param forwriting
201    *          when true, format is checked for containment in WRITEABLE_FORMATS
202    * @return true if format is valid
203    */
204   public static final boolean isValidFormat(String format,
205           boolean forwriting)
206   {
207     if (format == null)
208     {
209       return false;
210     }
211     boolean valid = false;
212     String[] format_list = (forwriting) ? WRITEABLE_FORMATS
213             : READABLE_FORMATS;
214     for (String element : format_list)
215     {
216       if (element.equalsIgnoreCase(format))
217       {
218         return true;
219       }
220     }
221
222     return valid;
223   }
224
225   /**
226    * Constructs the correct filetype parser for a characterised datasource
227    *
228    * @param inFile
229    *          data/data location
230    * @param type
231    *          type of datasource
232    * @param format
233    *          File format of data provided by datasource
234    *
235    * @return DOCUMENT ME!
236    */
237   public AlignmentI readFile(String inFile, String type, String format)
238           throws java.io.IOException
239   {
240     // TODO: generalise mapping between format string and io. class instances
241     // using Constructor.invoke reflection
242     this.inFile = inFile;
243     try
244     {
245       if (format.equals("FASTA"))
246       {
247         alignFile = new FastaFile(inFile, type);
248       }
249       else if (format.equals("MSF"))
250       {
251         alignFile = new MSFfile(inFile, type);
252       }
253       else if (format.equals("PileUp"))
254       {
255         alignFile = new PileUpfile(inFile, type);
256       }
257       else if (format.equals("CLUSTAL"))
258       {
259         alignFile = new ClustalFile(inFile, type);
260       }
261       else if (format.equals("BLC"))
262       {
263         alignFile = new BLCFile(inFile, type);
264       }
265       else if (format.equals("PIR"))
266       {
267         alignFile = new PIRFile(inFile, type);
268       }
269       else if (format.equals("PFAM"))
270       {
271         alignFile = new PfamFile(inFile, type);
272       }
273       else if (format.equals("JnetFile"))
274       {
275         alignFile = new JPredFile(inFile, type);
276         ((JPredFile) alignFile).removeNonSequences();
277       }
278       else if (format.equals("PDB"))
279       {
280         alignFile = new MCview.PDBfile(annotFromStructure,
281                 localSecondaryStruct, serviceSecondaryStruct, inFile, type);
282         // Uncomment to test Jmol data based PDB processing: JAL-1213
283         // afile = new jalview.ext.jmol.PDBFileWithJmol(inFile, type);
284       }
285       else if (format.equals("STH"))
286       {
287         alignFile = new StockholmFile(inFile, type);
288       }
289       else if (format.equals("SimpleBLAST"))
290       {
291         alignFile = new SimpleBlastFile(inFile, type);
292       }
293       else if (format.equals(PhylipFile.FILE_DESC))
294       {
295         alignFile = new PhylipFile(inFile, type);
296       }
297       else if (format.equals(JSONFile.FILE_DESC))
298       {
299         alignFile = new JSONFile(inFile, type);
300       }
301       else if (format.equals(HtmlFile.FILE_DESC))
302       {
303         alignFile = new HtmlFile(inFile, type);
304       }
305       else if (format.equals("RNAML"))
306       {
307         alignFile = new RnamlFile(inFile, type);
308       }
309       else if (format.equals(IdentifyFile.FeaturesFile))
310       {
311         alignFile = new FeaturesFile(true, inFile, type);
312       }
313       return buildAlignmentFrom(alignFile);
314     } catch (Exception e)
315     {
316       e.printStackTrace();
317       System.err.println("Failed to read alignment using the '" + format
318               + "' reader.\n" + e);
319
320       if (e.getMessage() != null
321               && e.getMessage().startsWith(INVALID_CHARACTERS))
322       {
323         throw new java.io.IOException(e.getMessage());
324       }
325
326       // Finally test if the user has pasted just the sequence, no id
327       if (type.equalsIgnoreCase("Paste"))
328       {
329         try
330         {
331           // Possible sequence is just residues with no label
332           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
333           return buildAlignmentFrom(alignFile);
334
335         } catch (Exception ex)
336         {
337           if (ex.toString().startsWith(INVALID_CHARACTERS))
338           {
339             throw new java.io.IOException(e.getMessage());
340           }
341
342           ex.printStackTrace();
343         }
344       }
345       if (format.equalsIgnoreCase("HTML"))
346       {
347         throw new IOException(e.getMessage());
348       }
349       // If we get to this stage, the format was not supported
350       throw new java.io.IOException(SUPPORTED_FORMATS);
351     }
352   }
353
354   /**
355    * Constructs the correct filetype parser for an already open datasource
356    *
357    * @param source
358    *          an existing datasource
359    * @param format
360    *          File format of data that will be provided by datasource
361    *
362    * @return DOCUMENT ME!
363    */
364   public AlignmentI readFromFile(FileParse source, String format)
365           throws java.io.IOException
366   {
367     // TODO: generalise mapping between format string and io. class instances
368     // using Constructor.invoke reflection
369     // This is exactly the same as the readFile method except we substitute
370     // 'inFile, type' with 'source'
371     this.inFile = source.getInFile();
372     String type = source.type;
373     try
374     {
375       if (format.equals("FASTA"))
376       {
377         alignFile = new FastaFile(source);
378       }
379       else if (format.equals("MSF"))
380       {
381         alignFile = new MSFfile(source);
382       }
383       else if (format.equals("PileUp"))
384       {
385         alignFile = new PileUpfile(source);
386       }
387       else if (format.equals("CLUSTAL"))
388       {
389         alignFile = new ClustalFile(source);
390       }
391       else if (format.equals("BLC"))
392       {
393         alignFile = new BLCFile(source);
394       }
395       else if (format.equals("PIR"))
396       {
397         alignFile = new PIRFile(source);
398       }
399       else if (format.equals("PFAM"))
400       {
401         alignFile = new PfamFile(source);
402       }
403       else if (format.equals("JnetFile"))
404       {
405         alignFile = new JPredFile(source);
406         ((JPredFile) alignFile).removeNonSequences();
407       }
408       else if (format.equals("PDB"))
409       {
410         alignFile = new MCview.PDBfile(annotFromStructure,
411                 localSecondaryStruct, serviceSecondaryStruct, source);
412       }
413       else if (format.equals("STH"))
414       {
415         alignFile = new StockholmFile(source);
416       }
417       else if (format.equals("RNAML"))
418       {
419         alignFile = new RnamlFile(source);
420       }
421       else if (format.equals("SimpleBLAST"))
422       {
423         alignFile = new SimpleBlastFile(source);
424       }
425       else if (format.equals(PhylipFile.FILE_DESC))
426       {
427         alignFile = new PhylipFile(source);
428       }
429       else if (format.equals(IdentifyFile.FeaturesFile))
430       {
431         // enforce 'parseImmediately' here:
432         alignFile = new FeaturesFile(true, inFile, type);
433       }
434       else if (format.equals(JSONFile.FILE_DESC))
435       {
436         alignFile = new JSONFile(source);
437       }
438       else if (format.equals(HtmlFile.FILE_DESC))
439       {
440         alignFile = new HtmlFile(source);
441       }
442
443       return buildAlignmentFrom(alignFile);
444
445     } catch (Exception e)
446     {
447       e.printStackTrace();
448       System.err.println("Failed to read alignment using the '" + format
449               + "' reader.\n" + e);
450
451       if (e.getMessage() != null
452               && e.getMessage().startsWith(INVALID_CHARACTERS))
453       {
454         throw new java.io.IOException(e.getMessage());
455       }
456
457       // Finally test if the user has pasted just the sequence, no id
458       if (type.equalsIgnoreCase("Paste"))
459       {
460         try
461         {
462           // Possible sequence is just residues with no label
463           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
464           return buildAlignmentFrom(alignFile);
465
466         } catch (Exception ex)
467         {
468           if (ex.toString().startsWith(INVALID_CHARACTERS))
469           {
470             throw new java.io.IOException(e.getMessage());
471           }
472
473           ex.printStackTrace();
474         }
475       }
476
477       // If we get to this stage, the format was not supported
478       throw new java.io.IOException(SUPPORTED_FORMATS);
479     }
480   }
481
482   /**
483    * boilerplate method to handle data from an AlignFile and construct a new
484    * alignment or import to an existing alignment
485    * 
486    * @param alignFile2
487    * @return AlignmentI instance ready to pass to a UI constructor
488    */
489   private AlignmentI buildAlignmentFrom(AlignFile alignFile2)
490   {
491     // Standard boilerplate for creating alignment from parser
492     // alignFile.configureForView(viewpanel);
493
494     AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
495
496     alignFile.addAnnotations(al);
497
498     alignFile.addGroups(al);
499
500     return al;
501   }
502
503   /**
504    * create an alignment flatfile from a Jalview alignment view
505    * 
506    * @param format
507    * @param jvsuffix
508    * @param av
509    * @param selectedOnly
510    * @return flatfile in a string
511    */
512   public String formatSequences(String format, boolean jvsuffix,
513           AlignmentViewPanel ap, boolean selectedOnly)
514   {
515
516     AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
517             selectedOnly, false);
518     AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
519             .getGapCharacter());
520     List<AlignmentAnnotation> ala = (ap.getAlignViewport()
521             .getVisibleAlignmentAnnotation(selectedOnly));
522     if (ala != null)
523     {
524       for (AlignmentAnnotation aa : ala)
525       {
526         aselview.addAnnotation(aa);
527       }
528     }
529     viewpanel = ap;
530     return formatSequences(format, aselview, jvsuffix);
531   }
532
533   /**
534    * Construct an output class for an alignment in a particular filetype TODO:
535    * allow caller to detect errors and warnings encountered when generating
536    * output
537    *
538    * @param format
539    *          string name of alignment format
540    * @param alignment
541    *          the alignment to be written out
542    * @param jvsuffix
543    *          passed to AlnFile class controls whether /START-END is added to
544    *          sequence names
545    *
546    * @return alignment flat file contents
547    */
548   public String formatSequences(String format, AlignmentI alignment,
549           boolean jvsuffix)
550   {
551     try
552     {
553       AlignFile afile = null;
554       if (format.equalsIgnoreCase("FASTA"))
555       {
556         afile = new FastaFile();
557       }
558       else if (format.equalsIgnoreCase("MSF"))
559       {
560         afile = new MSFfile();
561       }
562       else if (format.equalsIgnoreCase("PileUp"))
563       {
564         afile = new PileUpfile();
565       }
566       else if (format.equalsIgnoreCase("CLUSTAL"))
567       {
568         afile = new ClustalFile();
569       }
570       else if (format.equalsIgnoreCase("BLC"))
571       {
572         afile = new BLCFile();
573       }
574       else if (format.equalsIgnoreCase("PIR"))
575       {
576         afile = new PIRFile();
577       }
578       else if (format.equalsIgnoreCase("PFAM"))
579       {
580         afile = new PfamFile();
581       }
582       else if (format.equalsIgnoreCase("STH"))
583       {
584         afile = new StockholmFile(alignment);
585       }
586       else if (format.equalsIgnoreCase("AMSA"))
587       {
588         afile = new AMSAFile(alignment);
589       }
590       else if (format.equalsIgnoreCase(PhylipFile.FILE_DESC))
591       {
592         afile = new PhylipFile();
593       }
594       else if (format.equalsIgnoreCase(JSONFile.FILE_DESC))
595       {
596         afile = new JSONFile();
597       }
598       else if (format.equalsIgnoreCase("RNAML"))
599       {
600         afile = new RnamlFile();
601       }
602
603       else
604       {
605         throw new Exception(
606                 MessageManager
607                         .getString("error.implementation_error_unknown_file_format_string"));
608       }
609
610       afile.setNewlineString(newline);
611       afile.addJVSuffix(jvsuffix);
612       afile.setExportSettings(exportSettings);
613       afile.configureForView(viewpanel);
614
615       // check whether we were given a specific alignment to export, rather than
616       // the one in the viewpanel
617       if (viewpanel == null || viewpanel.getAlignment() == null
618               || viewpanel.getAlignment() != alignment)
619       {
620         afile.setSeqs(alignment.getSequencesArray());
621       }
622       else
623       {
624         afile.setSeqs(viewpanel.getAlignment().getSequencesArray());
625       }
626
627       String afileresp = afile.print();
628       if (afile.hasWarningMessage())
629       {
630         System.err.println("Warning raised when writing as " + format
631                 + " : " + afile.getWarningMessage());
632       }
633       return afileresp;
634     } catch (Exception e)
635     {
636       System.err.println("Failed to write alignment as a '" + format
637               + "' file\n");
638       e.printStackTrace();
639     }
640
641     return null;
642   }
643
644   public static String checkProtocol(String file)
645   {
646     String protocol = FILE;
647     String ft = file.toLowerCase().trim();
648     if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
649             || ft.indexOf("file:") == 0)
650     {
651       protocol = URL;
652     }
653     return protocol;
654   }
655
656   public static void main(String[] args)
657   {
658     int i = 0;
659     while (i < args.length)
660     {
661       File f = new File(args[i]);
662       if (f.exists())
663       {
664         try
665         {
666           System.out.println("Reading file: " + f);
667           AppletFormatAdapter afa = new AppletFormatAdapter();
668           Runtime r = Runtime.getRuntime();
669           System.gc();
670           long memf = -r.totalMemory() + r.freeMemory();
671           long t1 = -System.currentTimeMillis();
672           AlignmentI al = afa.readFile(args[i], FILE,
673                   new IdentifyFile().identify(args[i], FILE));
674           t1 += System.currentTimeMillis();
675           System.gc();
676           memf += r.totalMemory() - r.freeMemory();
677           if (al != null)
678           {
679             System.out.println("Alignment contains " + al.getHeight()
680                     + " sequences and " + al.getWidth() + " columns.");
681             try
682             {
683               System.out.println(new AppletFormatAdapter().formatSequences(
684                       "FASTA", al, true));
685             } catch (Exception e)
686             {
687               System.err
688                       .println("Couln't format the alignment for output as a FASTA file.");
689               e.printStackTrace(System.err);
690             }
691           }
692           else
693           {
694             System.out.println("Couldn't read alignment");
695           }
696           System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
697           System.out
698                   .println("Difference between free memory now and before is "
699                           + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
700         } catch (Exception e)
701         {
702           System.err.println("Exception when dealing with " + i
703                   + "'th argument: " + args[i] + "\n" + e);
704         }
705       }
706       else
707       {
708         System.err.println("Ignoring argument '" + args[i] + "' (" + i
709                 + "'th)- not a readable file.");
710       }
711       i++;
712     }
713   }
714
715   /**
716    * try to discover how to access the given file as a valid datasource that
717    * will be identified as the given type.
718    *
719    * @param file
720    * @param format
721    * @return protocol that yields the data parsable as the given type
722    */
723   public static String resolveProtocol(String file, String format)
724   {
725     return resolveProtocol(file, format, false);
726   }
727
728   public static String resolveProtocol(String file, String format,
729           boolean debug)
730   {
731     // TODO: test thoroughly!
732     String protocol = null;
733     if (debug)
734     {
735       System.out.println("resolving datasource started with:\n>>file\n"
736               + file + ">>endfile");
737     }
738
739     // This might throw a security exception in certain browsers
740     // Netscape Communicator for instance.
741     try
742     {
743       boolean rtn = false;
744       InputStream is = System.getSecurityManager().getClass()
745               .getResourceAsStream("/" + file);
746       if (is != null)
747       {
748         rtn = true;
749         is.close();
750       }
751       if (debug)
752       {
753         System.err.println("Resource '" + file + "' was "
754                 + (rtn ? "" : "not") + " located by classloader.");
755       }
756       ;
757       if (rtn)
758       {
759         protocol = AppletFormatAdapter.CLASSLOADER;
760       }
761
762     } catch (Exception ex)
763     {
764       System.err
765               .println("Exception checking resources: " + file + " " + ex);
766     }
767
768     if (file.indexOf("://") > -1)
769     {
770       protocol = AppletFormatAdapter.URL;
771     }
772     else
773     {
774       // skipping codebase prepend check.
775       protocol = AppletFormatAdapter.FILE;
776     }
777     FileParse fp = null;
778     try
779     {
780       if (debug)
781       {
782         System.out.println("Trying to get contents of resource as "
783                 + protocol + ":");
784       }
785       fp = new FileParse(file, protocol);
786       if (!fp.isValid())
787       {
788         fp = null;
789       }
790       else
791       {
792         if (debug)
793         {
794           System.out.println("Successful.");
795         }
796       }
797     } catch (Exception e)
798     {
799       if (debug)
800       {
801         System.err.println("Exception when accessing content: " + e);
802       }
803       fp = null;
804     }
805     if (fp == null)
806     {
807       if (debug)
808       {
809         System.out.println("Accessing as paste.");
810       }
811       protocol = AppletFormatAdapter.PASTE;
812       fp = null;
813       try
814       {
815         fp = new FileParse(file, protocol);
816         if (!fp.isValid())
817         {
818           fp = null;
819         }
820       } catch (Exception e)
821       {
822         System.err.println("Failed to access content as paste!");
823         e.printStackTrace();
824         fp = null;
825       }
826     }
827     if (fp == null)
828     {
829       return null;
830     }
831     if (format == null || format.length() == 0)
832     {
833       return protocol;
834     }
835     else
836     {
837       try
838       {
839         String idformat = new jalview.io.IdentifyFile().identify(file,
840                 protocol);
841         if (idformat == null)
842         {
843           if (debug)
844           {
845             System.out.println("Format not identified. Inaccessible file.");
846           }
847           return null;
848         }
849         if (debug)
850         {
851           System.out.println("Format identified as " + idformat
852                   + "and expected as " + format);
853         }
854         if (idformat.equals(format))
855         {
856           if (debug)
857           {
858             System.out.println("Protocol identified as " + protocol);
859           }
860           return protocol;
861         }
862         else
863         {
864           if (debug)
865           {
866             System.out
867                     .println("File deemed not accessible via " + protocol);
868           }
869           fp.close();
870           return null;
871         }
872       } catch (Exception e)
873       {
874         if (debug)
875         {
876           System.err.println("File deemed not accessible via " + protocol);
877           e.printStackTrace();
878         }
879         ;
880
881       }
882     }
883     return null;
884   }
885
886   public AlignFile getAlignFile()
887   {
888     return alignFile;
889   }
890
891   public void setAlignFile(AlignFile alignFile)
892   {
893     this.alignFile = alignFile;
894   }
895 }