JAL-4386 - Implementation of retrieving secondary structure
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
index 798b4bc..7d0ccdb 100644 (file)
@@ -37,6 +37,7 @@ import java.util.NoSuchElementException;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.Vector;
 
 import jalview.api.AlignCalcWorkerI;
 import jalview.bin.Console;
@@ -53,6 +54,7 @@ import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GeneLociI;
 import jalview.datamodel.IncompleteCodonException;
 import jalview.datamodel.Mapping;
+import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SeqCigar;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
@@ -2903,14 +2905,14 @@ public class AlignmentUtils
     return Color.gray;
   }
 
-  public static char findSSAnnotationForGivenSeqposition(AlignmentAnnotation[] aa,
+  public static char findSSAnnotationForGivenSeqposition(AlignmentAnnotation aa,
           int seqPosition)
   {
     char ss = '*'; 
         
     if (aa != null) {
-      if (aa[0].getAnnotationForPosition(seqPosition) != null) {
-        Annotation a = aa[0].getAnnotationForPosition(seqPosition);
+      if (aa.getAnnotationForPosition(seqPosition) != null) {
+        Annotation a = aa.getAnnotationForPosition(seqPosition);
         ss = a.secondaryStructure;
         
         //There is no representation for coil and it can be either ' ' or null. 
@@ -2926,11 +2928,118 @@ public class AlignmentUtils
     return ss;    
   }
   
-  public static String getSSSourceFromAnnotationDescription(AlignmentAnnotation[] annotations)
-  {
-    String ssSource = null;
+  public static List<String> extractSSSourceInAlignmentAnnotation(AlignmentAnnotation[] annotations) {
+    
+    List<String> ssSources = new ArrayList<>();
+    Set<String> addedSources = new HashSet<>(); // to keep track of added sources
+
+          
+    for (AlignmentAnnotation aa: annotations) {
+      
+      String ssSource = extractSSSourceFromAnnotationDescription(aa);
+      
+      if (ssSource!= null && !addedSources.contains(ssSource)) {
+          ssSources.add(ssSource);
+          addedSources.add(ssSource);
+      }
+      
+     }
+    Collections.sort(ssSources);
+       
+    return ssSources;
+    
+  }
+  
+  public static String extractSSSourceFromAnnotationDescription(AlignmentAnnotation aa) {
+    
+    
+    for (String label : Constants.SECONDARY_STRUCTURE_LABELS.keySet()) {
+      
+      if (label.equals(aa.label)) {  
+        
+        //For JPred 
+        if(aa.label.equals(Constants.SS_ANNOTATION_FROM_JPRED_LABEL)){
+          
+          return (Constants.SECONDARY_STRUCTURE_LABELS.get(aa.label));
+          
+         }
+        
+        //For input with secondary structure
+        if(aa.label.equals(Constants.SS_ANNOTATION_LABEL) 
+                && aa.description.equals(Constants.SS_ANNOTATION_LABEL)){
+          
+          return (Constants.SECONDARY_STRUCTURE_LABELS.get(aa.label));
+          
+         }
+        
+        //For other sources
+        if(aa.sequenceRef==null) {
+          return null;
+        }
+        else if(aa.sequenceRef.getDatasetSequence()==null) {
+          return null;
+        }
+         Vector<PDBEntry> pdbEntries = aa.sequenceRef.getDatasetSequence().getAllPDBEntries();
+         
+         for (PDBEntry entry : pdbEntries){
+           
+           String entryProvider = entry.getProvider();
+           if(entryProvider == null) {
+             entryProvider = "PDB";
+           }
+           
+           // Trim the string from first occurrence of colon
+           String entryID = entry.getId();
+           int index = entryID.indexOf(':');
+           
+           // Check if colon exists
+           if (index != -1) {
+             
+             // Trim the string from first occurrence of colon
+             entryID = entryID.substring(0, index); 
+             
+           }
+           
+           if(entryProvider == "PDB" && aa.description.toLowerCase().contains
+                     ("secondary structure for " + entryID.toLowerCase())){
+               
+               return entryProvider;
+             
+           }
+           
+           else if (entryProvider != "PDB" && aa.description.toLowerCase().contains(entryID.toLowerCase())){
+              
+              return entryProvider;
+            
+            }
+          
+          }
+          
+        }   
+      }
+    
+    return null;
+    
+  }
+  
+  //to do set priority for labels
+  public static AlignmentAnnotation getDisplayedAlignmentAnnotation(SequenceI seq){
     
-    return ssSource;
+    for(String ssLabel : Constants.SECONDARY_STRUCTURE_LABELS.keySet()) {
+    
+    AlignmentAnnotation[] aa = seq.getAnnotation(ssLabel);
+    if(aa!=null) {
+      
+      for (AlignmentAnnotation annot: aa) {
+        if(annot.visible) {
+          return annot;
+        }
+      }
+     }
+    }
+    
+    return null;
     
   }