JAL-2136 Introduced DynamicData model, modified AnnotionFile to store Phyre meta...
[jalview.git] / src / jalview / io / AnnotationUtil.java
1 package jalview.io;
2
3 import jalview.analysis.AAFrequency;
4 import jalview.analysis.Conservation;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.Annotation;
8 import jalview.datamodel.ProfilesI;
9 import jalview.datamodel.SequenceI;
10
11 public class AnnotationUtil
12 {
13
14   public static AlignmentAnnotation getConsensusAnnotationFromFile(
15           String file)
16   {
17     AlignmentI alignment = readAlignmentFile(file);
18     // AlignFrame af = new AlignFrame(alignment, 5, 5);
19     // af.setVisible(false);
20     // AlignmentAnnotation consensus = af.getViewport()
21     // .getAlignmentConsensusAnnotation();
22     // af.dispose();
23     // System.out.println(consensus);
24
25     SequenceI[] alignmentSeqs = alignment.getSequencesArray();
26     int width = alignment.getWidth();
27     long nseq = alignmentSeqs.length;
28     boolean ignoreGapsConsensus = true;
29     boolean showSequenceLogo = false;
30
31     AlignmentAnnotation consensusAnnotation = new AlignmentAnnotation(
32             "Consensus",
33             "PID", new Annotation[1], 0f, 100f,
34             AlignmentAnnotation.BAR_GRAPH);
35     consensusAnnotation.hasText = true;
36     consensusAnnotation.autoCalculated = true;
37
38     alignment.addAnnotation(consensusAnnotation);
39     ProfilesI hconsensus = AAFrequency.calculate(alignmentSeqs, width, 0,
40             width,
41             true);
42
43     AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
44             hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
45             ignoreGapsConsensus, showSequenceLogo, nseq);
46
47     System.out.println(">>>>> Consensus Annotation : "
48             + consensusAnnotation);
49     return consensusAnnotation;
50   }
51
52   public static AlignmentAnnotation getConservationAnnotationFromFile(
53           String file, SequenceI seqRef)
54   {
55     AlignmentAnnotation conservationAnnotation = null;
56     int foundAnnotCount = seqRef.getAnnotation().length;
57     if (seqRef != null && foundAnnotCount < 3)
58     {
59       AlignmentI alignment = readAlignmentFile("/Users/tcnofoegbu/Desktop/query.jal");
60       // AlignmentI alignment = readAlignmentFile(file);
61       // ColumnSelection cs = new ColumnSelection();
62       // SequenceI querySeq = alignment.getSequenceAt(0);
63       // cs.hideInsertionsFor(querySeq);
64       // AlignViewport av = new AlignViewport(alignment);
65       // av.setColumnSelection(cs);
66       //
67       // alignment = av.getAlignment();
68       int alWidth = alignment.getWidth();
69       int ConsPercGaps = 25;
70
71       AlignmentAnnotation quality = new AlignmentAnnotation("Quality",
72               "Alignment Quality based on Blosum62 scores",
73               new Annotation[1], 0f, 11f, AlignmentAnnotation.BAR_GRAPH);
74       quality.hasText = true;
75       quality.autoCalculated = true;
76
77       conservationAnnotation = new AlignmentAnnotation("Conservation",
78               "PID", new Annotation[1], 0f, 100f,
79               AlignmentAnnotation.BAR_GRAPH);
80       conservationAnnotation.hasText = true;
81       conservationAnnotation.autoCalculated = true;
82       alignment.addAnnotation(conservationAnnotation);
83       Conservation cons = Conservation.calculateConservation("All",
84               alignment.getSequences(), 0, alWidth - 1, false,
85               ConsPercGaps, quality != null);
86       cons.completeAnnotations(conservationAnnotation, quality, 0, alWidth);
87       System.out.println(">>>");
88
89       conservationAnnotation.createSequenceMapping(seqRef, 1, true);
90       seqRef.addAlignmentAnnotation(conservationAnnotation);
91
92       // conservationAnnotation.createSequenceMapping(
93       // seqRef.getDatasetSequence(), 1, true);
94       // seqRef.getDatasetSequence().addAlignmentAnnotation(
95       // conservationAnnotation);
96     }
97
98     return conservationAnnotation;
99   }
100
101   private static AlignmentI readAlignmentFile(String f)
102   {
103     System.out.println("Reading file: " + f);
104     try
105     {
106       FormatAdapter rf = new FormatAdapter();
107       DataSourceType protocol = AppletFormatAdapter.checkProtocol(f);
108       AlignmentI al = rf.readFile(f, protocol,
109               new IdentifyFile().identify(f, protocol));
110       return al;
111     } catch (Exception e)
112     {
113       e.printStackTrace();
114     }
115     return null;
116   }
117
118   public static void main(String[] args)
119   {
120     // getConservationAnnotationFromFile("http://www.sbg.bio.ic.ac.uk/phyre2/phyre2_output/cd1b897af035cdf5/query.jal");
121     getConsensusAnnotationFromFile("http://www.sbg.bio.ic.ac.uk/phyre2/phyre2_output/cd1b897af035cdf5/query.jal");
122   }
123
124 }