Formatted source
[jalview.git] / src / jalview / io / FormatAdapter.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.io;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.util.Vector;\r
24 \r
25 \r
26 public class FormatAdapter {\r
27     public static Vector formats = new Vector();\r
28 \r
29     static {\r
30         formats.addElement("FASTA");\r
31         formats.addElement("MSF");\r
32         formats.addElement("PileUp");\r
33         formats.addElement("CLUSTAL");\r
34         formats.addElement("BLC");\r
35         formats.addElement("PIR");\r
36         formats.addElement("PFAM");\r
37     }\r
38 \r
39     public static SequenceI[] readFile(String inFile, String type, String format) {\r
40         try {\r
41             AlignFile afile = null;\r
42 \r
43             if (format.equals("FASTA")) {\r
44                 afile = new FastaFile(inFile, type);\r
45             } else if (format.equals("MSF")) {\r
46                 afile = new MSFfile(inFile, type);\r
47             } else if (format.equals("PileUp")) {\r
48                 afile = new PileUpfile(inFile, type);\r
49             } else if (format.equals("CLUSTAL")) {\r
50                 afile = new ClustalFile(inFile, type);\r
51             } else if (format.equals("BLC")) {\r
52                 afile = new BLCFile(inFile, type);\r
53             } else if (format.equals("PIR")) {\r
54                 afile = new PIRFile(inFile, type);\r
55             } else if (format.equals("PFAM")) {\r
56                 afile = new PfamFile(inFile, type);\r
57             }\r
58 \r
59             return afile.getSeqsAsArray();\r
60         } catch (Exception e) {\r
61             System.err.println("Failed to read alignment using the '" + format +\r
62                 "' reader.");\r
63             e.printStackTrace();\r
64         }\r
65 \r
66         return null;\r
67     }\r
68 \r
69     public static String formatSequences(String format, Vector seqs) {\r
70         SequenceI[] s = new SequenceI[seqs.size()];\r
71 \r
72         for (int i = 0; i < seqs.size(); i++)\r
73             s[i] = (SequenceI) seqs.elementAt(i);\r
74 \r
75         try {\r
76             AlignFile afile = null;\r
77 \r
78             if (format.equals("FASTA")) {\r
79                 afile = new FastaFile();\r
80             } else if (format.equals("MSF")) {\r
81                 afile = new MSFfile();\r
82             } else if (format.equals("PileUp")) {\r
83                 afile = new PileUpfile();\r
84             } else if (format.equals("CLUSTAL")) {\r
85                 afile = new ClustalFile();\r
86             } else if (format.equals("BLC")) {\r
87                 afile = new BLCFile();\r
88             } else if (format.equals("PIR")) {\r
89                 afile = new PIRFile();\r
90             } else if (format.equals("PFAM")) {\r
91                 afile = new PfamFile();\r
92             }\r
93 \r
94             afile.setSeqs(s);\r
95 \r
96             return afile.print();\r
97         } catch (Exception e) {\r
98             System.err.println("Failed to write alignment as a '" + format +\r
99                 "' file\n");\r
100             e.printStackTrace();\r
101         }\r
102 \r
103         return null;\r
104     }\r
105 }\r