From 3a993bbe274824870c78bd7695c42fa93908cb30 Mon Sep 17 00:00:00 2001 From: jprocter Date: Tue, 29 Jan 2008 10:15:09 +0000 Subject: [PATCH] FileParse object can be re-used to read different files concatenated together --- src/MCview/PDBfile.java | 6 + src/jalview/io/AlignFile.java | 12 +- src/jalview/io/BLCFile.java | 4 + src/jalview/io/ClustalFile.java | 441 ++++++++++---------- src/jalview/io/FastaFile.java | 475 +++++++++++----------- src/jalview/io/FeaturesFile.java | 4 + src/jalview/io/FileParse.java | 54 +++ src/jalview/io/JPredFile.java | 5 +- src/jalview/io/MSFfile.java | 806 +++++++++++++++++++------------------ src/jalview/io/NewickFile.java | 5 +- src/jalview/io/PIRFile.java | 381 +++++++++--------- src/jalview/io/PfamFile.java | 347 ++++++++-------- src/jalview/io/PileUpfile.java | 373 ++++++++--------- src/jalview/io/StockholmFile.java | 6 +- 14 files changed, 1516 insertions(+), 1403 deletions(-) diff --git a/src/MCview/PDBfile.java b/src/MCview/PDBfile.java index 2c7bd9d..e5ad565 100755 --- a/src/MCview/PDBfile.java +++ b/src/MCview/PDBfile.java @@ -24,6 +24,7 @@ import java.util.*; import java.awt.*; import jalview.datamodel.*; +import jalview.io.FileParse; public class PDBfile extends jalview.io.AlignFile @@ -41,6 +42,11 @@ public class PDBfile super(inFile, inType); } + public PDBfile(FileParse source) throws IOException + { + super(source); + } + public String print() { return null; diff --git a/src/jalview/io/AlignFile.java b/src/jalview/io/AlignFile.java index da09590..2ad32f0 100755 --- a/src/jalview/io/AlignFile.java +++ b/src/jalview/io/AlignFile.java @@ -71,7 +71,17 @@ public abstract class AlignFile parse(); } - + /** + * Attempt to read from the position where some other parsing process left off. + * @param source + * @throws IOException + */ + public AlignFile(FileParse source) throws IOException + { + super(source); + initData(); + parse(); + } /** * Return the seqs Vector */ diff --git a/src/jalview/io/BLCFile.java b/src/jalview/io/BLCFile.java index c40e125..fa6f8e2 100755 --- a/src/jalview/io/BLCFile.java +++ b/src/jalview/io/BLCFile.java @@ -54,6 +54,10 @@ extends AlignFile { super(inFile, type); } + public BLCFile(FileParse source) throws IOException + { + super(source); + } /** * DOCUMENT ME! diff --git a/src/jalview/io/ClustalFile.java b/src/jalview/io/ClustalFile.java index 5d7be8c..d62d185 100755 --- a/src/jalview/io/ClustalFile.java +++ b/src/jalview/io/ClustalFile.java @@ -1,219 +1,222 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -import java.io.*; -import java.util.*; - -import jalview.datamodel.*; -import jalview.util.*; - -public class ClustalFile - extends AlignFile -{ - - public ClustalFile() - { - } - - public ClustalFile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - public void initData() - { - super.initData(); - } - - public void parse() - throws IOException - { - int i = 0; - boolean flag = false; - - Vector headers = new Vector(); - Hashtable seqhash = new Hashtable(); - StringBuffer tempseq; - String line, id; - StringTokenizer str; - - try - { - while ( (line = nextLine()) != null) - { - if (line.indexOf(" ") != 0) - { - str = new StringTokenizer(line, " "); - - if (str.hasMoreTokens()) - { - id = str.nextToken(); - - if (id.equalsIgnoreCase("CLUSTAL")) - { - flag = true; - } - else - { - if (flag) - { - if (seqhash.containsKey(id)) - { - tempseq = (StringBuffer) seqhash.get(id); - } - else - { - tempseq = new StringBuffer(); - seqhash.put(id, tempseq); - } - - if (! (headers.contains(id))) - { - headers.addElement(id); - } - - if (str.hasMoreTokens()) - { - tempseq.append(str.nextToken()); - } - } - } - } - else - { - flag = true; - } - } - } - } - catch (IOException e) - { - System.err.println("Exception parsing clustal file " + e); - e.printStackTrace(); - } - - if (flag) - { - this.noSeqs = headers.size(); - - //Add sequences to the hash - for (i = 0; i < headers.size(); i++) - { - if (seqhash.get(headers.elementAt(i)) != null) - { - if (maxLength < seqhash.get(headers.elementAt(i)).toString() - .length()) - { - maxLength = seqhash.get(headers.elementAt(i)).toString() - .length(); - } - - Sequence newSeq = parseId(headers.elementAt(i).toString()); - newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()). - toString()); - - seqs.addElement(newSeq); - } - else - { - System.err.println( - "Clustal File Reader: Can't find sequence for " + - headers.elementAt(i)); - } - } - } - } - - public String print() - { - return print(getSeqsAsArray()); - } - - public String print(SequenceI[] s) - { - StringBuffer out = new StringBuffer("CLUSTAL\n\n"); - - int max = 0; - int maxid = 0; - - int i = 0; - - while ( (i < s.length) && (s[i] != null)) - { - String tmp = printId(s[i]); - - if (s[i].getSequence().length > max) - { - max = s[i].getSequence().length; - } - - if (tmp.length() > maxid) - { - maxid = tmp.length(); - } - - i++; - } - - if (maxid < 15) - { - maxid = 15; - } - - maxid++; - - int len = 60; - int nochunks = (max / len) + 1; - - for (i = 0; i < nochunks; i++) - { - int j = 0; - - while ( (j < s.length) && (s[j] != null)) - { - out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " ")); - - int start = i * len; - int end = start + len; - - if ( (end < s[j].getSequence().length) && - (start < s[j].getSequence().length)) - { - out.append(s[j].getSequenceAsString(start, end)); - } - else - { - if (start < s[j].getSequence().length) - { - out.append(s[j].getSequenceAsString().substring(start)); - } - } - - out.append("\n"); - j++; - } - - out.append("\n"); - } - - return out.toString(); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; +import java.util.*; + +import jalview.datamodel.*; +import jalview.util.*; + +public class ClustalFile + extends AlignFile +{ + + public ClustalFile() + { + } + + public ClustalFile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + public ClustalFile(FileParse source) throws IOException + { + super(source); + } + public void initData() + { + super.initData(); + } + + public void parse() + throws IOException + { + int i = 0; + boolean flag = false; + + Vector headers = new Vector(); + Hashtable seqhash = new Hashtable(); + StringBuffer tempseq; + String line, id; + StringTokenizer str; + + try + { + while ( (line = nextLine()) != null) + { + if (line.indexOf(" ") != 0) + { + str = new StringTokenizer(line, " "); + + if (str.hasMoreTokens()) + { + id = str.nextToken(); + + if (id.equalsIgnoreCase("CLUSTAL")) + { + flag = true; + } + else + { + if (flag) + { + if (seqhash.containsKey(id)) + { + tempseq = (StringBuffer) seqhash.get(id); + } + else + { + tempseq = new StringBuffer(); + seqhash.put(id, tempseq); + } + + if (! (headers.contains(id))) + { + headers.addElement(id); + } + + if (str.hasMoreTokens()) + { + tempseq.append(str.nextToken()); + } + } + } + } + else + { + flag = true; + } + } + } + } + catch (IOException e) + { + System.err.println("Exception parsing clustal file " + e); + e.printStackTrace(); + } + + if (flag) + { + this.noSeqs = headers.size(); + + //Add sequences to the hash + for (i = 0; i < headers.size(); i++) + { + if (seqhash.get(headers.elementAt(i)) != null) + { + if (maxLength < seqhash.get(headers.elementAt(i)).toString() + .length()) + { + maxLength = seqhash.get(headers.elementAt(i)).toString() + .length(); + } + + Sequence newSeq = parseId(headers.elementAt(i).toString()); + newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()). + toString()); + + seqs.addElement(newSeq); + } + else + { + System.err.println( + "Clustal File Reader: Can't find sequence for " + + headers.elementAt(i)); + } + } + } + } + + public String print() + { + return print(getSeqsAsArray()); + } + + public String print(SequenceI[] s) + { + StringBuffer out = new StringBuffer("CLUSTAL\n\n"); + + int max = 0; + int maxid = 0; + + int i = 0; + + while ( (i < s.length) && (s[i] != null)) + { + String tmp = printId(s[i]); + + if (s[i].getSequence().length > max) + { + max = s[i].getSequence().length; + } + + if (tmp.length() > maxid) + { + maxid = tmp.length(); + } + + i++; + } + + if (maxid < 15) + { + maxid = 15; + } + + maxid++; + + int len = 60; + int nochunks = (max / len) + 1; + + for (i = 0; i < nochunks; i++) + { + int j = 0; + + while ( (j < s.length) && (s[j] != null)) + { + out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " ")); + + int start = i * len; + int end = start + len; + + if ( (end < s[j].getSequence().length) && + (start < s[j].getSequence().length)) + { + out.append(s[j].getSequenceAsString(start, end)); + } + else + { + if (start < s[j].getSequence().length) + { + out.append(s[j].getSequenceAsString().substring(start)); + } + } + + out.append("\n"); + j++; + } + + out.append("\n"); + } + + return out.toString(); + } +} diff --git a/src/jalview/io/FastaFile.java b/src/jalview/io/FastaFile.java index 8de469d..1cb016c 100755 --- a/src/jalview/io/FastaFile.java +++ b/src/jalview/io/FastaFile.java @@ -1,235 +1,240 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -import java.io.*; - -import jalview.datamodel.*; - -/** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ - */ -public class FastaFile - extends AlignFile -{ - /** - * Length of a sequence line - */ - int len = 72; - - StringBuffer out; - - /** - * Creates a new FastaFile object. - */ - public FastaFile() - { - } - - /** - * Creates a new FastaFile object. - * - * @param inFile DOCUMENT ME! - * @param type DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! - */ - public FastaFile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - /** - * DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! - */ - public void parse() - throws IOException - { - StringBuffer sb = new StringBuffer(); - boolean firstLine = true; - - String line; - Sequence seq = null; - - boolean annotation = false; - - while ( (line = nextLine()) != null) - { - line = line.trim(); - if (line.length() > 0) - { - if (line.charAt(0) == '>') - { - if (line.startsWith(">#_")) - { - if (annotation) - { - Annotation[] anots = new Annotation[sb.length()]; - String anotString = sb.toString(); - for (int i = 0; i < sb.length(); i++) - { - anots[i] = new Annotation(anotString.substring(i, i + 1), - null, - ' ', 0); - } - AlignmentAnnotation aa = new AlignmentAnnotation( - seq.getName().substring(2), seq.getDescription(), - anots); - - annotations.addElement(aa); - } - } - else - { - annotation = false; - } - - if (!firstLine) - { - seq.setSequence(sb.toString()); - - if (!annotation) - { - seqs.addElement(seq); - } - } - - seq = parseId(line.substring(1)); - firstLine = false; - - sb = new StringBuffer(); - - if (line.startsWith(">#_")) - { - annotation = true; - } - } - else - { - sb.append(line); - } - } - } - - if (annotation) - { - Annotation[] anots = new Annotation[sb.length()]; - String anotString = sb.toString(); - for (int i = 0; i < sb.length(); i++) - { - anots[i] = new Annotation(anotString.substring(i, i + 1), - null, - ' ', 0); - } - AlignmentAnnotation aa = new AlignmentAnnotation( - seq.getName().substring(2), seq.getDescription(), - anots); - - annotations.addElement(aa); - } - - else if (!firstLine) - { - seq.setSequence(sb.toString()); - seqs.addElement(seq); - } - } - - /** - * called by AppletFormatAdapter to generate - * an annotated alignment, rather than bare - * sequences. - * @param al - */ - public void addAnnotations(Alignment al) - { - addProperties(al); - for (int i = 0; i < annotations.size(); i++) - { - AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i); - aa.setPadGaps(true, al.getGapCharacter()); - al.addAnnotation( aa ); - } - } - - - /** - * DOCUMENT ME! - * - * @param s DOCUMENT ME! - * @param len DOCUMENT ME! - * @param gaps DOCUMENT ME! - * @param displayId DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String print(SequenceI[] s) - { - out = new StringBuffer(); - int i = 0; - - while ( (i < s.length) && (s[i] != null)) - { - out.append(">" + printId(s[i])); - if (s[i].getDescription() != null) - { - out.append(" " + s[i].getDescription()); - } - - out.append("\n"); - - int nochunks = (s[i].getLength() / len) + 1; - - for (int j = 0; j < nochunks; j++) - { - int start = j * len; - int end = start + len; - - if (end < s[i].getLength()) - { - out.append(s[i].getSequenceAsString(start, end) + "\n"); - } - else if (start < s[i].getLength()) - { - out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n"); - } - } - - i++; - } - - return out.toString(); - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String print() - { - return print(getSeqsAsArray()); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; + +import jalview.datamodel.*; + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class FastaFile + extends AlignFile +{ + /** + * Length of a sequence line + */ + int len = 72; + + StringBuffer out; + + /** + * Creates a new FastaFile object. + */ + public FastaFile() + { + } + + /** + * Creates a new FastaFile object. + * + * @param inFile DOCUMENT ME! + * @param type DOCUMENT ME! + * + * @throws IOException DOCUMENT ME! + */ + public FastaFile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + + public FastaFile(FileParse source) throws IOException + { + super(source); + } + + /** + * DOCUMENT ME! + * + * @throws IOException DOCUMENT ME! + */ + public void parse() + throws IOException + { + StringBuffer sb = new StringBuffer(); + boolean firstLine = true; + + String line; + Sequence seq = null; + + boolean annotation = false; + + while ( (line = nextLine()) != null) + { + line = line.trim(); + if (line.length() > 0) + { + if (line.charAt(0) == '>') + { + if (line.startsWith(">#_")) + { + if (annotation) + { + Annotation[] anots = new Annotation[sb.length()]; + String anotString = sb.toString(); + for (int i = 0; i < sb.length(); i++) + { + anots[i] = new Annotation(anotString.substring(i, i + 1), + null, + ' ', 0); + } + AlignmentAnnotation aa = new AlignmentAnnotation( + seq.getName().substring(2), seq.getDescription(), + anots); + + annotations.addElement(aa); + } + } + else + { + annotation = false; + } + + if (!firstLine) + { + seq.setSequence(sb.toString()); + + if (!annotation) + { + seqs.addElement(seq); + } + } + + seq = parseId(line.substring(1)); + firstLine = false; + + sb = new StringBuffer(); + + if (line.startsWith(">#_")) + { + annotation = true; + } + } + else + { + sb.append(line); + } + } + } + + if (annotation) + { + Annotation[] anots = new Annotation[sb.length()]; + String anotString = sb.toString(); + for (int i = 0; i < sb.length(); i++) + { + anots[i] = new Annotation(anotString.substring(i, i + 1), + null, + ' ', 0); + } + AlignmentAnnotation aa = new AlignmentAnnotation( + seq.getName().substring(2), seq.getDescription(), + anots); + + annotations.addElement(aa); + } + + else if (!firstLine) + { + seq.setSequence(sb.toString()); + seqs.addElement(seq); + } + } + + /** + * called by AppletFormatAdapter to generate + * an annotated alignment, rather than bare + * sequences. + * @param al + */ + public void addAnnotations(Alignment al) + { + addProperties(al); + for (int i = 0; i < annotations.size(); i++) + { + AlignmentAnnotation aa = (AlignmentAnnotation) annotations.elementAt(i); + aa.setPadGaps(true, al.getGapCharacter()); + al.addAnnotation( aa ); + } + } + + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param len DOCUMENT ME! + * @param gaps DOCUMENT ME! + * @param displayId DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print(SequenceI[] s) + { + out = new StringBuffer(); + int i = 0; + + while ( (i < s.length) && (s[i] != null)) + { + out.append(">" + printId(s[i])); + if (s[i].getDescription() != null) + { + out.append(" " + s[i].getDescription()); + } + + out.append("\n"); + + int nochunks = (s[i].getLength() / len) + 1; + + for (int j = 0; j < nochunks; j++) + { + int start = j * len; + int end = start + len; + + if (end < s[i].getLength()) + { + out.append(s[i].getSequenceAsString(start, end) + "\n"); + } + else if (start < s[i].getLength()) + { + out.append(s[i].getSequenceAsString(start, s[i].getLength()) + "\n"); + } + } + + i++; + } + + return out.toString(); + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print() + { + return print(getSeqsAsArray()); + } +} diff --git a/src/jalview/io/FeaturesFile.java b/src/jalview/io/FeaturesFile.java index 9b39d02..e961683 100755 --- a/src/jalview/io/FeaturesFile.java +++ b/src/jalview/io/FeaturesFile.java @@ -55,6 +55,10 @@ public class FeaturesFile { super(inFile, type); } + public FeaturesFile(FileParse source) throws IOException + { + super(source); + } /** * The Application can render HTML, but the applet will diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index 8a97e39..4c69ce0 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -27,6 +27,7 @@ import java.net.*; public class FileParse { public File inFile=null; + public int index = 1; // sequence counter for FileParse object created from same data source protected char suffixSeparator = '#'; /** * '#' separated string tagged on to end of filename @@ -46,6 +47,34 @@ public class FileParse { } /** + * Create a new FileParse instance reading from the same datasource starting at the current position. + * WARNING! Subsequent reads from either object will affect the read position of the other, but not + * the error state. + * + * @param from + */ + public FileParse(FileParse from) throws IOException + { + if (from==null) + { + throw new Error("Implementation error. Null FileParse in copy constructor"); + } + if (from==this) + return; + index = ++from.index; + inFile = from.inFile; + suffixSeparator = from.suffixSeparator; + suffix = from.suffix; + errormessage = from.errormessage; // inherit potential error messages + error = false; // reset any error condition. + type = from.type; + dataIn = from.dataIn; + if (dataIn!=null) + { + mark(); + } + } + /** * Attempt to open a file as a datasource. * Sets error and errormessage if fileStr was invalid. * @param fileStr @@ -183,6 +212,20 @@ public class FileParse error=false; dataIn.mark(READAHEAD_LIMIT); } + /** + * mark the current position in the source as start + * for the purposes of it being analysed by IdentifyFile().identify + * @throws IOException + */ + public void mark() throws IOException + { + if (dataIn!=null) + { + dataIn.mark(READAHEAD_LIMIT); + } else { + throw new IOException("Unitialised Source Stream"); + } + } public String nextLine() throws IOException { @@ -233,4 +276,15 @@ public class FileParse public String getWarningMessage() { return warningMessage; } + public String getInFile() + { + if (inFile!=null) + { + return inFile.getAbsolutePath()+" ("+index+")"; + } + else + { + return "From Paste + ("+index+")"; + } + } } diff --git a/src/jalview/io/JPredFile.java b/src/jalview/io/JPredFile.java index bd57050..dfe1ecf 100755 --- a/src/jalview/io/JPredFile.java +++ b/src/jalview/io/JPredFile.java @@ -63,7 +63,10 @@ public class JPredFile { super(inFile, type); } - + public JPredFile(FileParse source) throws IOException + { + super(source); + } /** * DOCUMENT ME! * diff --git a/src/jalview/io/MSFfile.java b/src/jalview/io/MSFfile.java index b9a14ba..e2d3704 100755 --- a/src/jalview/io/MSFfile.java +++ b/src/jalview/io/MSFfile.java @@ -1,399 +1,407 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -import java.io.*; -import java.util.*; - -import jalview.datamodel.*; -import jalview.util.*; - -/** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ - */ -public class MSFfile - extends AlignFile -{ - - /** - * Creates a new MSFfile object. - */ - public MSFfile() - { - } - - /** - * Creates a new MSFfile object. - * - * @param inFile DOCUMENT ME! - * @param type DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! - */ - public MSFfile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - /** - * DOCUMENT ME! - */ - public void parse() - throws IOException - { - int i = 0; - boolean seqFlag = false; - String key = new String(); - Vector headers = new Vector(); - Hashtable seqhash = new Hashtable(); - String line; - - try - { - while ( (line = nextLine()) != null) - { - StringTokenizer str = new StringTokenizer(line); - - while (str.hasMoreTokens()) - { - String inStr = str.nextToken(); - - //If line has header information add to the headers vector - if (inStr.indexOf("Name:") != -1) - { - key = str.nextToken(); - headers.addElement(key); - } - - //if line has // set SeqFlag to 1 so we know sequences are coming - if (inStr.indexOf("//") != -1) - { - seqFlag = true; - } - - //Process lines as sequence lines if seqFlag is set - if ( (inStr.indexOf("//") == -1) && (seqFlag == true)) - { - //seqeunce id is the first field - key = inStr; - - StringBuffer tempseq; - - //Get sequence from hash if it exists - if (seqhash.containsKey(key)) - { - tempseq = (StringBuffer) seqhash.get(key); - } - else - { - tempseq = new StringBuffer(); - seqhash.put(key, tempseq); - } - - //loop through the rest of the words - while (str.hasMoreTokens()) - { - //append the word to the sequence - tempseq.append(str.nextToken()); - } - } - } - } - } - catch (IOException e) - { - System.err.println("Exception parsing MSFFile " + e); - e.printStackTrace(); - } - - this.noSeqs = headers.size(); - - //Add sequences to the hash - for (i = 0; i < headers.size(); i++) - { - if (seqhash.get(headers.elementAt(i)) != null) - { - String head = headers.elementAt(i).toString(); - String seq = seqhash.get(head).toString(); - - if (maxLength < head.length()) - { - maxLength = head.length(); - } - - // Replace ~ with a sensible gap character - seq = seq.replace('~', '-'); - - Sequence newSeq = parseId(head); - - newSeq.setSequence(seq); - - seqs.addElement(newSeq); - } - else - { - System.err.println("MSFFile Parser: Can't find sequence for " + - headers.elementAt(i)); - } - } - } - - /** - * DOCUMENT ME! - * - * @param seq DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int checkSum(String seq) - { - int check = 0; - String sequence = seq.toUpperCase(); - - for (int i = 0; i < sequence.length(); i++) - { - try - { - - int value = sequence.charAt(i); - if (value != -1) - { - check += (i % 57 + 1) * value; - } - } - catch (Exception e) - { - System.err.println("Exception during MSF Checksum calculation"); - e.printStackTrace(); - } - } - - return check % 10000; - } - - /** - * DOCUMENT ME! - * - * @param s DOCUMENT ME! - * @param is_NA DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String print(SequenceI[] seqs) - { - - boolean is_NA = jalview.util.Comparison.isNucleotide(seqs); - - SequenceI[] s = new SequenceI[seqs.length]; - - StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA") + - "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't remember NA or AA yet. - - int max = 0; - int maxid = 0; - int i = 0; - - while ( (i < seqs.length) && (seqs[i] != null)) - { - // Replace all internal gaps with . and external spaces with ~ - s[i] = new Sequence(seqs[i].getName(), - seqs[i].getSequenceAsString().replace('-', '.')); - - StringBuffer sb = new StringBuffer(); - sb.append(s[i].getSequence()); - - for (int ii = 0; ii < sb.length(); ii++) - { - if (sb.charAt(ii) == '.') - { - sb.setCharAt(ii, '~'); - } - else - { - break; - } - } - - for (int ii = sb.length() - 1; ii > 0; ii--) - { - if (sb.charAt(ii) == '.') - { - sb.setCharAt(ii, '~'); - } - else - { - break; - } - } - - s[i].setSequence(sb.toString()); - - if (s[i].getSequence().length > max) - { - max = s[i].getSequence().length; - } - - i++; - } - - Format maxLenpad = new Format("%" + (new String("" + max)).length() + - "d"); - Format maxChkpad = new Format("%" + (new String("1" + max)).length() + - "d"); - i = 0; - - int bigChecksum = 0; - int[] checksums = new int[s.length]; - while (i < s.length) - { - checksums[i] = checkSum(s[i].getSequenceAsString()); - bigChecksum += checksums[i]; - i++; - } - - long maxNB = 0; - out.append(" MSF: " + s[0].getSequence().length + " Type: " + - (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000) + - " ..\n\n\n"); - - String[] nameBlock = new String[s.length]; - String[] idBlock = new String[s.length]; - - i = 0; - while ( (i < s.length) && (s[i] != null)) - { - - nameBlock[i] = new String(" Name: " + printId(s[i]) + " "); - - idBlock[i] = new String("Len: " + - maxLenpad.form(s[i].getSequence().length) + - " Check: " + - maxChkpad.form(checksums[i]) + " Weight: 1.00\n"); - - if (s[i].getName().length() > maxid) - { - maxid = s[i].getName().length(); - } - - if (nameBlock[i].length() > maxNB) - { - maxNB = nameBlock[i].length(); - } - - i++; - } - - if (maxid < 10) - { - maxid = 10; - } - - if (maxNB < 15) - { - maxNB = 15; - } - - Format nbFormat = new Format("%-" + maxNB + "s"); - - for (i = 0; (i < s.length) && (s[i] != null); i++) - { - out.append(nbFormat.form(nameBlock[i]) + idBlock[i]); - } - - maxid++; - out.append("\n\n//\n\n"); - - int len = 50; - - int nochunks = (max / len) + 1; - - if ( (max % len) == 0) - { - nochunks--; - } - - for (i = 0; i < nochunks; i++) - { - int j = 0; - - while ( (j < s.length) && (s[j] != null)) - { - String name = printId(s[j]); - - out.append(new Format("%-" + maxid + "s").form(name + " ")); - - for (int k = 0; k < 5; k++) - { - int start = (i * 50) + (k * 10); - int end = start + 10; - - if ( (end < s[j].getSequence().length) && - (start < s[j].getSequence().length)) - { - out.append(s[j].getSequence(start, end)); - - if (k < 4) - { - out.append(" "); - } - else - { - out.append("\n"); - } - } - else - { - if (start < s[j].getSequence().length) - { - out.append(s[j].getSequenceAsString().substring(start)); - out.append("\n"); - } - else - { - if (k == 0) - { - out.append("\n"); - } - } - } - } - - j++; - } - - out.append("\n"); - } - - return out.toString(); - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String print() - { - return print(getSeqsAsArray()); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; +import java.util.*; + +import jalview.datamodel.*; +import jalview.util.*; + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class MSFfile + extends AlignFile +{ + + /** + * Creates a new MSFfile object. + */ + public MSFfile() + { + } + + /** + * Creates a new MSFfile object. + * + * @param inFile DOCUMENT ME! + * @param type DOCUMENT ME! + * + * @throws IOException DOCUMENT ME! + */ + public MSFfile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + + public MSFfile(FileParse source) throws IOException + { + super(source); + } +{ + // TODO Auto-generated constructor stub + } + + /** + * DOCUMENT ME! + */ + public void parse() + throws IOException + { + int i = 0; + boolean seqFlag = false; + String key = new String(); + Vector headers = new Vector(); + Hashtable seqhash = new Hashtable(); + String line; + + try + { + while ( (line = nextLine()) != null) + { + StringTokenizer str = new StringTokenizer(line); + + while (str.hasMoreTokens()) + { + String inStr = str.nextToken(); + + //If line has header information add to the headers vector + if (inStr.indexOf("Name:") != -1) + { + key = str.nextToken(); + headers.addElement(key); + } + + //if line has // set SeqFlag to 1 so we know sequences are coming + if (inStr.indexOf("//") != -1) + { + seqFlag = true; + } + + //Process lines as sequence lines if seqFlag is set + if ( (inStr.indexOf("//") == -1) && (seqFlag == true)) + { + //seqeunce id is the first field + key = inStr; + + StringBuffer tempseq; + + //Get sequence from hash if it exists + if (seqhash.containsKey(key)) + { + tempseq = (StringBuffer) seqhash.get(key); + } + else + { + tempseq = new StringBuffer(); + seqhash.put(key, tempseq); + } + + //loop through the rest of the words + while (str.hasMoreTokens()) + { + //append the word to the sequence + tempseq.append(str.nextToken()); + } + } + } + } + } + catch (IOException e) + { + System.err.println("Exception parsing MSFFile " + e); + e.printStackTrace(); + } + + this.noSeqs = headers.size(); + + //Add sequences to the hash + for (i = 0; i < headers.size(); i++) + { + if (seqhash.get(headers.elementAt(i)) != null) + { + String head = headers.elementAt(i).toString(); + String seq = seqhash.get(head).toString(); + + if (maxLength < head.length()) + { + maxLength = head.length(); + } + + // Replace ~ with a sensible gap character + seq = seq.replace('~', '-'); + + Sequence newSeq = parseId(head); + + newSeq.setSequence(seq); + + seqs.addElement(newSeq); + } + else + { + System.err.println("MSFFile Parser: Can't find sequence for " + + headers.elementAt(i)); + } + } + } + + /** + * DOCUMENT ME! + * + * @param seq DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public int checkSum(String seq) + { + int check = 0; + String sequence = seq.toUpperCase(); + + for (int i = 0; i < sequence.length(); i++) + { + try + { + + int value = sequence.charAt(i); + if (value != -1) + { + check += (i % 57 + 1) * value; + } + } + catch (Exception e) + { + System.err.println("Exception during MSF Checksum calculation"); + e.printStackTrace(); + } + } + + return check % 10000; + } + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param is_NA DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print(SequenceI[] seqs) + { + + boolean is_NA = jalview.util.Comparison.isNucleotide(seqs); + + SequenceI[] s = new SequenceI[seqs.length]; + + StringBuffer out = new StringBuffer("!!" + (is_NA ? "NA" : "AA") + + "_MULTIPLE_ALIGNMENT 1.0\n\n"); // TODO: JBPNote : Jalview doesn't remember NA or AA yet. + + int max = 0; + int maxid = 0; + int i = 0; + + while ( (i < seqs.length) && (seqs[i] != null)) + { + // Replace all internal gaps with . and external spaces with ~ + s[i] = new Sequence(seqs[i].getName(), + seqs[i].getSequenceAsString().replace('-', '.')); + + StringBuffer sb = new StringBuffer(); + sb.append(s[i].getSequence()); + + for (int ii = 0; ii < sb.length(); ii++) + { + if (sb.charAt(ii) == '.') + { + sb.setCharAt(ii, '~'); + } + else + { + break; + } + } + + for (int ii = sb.length() - 1; ii > 0; ii--) + { + if (sb.charAt(ii) == '.') + { + sb.setCharAt(ii, '~'); + } + else + { + break; + } + } + + s[i].setSequence(sb.toString()); + + if (s[i].getSequence().length > max) + { + max = s[i].getSequence().length; + } + + i++; + } + + Format maxLenpad = new Format("%" + (new String("" + max)).length() + + "d"); + Format maxChkpad = new Format("%" + (new String("1" + max)).length() + + "d"); + i = 0; + + int bigChecksum = 0; + int[] checksums = new int[s.length]; + while (i < s.length) + { + checksums[i] = checkSum(s[i].getSequenceAsString()); + bigChecksum += checksums[i]; + i++; + } + + long maxNB = 0; + out.append(" MSF: " + s[0].getSequence().length + " Type: " + + (is_NA ? "N" : "P") + " Check: " + (bigChecksum % 10000) + + " ..\n\n\n"); + + String[] nameBlock = new String[s.length]; + String[] idBlock = new String[s.length]; + + i = 0; + while ( (i < s.length) && (s[i] != null)) + { + + nameBlock[i] = new String(" Name: " + printId(s[i]) + " "); + + idBlock[i] = new String("Len: " + + maxLenpad.form(s[i].getSequence().length) + + " Check: " + + maxChkpad.form(checksums[i]) + " Weight: 1.00\n"); + + if (s[i].getName().length() > maxid) + { + maxid = s[i].getName().length(); + } + + if (nameBlock[i].length() > maxNB) + { + maxNB = nameBlock[i].length(); + } + + i++; + } + + if (maxid < 10) + { + maxid = 10; + } + + if (maxNB < 15) + { + maxNB = 15; + } + + Format nbFormat = new Format("%-" + maxNB + "s"); + + for (i = 0; (i < s.length) && (s[i] != null); i++) + { + out.append(nbFormat.form(nameBlock[i]) + idBlock[i]); + } + + maxid++; + out.append("\n\n//\n\n"); + + int len = 50; + + int nochunks = (max / len) + 1; + + if ( (max % len) == 0) + { + nochunks--; + } + + for (i = 0; i < nochunks; i++) + { + int j = 0; + + while ( (j < s.length) && (s[j] != null)) + { + String name = printId(s[j]); + + out.append(new Format("%-" + maxid + "s").form(name + " ")); + + for (int k = 0; k < 5; k++) + { + int start = (i * 50) + (k * 10); + int end = start + 10; + + if ( (end < s[j].getSequence().length) && + (start < s[j].getSequence().length)) + { + out.append(s[j].getSequence(start, end)); + + if (k < 4) + { + out.append(" "); + } + else + { + out.append("\n"); + } + } + else + { + if (start < s[j].getSequence().length) + { + out.append(s[j].getSequenceAsString().substring(start)); + out.append("\n"); + } + else + { + if (k == 0) + { + out.append("\n"); + } + } + } + } + + j++; + } + + out.append("\n"); + } + + return out.toString(); + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print() + { + return print(getSeqsAsArray()); + } +} diff --git a/src/jalview/io/NewickFile.java b/src/jalview/io/NewickFile.java index aa9a129..74612a6 100755 --- a/src/jalview/io/NewickFile.java +++ b/src/jalview/io/NewickFile.java @@ -80,7 +80,10 @@ public class NewickFile { super(inFile, type); } - + public NewickFile(FileParse source) throws IOException + { + super(source); + } /** * Creates a new NewickFile object. * diff --git a/src/jalview/io/PIRFile.java b/src/jalview/io/PIRFile.java index 910d189..b18b8f4 100755 --- a/src/jalview/io/PIRFile.java +++ b/src/jalview/io/PIRFile.java @@ -1,189 +1,192 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -import java.io.*; -import java.util.*; - -import jalview.datamodel.*; - -public class PIRFile - extends AlignFile -{ - public static boolean useModellerOutput = false; - - Vector words = new Vector(); //Stores the words in a line after splitting - - public PIRFile() - { - } - - public PIRFile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - public void parse() - throws IOException - { - StringBuffer sequence; - String line = null; - ModellerDescription md; - - while ( (line = nextLine()) != null) - { - if (line.length() == 0) - { - //System.out.println("blank line"); - continue; - } - if (line.indexOf("C;") == 0 || line.indexOf("#") == 0) - { - continue; - } - Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1)); - - sequence = new StringBuffer(); - - newSeq.setDescription(nextLine()); // this is the title line - - boolean starFound = false; - - while (!starFound) - { - line = nextLine(); - sequence.append(line); - - if (line == null) - { - break; - } - - if (line.indexOf("*") > -1) - { - starFound = true; - } - } - - if (sequence.length() > 0) - { - sequence.setLength(sequence.length() - 1); - newSeq.setSequence(sequence.toString()); - - seqs.addElement(newSeq); - - md = new ModellerDescription(newSeq. - getDescription()); - md.updateSequenceI(newSeq); - } - } - } - - public String print() - { - return print(getSeqsAsArray()); - } - - public String print(SequenceI[] s) - { - boolean is_NA = jalview.util.Comparison.isNucleotide(s); - int len = 72; - StringBuffer out = new StringBuffer(); - int i = 0; - ModellerDescription md; - - while ( (i < s.length) && (s[i] != null)) - { - String seq = s[i].getSequenceAsString(); - seq = seq + "*"; - - if (is_NA) - { - // modeller doesn't really do nucleotides, so we don't do anything fancy - // Official tags area as follows, for now we'll use P1 and DL - // Protein (complete) P1 - // Protein (fragment) F1 - // DNA (linear) Dl - // DNA (circular) DC - // RNA (linear) RL - // RNA (circular) RC - // tRNA N3 - // other functional RNA N1 - - out.append(">N1;" + s[i].getName() + "\n"); - if (s[i].getDescription() == null) - { - out.append(s[i].getName() + " " + - (s[i].getEnd() - s[i].getStart() + 1)); - out.append(is_NA ? " bases\n" : " residues\n"); - } - else - { - out.append(s[i].getDescription() + "\n"); - } - } - else - { - - if (useModellerOutput) - { - out.append(">P1;" + s[i].getName() + "\n"); - md = new ModellerDescription(s[i]); - out.append(md.getDescriptionLine() + "\n"); - } - else - { - out.append(">P1;" + printId(s[i]) + "\n"); - if (s[i].getDescription() != null) - { - out.append(s[i].getDescription() + "\n"); - } - else - { - out.append(s[i].getName() + " " - + (s[i].getEnd() - s[i].getStart() + 1) - + " residues\n"); - } - } - } - int nochunks = (seq.length() / len) + 1; - - for (int j = 0; j < nochunks; j++) - { - int start = j * len; - int end = start + len; - - if (end < seq.length()) - { - out.append(seq.substring(start, end) + "\n"); - } - else if (start < seq.length()) - { - out.append(seq.substring(start) + "\n"); - } - } - - i++; - } - - return out.toString(); - } - -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; +import java.util.*; + +import jalview.datamodel.*; + +public class PIRFile + extends AlignFile +{ + public static boolean useModellerOutput = false; + + Vector words = new Vector(); //Stores the words in a line after splitting + + public PIRFile() + { + } + + public PIRFile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + public PIRFile(FileParse source) throws IOException + { + super(source); + } + public void parse() + throws IOException + { + StringBuffer sequence; + String line = null; + ModellerDescription md; + + while ( (line = nextLine()) != null) + { + if (line.length() == 0) + { + //System.out.println("blank line"); + continue; + } + if (line.indexOf("C;") == 0 || line.indexOf("#") == 0) + { + continue; + } + Sequence newSeq = parseId(line.substring(line.indexOf(";") + 1)); + + sequence = new StringBuffer(); + + newSeq.setDescription(nextLine()); // this is the title line + + boolean starFound = false; + + while (!starFound) + { + line = nextLine(); + sequence.append(line); + + if (line == null) + { + break; + } + + if (line.indexOf("*") > -1) + { + starFound = true; + } + } + + if (sequence.length() > 0) + { + sequence.setLength(sequence.length() - 1); + newSeq.setSequence(sequence.toString()); + + seqs.addElement(newSeq); + + md = new ModellerDescription(newSeq. + getDescription()); + md.updateSequenceI(newSeq); + } + } + } + + public String print() + { + return print(getSeqsAsArray()); + } + + public String print(SequenceI[] s) + { + boolean is_NA = jalview.util.Comparison.isNucleotide(s); + int len = 72; + StringBuffer out = new StringBuffer(); + int i = 0; + ModellerDescription md; + + while ( (i < s.length) && (s[i] != null)) + { + String seq = s[i].getSequenceAsString(); + seq = seq + "*"; + + if (is_NA) + { + // modeller doesn't really do nucleotides, so we don't do anything fancy + // Official tags area as follows, for now we'll use P1 and DL + // Protein (complete) P1 + // Protein (fragment) F1 + // DNA (linear) Dl + // DNA (circular) DC + // RNA (linear) RL + // RNA (circular) RC + // tRNA N3 + // other functional RNA N1 + + out.append(">N1;" + s[i].getName() + "\n"); + if (s[i].getDescription() == null) + { + out.append(s[i].getName() + " " + + (s[i].getEnd() - s[i].getStart() + 1)); + out.append(is_NA ? " bases\n" : " residues\n"); + } + else + { + out.append(s[i].getDescription() + "\n"); + } + } + else + { + + if (useModellerOutput) + { + out.append(">P1;" + s[i].getName() + "\n"); + md = new ModellerDescription(s[i]); + out.append(md.getDescriptionLine() + "\n"); + } + else + { + out.append(">P1;" + printId(s[i]) + "\n"); + if (s[i].getDescription() != null) + { + out.append(s[i].getDescription() + "\n"); + } + else + { + out.append(s[i].getName() + " " + + (s[i].getEnd() - s[i].getStart() + 1) + + " residues\n"); + } + } + } + int nochunks = (seq.length() / len) + 1; + + for (int j = 0; j < nochunks; j++) + { + int start = j * len; + int end = start + len; + + if (end < seq.length()) + { + out.append(seq.substring(start, end) + "\n"); + } + else if (start < seq.length()) + { + out.append(seq.substring(start) + "\n"); + } + } + + i++; + } + + return out.toString(); + } + +} diff --git a/src/jalview/io/PfamFile.java b/src/jalview/io/PfamFile.java index 4c90bca..2b0bf13 100755 --- a/src/jalview/io/PfamFile.java +++ b/src/jalview/io/PfamFile.java @@ -1,172 +1,175 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -import java.io.*; -import java.util.*; - -import jalview.datamodel.*; -import jalview.util.*; - -public class PfamFile - extends AlignFile -{ - - public PfamFile() - { - } - - public PfamFile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - public void initData() - { - super.initData(); - } - - public void parse() - throws IOException - { - int i = 0; - String line; - - Hashtable seqhash = new Hashtable(); - Vector headers = new Vector(); - - while ( (line = nextLine()) != null) - { - if (line.indexOf(" ") != 0) - { - if (line.indexOf("#") != 0) - { - StringTokenizer str = new StringTokenizer(line, " "); - String id = ""; - - if (str.hasMoreTokens()) - { - id = str.nextToken(); - - StringBuffer tempseq; - - if (seqhash.containsKey(id)) - { - tempseq = (StringBuffer) seqhash.get(id); - } - else - { - tempseq = new StringBuffer(); - seqhash.put(id, tempseq); - } - - if (! (headers.contains(id))) - { - headers.addElement(id); - } - - tempseq.append(str.nextToken()); - } - } - } - } - - this.noSeqs = headers.size(); - - if (noSeqs < 1) - { - throw new IOException("No sequences found (PFAM input)"); - } - - for (i = 0; i < headers.size(); i++) - { - if (seqhash.get(headers.elementAt(i)) != null) - { - if (maxLength < seqhash.get(headers.elementAt(i)).toString() - .length()) - { - maxLength = seqhash.get(headers.elementAt(i)).toString() - .length(); - } - - Sequence newSeq = parseId(headers.elementAt(i).toString()); - newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()). - toString()); - seqs.addElement(newSeq); - } - else - { - System.err.println("PFAM File reader: Can't find sequence for " + - headers.elementAt(i)); - } - } - } - - public String print(SequenceI[] s) - { - StringBuffer out = new StringBuffer(""); - - int max = 0; - int maxid = 0; - - int i = 0; - - while ( (i < s.length) && (s[i] != null)) - { - String tmp = printId(s[i]); - - if (s[i].getSequence().length > max) - { - max = s[i].getSequence().length; - } - - if (tmp.length() > maxid) - { - maxid = tmp.length(); - } - - i++; - } - - if (maxid < 15) - { - maxid = 15; - } - - int j = 0; - - while ( (j < s.length) && (s[j] != null)) - { - out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " ")); - - out.append(s[j].getSequenceAsString() + "\n"); - j++; - } - - out.append("\n"); - - return out.toString(); - } - - public String print() - { - return print(getSeqsAsArray()); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +import java.io.*; +import java.util.*; + +import jalview.datamodel.*; +import jalview.util.*; + +public class PfamFile + extends AlignFile +{ + + public PfamFile() + { + } + + public PfamFile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + public PfamFile(FileParse source) throws IOException + { + super(source); + } + public void initData() + { + super.initData(); + } + + public void parse() + throws IOException + { + int i = 0; + String line; + + Hashtable seqhash = new Hashtable(); + Vector headers = new Vector(); + + while ( (line = nextLine()) != null) + { + if (line.indexOf(" ") != 0) + { + if (line.indexOf("#") != 0) + { + StringTokenizer str = new StringTokenizer(line, " "); + String id = ""; + + if (str.hasMoreTokens()) + { + id = str.nextToken(); + + StringBuffer tempseq; + + if (seqhash.containsKey(id)) + { + tempseq = (StringBuffer) seqhash.get(id); + } + else + { + tempseq = new StringBuffer(); + seqhash.put(id, tempseq); + } + + if (! (headers.contains(id))) + { + headers.addElement(id); + } + + tempseq.append(str.nextToken()); + } + } + } + } + + this.noSeqs = headers.size(); + + if (noSeqs < 1) + { + throw new IOException("No sequences found (PFAM input)"); + } + + for (i = 0; i < headers.size(); i++) + { + if (seqhash.get(headers.elementAt(i)) != null) + { + if (maxLength < seqhash.get(headers.elementAt(i)).toString() + .length()) + { + maxLength = seqhash.get(headers.elementAt(i)).toString() + .length(); + } + + Sequence newSeq = parseId(headers.elementAt(i).toString()); + newSeq.setSequence(seqhash.get(headers.elementAt(i).toString()). + toString()); + seqs.addElement(newSeq); + } + else + { + System.err.println("PFAM File reader: Can't find sequence for " + + headers.elementAt(i)); + } + } + } + + public String print(SequenceI[] s) + { + StringBuffer out = new StringBuffer(""); + + int max = 0; + int maxid = 0; + + int i = 0; + + while ( (i < s.length) && (s[i] != null)) + { + String tmp = printId(s[i]); + + if (s[i].getSequence().length > max) + { + max = s[i].getSequence().length; + } + + if (tmp.length() > maxid) + { + maxid = tmp.length(); + } + + i++; + } + + if (maxid < 15) + { + maxid = 15; + } + + int j = 0; + + while ( (j < s.length) && (s[j] != null)) + { + out.append(new Format("%-" + maxid + "s").form(printId(s[j]) + " ")); + + out.append(s[j].getSequenceAsString() + "\n"); + j++; + } + + out.append("\n"); + + return out.toString(); + } + + public String print() + { + return print(getSeqsAsArray()); + } +} diff --git a/src/jalview/io/PileUpfile.java b/src/jalview/io/PileUpfile.java index 4715d9c..cd46541 100755 --- a/src/jalview/io/PileUpfile.java +++ b/src/jalview/io/PileUpfile.java @@ -1,185 +1,188 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ -package jalview.io; - -/** - *

Title:

- * PileUpfile - *

Description:

- * - * Read and write PileUp style MSF Files. - * This used to be the MSFFile class, and was written according to the EBI's idea - * of a subset of the MSF alignment format. But, that was updated to reflect current - * GCG style IO fashion, as found in Emboss (thanks David Martin!) - * - **/ -import java.io.*; - -import jalview.datamodel.*; -import jalview.util.*; - -public class PileUpfile - extends MSFfile -{ - - /** - * Creates a new MSFfile object. - */ - public PileUpfile() - { - } - - /** - * Creates a new MSFfile object. - * - * @param inFile DOCUMENT ME! - * @param type DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! - */ - public PileUpfile(String inFile, String type) - throws IOException - { - super(inFile, type); - } - - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public String print() - { - return print(getSeqsAsArray()); - } - - public String print(SequenceI[] s) - { - StringBuffer out = new StringBuffer("PileUp\n\n"); - - int max = 0; - int maxid = 0; - - int i = 0; - int bigChecksum = 0; - int[] checksums = new int[s.length]; - while (i < s.length) - { - checksums[i] = checkSum(s[i].getSequenceAsString()); - bigChecksum += checksums[i]; - i++; - } - - out.append(" MSF: " + s[0].getSequence().length + - " Type: P Check: " + bigChecksum % 10000 + " ..\n\n\n"); - - i = 0; - while ( (i < s.length) && (s[i] != null)) - { - String seq = s[i].getSequenceAsString(); - out.append(" Name: " + printId(s[i]) + - " oo Len: " + - seq.length() + " Check: " + checksums[i] + - " Weight: 1.00\n"); - - if (seq.length() > max) - { - max = seq.length(); - } - - if (s[i].getName().length() > maxid) - { - maxid = s[i].getName().length(); - } - - i++; - } - - if (maxid < 10) - { - maxid = 10; - } - - maxid++; - out.append("\n\n//\n\n"); - - int len = 50; - - int nochunks = (max / len) + 1; - - if ( (max % len) == 0) - { - nochunks--; - } - - for (i = 0; i < nochunks; i++) - { - int j = 0; - - while ( (j < s.length) && (s[j] != null)) - { - String name = printId(s[j]); - - out.append(new Format("%-" + maxid + "s").form(name + " ")); - - for (int k = 0; k < 5; k++) - { - int start = (i * 50) + (k * 10); - int end = start + 10; - - if ( (end < s[j].getSequence().length) && - (start < s[j].getSequence().length)) - { - out.append(s[j].getSequence(start, end)); - - if (k < 4) - { - out.append(" "); - } - else - { - out.append("\n"); - } - } - else - { - if (start < s[j].getSequence().length) - { - out.append(s[j].getSequenceAsString().substring(start)); - out.append("\n"); - } - else - { - if (k == 0) - { - out.append("\n"); - } - } - } - } - - j++; - } - - out.append("\n"); - } - - return out.toString(); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.io; + +/** + *

Title:

+ * PileUpfile + *

Description:

+ * + * Read and write PileUp style MSF Files. + * This used to be the MSFFile class, and was written according to the EBI's idea + * of a subset of the MSF alignment format. But, that was updated to reflect current + * GCG style IO fashion, as found in Emboss (thanks David Martin!) + * + **/ +import java.io.*; + +import jalview.datamodel.*; +import jalview.util.*; + +public class PileUpfile + extends MSFfile +{ + + /** + * Creates a new MSFfile object. + */ + public PileUpfile() + { + } + + /** + * Creates a new MSFfile object. + * + * @param inFile DOCUMENT ME! + * @param type DOCUMENT ME! + * + * @throws IOException DOCUMENT ME! + */ + public PileUpfile(String inFile, String type) + throws IOException + { + super(inFile, type); + } + public PileUpfile(FileParse source) throws IOException + { + super(source); + } + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print() + { + return print(getSeqsAsArray()); + } + + public String print(SequenceI[] s) + { + StringBuffer out = new StringBuffer("PileUp\n\n"); + + int max = 0; + int maxid = 0; + + int i = 0; + int bigChecksum = 0; + int[] checksums = new int[s.length]; + while (i < s.length) + { + checksums[i] = checkSum(s[i].getSequenceAsString()); + bigChecksum += checksums[i]; + i++; + } + + out.append(" MSF: " + s[0].getSequence().length + + " Type: P Check: " + bigChecksum % 10000 + " ..\n\n\n"); + + i = 0; + while ( (i < s.length) && (s[i] != null)) + { + String seq = s[i].getSequenceAsString(); + out.append(" Name: " + printId(s[i]) + + " oo Len: " + + seq.length() + " Check: " + checksums[i] + + " Weight: 1.00\n"); + + if (seq.length() > max) + { + max = seq.length(); + } + + if (s[i].getName().length() > maxid) + { + maxid = s[i].getName().length(); + } + + i++; + } + + if (maxid < 10) + { + maxid = 10; + } + + maxid++; + out.append("\n\n//\n\n"); + + int len = 50; + + int nochunks = (max / len) + 1; + + if ( (max % len) == 0) + { + nochunks--; + } + + for (i = 0; i < nochunks; i++) + { + int j = 0; + + while ( (j < s.length) && (s[j] != null)) + { + String name = printId(s[j]); + + out.append(new Format("%-" + maxid + "s").form(name + " ")); + + for (int k = 0; k < 5; k++) + { + int start = (i * 50) + (k * 10); + int end = start + 10; + + if ( (end < s[j].getSequence().length) && + (start < s[j].getSequence().length)) + { + out.append(s[j].getSequence(start, end)); + + if (k < 4) + { + out.append(" "); + } + else + { + out.append("\n"); + } + } + else + { + if (start < s[j].getSequence().length) + { + out.append(s[j].getSequenceAsString().substring(start)); + out.append("\n"); + } + else + { + if (k == 0) + { + out.append("\n"); + } + } + } + } + + j++; + } + + out.append("\n"); + } + + return out.toString(); + } +} diff --git a/src/jalview/io/StockholmFile.java b/src/jalview/io/StockholmFile.java index 945b8c5..d5f3cc2 100644 --- a/src/jalview/io/StockholmFile.java +++ b/src/jalview/io/StockholmFile.java @@ -53,7 +53,10 @@ public class StockholmFile extends AlignFile { super(inFile, type); } - + public StockholmFile(FileParse source) throws IOException + { + super(source); + } public void initData() { super.initData(); @@ -221,6 +224,7 @@ public class StockholmFile extends AlignFile // + ": " + seq); this.seqs.addElement(seqO); } + return; // finished parsing this segment of source } else if (!r.search(line)) { -- 1.7.10.2