javadoc
[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  * Additional formatting methods used by the application
25  * in a number of places. 
26  *
27  * @author $author$
28  * @version $Revision$
29  */
30 public class FormatAdapter
31     extends AppletFormatAdapter
32 {
33   
34   public String formatSequences(String format,
35                                 SequenceI[] seqs,
36                                 String[] omitHiddenColumns)
37   {
38
39     return formatSequences(format, replaceStrings(seqs, omitHiddenColumns));
40   }
41
42   /**
43    * create sequences with each seuqence string replaced with the one given in omitHiddenCOlumns
44    * @param seqs
45    * @param omitHiddenColumns
46    * @return new sequences
47    */
48   public SequenceI[] replaceStrings(SequenceI[] seqs, String[] omitHiddenColumns)
49   {
50     if (omitHiddenColumns != null)
51     {
52       SequenceI[] tmp = new SequenceI[seqs.length];
53       for (int i = 0; i < seqs.length; i++)
54       {
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());
59       }
60       seqs = tmp;
61     }
62     return seqs;
63   }
64
65   /**
66    * Format a vector of sequences as a flat alignment file.
67    *
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
70    *
71    * @return String containing sequences in desired format
72    */
73   public String formatSequences(String format,
74                                 SequenceI[] seqs)
75   {
76
77     try
78     {
79       AlignFile afile = null;
80
81       if (format.equalsIgnoreCase("FASTA"))
82       {
83         afile = new FastaFile();
84         afile.addJVSuffix(
85             jalview.bin.Cache.getDefault("FASTA_JVSUFFIX", true));
86       }
87       else if (format.equalsIgnoreCase("MSF"))
88       {
89         afile = new MSFfile();
90         afile.addJVSuffix(
91             jalview.bin.Cache.getDefault("MSF_JVSUFFIX", true));
92       }
93       else if (format.equalsIgnoreCase("PileUp"))
94       {
95         afile = new PileUpfile();
96         afile.addJVSuffix(
97             jalview.bin.Cache.getDefault("PILEUP_JVSUFFIX", true));
98       }
99       else if (format.equalsIgnoreCase("CLUSTAL"))
100       {
101         afile = new ClustalFile();
102         afile.addJVSuffix(
103             jalview.bin.Cache.getDefault("CLUSTAL_JVSUFFIX", true));
104       }
105       else if (format.equalsIgnoreCase("BLC"))
106       {
107         afile = new BLCFile();
108         afile.addJVSuffix(
109             jalview.bin.Cache.getDefault("BLC_JVSUFFIX", true));
110       }
111       else if (format.equalsIgnoreCase("PIR"))
112       {
113         afile = new PIRFile();
114         afile.addJVSuffix(
115             jalview.bin.Cache.getDefault("PIR_JVSUFFIX", true));
116       }
117       else if (format.equalsIgnoreCase("PFAM"))
118       {
119         afile = new PfamFile();
120         afile.addJVSuffix(
121             jalview.bin.Cache.getDefault("PFAM_JVSUFFIX", true));
122       }
123       /* amsa is not supported by this function - it requires an alignment rather than a sequence vector
124       else if (format.equalsIgnoreCase("AMSA"))
125       {
126         afile = new AMSAFile();
127         afile.addJVSuffix(
128             jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true));
129       }*/
130
131       afile.setSeqs(seqs);
132
133       return afile.print();
134     }
135     catch (Exception e)
136     {
137       System.err.println("Failed to write alignment as a '" + format +
138                          "' file\n");
139       e.printStackTrace();
140     }
141
142     return null;
143   }
144   public boolean getCacheSuffixDefault(String format)
145   {
146     if (isValidFormat(format))
147       return jalview.bin.Cache.getDefault(format.toUpperCase()+"_JVSUFFIX", true);
148     return false;
149   }
150   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel)
151   {
152     return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, null);
153   }
154   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
155   {
156     return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, sgp);
157   }
158     /**
159    * hack function to replace seuqences with visible sequence strings before generating a
160    * string of the alignment in the given format.
161    * @param format
162      * @param alignment
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
166    */
167   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, ColumnSelection colSel)
168   {
169     return formatSequences(format, alignment, omitHidden, suffix, colSel, null);
170   }
171
172   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, 
173           ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp)
174   {
175     if (omitHidden!=null)
176     {
177       // 
178       Alignment alv = new Alignment(replaceStrings(alignment.getSequencesArray(), omitHidden));
179       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
180       for (int i=0; i<ala.length; i++)
181       {
182         AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
183         if (selgp!=null)
184         {
185           colSel.makeVisibleAnnotation(selgp.getStartRes(),selgp.getEndRes(), na);
186         } else {
187           colSel.makeVisibleAnnotation(na);
188         }
189         alv.addAnnotation(na);
190       }
191       return this.formatSequences(format, alv, suffix);
192     }
193     return this.formatSequences(format, alignment, suffix);
194   }
195 }