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