3cbd73b7dcddc0c75b007f6c899988a808d09b03
[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       /* amsa is not supported by this function - it requires an alignment rather than a sequence vector
123       else if (format.equalsIgnoreCase("AMSA"))
124       {
125         afile = new AMSAFile();
126         afile.addJVSuffix(
127             jalview.bin.Cache.getDefault("AMSA_JVSUFFIX", true));
128       }*/
129
130       afile.setSeqs(seqs);
131
132       return afile.print();
133     }
134     catch (Exception e)
135     {
136       System.err.println("Failed to write alignment as a '" + format +
137                          "' file\n");
138       e.printStackTrace();
139     }
140
141     return null;
142   }
143   public boolean getCacheSuffixDefault(String format)
144   {
145     if (isValidFormat(format))
146       return jalview.bin.Cache.getDefault(format.toUpperCase()+"_JVSUFFIX", true);
147     return false;
148   }
149   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel)
150   {
151     return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, null);
152   }
153   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, ColumnSelection colSel, SequenceGroup sgp)
154   {
155     return formatSequences(format, alignment, omitHidden, getCacheSuffixDefault(format), colSel, sgp);
156   }
157     /**
158    * hack function to replace seuqences with visible sequence strings before generating a
159    * string of the alignment in the given format.
160    * @param format
161      * @param alignment
162      * @param omitHidden sequence strings to write out in order of sequences in alignment
163      * @param colSel defines hidden columns that are edited out of annotation 
164    * @return string representation of the alignment formatted as format
165    */
166   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, ColumnSelection colSel)
167   {
168     return formatSequences(format, alignment, omitHidden, suffix, colSel, null);
169   }
170
171   public String formatSequences(String format, AlignmentI alignment, String[] omitHidden, boolean suffix, 
172           ColumnSelection colSel, jalview.datamodel.SequenceGroup selgp)
173   {
174     if (omitHidden!=null)
175     {
176       // 
177       Alignment alv = new Alignment(replaceStrings(alignment.getSequencesArray(), omitHidden));
178       AlignmentAnnotation[] ala = alignment.getAlignmentAnnotation();
179       for (int i=0; i<ala.length; i++)
180       {
181         AlignmentAnnotation na = new AlignmentAnnotation(ala[i]);
182         if (selgp!=null)
183         {
184           colSel.makeVisibleAnnotation(selgp.getStartRes(),selgp.getEndRes(), na);
185         } else {
186           colSel.makeVisibleAnnotation(na);
187         }
188         alv.addAnnotation(na);
189       }
190       return this.formatSequences(format, alv, suffix);
191     }
192     return this.formatSequences(format, alignment, suffix);
193   }
194 }