JAL-2136 Introduced DynamicData model, modified AnnotionFile to store Phyre meta...
[jalview.git] / src / jalview / io / AnnotationUtil.java
diff --git a/src/jalview/io/AnnotationUtil.java b/src/jalview/io/AnnotationUtil.java
new file mode 100644 (file)
index 0000000..4973d5e
--- /dev/null
@@ -0,0 +1,124 @@
+package jalview.io;
+
+import jalview.analysis.AAFrequency;
+import jalview.analysis.Conservation;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.ProfilesI;
+import jalview.datamodel.SequenceI;
+
+public class AnnotationUtil
+{
+
+  public static AlignmentAnnotation getConsensusAnnotationFromFile(
+          String file)
+  {
+    AlignmentI alignment = readAlignmentFile(file);
+    // AlignFrame af = new AlignFrame(alignment, 5, 5);
+    // af.setVisible(false);
+    // AlignmentAnnotation consensus = af.getViewport()
+    // .getAlignmentConsensusAnnotation();
+    // af.dispose();
+    // System.out.println(consensus);
+
+    SequenceI[] alignmentSeqs = alignment.getSequencesArray();
+    int width = alignment.getWidth();
+    long nseq = alignmentSeqs.length;
+    boolean ignoreGapsConsensus = true;
+    boolean showSequenceLogo = false;
+
+    AlignmentAnnotation consensusAnnotation = new AlignmentAnnotation(
+            "Consensus",
+            "PID", new Annotation[1], 0f, 100f,
+            AlignmentAnnotation.BAR_GRAPH);
+    consensusAnnotation.hasText = true;
+    consensusAnnotation.autoCalculated = true;
+
+    alignment.addAnnotation(consensusAnnotation);
+    ProfilesI hconsensus = AAFrequency.calculate(alignmentSeqs, width, 0,
+            width,
+            true);
+
+    AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
+            hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
+            ignoreGapsConsensus, showSequenceLogo, nseq);
+
+    System.out.println(">>>>> Consensus Annotation : "
+            + consensusAnnotation);
+    return consensusAnnotation;
+  }
+
+  public static AlignmentAnnotation getConservationAnnotationFromFile(
+          String file, SequenceI seqRef)
+  {
+    AlignmentAnnotation conservationAnnotation = null;
+    int foundAnnotCount = seqRef.getAnnotation().length;
+    if (seqRef != null && foundAnnotCount < 3)
+    {
+      AlignmentI alignment = readAlignmentFile("/Users/tcnofoegbu/Desktop/query.jal");
+      // AlignmentI alignment = readAlignmentFile(file);
+      // ColumnSelection cs = new ColumnSelection();
+      // SequenceI querySeq = alignment.getSequenceAt(0);
+      // cs.hideInsertionsFor(querySeq);
+      // AlignViewport av = new AlignViewport(alignment);
+      // av.setColumnSelection(cs);
+      //
+      // alignment = av.getAlignment();
+      int alWidth = alignment.getWidth();
+      int ConsPercGaps = 25;
+
+      AlignmentAnnotation quality = new AlignmentAnnotation("Quality",
+              "Alignment Quality based on Blosum62 scores",
+              new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
+      quality.hasText = true;
+      quality.autoCalculated = true;
+
+      conservationAnnotation = new AlignmentAnnotation("Conservation",
+              "PID", new Annotation[1], 0f, 100f,
+              AlignmentAnnotation.BAR_GRAPH);
+      conservationAnnotation.hasText = true;
+      conservationAnnotation.autoCalculated = true;
+      alignment.addAnnotation(conservationAnnotation);
+      Conservation cons = Conservation.calculateConservation("All",
+              alignment.getSequences(), 0, alWidth - 1, false,
+              ConsPercGaps, quality != null);
+      cons.completeAnnotations(conservationAnnotation, quality, 0, alWidth);
+      System.out.println(">>>");
+
+      conservationAnnotation.createSequenceMapping(seqRef, 1, true);
+      seqRef.addAlignmentAnnotation(conservationAnnotation);
+
+      // conservationAnnotation.createSequenceMapping(
+      // seqRef.getDatasetSequence(), 1, true);
+      // seqRef.getDatasetSequence().addAlignmentAnnotation(
+      // conservationAnnotation);
+    }
+
+    return conservationAnnotation;
+  }
+
+  private static AlignmentI readAlignmentFile(String f)
+  {
+    System.out.println("Reading file: " + f);
+    try
+    {
+      FormatAdapter rf = new FormatAdapter();
+      DataSourceType protocol = AppletFormatAdapter.checkProtocol(f);
+      AlignmentI al = rf.readFile(f, protocol,
+              new IdentifyFile().identify(f, protocol));
+      return al;
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+    return null;
+  }
+
+  public static void main(String[] args)
+  {
+    // getConservationAnnotationFromFile("http://www.sbg.bio.ic.ac.uk/phyre2/phyre2_output/cd1b897af035cdf5/query.jal");
+    getConsensusAnnotationFromFile("http://www.sbg.bio.ic.ac.uk/phyre2/phyre2_output/cd1b897af035cdf5/query.jal");
+  }
+
+}