2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 in a number of places.
29 public class FormatAdapter extends AppletFormatAdapter
32 public String formatSequences(String format, SequenceI[] seqs,
33 String[] omitHiddenColumns)
36 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
40 * create sequences with each seuqence string replaced with the one given in
44 * @param omitHiddenColumns
45 * @return new sequences
47 public SequenceI[] replaceStrings(SequenceI[] seqs,
48 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(seqs[i].getName(), omitHiddenColumns[i],
56 seqs[i].getStart(), seqs[i].getEnd());
57 tmp[i].setDescription(seqs[i].getDescription());
65 * Format a vector of sequences as a flat alignment file.
68 * Format string as givien in the AppletFormatAdaptor list
69 * (exact match to name of class implementing file io for that
72 * vector of sequences to write
74 * @return String containing sequences in desired format
76 public String formatSequences(String format, SequenceI[] seqs)
81 AlignFile afile = null;
83 if (format.equalsIgnoreCase("FASTA"))
85 afile = new FastaFile();
86 afile.addJVSuffix(jalview.bin.Cache.getDefault("FASTA_JVSUFFIX",
89 else if (format.equalsIgnoreCase("MSF"))
91 afile = new MSFfile();
92 afile.addJVSuffix(jalview.bin.Cache
93 .getDefault("MSF_JVSUFFIX", true));
95 else if (format.equalsIgnoreCase("PileUp"))
97 afile = new PileUpfile();
98 afile.addJVSuffix(jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX",
101 else if (format.equalsIgnoreCase("CLUSTAL"))
103 afile = new ClustalFile();
104 afile.addJVSuffix(jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX",
107 else if (format.equalsIgnoreCase("BLC"))
109 afile = new BLCFile();
110 afile.addJVSuffix(jalview.bin.Cache
111 .getDefault("BLC_JVSUFFIX", true));
113 else if (format.equalsIgnoreCase("PIR"))
115 afile = new PIRFile();
116 afile.addJVSuffix(jalview.bin.Cache
117 .getDefault("PIR_JVSUFFIX", true));
119 else if (format.equalsIgnoreCase("PFAM"))
121 afile = new PfamFile();
122 afile.addJVSuffix(jalview.bin.Cache.getDefault("PFAM_JVSUFFIX",
126 * amsa is not supported by this function - it requires an alignment
127 * rather than a sequence vector else if (format.equalsIgnoreCase("AMSA")) {
128 * afile = new AMSAFile(); afile.addJVSuffix(
129 * jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true)); }
134 return afile.print();
135 } catch (Exception e)
137 System.err.println("Failed to write alignment as a '" + format
145 public boolean getCacheSuffixDefault(String format)
147 if (isValidFormat(format))
148 return jalview.bin.Cache.getDefault(format.toUpperCase()
149 + "_JVSUFFIX", true);
153 public String formatSequences(String format, AlignmentI alignment,
154 String[] omitHidden, ColumnSelection colSel)
156 return formatSequences(format, alignment, omitHidden,
157 getCacheSuffixDefault(format), colSel, null);
160 public String formatSequences(String format, AlignmentI alignment,
161 String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
163 return formatSequences(format, alignment, omitHidden,
164 getCacheSuffixDefault(format), colSel, sgp);
168 * hack function to replace seuqences with visible sequence strings before
169 * generating a string of the alignment in the given format.
174 * sequence strings to write out in order of sequences in
177 * defines hidden columns that are edited out of annotation
178 * @return string representation of the alignment formatted as format
180 public String formatSequences(String format, AlignmentI alignment,
181 String[] omitHidden, boolean suffix, ColumnSelection colSel)
183 return formatSequences(format, alignment, omitHidden, suffix, colSel,
187 public String formatSequences(String format, AlignmentI alignment,
188 String[] omitHidden, boolean suffix, ColumnSelection colSel,
189 jalview.datamodel.SequenceGroup selgp)
191 if (omitHidden != null)
194 Alignment alv = new Alignment(replaceStrings(alignment
195 .getSequencesArray(), omitHidden));
196 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
197 for (int i = 0; i < ala.length; i++)
199 AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
202 colSel.makeVisibleAnnotation(selgp.getStartRes(), selgp
207 colSel.makeVisibleAnnotation(na);
209 alv.addAnnotation(na);
211 return this.formatSequences(format, alv, suffix);
213 return this.formatSequences(format, alignment, suffix);