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