+ private String dbref_to_ac_record(DBRefEntry ref)
+ {
+ return ref.getSource().toString() + " ; "
+ + ref.getAccessionId().toString();
+ }
+
+ @Override
+ public String print(SequenceI[] s, boolean jvSuffix)
+ {
+ out = new StringBuffer();
+ out.append("# STOCKHOLM 1.0");
+ out.append(newline);
+
+ // find max length of id
+ int max = 0;
+ int maxid = 0;
+ int in = 0;
+ int slen = s.length;
+ SequenceI seq;
+ Hashtable<String, String> dataRef = null;
+ boolean isAA = s[in].isProtein();
+ while ((in < slen) && ((seq = s[in]) != null))
+ {
+ String tmp = printId(seq, jvSuffix);
+ max = Math.max(max, seq.getLength());
+
+ if (tmp.length() > maxid)
+ {
+ maxid = tmp.length();
+ }
+ List<DBRefEntry> seqrefs = seq.getDBRefs();
+ int ndb;
+ if (seqrefs != null && (ndb = seqrefs.size()) > 0)
+ {
+ if (dataRef == null)
+ {
+ dataRef = new Hashtable<>();
+ }
+ List<DBRefEntry> primrefs = seq.getPrimaryDBRefs();
+ if (primrefs.size() >= 1)
+ {
+ dataRef.put(tmp, dbref_to_ac_record(primrefs.get(0)));
+ }
+ else
+ {
+ for (int idb = 0; idb < seq.getDBRefs().size(); idb++)
+ {
+ DBRefEntry dbref = seq.getDBRefs().get(idb);
+ dataRef.put(tmp, dbref_to_ac_record(dbref));
+ // if we put in a uniprot or EMBL record then we're done:
+ if (isAA && DBRefSource.UNIPROT
+ .equals(DBRefUtils.getCanonicalName(dbref.getSource())))
+ {
+ break;
+ }
+ if (!isAA && DBRefSource.EMBL
+ .equals(DBRefUtils.getCanonicalName(dbref.getSource())))
+ {
+ break;
+ }
+ }
+ }
+ }
+ in++;
+ }
+ maxid += 9;
+ int i = 0;
+
+ // output database type
+ if (al.getProperties() != null)
+ {
+ if (!al.getProperties().isEmpty())
+ {
+ Enumeration key = al.getProperties().keys();
+ Enumeration val = al.getProperties().elements();
+ while (key.hasMoreElements())
+ {
+ out.append("#=GF " + key.nextElement() + " " + val.nextElement());
+ out.append(newline);
+ }
+ }
+ }
+
+ // output database accessions
+ if (dataRef != null)
+ {
+ Enumeration<String> en = dataRef.keys();
+ while (en.hasMoreElements())
+ {
+ Object idd = en.nextElement();
+ String type = dataRef.remove(idd);
+ out.append(new Format("%-" + (maxid - 2) + "s")
+ .form("#=GS " + idd.toString() + " "));
+ if (isAA && type.contains("UNIPROT")
+ || (!isAA && type.contains("EMBL")))
+ {
+
+ out.append(" AC " + type.substring(type.indexOf(";") + 1));
+ }
+ else
+ {
+ out.append(" DR " + type + " ");
+ }
+ out.append(newline);
+ }
+ }
+
+ // output annotations
+ while (i < slen && (seq = s[i]) != null)
+ {
+ AlignmentAnnotation[] alAnot = seq.getAnnotation();
+ if (alAnot != null)
+ {
+ Annotation[] ann;
+ for (int j = 0, nj = alAnot.length; j < nj; j++)
+ {
+
+ String key = type2id(alAnot[j].label);
+ boolean isrna = alAnot[j].isValidStruc();
+
+ if (isrna)
+ {
+ // hardwire to secondary structure if there is RNA secondary
+ // structure on the annotation
+ key = "SS";
+ }
+ if (key == null)
+ {
+
+ continue;
+ }
+
+ // out.append("#=GR ");
+ out.append(new Format("%-" + maxid + "s").form(
+ "#=GR " + printId(seq, jvSuffix) + " " + key + " "));
+ ann = alAnot[j].annotations;
+ String sseq = "";
+ for (int k = 0, nk = ann.length; k < nk; k++)
+ {
+ sseq += outputCharacter(key, k, isrna, ann, seq);
+ }
+ out.append(sseq);
+ out.append(newline);
+ }
+ }
+
+ out.append(new Format("%-" + maxid + "s")
+ .form(printId(seq, jvSuffix) + " "));
+ out.append(seq.getSequenceAsString());
+ out.append(newline);
+ i++;
+ }
+
+ // alignment annotation
+ AlignmentAnnotation aa;
+ AlignmentAnnotation[] an = al.getAlignmentAnnotation();
+ if (an != null)
+ {
+ for (int ia = 0, na = an.length; ia < na; ia++)
+ {
+ aa = an[ia];
+ if (aa.autoCalculated || !aa.visible || aa.sequenceRef != null)
+ {
+ continue;
+ }
+ String sseq = "";
+ String label;
+ String key = "";
+ if (aa.label.equals("seq"))
+ {
+ label = "seq_cons";
+ }
+ else
+ {
+ key = type2id(aa.label.toLowerCase(Locale.ROOT));
+ if (key == null)
+ {
+ label = aa.label;
+ }
+ else
+ {
+ label = key + "_cons";
+ }
+ }
+ if (label == null)
+ {
+ label = aa.label;
+ }
+ label = label.replace(" ", "_");
+
+ out.append(
+ new Format("%-" + maxid + "s").form("#=GC " + label + " "));
+ boolean isrna = aa.isValidStruc();
+ for (int j = 0, nj = aa.annotations.length; j < nj; j++)
+ {
+ sseq += outputCharacter(key, j, isrna, aa.annotations, null);
+ }
+ out.append(sseq);
+ out.append(newline);
+ }
+ }
+
+ out.append("//");
+ out.append(newline);
+
+ return out.toString();
+ }
+
+ /**
+ * add an annotation character to the output row
+ *
+ * @param seq
+ * @param key
+ * @param k
+ * @param isrna
+ * @param ann
+ * @param sequenceI
+ */
+ private char outputCharacter(String key, int k, boolean isrna,
+ Annotation[] ann, SequenceI sequenceI)