X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FStockholmFile.java;fp=src%2Fjalview%2Fio%2FStockholmFile.java;h=62fec79db681ebcd0daa92a04657fcd61c4d04b7;hb=91f6f98109a5c9ffda2ed8da3875773e3469f2f2;hp=669181a3670c5b16c1ff2f783d7e4e86b2e7a89a;hpb=d1996d82c61da9a1b97fce6dc0020da96d0f3f16;p=jalview.git diff --git a/src/jalview/io/StockholmFile.java b/src/jalview/io/StockholmFile.java index 669181a..62fec79 100644 --- a/src/jalview/io/StockholmFile.java +++ b/src/jalview/io/StockholmFile.java @@ -25,6 +25,7 @@ import java.util.*; import com.stevesoft.pat.*; import jalview.datamodel.*; +import jalview.util.Format; // import org.apache.log4j.*; @@ -43,11 +44,21 @@ import jalview.datamodel.*; public class StockholmFile extends AlignFile { // static Logger logger = Logger.getLogger("jalview.io.StockholmFile"); - + StringBuffer out; + AlignmentI al; + public StockholmFile() { } + /** + * Creates a new StockholmFile object for output. + */ + public StockholmFile(AlignmentI al) + { + this.al = al; + } + public StockholmFile(String inFile, String type) throws IOException { super(inFile, type); @@ -187,18 +198,7 @@ public class StockholmFile extends AlignFile jalview.util.DBRefUtils.parseToDbRef(seqO, src, "0", acn); // seqO.addDBRef(dbref); } - } - if (accAnnotations != null && accAnnotations.containsKey("SS")) - { - Vector v = (Vector) accAnnotations.get("SS"); - - for (int i = 0; i < v.size(); i++) - { - AlignmentAnnotation an = (AlignmentAnnotation) v.elementAt(i); - seqO.addAlignmentAnnotation(an); - // annotations.add(an); - } - } + } Hashtable features = null; // We need to adjust the positions of all features to account for gaps @@ -224,6 +224,22 @@ public class StockholmFile extends AlignFile // TODO: map coding region to core jalview feature types String type = i.nextElement().toString(); Hashtable content = (Hashtable) features.remove(type); + + // add alignment annotation for this feature + String key = type2id(type); + if (key != null) + { + if (accAnnotations != null && accAnnotations.containsKey(key)) + { + Vector vv = (Vector) accAnnotations.get(key); + for (int ii = 0; ii < vv.size(); ii++) + { + AlignmentAnnotation an = (AlignmentAnnotation) vv.elementAt(ii); + seqO.addAlignmentAnnotation(an); + } + } + } + Enumeration j = content.keys(); while (j.hasMoreElements()) { @@ -477,25 +493,20 @@ public class StockholmFile extends AlignFile } ns += seq; content.put(description, ns); - - if (type.equals("SS")) + Hashtable strucAnn; + if (seqAnn.containsKey(acc)) { - Hashtable strucAnn; - if (seqAnn.containsKey(acc)) - { - strucAnn = (Hashtable) seqAnn.get(acc); - } - else - { - strucAnn = new Hashtable(); - } - - Vector newStruc = new Vector(); - parseAnnotationRow(newStruc, type, ns); - - strucAnn.put(type, newStruc); - seqAnn.put(acc, strucAnn); + strucAnn = (Hashtable) seqAnn.get(acc); + } + else + { + strucAnn = new Hashtable(); } + + Vector newStruc = new Vector(); + parseAnnotationRow(newStruc, type, ns); + strucAnn.put(type, newStruc); + seqAnn.put(acc, strucAnn); } else { @@ -606,14 +617,141 @@ public class StockholmFile extends AlignFile return annot; } - public static String print(SequenceI[] s) + public String print(SequenceI[] s) { - return "not yet implemented"; + // find max length of id + int max = 0; + int maxid = 0; + int in = 0; + while ((in < s.length) && (s[in] != null)) + { + String tmp = printId(s[in]); + if (s[in].getSequence().length > max) + { + max = s[in].getSequence().length; + } + + if (tmp.length() > maxid) + { + maxid = tmp.length(); + } + + in++; + } + maxid += 9; + int i = 0; + + while (i < s.length && s[i] != null) + { + if (s[i].getDatasetSequence() != null) + { + SequenceI ds = s[i].getDatasetSequence(); + AlignmentAnnotation[] alAnot; + Annotation[] ann; + Annotation annot; + alAnot = s[i].getAnnotation(); + String feature = ""; + if (alAnot != null) + { + for (int j = 0; j < alAnot.length; j++) + { + if (ds.getSequenceFeatures() != null) + { + feature = ds.getSequenceFeatures()[0].type; + } + String key = type2id(feature); + + // output annotations + if (key == null) + continue; + + // out.append("#=GR "); + out.append(new Format("%-" + maxid + "s").form("#=GR " + printId(s[i]) + " " + key + " ")); + ann = alAnot[j].annotations; + String seq = ""; + for (int k = 0; k < ann.length; k++) + { + annot = ann[k]; + String ch = (annot == null) ? Character.toString(s[i].getCharAt(k)) : annot.displayCharacter; + if (ch.length() == 0) + { + if (key.equals("SS")) { + char ll = annot.secondaryStructure; + seq = (Character.toString(ll).equals(" ")) ? seq + "C" : seq + ll; + } else { + seq += "."; + } + } else if (ch.length() == 1) { + seq += ch; + } else if (ch.length() > 1) { + seq += ch.charAt(1) ; + } + } + out.append(seq); + out.append(newline); + } + } + } + + out.append(new Format("%-" + maxid + "s").form(printId(s[i])+" ")); + out.append(s[i].getSequenceAsString()); + out.append(newline); + i++; + } + + // alignment annotation + AlignmentAnnotation aa; + if (al.getAlignmentAnnotation() != null) + { + for (int ia = 0; ia < al.getAlignmentAnnotation().length; ia++) + { + aa = al.getAlignmentAnnotation()[ia]; + if (aa.autoCalculated || !aa.visible) + { + continue; + } + String seq = ""; + String label = type2id(aa.label); + + if (label == null) + label = aa.label; + + label += "_cons"; + + out.append(new Format("%-" + maxid + "s").form("#=GC " + label+" ")); + for (int j = 0; j < aa.annotations.length; j++) + { + String ch = (aa.annotations[j] == null) ? "-" : aa.annotations[j].displayCharacter ; + if (ch.length() == 0) + { + char ll = aa.annotations[j].secondaryStructure; + if (Character.toString(ll).equals(" ")) + seq += "C"; + else + seq += ll; + } else if (ch.length() == 1) { + seq += ch; + } else if (ch.length() > 1) { + seq += ch.charAt(1) ; + } + } + out.append(seq); + out.append(newline); + } + } + return out.toString(); } public String print() { - return print(getSeqsAsArray()); + out = new StringBuffer(); + out.append("# STOCKHOLM 1.0"); + out.append(newline); + print(getSeqsAsArray()); + + out.append("//"); + out.append(newline); + return out.toString(); } private static Hashtable typeIds = null; @@ -651,6 +789,28 @@ public class StockholmFile extends AlignFile + id); return id; } + + protected static String type2id(String type) + { + String key = null; + Enumeration e = typeIds.keys(); + while (e.hasMoreElements()) + { + Object ll = e.nextElement(); + if (typeIds.get(ll).toString().equals(type)) + { + key = (String) ll; + break; + } + } + if (key != null) + { + return (String) key; + } + System.err.println("Warning : Unknown Stockholm annotation type: " + + type); + return key; + } /** * //ssline is complete secondary structure line private AlignmentAnnotation * addHelices(Vector annotation, String label, String ssline) {