2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
24 * Additional formatting methods used by the application
25 * in a number of places.
30 public class FormatAdapter
31 extends AppletFormatAdapter
34 public String formatSequences(String format,
36 String[] omitHiddenColumns)
39 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
43 * create sequences with each seuqence string replaced with the one given in omitHiddenCOlumns
45 * @param omitHiddenColumns
46 * @return new sequences
48 public SequenceI[] replaceStrings(SequenceI[] seqs, String[] omitHiddenColumns)
50 if (omitHiddenColumns != null)
52 SequenceI[] tmp = new SequenceI[seqs.length];
53 for (int i = 0; i < seqs.length; i++)
55 tmp[i] = new Sequence(
56 seqs[i].getName(), omitHiddenColumns[i],
57 seqs[i].getStart(), seqs[i].getEnd());
58 tmp[i].setDescription(seqs[i].getDescription());
66 * Format a vector of sequences as a flat alignment file.
68 * @param format Format string as givien in the AppletFormatAdaptor list (exact match to name of class implementing file io for that format)
69 * @param seqs vector of sequences to write
71 * @return String containing sequences in desired format
73 public String formatSequences(String format,
79 AlignFile afile = null;
81 if (format.equalsIgnoreCase("FASTA"))
83 afile = new FastaFile();
85 jalview.bin.Cache.getDefault("FASTA_JVSUFFIX", true));
87 else if (format.equalsIgnoreCase("MSF"))
89 afile = new MSFfile();
91 jalview.bin.Cache.getDefault("MSF_JVSUFFIX", true));
93 else if (format.equalsIgnoreCase("PileUp"))
95 afile = new PileUpfile();
97 jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX", true));
99 else if (format.equalsIgnoreCase("CLUSTAL"))
101 afile = new ClustalFile();
103 jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX", true));
105 else if (format.equalsIgnoreCase("BLC"))
107 afile = new BLCFile();
109 jalview.bin.Cache.getDefault("BLC_JVSUFFIX", true));
111 else if (format.equalsIgnoreCase("PIR"))
113 afile = new PIRFile();
115 jalview.bin.Cache.getDefault("PIR_JVSUFFIX", true));
117 else if (format.equalsIgnoreCase("PFAM"))
119 afile = new PfamFile();
121 jalview.bin.Cache.getDefault("PFAM_JVSUFFIX", true));
123 /* amsa is not supported by this function - it requires an alignment rather than a sequence vector
124 else if (format.equalsIgnoreCase("AMSA"))
126 afile = new AMSAFile();
128 jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true));
133 return afile.print();
137 System.err.println("Failed to write alignment as a '" + format +
144 public boolean getCacheSuffixDefault(String format)
146 if (isValidFormat(format))
147 return jalview.bin.Cache.getDefault(format.toUpperCase()+"_JVSUFFIX", true);
150 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel)
152 return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, null);
154 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
156 return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, sgp);
159 * hack function to replace seuqences with visible sequence strings before generating a
160 * string of the alignment in the given format.
163 * @param omitHidden sequence strings to write out in order of sequences in alignment
164 * @param colSel defines hidden columns that are edited out of annotation
165 * @return string representation of the alignment formatted as format
167 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, ColumnSelection colSel)
169 return formatSequences(format, alignment, omitHidden, suffix, colSel, null);
172 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix,
173 ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp)
175 if (omitHidden!=null)
178 Alignment alv = new Alignment(replaceStrings(alignment.getSequencesArray(), omitHidden));
179 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
180 for (int i=0; i<ala.length; i++)
182 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
185 colSel.makeVisibleAnnotation(selgp.getStartRes(),selgp.getEndRes(), na);
187 colSel.makeVisibleAnnotation(na);
189 alv.addAnnotation(na);
191 return this.formatSequences(format, alv, suffix);
193 return this.formatSequences(format, alignment, suffix);