(no commit message)
[jalview.git] / forester / java / src / org / forester / protein / BasicProtein.java
index 9b688c9..03bf129 100644 (file)
@@ -27,6 +27,8 @@
 package org.forester.protein;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -39,13 +41,25 @@ import org.forester.util.ForesterUtil;
 // proteins could have the same name and/or id!
 public class BasicProtein implements Protein {
 
-    private final ProteinId    _id;
-    private final int          _length;
-    private final Species      _species;
-    private String             _name;
-    private String             _desc;
-    private String             _accession;
-    private final List<Domain> _protein_domains;
+    private final ProteinId          _id;
+    private final int                _length;
+    private final Species            _species;
+    private String                   _name;
+    private String                   _desc;
+    private String                   _accession;
+    private final List<Domain>       _protein_domains;
+    public static Comparator<Domain> DomainMidPositionComparator = new Comparator<Domain>() {
+
+        @Override
+        public int compare( final Domain d1,
+                            final Domain d2 ) {
+            final int m1 = ( d1.getTo() + d1.getFrom() );
+            final int m2 = ( d2.getTo() + d2.getFrom() );
+            return m1 < m2 ? -1 : m1 > m2 ? 1 : d1
+                    .getDomainId()
+                    .compareTo( d2.getDomainId() );
+        }
+    };
 
     public BasicProtein( final String id_str, final String species_str, final int length ) {
         if ( length < 0 ) {
@@ -74,17 +88,17 @@ public class BasicProtein implements Protein {
      * If in_nc_order is set to true, this returns true only and only if
      * the order in List 'domains' and this protein (as determined by the start positions
      * of the domains of this proteins, _not_ by their index) are the same
-     * (interspersing, 'other', domains in this are ignored). 
+     * (interspersing, 'other', domains in this are ignored).
      * If in_nc_order is set to false, this returns true only and only if
      * this contains all domains listed in 'domains' (order and count do not matter).
-     * 
+     *
      * @param domains a list of domain ids in a certain order.
      * @param in_nc_order to consider order
      * @return
      */
-    public boolean contains( final List<DomainId> query_domain_ids, final boolean in_nc_order ) {
+    public boolean contains( final List<String> query_domain_ids, final boolean in_nc_order ) {
         if ( !in_nc_order ) {
-            for( final DomainId query_domain_id : query_domain_ids ) {
+            for( final String query_domain_id : query_domain_ids ) {
                 if ( !getProteinDomainIds().contains( query_domain_id ) ) {
                     return false;
                 }
@@ -93,7 +107,7 @@ public class BasicProtein implements Protein {
         }
         else {
             int current_start_position = -1;
-            I: for( final DomainId query_domain_id : query_domain_ids ) {
+            I: for( final String query_domain_id : query_domain_ids ) {
                 if ( getProteinDomainIds().contains( query_domain_id ) ) {
                     final List<Domain> found_domains = getProteinDomains( query_domain_id );
                     final SortedSet<Integer> ordered_start_positions = new TreeSet<Integer>();
@@ -127,6 +141,21 @@ public class BasicProtein implements Protein {
     }
 
     @Override
+    public List<Domain> getDomainsSortedByPosition() {
+        final List<Domain> domains = new ArrayList<Domain>( getProteinDomains().size() );
+        for( final Domain domain : getProteinDomains() ) {
+            domains.add( domain );
+        }
+        Collections.sort( domains, DomainMidPositionComparator );
+        return domains;
+    }
+
+    @Override
+    public int getLength() {
+        return _length;
+    }
+
+    @Override
     public String getName() {
         return _name;
     }
@@ -142,25 +171,17 @@ public class BasicProtein implements Protein {
     }
 
     @Override
-    public int getProteinDomainCount( final DomainId domain_id ) {
+    public int getProteinDomainCount( final String domain_id ) {
         return getProteinDomains( domain_id ).size();
     }
 
-    private List<DomainId> getProteinDomainIds() {
-        final List<DomainId> ids = new ArrayList<DomainId>( getProteinDomains().size() );
-        for( final Domain domain : getProteinDomains() ) {
-            ids.add( domain.getDomainId() );
-        }
-        return ids;
-    }
-
     @Override
     public List<Domain> getProteinDomains() {
         return _protein_domains;
     }
 
     @Override
-    public List<Domain> getProteinDomains( final DomainId domain_id ) {
+    public List<Domain> getProteinDomains( final String domain_id ) {
         final List<Domain> domains = new ArrayList<Domain>();
         for( final Domain domain : getProteinDomains() ) {
             if ( domain.getDomainId().equals( domain_id ) ) {
@@ -180,12 +201,6 @@ public class BasicProtein implements Protein {
         return _species;
     }
 
-    private void init() {
-        _desc = "";
-        _accession = "";
-        _name = "";
-    }
-
     public void setAccession( final String accession ) {
         _accession = accession;
     }
@@ -198,8 +213,76 @@ public class BasicProtein implements Protein {
         _name = name;
     }
 
+    public String toDomainArchitectureString( final String separator ) {
+        final StringBuilder sb = new StringBuilder();
+        boolean first = true;
+        for( final Domain d : getDomainsSortedByPosition() ) {
+            if ( first ) {
+                first = false;
+            }
+            else {
+                sb.append( separator );
+            }
+            sb.append( d.getDomainId() );
+        }
+        return sb.toString();
+    }
+
+    public String toDomainArchitectureString( final String separator,
+                                              final int repeats_limit,
+                                              final String repeat_separator ) {
+        if ( repeats_limit < 3 ) {
+            throw new IllegalArgumentException( "repeats limit cannot be smaller than 3" );
+        }
+        final StringBuilder sb = new StringBuilder();
+        StringBuilder buffer = new StringBuilder();
+        String prev_id = "";
+        int counter = 1;
+        for( final Domain d : getDomainsSortedByPosition() ) {
+            final String id = d.getDomainId();
+            if ( prev_id.equals( id ) ) {
+                counter++;
+            }
+            else {
+                counter = 1;
+                sb.append( buffer );
+                buffer = new StringBuilder();
+            }
+            if ( counter < repeats_limit ) {
+                buffer.append( id );
+                buffer.append( separator );
+            }
+            else if ( counter == repeats_limit ) {
+                buffer = new StringBuilder();
+                buffer.append( id );
+                buffer.append( repeat_separator );
+                buffer.append( id );
+                buffer.append( repeat_separator );
+                buffer.append( id );
+                buffer.append( separator );
+            }
+            prev_id = id;
+        }
+        sb.append( buffer.substring( 0, buffer.length() - 1 ) );
+        return sb.toString();
+    }
+
     @Override
-    public int getLength() {
-        return _length;
+    public String toString() {
+        return toDomainArchitectureString( "~" );
+    }
+
+    private List<String> getProteinDomainIds() {
+        final List<String> ids = new ArrayList<String>( getProteinDomains().size() );
+        for( final Domain domain : getProteinDomains() ) {
+            ids.add( domain.getDomainId() );
+        }
+        return ids;
+    }
+
+    private void init() {
+        _desc = "";
+        _accession = "";
+        _name = "";
     }
 }