JAL-1919 basic support for importing and viewing mmCIF files - mapping support not...
[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.GFF3File, "HTML", "mmCIF" };
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, "cif" };
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.GFF3File,
107       "Jalview", HtmlFile.FILE_DESC, "mmCIF" };
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         // TODO obtain config value from preference settings.
281         // Set value to 'true' to test PDB processing with Jmol: JAL-1213
282         boolean isParseWithJMOL = false;
283         if (isParseWithJMOL)
284         {
285           alignFile = new jalview.ext.jmol.JmolParser(inFile, type);
286         }
287         else
288         {
289           alignFile = new MCview.PDBfile(annotFromStructure,
290                   localSecondaryStruct, serviceSecondaryStruct, inFile,
291                   type);
292         }
293       }
294       else if (format.equals("mmCIF"))
295       {
296         alignFile = new jalview.ext.jmol.JmolParser(inFile, type);
297       }
298       else if (format.equals("STH"))
299       {
300         alignFile = new StockholmFile(inFile, type);
301       }
302       else if (format.equals("SimpleBLAST"))
303       {
304         alignFile = new SimpleBlastFile(inFile, type);
305       }
306       else if (format.equals(PhylipFile.FILE_DESC))
307       {
308         alignFile = new PhylipFile(inFile, type);
309       }
310       else if (format.equals(JSONFile.FILE_DESC))
311       {
312         alignFile = new JSONFile(inFile, type);
313       }
314       else if (format.equals(HtmlFile.FILE_DESC))
315       {
316         alignFile = new HtmlFile(inFile, type);
317       }
318       else if (format.equals("RNAML"))
319       {
320         alignFile = new RnamlFile(inFile, type);
321       }
322       else if (format.equals(IdentifyFile.GFF3File))
323       {
324         alignFile = new Gff3File(inFile, type);
325       }
326       return buildAlignmentFrom(alignFile);
327     } catch (Exception e)
328     {
329       e.printStackTrace();
330       System.err.println("Failed to read alignment using the '" + format
331               + "' reader.\n" + e);
332
333       if (e.getMessage() != null
334               && e.getMessage().startsWith(INVALID_CHARACTERS))
335       {
336         throw new java.io.IOException(e.getMessage());
337       }
338
339       // Finally test if the user has pasted just the sequence, no id
340       if (type.equalsIgnoreCase("Paste"))
341       {
342         try
343         {
344           // Possible sequence is just residues with no label
345           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
346           return buildAlignmentFrom(alignFile);
347
348         } catch (Exception ex)
349         {
350           if (ex.toString().startsWith(INVALID_CHARACTERS))
351           {
352             throw new java.io.IOException(e.getMessage());
353           }
354
355           ex.printStackTrace();
356         }
357       }
358       if (format.equalsIgnoreCase("HTML"))
359       {
360         throw new IOException(e.getMessage());
361       }
362       // If we get to this stage, the format was not supported
363       throw new java.io.IOException(SUPPORTED_FORMATS);
364     }
365   }
366
367   /**
368    * Constructs the correct filetype parser for an already open datasource
369    *
370    * @param source
371    *          an existing datasource
372    * @param format
373    *          File format of data that will be provided by datasource
374    *
375    * @return DOCUMENT ME!
376    */
377   public AlignmentI readFromFile(FileParse source, String format)
378           throws java.io.IOException
379   {
380     // TODO: generalise mapping between format string and io. class instances
381     // using Constructor.invoke reflection
382     // This is exactly the same as the readFile method except we substitute
383     // 'inFile, type' with 'source'
384     this.inFile = source.getInFile();
385     String type = source.type;
386     try
387     {
388       if (format.equals("FASTA"))
389       {
390         alignFile = new FastaFile(source);
391       }
392       else if (format.equals("MSF"))
393       {
394         alignFile = new MSFfile(source);
395       }
396       else if (format.equals("PileUp"))
397       {
398         alignFile = new PileUpfile(source);
399       }
400       else if (format.equals("CLUSTAL"))
401       {
402         alignFile = new ClustalFile(source);
403       }
404       else if (format.equals("BLC"))
405       {
406         alignFile = new BLCFile(source);
407       }
408       else if (format.equals("PIR"))
409       {
410         alignFile = new PIRFile(source);
411       }
412       else if (format.equals("PFAM"))
413       {
414         alignFile = new PfamFile(source);
415       }
416       else if (format.equals("JnetFile"))
417       {
418         alignFile = new JPredFile(source);
419         ((JPredFile) alignFile).removeNonSequences();
420       }
421       else if (format.equals("PDB"))
422       {
423         // TODO obtain config value from preference settings
424         boolean isParseWithJMOL = false;
425         if (isParseWithJMOL)
426         {
427           alignFile = new jalview.ext.jmol.JmolParser(source);
428         }
429         else
430         {
431           alignFile = new MCview.PDBfile(annotFromStructure,
432                   localSecondaryStruct, serviceSecondaryStruct, source);
433         }
434       }
435       else if (format.equals("mmCIF"))
436       {
437         alignFile = new jalview.ext.jmol.JmolParser(source);
438       }
439       else if (format.equals("STH"))
440       {
441         alignFile = new StockholmFile(source);
442       }
443       else if (format.equals("RNAML"))
444       {
445         alignFile = new RnamlFile(source);
446       }
447       else if (format.equals("SimpleBLAST"))
448       {
449         alignFile = new SimpleBlastFile(source);
450       }
451       else if (format.equals(PhylipFile.FILE_DESC))
452       {
453         alignFile = new PhylipFile(source);
454       }
455       else if (format.equals(IdentifyFile.GFF3File))
456       {
457         alignFile = new Gff3File(inFile, type);
458       }
459       else if (format.equals(JSONFile.FILE_DESC))
460       {
461         alignFile = new JSONFile(source);
462       }
463       else if (format.equals(HtmlFile.FILE_DESC))
464       {
465         alignFile = new HtmlFile(source);
466       }
467
468       return buildAlignmentFrom(alignFile);
469
470     } catch (Exception e)
471     {
472       e.printStackTrace();
473       System.err.println("Failed to read alignment using the '" + format
474               + "' reader.\n" + e);
475
476       if (e.getMessage() != null
477               && e.getMessage().startsWith(INVALID_CHARACTERS))
478       {
479         throw new java.io.IOException(e.getMessage());
480       }
481
482       // Finally test if the user has pasted just the sequence, no id
483       if (type.equalsIgnoreCase("Paste"))
484       {
485         try
486         {
487           // Possible sequence is just residues with no label
488           alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
489           return buildAlignmentFrom(alignFile);
490
491         } catch (Exception ex)
492         {
493           if (ex.toString().startsWith(INVALID_CHARACTERS))
494           {
495             throw new java.io.IOException(e.getMessage());
496           }
497
498           ex.printStackTrace();
499         }
500       }
501
502       // If we get to this stage, the format was not supported
503       throw new java.io.IOException(SUPPORTED_FORMATS);
504     }
505   }
506
507   /**
508    * boilerplate method to handle data from an AlignFile and construct a new
509    * alignment or import to an existing alignment
510    * 
511    * @param alignFile2
512    * @return AlignmentI instance ready to pass to a UI constructor
513    */
514   private AlignmentI buildAlignmentFrom(AlignFile alignFile2)
515   {
516     // Standard boilerplate for creating alignment from parser
517     // alignFile.configureForView(viewpanel);
518
519     AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
520
521     alignFile.addAnnotations(al);
522
523     alignFile.addGroups(al);
524
525     return al;
526   }
527
528   /**
529    * create an alignment flatfile from a Jalview alignment view
530    * 
531    * @param format
532    * @param jvsuffix
533    * @param av
534    * @param selectedOnly
535    * @return flatfile in a string
536    */
537   public String formatSequences(String format, boolean jvsuffix,
538           AlignmentViewPanel ap, boolean selectedOnly)
539   {
540
541     AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
542             selectedOnly, false);
543     AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
544             .getGapCharacter());
545     List<AlignmentAnnotation> ala = (ap.getAlignViewport()
546             .getVisibleAlignmentAnnotation(selectedOnly));
547     if (ala != null)
548     {
549       for (AlignmentAnnotation aa : ala)
550       {
551         aselview.addAnnotation(aa);
552       }
553     }
554     viewpanel = ap;
555     return formatSequences(format, aselview, jvsuffix);
556   }
557
558   /**
559    * Construct an output class for an alignment in a particular filetype TODO:
560    * allow caller to detect errors and warnings encountered when generating
561    * output
562    *
563    * @param format
564    *          string name of alignment format
565    * @param alignment
566    *          the alignment to be written out
567    * @param jvsuffix
568    *          passed to AlnFile class controls whether /START-END is added to
569    *          sequence names
570    *
571    * @return alignment flat file contents
572    */
573   public String formatSequences(String format, AlignmentI alignment,
574           boolean jvsuffix)
575   {
576     try
577     {
578       AlignFile afile = null;
579       if (format.equalsIgnoreCase("FASTA"))
580       {
581         afile = new FastaFile();
582       }
583       else if (format.equalsIgnoreCase("MSF"))
584       {
585         afile = new MSFfile();
586       }
587       else if (format.equalsIgnoreCase("PileUp"))
588       {
589         afile = new PileUpfile();
590       }
591       else if (format.equalsIgnoreCase("CLUSTAL"))
592       {
593         afile = new ClustalFile();
594       }
595       else if (format.equalsIgnoreCase("BLC"))
596       {
597         afile = new BLCFile();
598       }
599       else if (format.equalsIgnoreCase("PIR"))
600       {
601         afile = new PIRFile();
602       }
603       else if (format.equalsIgnoreCase("PFAM"))
604       {
605         afile = new PfamFile();
606       }
607       else if (format.equalsIgnoreCase("STH"))
608       {
609         afile = new StockholmFile(alignment);
610       }
611       else if (format.equalsIgnoreCase("AMSA"))
612       {
613         afile = new AMSAFile(alignment);
614       }
615       else if (format.equalsIgnoreCase(PhylipFile.FILE_DESC))
616       {
617         afile = new PhylipFile();
618       }
619       else if (format.equalsIgnoreCase(JSONFile.FILE_DESC))
620       {
621         afile = new JSONFile();
622       }
623       else if (format.equalsIgnoreCase("RNAML"))
624       {
625         afile = new RnamlFile();
626       }
627
628       else
629       {
630         throw new Exception(
631                 MessageManager
632                         .getString("error.implementation_error_unknown_file_format_string"));
633       }
634
635       afile.setNewlineString(newline);
636       afile.addJVSuffix(jvsuffix);
637       afile.setExportSettings(exportSettings);
638       afile.configureForView(viewpanel);
639
640       // check whether we were given a specific alignment to export, rather than
641       // the one in the viewpanel
642       if (viewpanel == null || viewpanel.getAlignment() == null
643               || viewpanel.getAlignment() != alignment)
644       {
645         afile.setSeqs(alignment.getSequencesArray());
646       }
647       else
648       {
649         afile.setSeqs(viewpanel.getAlignment().getSequencesArray());
650       }
651
652       String afileresp = afile.print();
653       if (afile.hasWarningMessage())
654       {
655         System.err.println("Warning raised when writing as " + format
656                 + " : " + afile.getWarningMessage());
657       }
658       return afileresp;
659     } catch (Exception e)
660     {
661       System.err.println("Failed to write alignment as a '" + format
662               + "' file\n");
663       e.printStackTrace();
664     }
665
666     return null;
667   }
668
669   public static String checkProtocol(String file)
670   {
671     String protocol = FILE;
672     String ft = file.toLowerCase().trim();
673     if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
674             || ft.indexOf("file:") == 0)
675     {
676       protocol = URL;
677     }
678     return protocol;
679   }
680
681   public static void main(String[] args)
682   {
683     int i = 0;
684     while (i < args.length)
685     {
686       File f = new File(args[i]);
687       if (f.exists())
688       {
689         try
690         {
691           System.out.println("Reading file: " + f);
692           AppletFormatAdapter afa = new AppletFormatAdapter();
693           Runtime r = Runtime.getRuntime();
694           System.gc();
695           long memf = -r.totalMemory() + r.freeMemory();
696           long t1 = -System.currentTimeMillis();
697           AlignmentI al = afa.readFile(args[i], FILE,
698                   new IdentifyFile().Identify(args[i], FILE));
699           t1 += System.currentTimeMillis();
700           System.gc();
701           memf += r.totalMemory() - r.freeMemory();
702           if (al != null)
703           {
704             System.out.println("Alignment contains " + al.getHeight()
705                     + " sequences and " + al.getWidth() + " columns.");
706             try
707             {
708               System.out.println(new AppletFormatAdapter().formatSequences(
709                       "FASTA", al, true));
710             } catch (Exception e)
711             {
712               System.err
713                       .println("Couln't format the alignment for output as a FASTA file.");
714               e.printStackTrace(System.err);
715             }
716           }
717           else
718           {
719             System.out.println("Couldn't read alignment");
720           }
721           System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
722           System.out
723                   .println("Difference between free memory now and before is "
724                           + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
725         } catch (Exception e)
726         {
727           System.err.println("Exception when dealing with " + i
728                   + "'th argument: " + args[i] + "\n" + e);
729         }
730       }
731       else
732       {
733         System.err.println("Ignoring argument '" + args[i] + "' (" + i
734                 + "'th)- not a readable file.");
735       }
736       i++;
737     }
738   }
739
740   /**
741    * try to discover how to access the given file as a valid datasource that
742    * will be identified as the given type.
743    *
744    * @param file
745    * @param format
746    * @return protocol that yields the data parsable as the given type
747    */
748   public static String resolveProtocol(String file, String format)
749   {
750     return resolveProtocol(file, format, false);
751   }
752
753   public static String resolveProtocol(String file, String format,
754           boolean debug)
755   {
756     // TODO: test thoroughly!
757     String protocol = null;
758     if (debug)
759     {
760       System.out.println("resolving datasource started with:\n>>file\n"
761               + file + ">>endfile");
762     }
763
764     // This might throw a security exception in certain browsers
765     // Netscape Communicator for instance.
766     try
767     {
768       boolean rtn = false;
769       InputStream is = System.getSecurityManager().getClass()
770               .getResourceAsStream("/" + file);
771       if (is != null)
772       {
773         rtn = true;
774         is.close();
775       }
776       if (debug)
777       {
778         System.err.println("Resource '" + file + "' was "
779                 + (rtn ? "" : "not") + " located by classloader.");
780       }
781       ;
782       if (rtn)
783       {
784         protocol = AppletFormatAdapter.CLASSLOADER;
785       }
786
787     } catch (Exception ex)
788     {
789       System.err
790               .println("Exception checking resources: " + file + " " + ex);
791     }
792
793     if (file.indexOf("://") > -1)
794     {
795       protocol = AppletFormatAdapter.URL;
796     }
797     else
798     {
799       // skipping codebase prepend check.
800       protocol = AppletFormatAdapter.FILE;
801     }
802     FileParse fp = null;
803     try
804     {
805       if (debug)
806       {
807         System.out.println("Trying to get contents of resource as "
808                 + protocol + ":");
809       }
810       fp = new FileParse(file, protocol);
811       if (!fp.isValid())
812       {
813         fp = null;
814       }
815       else
816       {
817         if (debug)
818         {
819           System.out.println("Successful.");
820         }
821       }
822     } catch (Exception e)
823     {
824       if (debug)
825       {
826         System.err.println("Exception when accessing content: " + e);
827       }
828       fp = null;
829     }
830     if (fp == null)
831     {
832       if (debug)
833       {
834         System.out.println("Accessing as paste.");
835       }
836       protocol = AppletFormatAdapter.PASTE;
837       fp = null;
838       try
839       {
840         fp = new FileParse(file, protocol);
841         if (!fp.isValid())
842         {
843           fp = null;
844         }
845       } catch (Exception e)
846       {
847         System.err.println("Failed to access content as paste!");
848         e.printStackTrace();
849         fp = null;
850       }
851     }
852     if (fp == null)
853     {
854       return null;
855     }
856     if (format == null || format.length() == 0)
857     {
858       return protocol;
859     }
860     else
861     {
862       try
863       {
864         String idformat = new jalview.io.IdentifyFile().Identify(file,
865                 protocol);
866         if (idformat == null)
867         {
868           if (debug)
869           {
870             System.out.println("Format not identified. Inaccessible file.");
871           }
872           return null;
873         }
874         if (debug)
875         {
876           System.out.println("Format identified as " + idformat
877                   + "and expected as " + format);
878         }
879         if (idformat.equals(format))
880         {
881           if (debug)
882           {
883             System.out.println("Protocol identified as " + protocol);
884           }
885           return protocol;
886         }
887         else
888         {
889           if (debug)
890           {
891             System.out
892                     .println("File deemed not accessible via " + protocol);
893           }
894           fp.close();
895           return null;
896         }
897       } catch (Exception e)
898       {
899         if (debug)
900         {
901           System.err.println("File deemed not accessible via " + protocol);
902           e.printStackTrace();
903         }
904         ;
905
906       }
907     }
908     return null;
909   }
910
911   public AlignFile getAlignFile()
912   {
913     return alignFile;
914   }
915
916   public void setAlignFile(AlignFile alignFile)
917   {
918     this.alignFile = alignFile;
919   }
920 }