4b7e26eec12cb5773a7b76ed081c5ecade318690
[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         // MC this is pointless? Only performed if viewpanel.getAlignment() ==
640         // alignment i.e. the same case as the if condition
641         afile.setSeqs(viewpanel.getAlignment().getSequencesArray());
642       }
643
644       String afileresp = afile.print(alignment);
645       if (afile.hasWarningMessage())
646       {
647         System.err.println("Warning raised when writing as " + format
648                 + " : " + afile.getWarningMessage());
649       }
650       return afileresp;
651     } catch (Exception e)
652     {
653       System.err.println("Failed to write alignment as a '" + format
654               + "' file\n");
655       e.printStackTrace();
656     }
657
658     return null;
659   }
660
661   public static String checkProtocol(String file)
662   {
663     String protocol = FILE;
664     String ft = file.toLowerCase().trim();
665     if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
666             || ft.indexOf("file:") == 0)
667     {
668       protocol = URL;
669     }
670     return protocol;
671   }
672
673   public static void main(String[] args)
674   {
675     int i = 0;
676     while (i < args.length)
677     {
678       File f = new File(args[i]);
679       if (f.exists())
680       {
681         try
682         {
683           System.out.println("Reading file: " + f);
684           AppletFormatAdapter afa = new AppletFormatAdapter();
685           Runtime r = Runtime.getRuntime();
686           System.gc();
687           long memf = -r.totalMemory() + r.freeMemory();
688           long t1 = -System.currentTimeMillis();
689           AlignmentI al = afa.readFile(args[i], FILE,
690                   new IdentifyFile().Identify(args[i], FILE));
691           t1 += System.currentTimeMillis();
692           System.gc();
693           memf += r.totalMemory() - r.freeMemory();
694           if (al != null)
695           {
696             System.out.println("Alignment contains " + al.getHeight()
697                     + " sequences and " + al.getWidth() + " columns.");
698             try
699             {
700               System.out.println(new AppletFormatAdapter().formatSequences(
701                       "FASTA", al, true));
702             } catch (Exception e)
703             {
704               System.err
705                       .println("Couln't format the alignment for output as a FASTA file.");
706               e.printStackTrace(System.err);
707             }
708           }
709           else
710           {
711             System.out.println("Couldn't read alignment");
712           }
713           System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
714           System.out
715                   .println("Difference between free memory now and before is "
716                           + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
717         } catch (Exception e)
718         {
719           System.err.println("Exception when dealing with " + i
720                   + "'th argument: " + args[i] + "\n" + e);
721         }
722       }
723       else
724       {
725         System.err.println("Ignoring argument '" + args[i] + "' (" + i
726                 + "'th)- not a readable file.");
727       }
728       i++;
729     }
730   }
731
732   /**
733    * try to discover how to access the given file as a valid datasource that
734    * will be identified as the given type.
735    *
736    * @param file
737    * @param format
738    * @return protocol that yields the data parsable as the given type
739    */
740   public static String resolveProtocol(String file, String format)
741   {
742     return resolveProtocol(file, format, false);
743   }
744
745   public static String resolveProtocol(String file, String format,
746           boolean debug)
747   {
748     // TODO: test thoroughly!
749     String protocol = null;
750     if (debug)
751     {
752       System.out.println("resolving datasource started with:\n>>file\n"
753               + file + ">>endfile");
754     }
755
756     // This might throw a security exception in certain browsers
757     // Netscape Communicator for instance.
758     try
759     {
760       boolean rtn = false;
761       InputStream is = System.getSecurityManager().getClass()
762               .getResourceAsStream("/" + file);
763       if (is != null)
764       {
765         rtn = true;
766         is.close();
767       }
768       if (debug)
769       {
770         System.err.println("Resource '" + file + "' was "
771                 + (rtn ? "" : "not") + " located by classloader.");
772       }
773       ;
774       if (rtn)
775       {
776         protocol = AppletFormatAdapter.CLASSLOADER;
777       }
778
779     } catch (Exception ex)
780     {
781       System.err
782               .println("Exception checking resources: " + file + " " + ex);
783     }
784
785     if (file.indexOf("://") > -1)
786     {
787       protocol = AppletFormatAdapter.URL;
788     }
789     else
790     {
791       // skipping codebase prepend check.
792       protocol = AppletFormatAdapter.FILE;
793     }
794     FileParse fp = null;
795     try
796     {
797       if (debug)
798       {
799         System.out.println("Trying to get contents of resource as "
800                 + protocol + ":");
801       }
802       fp = new FileParse(file, protocol);
803       if (!fp.isValid())
804       {
805         fp = null;
806       }
807       else
808       {
809         if (debug)
810         {
811           System.out.println("Successful.");
812         }
813       }
814     } catch (Exception e)
815     {
816       if (debug)
817       {
818         System.err.println("Exception when accessing content: " + e);
819       }
820       fp = null;
821     }
822     if (fp == null)
823     {
824       if (debug)
825       {
826         System.out.println("Accessing as paste.");
827       }
828       protocol = AppletFormatAdapter.PASTE;
829       fp = null;
830       try
831       {
832         fp = new FileParse(file, protocol);
833         if (!fp.isValid())
834         {
835           fp = null;
836         }
837       } catch (Exception e)
838       {
839         System.err.println("Failed to access content as paste!");
840         e.printStackTrace();
841         fp = null;
842       }
843     }
844     if (fp == null)
845     {
846       return null;
847     }
848     if (format == null || format.length() == 0)
849     {
850       return protocol;
851     }
852     else
853     {
854       try
855       {
856         String idformat = new jalview.io.IdentifyFile().Identify(file,
857                 protocol);
858         if (idformat == null)
859         {
860           if (debug)
861           {
862             System.out.println("Format not identified. Inaccessible file.");
863           }
864           return null;
865         }
866         if (debug)
867         {
868           System.out.println("Format identified as " + idformat
869                   + "and expected as " + format);
870         }
871         if (idformat.equals(format))
872         {
873           if (debug)
874           {
875             System.out.println("Protocol identified as " + protocol);
876           }
877           return protocol;
878         }
879         else
880         {
881           if (debug)
882           {
883             System.out
884                     .println("File deemed not accessible via " + protocol);
885           }
886           fp.close();
887           return null;
888         }
889       } catch (Exception e)
890       {
891         if (debug)
892         {
893           System.err.println("File deemed not accessible via " + protocol);
894           e.printStackTrace();
895         }
896         ;
897
898       }
899     }
900     return null;
901   }
902
903   public AlignFile getAlignFile()
904   {
905     return alignFile;
906   }
907
908   public void setAlignFile(AlignFile alignFile)
909   {
910     this.alignFile = alignFile;
911   }
912 }