X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBLCFile.java;h=c0ecd580e2b3329ef3d1f172acdbf6bf1fcd8b11;hb=ee198b3ca3687f18a2ee186f4e7c7330f4ea30f0;hp=687eef9cacef4f0a7e36bc5215695d9862a8e3e2;hpb=588042b69abf8e60bcc950b24c283933c7dd422f;p=jalview.git diff --git a/src/jalview/io/BLCFile.java b/src/jalview/io/BLCFile.java index 687eef9..c0ecd58 100755 --- a/src/jalview/io/BLCFile.java +++ b/src/jalview/io/BLCFile.java @@ -1,183 +1,296 @@ -/* -* Jalview - A Sequence Alignment Editor and Viewer -* Copyright (C) 2005 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 jalview.datamodel.*; - -import java.io.*; - -import java.util.*; - - -public class BLCFile extends AlignFile { - Vector titles; - - public BLCFile() { - } - - public BLCFile(String inStr) { - super(inStr); - } - - public BLCFile(String inFile, String type) throws IOException { - super(inFile, type); - } - - public void initData() { - super.initData(); - titles = new Vector(); - } - - public void parse() { - boolean idsFound = false; - Vector ids = new Vector(); - StringBuffer[] seqstrings; - Vector starts = new Vector(); - Vector ends = new Vector(); - - String line = null; - - try { - do { - line = nextLine(); - - // seek end of ids - if (line.indexOf("*") > -1) { - idsFound = true; - - break; - } - - int abracket = line.indexOf(">"); - - if (abracket > -1) { - if (line.indexOf(" ") > -1) // - { - ///Colur it be this format? - //>54402046 0 1 137 137: - // or this?? - // 1 >L1H14 30539 343 - try { - ids.addElement(line.substring(abracket + 1, - line.indexOf(" ", abracket + 1))); - - // remove p Value - line = line.substring(abracket + 1); - line = line.substring(line.indexOf(" ") + 1); - line = line.trim(); - line = line.substring(line.indexOf(" ") + 1); - line = line.trim(); - - int value = Integer.parseInt(line.substring(0, - line.indexOf(" "))); - starts.addElement(value + ""); - line = line.substring(line.indexOf(" ") + 1); - line = line.trim(); - value = Integer.parseInt(line.substring(0, - line.indexOf(" "))); - ends.addElement(value + ""); - } catch (Exception ex) { - System.err.println("Error during blockfile read."); - ex.printStackTrace(); - starts.addElement("0"); - ends.addElement("0"); - } - } else { - ids.addElement(line.substring(abracket + 1, - line.indexOf("/"))); - line = line.substring(line.indexOf("/") + 1); - starts.addElement(line.substring(0, line.indexOf("-"))); - ends.addElement(line.substring(line.indexOf("-") + 1)); - } - } - } while (!idsFound); - - int starCol = line.indexOf("*"); - seqstrings = new StringBuffer[ids.size()]; - - for (int i = 0; i < ids.size(); i++) { - if (seqstrings[i] == null) { - seqstrings[i] = new StringBuffer(); - } - } - - while ((line = nextLine()).indexOf("*") == -1) { - for (int i = 0; i < ids.size(); i++) { - if (line.length() > (i + starCol)) { - seqstrings[i].append(line.charAt(i + starCol)); - } - } - } - - for (int i = 0; i < ids.size(); i++) { - Sequence newSeq = new Sequence(ids.elementAt(i).toString(), - seqstrings[i].toString(), - Integer.parseInt(starts.elementAt(i).toString()), - Integer.parseInt(ends.elementAt(i).toString())); - seqs.addElement(newSeq); - } - } catch (Exception ex) { - ex.printStackTrace(); - } - } - - public String print() { - return print(getSeqsAsArray()); - } - - public static String print(SequenceI[] s) { - StringBuffer out = new StringBuffer(); - - int i = 0; - int max = -1; - - while ((i < s.length) && (s[i] != null)) { - out.append(">" + s[i].getName() + "/" + s[i].getStart() + "-" + - s[i].getEnd() + "\n"); - - if (s[i].getSequence().length() > max) { - max = s[i].getSequence().length(); - } - - i++; - } - - out.append("* iteration 1\n"); - - for (int j = 0; j < max; j++) { - i = 0; - - while ((i < s.length) && (s[i] != null)) { - if (s[i].getSequence().length() > j) { - out.append(s[i].getSequence().substring(j, j + 1)); - } else { - out.append("-"); - } - - i++; - } - - out.append("\n"); - } - - out.append("*\n"); - - return out.toString(); - } -} +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview 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 3 + * of the License, or (at your option) any later version. + * + * Jalview 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 Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.io; + +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; + +import java.io.IOException; +import java.util.Vector; + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class BLCFile extends AlignFile +{ + Vector titles; + + /** + * Creates a new BLCFile object. + */ + public BLCFile() + { + } + + /** + * Creates a new BLCFile object. + * + * @param inFile + * DOCUMENT ME! + * @param sourceType + * DOCUMENT ME! + * + * @throws IOException + * DOCUMENT ME! + */ + public BLCFile(String inFile, DataSourceType sourceType) + throws IOException + { + super(inFile, sourceType); + } + + public BLCFile(FileParse source) throws IOException + { + super(source); + } + + /** + * DOCUMENT ME! + */ + @Override + public void initData() + { + super.initData(); + titles = new Vector(); + } + + /** + * Control the number of block iterations to skip before returning. set to 0 + * to read first block file entry only set to -1 to read last block file entry + * only set to greater than zero to skip at most that many entries before + * parsing + */ + int iterationSkips = 0; + + /** + * The iteration number for the alignment actually parsed from the blc file + */ + int iterationCount = 0; + + /** + * DOCUMENT ME! + */ + @Override + public void parse() throws IOException + { + StringBuffer headerLines = new StringBuffer(); + int numHeaderLines = 0; // number of lines appended. + StringBuffer[] seqstrings = null; + if (suffix != null) + { + try + { + iterationSkips = Integer.parseInt(suffix); + } catch (NumberFormatException e) + { + iterationSkips = 0; // first + } + } + + String line = null; + + do + { + boolean idsFound = false; + boolean newids = false; + // search for ID header. + do + { + line = nextLine(); + if (line == null) + { + break; + } + // seek end of ids + if (line.indexOf("*") > -1) + { + idsFound = true; + + break; + } + + int abracket = line.indexOf(">"); + + if (abracket > -1) + { + + if (iterationCount > 0 && !newids) + { + // we have a new set of IDs to record. + newids = true; + seqs.removeAllElements(); + } + + line = line.substring(abracket + 1); + + Sequence seq = parseId(line); + seqs.addElement(seq); + } + else + { + // header lines - keep them for the alignment comments. + headerLines.append(line); + headerLines.append(newline); + numHeaderLines++; + } + } while (!idsFound); + if (line == null) + { + break; // end of file. + } + int starCol = line.indexOf("*"); + seqstrings = new StringBuffer[seqs.size()]; + + for (int i = 0; i < seqs.size(); i++) + { + if (seqstrings[i] == null) + { + seqstrings[i] = new StringBuffer(); + } + } + + try + { + line = nextLine(); + while (line != null && line.indexOf("*") == -1) + { + for (int i = 0; i < seqs.size(); i++) + { + if (line.length() > (i + starCol)) + { + seqstrings[i].append(line.charAt(i + starCol)); + } + } + line = nextLine(); + } + } catch (IOException e) + { + if (iterationCount == 0) + { + throw (e); // otherwise we've just run out of iterations. + } + else + { + iterationSkips = 0; + } + } + iterationCount++; + } while (--iterationSkips != -1); + + for (int i = 0; i < seqs.size(); i++) + { + Sequence newSeq = (Sequence) seqs.elementAt(i); + + newSeq.setSequence(seqstrings[i].toString()); + } + if (seqs.size() > 0) + { + if (headerLines.length() > 1 + numHeaderLines) + { + // just whitespace or not. + setAlignmentProperty("Comments", headerLines.toString()); + } + setAlignmentProperty("iteration", "" + iterationCount); + } + } + + /** + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + @Override + public String print() + { + return print(getSeqsAsArray()); + } + + /** + * DOCUMENT ME! + * + * @param s + * DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String print(SequenceI[] s) + { + StringBuffer out = new StringBuffer(); + /** + * A general parser for ids. Will look for dbrefs in Uniprot format + * source|id And also Jalview /start-end + * + * @String id Id to be parsed + */ + int i = 0; + int max = -1; + + while ((i < s.length) && (s[i] != null)) + { + out.append(">" + printId(s[i])); + if (s[i].getDescription() != null) + { + out.append(" " + s[i].getDescription()); + } + + out.append(newline); + + if (s[i].getSequence().length > max) + { + max = s[i].getSequence().length; + } + + i++; + } + + out.append("* iteration 1"); + out.append(newline); + + for (int j = 0; j < max; j++) + { + i = 0; + + while ((i < s.length) && (s[i] != null)) + { + if (s[i].getSequence().length > j) + { + out.append(s[i].getSequenceAsString(j, j + 1)); + } + else + { + out.append("-"); + } + + i++; + } + + out.append(newline); + } + + out.append("*"); + out.append(newline); + + return out.toString(); + } +}