added colSel as a parameter formatAdaptor print function to allow editing of annotati...
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19 package jalview.io;
20
21 import jalview.datamodel.*;
22
23 /**
24  * DOCUMENT ME!
25  *
26  * @author $author$
27  * @version $Revision$
28  */
29 public class FormatAdapter
30     extends AppletFormatAdapter
31 {
32   
33   public String formatSequences(String format,
34                                 SequenceI[] seqs,
35                                 String[] omitHiddenColumns)
36   {
37
38     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
39   }
40
41   /**
42    * create sequences with each seuqence string replaced with the one given in omitHiddenCOlumns
43    * @param seqs
44    * @param omitHiddenColumns
45    * @return new sequences
46    */
47   public SequenceI[] replaceStrings(SequenceI[] seqs, String[] omitHiddenColumns)
48   {
49     if (omitHiddenColumns != null)
50     {
51       SequenceI[] tmp = new SequenceI[seqs.length];
52       for (int i = 0; i < seqs.length; i++)
53       {
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());
58       }
59       seqs = tmp;
60     }
61     return seqs;
62   }
63
64   /**
65    * Format a vector of sequences as a flat alignment file.
66    *
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
69    *
70    * @return String containing sequences in desired format
71    */
72   public String formatSequences(String format,
73                                 SequenceI[] seqs)
74   {
75
76     try
77     {
78       AlignFile afile = null;
79
80       if (format.equalsIgnoreCase("FASTA"))
81       {
82         afile = new FastaFile();
83         afile.addJVSuffix(
84             jalview.bin.Cache.getDefault("FASTA_JVSUFFIX", true));
85       }
86       else if (format.equalsIgnoreCase("MSF"))
87       {
88         afile = new MSFfile();
89         afile.addJVSuffix(
90             jalview.bin.Cache.getDefault("MSF_JVSUFFIX", true));
91       }
92       else if (format.equalsIgnoreCase("PileUp"))
93       {
94         afile = new PileUpfile();
95         afile.addJVSuffix(
96             jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX", true));
97       }
98       else if (format.equalsIgnoreCase("CLUSTAL"))
99       {
100         afile = new ClustalFile();
101         afile.addJVSuffix(
102             jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX", true));
103       }
104       else if (format.equalsIgnoreCase("BLC"))
105       {
106         afile = new BLCFile();
107         afile.addJVSuffix(
108             jalview.bin.Cache.getDefault("BLC_JVSUFFIX", true));
109       }
110       else if (format.equalsIgnoreCase("PIR"))
111       {
112         afile = new PIRFile();
113         afile.addJVSuffix(
114             jalview.bin.Cache.getDefault("PIR_JVSUFFIX", true));
115       }
116       else if (format.equalsIgnoreCase("PFAM"))
117       {
118         afile = new PfamFile();
119         afile.addJVSuffix(
120             jalview.bin.Cache.getDefault("PFAM_JVSUFFIX", true));
121       }
122
123       afile.setSeqs(seqs);
124
125       return afile.print();
126     }
127     catch (Exception e)
128     {
129       System.err.println("Failed to write alignment as a '" + format +
130                          "' file\n");
131       e.printStackTrace();
132     }
133
134     return null;
135   }
136   public boolean getCacheSuffixDefault(String format)
137   {
138     if (isValidFormat(format))
139       return jalview.bin.Cache.getDefault(format.toUpperCase()+"_JVSUFFIX", true);
140     return false;
141   }
142   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel)
143   {
144     return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel);
145   }
146     /**
147    * hack function to replace seuqences with visible sequence strings before generating a
148    * string of the alignment in the given format.
149    * @param format
150      * @param alignment
151      * @param omitHidden sequence strings to write out in order of sequences in alignment
152      * @param colSel defines hidden columns that are edited out of annotation 
153    * @return string representation of the alignment formatted as format
154    */
155   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, ColumnSelection colSel)
156   {
157     AlignFile afile = null;
158     if (omitHidden!=null)
159     {
160       // 
161       Alignment alv = new Alignment(replaceStrings(alignment.getSequencesArray(), omitHidden));
162       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
163       for (int i=0; i<ala.length; i++)
164       {
165         AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
166         colSel.makeVisibleAnnotation(na);
167         alv.addAnnotation(na);
168       }
169       return this.formatSequences(format, alv, suffix);
170     }
171     return this.formatSequences(format, alignment, suffix);
172   }
173 }