2 * Created on 17-May-2005
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
7 package org.vamsas.objects.utils;
9 import java.io.BufferedOutputStream;
10 import java.io.BufferedReader;
11 import java.io.BufferedWriter;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.util.Hashtable;
16 import java.util.StringTokenizer;
17 import java.util.Vector;
18 import java.util.regex.Pattern;
20 import vamsas.objects.simple.Alignment;
21 import vamsas.objects.simple.Sequence;
22 import vamsas.objects.simple.SequenceSet;
27 * TODO To change the template for this generated type comment go to
28 * Window - Preferences - Java - Code Style - Code Templates
30 public class SeqAln extends vamsas.objects.simple.Alignment {
32 public static Sequence[] ReadClustalFile(InputStream os) throws Exception {
34 Pattern nonGap = Pattern.compile("[A-Z*0-9]", Pattern.CASE_INSENSITIVE);
41 Vector headers = new Vector();
42 Hashtable seqhash = new Hashtable();
48 BufferedReader ins = new BufferedReader(new InputStreamReader(os));
49 while ((line = ins.readLine()) != null) {
50 if (line.indexOf(" ") != 0) {
51 java.util.StringTokenizer str = new StringTokenizer(line," ");
54 if (str.hasMoreTokens()) {
56 if (id.equals("CLUSTAL")) {
61 if (seqhash.containsKey(id)) {
62 tempseq = (StringBuffer)seqhash.get(id);
64 tempseq = new StringBuffer();
65 seqhash.put(id,tempseq);
68 if (!(headers.contains(id))) {
69 headers.addElement(id);
72 tempseq.append(str.nextToken());
79 } catch (IOException e) {
80 throw(new Exception("Exception parsing clustal file ",e));
84 noSeqs = headers.size();
86 //Add sequences to the hash
87 seqs = new Sequence[headers.size()];
88 for (i = 0; i < headers.size(); i++ ) {
89 if ( seqhash.get(headers.elementAt(i)) != null) {
91 Sequence newSeq = new Sequence(headers.elementAt(i).toString(),
92 seqhash.get(headers.elementAt(i).toString()).toString());
97 throw(new Exception("Bizarreness! Can't find sequence for " + headers.elementAt(i)));
104 public static void WriteClustalWAlignment(java.io.OutputStream os, Alignment seqAl) throws IOException {
105 Sequence[] s = seqAl.getSeqs().getSeqs();
107 java.io.BufferedWriter out = new BufferedWriter(new java.io.OutputStreamWriter(os));
109 out.write("CLUSTAL\n\n");
116 while (i < s.length && s[i] != null) {
117 String tmp = s[i].getId();
119 if (s[i].getSeq().length() > max) {
120 max = s[i].getSeq().length();
122 if (tmp.length() > maxid) {
123 maxid = tmp.length();
133 int nochunks = max / len + 1;
135 for (i = 0; i < nochunks; i++) {
137 while ( j < s.length && s[j] != null) {
138 out.write(new Format("%-" + maxid + "s").form(s[j].getId()+" "));
140 int end = start + len;
142 if (end < s[j].getSeq().length() && start < s[j].getSeq().length() ) {
143 out.write(s[j].getSeq().substring(start,end) + "\n");
145 if (start < s[j].getSeq().length()) {
146 out.write(s[j].getSeq().substring(start) + "\n");
157 public static Alignment make_Alignment(vamsas.objects.simple.Action origin, Sequence[] seqs, String[] source) throws Exception {
158 Pattern nonGap = Pattern.compile("[A-Z*0-9]", Pattern.CASE_INSENSITIVE);
159 boolean gapsset = false;
164 for (int i=0, nseq=seqs.length; i<nseq; i++) {
165 String seq = seqs[i].getSeq();
166 String gaps = nonGap.matcher(seq).replaceAll("");
168 seqLength=seq.length();
170 if (seqLength!=seq.length())
171 throw(new Exception(i+"th Sequence (>"+seqs[i].getId()+") is not aligned.\n"));//TODO: move this to assertions part of Alignment
173 // common check for any sequence...
174 if (gaps!=null && gaps.length()>0) {
176 gapchar = gaps.charAt(0);
177 for (int c=0, gc=gaps.length(); c<gc; c++) {
178 if (gapchar!=gaps.charAt(c)) {
179 throw(new IOException("Inconsistent gap characters in sequence "+i+": '"+seq+"'"));
185 return new Alignment(origin, new SequenceSet(seqs), source, new String(""+gapchar));
188 public static Alignment read_FastaAlignment(InputStream os, String[] source) throws Exception {
191 seqs = SeqSet.read_SeqFasta(os);
193 throw(new Exception("Empty alignment stream!\n"));
194 } catch (Exception e) {
195 throw new Exception("Invalid fasta alignment\n",e);
198 return make_Alignment(new vamsas.objects.simple.Action(source[0]), seqs, source);
200 public static Alignment read_ClustalAlignment(InputStream os, String[] source) throws Exception {
203 seqs = SeqAln.ReadClustalFile(os);
205 throw(new Exception("Empty alignment stream!\n"));
206 } catch (Exception e) {
207 throw new Exception("Invalid fasta alignment\n",e);
210 return make_Alignment(new vamsas.objects.simple.Action(source[0]), seqs, source);