in progress (special coloring is still true)
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TreePanelUtil.java
index 082efa9..6f6999d 100644 (file)
@@ -22,15 +22,17 @@ import org.forester.analysis.TaxonomyDataManager;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.data.Accession;
 import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.BranchColor;
+import org.forester.phylogeny.data.NodeData.NODE_DATA;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 import org.forester.phylogeny.iterators.PreorderTreeIterator;
 import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
-import org.forester.util.SequenceIdParser;
+import org.forester.util.SequenceAccessionTools;
 import org.forester.ws.seqdb.UniProtTaxonomy;
 
 public class TreePanelUtil {
@@ -39,7 +41,7 @@ public class TreePanelUtil {
                                                    final Configuration conf,
                                                    final TreePanel tp ) {
         String uri_str = null;
-        final String upkb = ForesterUtil.extractUniProtKbProteinSeqIdentifier( node );
+        final String upkb = SequenceAccessionTools.obtainUniProtAccessorFromDataFields( node );
         if ( !ForesterUtil.isEmpty( upkb ) ) {
             try {
                 uri_str = ForesterUtil.UNIPROT_KB + URLEncoder.encode( upkb, ForesterConstants.UTF8 );
@@ -50,10 +52,10 @@ public class TreePanelUtil {
             }
         }
         if ( ForesterUtil.isEmpty( uri_str ) ) {
-            final String v = ForesterUtil.extractGenbankAccessor( node );
+            final String v = SequenceAccessionTools.obtainGenbankAccessorFromDataFields( node );
             if ( !ForesterUtil.isEmpty( v ) ) {
                 try {
-                    if ( SequenceIdParser.isProtein( v ) ) {
+                    if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
                         uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
                     }
                     else {
@@ -67,10 +69,10 @@ public class TreePanelUtil {
             }
         }
         if ( ForesterUtil.isEmpty( uri_str ) ) {
-            final String v = ForesterUtil.extractRefSeqAccessorAccessor( node );
+            final String v = SequenceAccessionTools.obtainRefSeqAccessorFromDataFields( node );
             if ( !ForesterUtil.isEmpty( v ) ) {
                 try {
-                    if ( SequenceIdParser.isProtein( v ) ) {
+                    if ( SequenceAccessionTools.isProteinDbQuery( v ) ) {
                         uri_str = ForesterUtil.NCBI_PROTEIN + URLEncoder.encode( v, ForesterConstants.UTF8 );
                     }
                     else {
@@ -84,7 +86,7 @@ public class TreePanelUtil {
             }
         }
         if ( ForesterUtil.isEmpty( uri_str ) ) {
-            final String v = ForesterUtil.extractGInumber( node );
+            final String v = SequenceAccessionTools.obtainGiNumberFromDataFields( node );
             if ( !ForesterUtil.isEmpty( v ) ) {
                 try {
                     uri_str = ForesterUtil.NCBI_GI + URLEncoder.encode( v, ForesterConstants.UTF8 );
@@ -98,6 +100,21 @@ public class TreePanelUtil {
         return uri_str;
     }
 
+    public static List<String> createUrisForPdbWeb( final PhylogenyNode node,
+                                                    final List<Accession> pdb_accs,
+                                                    final Configuration configuration,
+                                                    final TreePanel treePanel ) {
+        final List<String> uris = new ArrayList<String>();
+        if ( !ForesterUtil.isEmpty( pdb_accs ) ) {
+            for( final Accession pdb_acc : pdb_accs ) {
+                if ( !ForesterUtil.isEmpty( pdb_acc.getValue() ) ) {
+                    uris.add( ForesterUtil.PDB + pdb_acc.getValue() );
+                }
+            }
+        }
+        return uris;
+    }
+
     /**
      * Returns the set of distinct taxonomies of
      * all external nodes of node.
@@ -124,15 +141,20 @@ public class TreePanelUtil {
         if ( cp.isShowNodeNames() && !ForesterUtil.isEmpty( node.getName() ) ) {
             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getName(), sb );
         }
-        if ( cp.isShowGeneNames() && node.getNodeData().isHasSequence()
+        if ( cp.isShowSeqNames() && node.getNodeData().isHasSequence()
                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getName() ) ) {
             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getName(), sb );
         }
-        if ( cp.isShowGeneSymbols() && node.getNodeData().isHasSequence()
+        if ( cp.isShowSeqSymbols() && node.getNodeData().isHasSequence()
                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getSymbol() ) ) {
             TreePanelUtil
                     .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getSymbol(), sb );
         }
+        if ( cp.isShowGeneNames() && node.getNodeData().isHasSequence()
+                && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getGeneName() ) ) {
+            TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence().getGeneName(),
+                                                                       sb );
+        }
         if ( cp.isShowSequenceAcc() && node.getNodeData().isHasSequence()
                 && ( node.getNodeData().getSequence().getAccession() != null )
                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getAccession().toString() ) ) {
@@ -154,7 +176,7 @@ public class TreePanelUtil {
             TreePanelUtil
                     .showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getTaxonomy().getCommonName(), sb );
         }
-        if ( ( cp.isShowGeneNames() || cp.isShowGeneSymbols() || cp.isShowSequenceAcc() )
+        if ( ( cp.isShowSeqNames() || cp.isShowSeqSymbols() || cp.isShowSequenceAcc() )
                 && node.getNodeData().isHasSequence()
                 && !ForesterUtil.isEmpty( node.getNodeData().getSequence().getMolecularSequence() ) ) {
             TreePanelUtil.showExtDescNodeDataUserSelectedHelperHelper( node.getNodeData().getSequence()
@@ -177,44 +199,6 @@ public class TreePanelUtil {
         JOptionPane.showMessageDialog( parent, msg, title, JOptionPane.INFORMATION_MESSAGE );
     }
 
-    final static Color calculateColorFromString( final String str, final boolean is_taxonomy ) {
-        final String my_str = str.toUpperCase();
-        char first = my_str.charAt( 0 );
-        char second = ' ';
-        char third = ' ';
-        if ( my_str.length() > 1 ) {
-            if ( is_taxonomy ) {
-                second = my_str.charAt( 1 );
-            }
-            else {
-                second = my_str.charAt( my_str.length() - 1 );
-            }
-            if ( is_taxonomy ) {
-                if ( my_str.length() > 2 ) {
-                    if ( my_str.indexOf( " " ) > 0 ) {
-                        third = my_str.charAt( my_str.indexOf( " " ) + 1 );
-                    }
-                    else {
-                        third = my_str.charAt( 2 );
-                    }
-                }
-            }
-            else if ( my_str.length() > 2 ) {
-                third = my_str.charAt( ( my_str.length() - 1 ) / 2 );
-            }
-        }
-        first = TreePanelUtil.normalizeCharForRGB( first );
-        second = TreePanelUtil.normalizeCharForRGB( second );
-        third = TreePanelUtil.normalizeCharForRGB( third );
-        if ( ( first > 235 ) && ( second > 235 ) && ( third > 235 ) ) {
-            first = 0;
-        }
-        else if ( ( first < 60 ) && ( second < 60 ) && ( third < 60 ) ) {
-            second = 255;
-        }
-        return new Color( first, second, third );
-    }
-
     final static void collapseSpeciesSpecificSubtrees( final Phylogeny phy ) {
         boolean inferred = false;
         for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
@@ -312,7 +296,7 @@ public class TreePanelUtil {
             if ( n.getNodeData().isHasTaxonomy()
                     && ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() )
                             || !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getCommonName() ) || !ForesterUtil
-                            .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
+                                .isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
                 if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getRank() )
                         && n.getNodeData().getTaxonomy().getRank().equalsIgnoreCase( rank ) ) {
                     final BranchColor c = new BranchColor( tree_panel.calculateTaxonomyBasedColor( n.getNodeData()
@@ -413,7 +397,7 @@ public class TreePanelUtil {
                 if ( sb.charAt( sb.length() - 1 ) == ' ' ) {
                     sb.deleteCharAt( sb.length() - 1 );
                 }
-                if ( i < ans.size() - 1 ) {
+                if ( i < ( ans.size() - 1 ) ) {
                     sb.append( ", " );
                 }
             }
@@ -444,7 +428,7 @@ public class TreePanelUtil {
 
     final static boolean isSequenceEmpty( final Sequence seq ) {
         return ( seq.getAccession() == null ) && ForesterUtil.isEmpty( seq.getName() )
-                && ForesterUtil.isEmpty( seq.getSymbol() );
+                && ForesterUtil.isEmpty( seq.getGeneName() ) && ForesterUtil.isEmpty( seq.getSymbol() );
     }
 
     final static boolean isTaxonomyEmpty( final Taxonomy tax ) {
@@ -453,12 +437,51 @@ public class TreePanelUtil {
                 .getSynonyms().isEmpty() );
     }
 
-    final static char normalizeCharForRGB( char c ) {
-        c -= 65;
-        c *= 10.2;
-        c = c > 255 ? 255 : c;
-        c = c < 0 ? 0 : c;
-        return c;
+    static int makeSB( final List<String> data, final Options optz, final StringBuilder sb ) {
+        final SortedMap<String, Integer> map = new TreeMap<String, Integer>();
+        if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ )
+                && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) {
+            for( final String d : data ) {
+                if ( !ForesterUtil.isEmpty( d ) ) {
+                    if ( map.containsKey( d ) ) {
+                        map.put( d, map.get( d ) + 1 );
+                    }
+                    else {
+                        map.put( d, 1 );
+                    }
+                }
+            }
+        }
+        int size = 0;
+        if ( ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ )
+                && ( optz.getExtDescNodeDataToReturn() != NODE_DATA.SEQUENCE_MOL_SEQ_FASTA ) ) {
+            for( final Entry<String, Integer> e : map.entrySet() ) {
+                final String v = e.getKey();
+                final Object c = e.getValue();
+                sb.append( v );
+                sb.append( "\t" );
+                sb.append( c );
+                sb.append( ForesterUtil.LINE_SEPARATOR );
+            }
+            size = map.size();
+        }
+        else {
+            for( final String d : data ) {
+                if ( !ForesterUtil.isEmpty( d ) ) {
+                    sb.append( d );
+                    sb.append( ForesterUtil.LINE_SEPARATOR );
+                }
+            }
+            size = data.size();
+        }
+        return size;
+    }
+
+    final static String pdbAccToString( final List<Accession> accs, final int i ) {
+        if ( ForesterUtil.isEmpty( accs.get( i ).getComment() ) ) {
+            return accs.get( i ).getValue();
+        }
+        return accs.get( i ).getValue() + " (" + accs.get( i ).getComment().toLowerCase() + ")";
     }
 
     final static Phylogeny subTree( final PhylogenyNode new_root, final Phylogeny source_phy ) {