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.*;
29 public class FormatAdapter
30 extends AppletFormatAdapter
33 public String formatSequences(String format,
35 String[] omitHiddenColumns)
38 return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
42 * create sequences with each seuqence string replaced with the one given in omitHiddenCOlumns
44 * @param omitHiddenColumns
45 * @return new sequences
47 public SequenceI[] replaceStrings(SequenceI[] seqs, String[] omitHiddenColumns)
49 if (omitHiddenColumns != null)
51 SequenceI[] tmp = new SequenceI[seqs.length];
52 for (int i = 0; i < seqs.length; i++)
54 tmp[i] = new Sequence(
55 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.
67 * @param format Format string as givien in the AppletFormatAdaptor list (exact match to name of class implementing file io for that format)
68 * @param seqs vector of sequences to write
70 * @return String containing sequences in desired format
72 public String formatSequences(String format,
78 AlignFile afile = null;
80 if (format.equalsIgnoreCase("FASTA"))
82 afile = new FastaFile();
84 jalview.bin.Cache.getDefault("FASTA_JVSUFFIX", true));
86 else if (format.equalsIgnoreCase("MSF"))
88 afile = new MSFfile();
90 jalview.bin.Cache.getDefault("MSF_JVSUFFIX", true));
92 else if (format.equalsIgnoreCase("PileUp"))
94 afile = new PileUpfile();
96 jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX", true));
98 else if (format.equalsIgnoreCase("CLUSTAL"))
100 afile = new ClustalFile();
102 jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX", true));
104 else if (format.equalsIgnoreCase("BLC"))
106 afile = new BLCFile();
108 jalview.bin.Cache.getDefault("BLC_JVSUFFIX", true));
110 else if (format.equalsIgnoreCase("PIR"))
112 afile = new PIRFile();
114 jalview.bin.Cache.getDefault("PIR_JVSUFFIX", true));
116 else if (format.equalsIgnoreCase("PFAM"))
118 afile = new PfamFile();
120 jalview.bin.Cache.getDefault("PFAM_JVSUFFIX", true));
125 return afile.print();
129 System.err.println("Failed to write alignment as a '" + format +
136 public boolean getCacheSuffixDefault(String format)
138 if (isValidFormat(format))
139 return jalview.bin.Cache.getDefault(format.toUpperCase()+"_JVSUFFIX", true);
142 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden)
144 return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format));
147 * hack function to replace seuqences with visible sequence strings before generating a
148 * string of the alignment in the given format.
151 * @param omitHidden sequence strings to write out in order of sequences in alignment
152 * @return string representation of the alignment formatted as format
154 public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix)
156 AlignFile afile = null;
157 if (omitHidden!=null)
160 Alignment alv = new Alignment(replaceStrings(alignment.getSequencesArray(), omitHidden));
161 AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
162 for (int i=0; i<ala.length; i++)
164 alv.addAnnotation(new AlignmentAnnotation(ala[i]));
166 return this.formatSequences(format, alv, suffix);
168 return this.formatSequences(format, alignment, suffix);