X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FAppletFormatAdapter.java;h=7dc263bdf6261ec8e148bacf7bba6000c82b66ca;hb=2526066acdc3faf3a178e50672c78e428287e9ef;hp=7810771fa907e5c1e8fb0cc113c5b3427d81d33a;hpb=ebe0c31476d408f10e968bed024e898c02c7204e;p=jalview.git diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index 7810771..7dc263b 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -20,7 +20,8 @@ */ package jalview.io; -import jalview.api.AlignViewportI; +import jalview.api.AlignExportSettingI; +import jalview.api.AlignmentViewPanel; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.AlignmentI; @@ -28,6 +29,7 @@ import jalview.datamodel.AlignmentView; import jalview.util.MessageManager; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -42,7 +44,7 @@ import java.util.List; */ public class AppletFormatAdapter { - private AlignViewportI viewport; + private AlignmentViewPanel viewpanel; public static String FILE = "File"; @@ -76,6 +78,8 @@ public class AppletFormatAdapter */ protected String newline = System.getProperty("line.separator"); + private AlignExportSettingI exportSettings; + /** * List of valid format strings used in the isValidFormat method */ @@ -137,9 +141,16 @@ public class AppletFormatAdapter { } - public AppletFormatAdapter(AlignViewportI viewport) + public AppletFormatAdapter(AlignmentViewPanel viewpanel) { - this.viewport = viewport; + this.viewpanel = viewpanel; + } + + public AppletFormatAdapter(AlignmentViewPanel alignPanel, + AlignExportSettingI settings) + { + viewpanel = alignPanel; + exportSettings = settings; } /** @@ -195,6 +206,10 @@ public class AppletFormatAdapter public static final boolean isValidFormat(String format, boolean forwriting) { + if (format == null) + { + return false; + } boolean valid = false; String[] format_list = (forwriting) ? WRITEABLE_FORMATS : READABLE_FORMATS; @@ -221,7 +236,7 @@ public class AppletFormatAdapter * * @return DOCUMENT ME! */ - public Alignment readFile(String inFile, String type, String format) + public AlignmentI readFile(String inFile, String type, String format) throws java.io.IOException { // TODO: generalise mapping between format string and io. class instances @@ -229,7 +244,6 @@ public class AppletFormatAdapter this.inFile = inFile; try { - Alignment al; if (format.equals("FASTA")) { alignFile = new FastaFile(inFile, type); @@ -298,16 +312,7 @@ public class AppletFormatAdapter { alignFile = new Gff3File(inFile, type); } - // Standard boilerplate for creating alignment from parser - alignFile.setViewport(viewport); - - al = new Alignment(alignFile.getSeqsAsArray()); - - alignFile.addAnnotations(al); - - alignFile.addGroups(al); - - return al; + return buildAlignmentFrom(alignFile); } catch (Exception e) { e.printStackTrace(); @@ -327,11 +332,7 @@ public class AppletFormatAdapter { // Possible sequence is just residues with no label alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste"); - Alignment al = new Alignment(alignFile.getSeqsAsArray()); - - alignFile.addSeqGroups(al); - alignFile.addAnnotations(al); - return al; + return buildAlignmentFrom(alignFile); } catch (Exception ex) { @@ -343,7 +344,10 @@ public class AppletFormatAdapter ex.printStackTrace(); } } - + if (format.equalsIgnoreCase("HTML")) + { + throw new IOException(e.getMessage()); + } // If we get to this stage, the format was not supported throw new java.io.IOException(SUPPORTED_FORMATS); } @@ -370,7 +374,6 @@ public class AppletFormatAdapter String type = source.type; try { - Alignment al; if (format.equals("FASTA")) { alignFile = new FastaFile(source); @@ -432,19 +435,14 @@ public class AppletFormatAdapter else if (format.equals(JSONFile.FILE_DESC)) { alignFile = new JSONFile(source); - al = new Alignment(alignFile.getSeqsAsArray()); - alignFile.addAnnotations(al); - alignFile.addSeqGroups(al); - return al; } else if (format.equals(HtmlFile.FILE_DESC)) { alignFile = new HtmlFile(source); } - al = new Alignment(alignFile.getSeqsAsArray()); - alignFile.addAnnotations(al); - return al; + return buildAlignmentFrom(alignFile); + } catch (Exception e) { e.printStackTrace(); @@ -464,10 +462,7 @@ public class AppletFormatAdapter { // Possible sequence is just residues with no label alignFile = new FastaFile(">UNKNOWN\n" + inFile, "Paste"); - Alignment al = new Alignment(alignFile.getSeqsAsArray()); - alignFile.addAnnotations(al); - alignFile.addSeqGroups(al); - return al; + return buildAlignmentFrom(alignFile); } catch (Exception ex) { @@ -487,6 +482,27 @@ public class AppletFormatAdapter /** + * boilerplate method to handle data from an AlignFile and construct a new + * alignment or import to an existing alignment + * + * @param alignFile2 + * @return AlignmentI instance ready to pass to a UI constructor + */ + private AlignmentI buildAlignmentFrom(AlignFile alignFile2) + { + // Standard boilerplate for creating alignment from parser + // alignFile.configureForView(viewpanel); + + AlignmentI al = new Alignment(alignFile.getSeqsAsArray()); + + alignFile.addAnnotations(al); + + alignFile.addGroups(al); + + return al; + } + + /** * create an alignment flatfile from a Jalview alignment view * @param format * @param jvsuffix @@ -495,13 +511,14 @@ public class AppletFormatAdapter * @return flatfile in a string */ public String formatSequences(String format, boolean jvsuffix, - AlignViewportI av, boolean selectedOnly) + AlignmentViewPanel ap, boolean selectedOnly) { - AlignmentView selvew = av.getAlignmentView(selectedOnly, false); - AlignmentI aselview = selvew.getVisibleAlignment(av + AlignmentView selvew = ap.getAlignViewport().getAlignmentView( + selectedOnly, false); + AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport() .getGapCharacter()); - List ala = (av + List ala = (ap.getAlignViewport() .getVisibleAlignmentAnnotation(selectedOnly)); if (ala != null) { @@ -510,7 +527,7 @@ public class AppletFormatAdapter aselview.addAnnotation(aa); } } - + viewpanel = ap; return formatSequences(format, aselview, jvsuffix); } @@ -578,22 +595,6 @@ public class AppletFormatAdapter else if (format.equalsIgnoreCase(JSONFile.FILE_DESC)) { afile = new JSONFile(); - afile.setViewport(viewport); - // Add groups to AlignFile - afile.seqGroups = alignment.getGroups(); - - // Add non auto calculated annotation to AlignFile - for (AlignmentAnnotation annot : alignment.getAlignmentAnnotation()) - { - if (annot != null && !annot.autoCalculated) - { - if (annot.label.equals("PDB.CATempFactor")) - { - continue; - } - afile.annotations.add(annot); - } - } } else if (format.equalsIgnoreCase("RNAML")) { @@ -604,11 +605,23 @@ public class AppletFormatAdapter { throw new Exception(MessageManager.getString("error.implementation_error_unknown_file_format_string")); } + afile.setNewlineString(newline); afile.addJVSuffix(jvsuffix); + afile.setExportSettings(exportSettings); + afile.configureForView(viewpanel); - afile.setSeqs(alignment.getSequencesArray()); - + // check whether we were given a specific alignment to export, rather than + // the one in the viewpanel + if (viewpanel == null || viewpanel.getAlignment() == null + || viewpanel.getAlignment() != alignment) + { + afile.setSeqs(alignment.getSequencesArray()); + } + else + { + afile.setSeqs(viewpanel.getAlignment().getSequencesArray()); + } String afileresp = afile.print(); if (afile.hasWarningMessage()) @@ -655,7 +668,7 @@ public class AppletFormatAdapter System.gc(); long memf = -r.totalMemory() + r.freeMemory(); long t1 = -System.currentTimeMillis(); - Alignment al = afa.readFile(args[i], FILE, + AlignmentI al = afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE)); t1 += System.currentTimeMillis(); System.gc();