X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsurfacing%2FDomainParsimonyCalculator.java;h=bffe413d744140780c89e4f52050e11cf840bd70;hb=bf4f34e0511813622f68e54ffb783e4e6d92bfa4;hp=82de445797053cc268fe6fb1e55be70e77a7a9b8;hpb=48f7a89be9d34f1930a1f863e608235cc27184c5;p=jalview.git diff --git a/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java b/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java index 82de445..bffe413 100644 --- a/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java +++ b/forester/java/src/org/forester/surfacing/DomainParsimonyCalculator.java @@ -6,7 +6,7 @@ // Copyright (C) 2008-2009 Christian M. Zmasek // Copyright (C) 2008-2009 Burnham Institute for Medical Research // All rights reserved -// +// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either @@ -16,23 +16,27 @@ // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.surfacing; +import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import org.forester.application.surfacing; import org.forester.evoinference.matrix.character.BasicCharacterStateMatrix; import org.forester.evoinference.matrix.character.CharacterStateMatrix; import org.forester.evoinference.matrix.character.CharacterStateMatrix.BinaryStates; @@ -43,22 +47,24 @@ import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.BinaryCharacters; import org.forester.phylogeny.iterators.PhylogenyNodeIterator; -import org.forester.surfacing.BinaryDomainCombination.DomainCombinationType; +import org.forester.protein.BinaryDomainCombination; +import org.forester.protein.BinaryDomainCombination.DomainCombinationType; +import org.forester.species.Species; import org.forester.util.ForesterUtil; public final class DomainParsimonyCalculator { private static final String TYPE_FORBINARY_CHARACTERS = "parsimony inferred"; - private CharacterStateMatrix _gain_loss_matrix; private CharacterStateMatrix _binary_internal_states_matrix; + private int _cost; + private Map> _domain_id_to_secondary_features_map; + private CharacterStateMatrix _gain_loss_matrix; private final List _gwcd_list; private final Phylogeny _phylogeny; - private int _total_losses; + private SortedSet _positive_filter; private int _total_gains; + private int _total_losses; private int _total_unchanged; - private int _cost; - private Map> _domain_id_to_secondary_features_map; - private SortedSet _positive_filter; private DomainParsimonyCalculator( final Phylogeny phylogeny ) { init(); @@ -74,77 +80,13 @@ public final class DomainParsimonyCalculator { private DomainParsimonyCalculator( final Phylogeny phylogeny, final List gwcd_list, - final Map> domain_id_to_secondary_features_map ) { + final Map> domain_id_to_secondary_features_map ) { init(); _phylogeny = phylogeny; _gwcd_list = gwcd_list; setDomainIdToSecondaryFeaturesMap( domain_id_to_secondary_features_map ); } - int calculateNumberOfBinaryDomainCombination() { - if ( getGenomeWideCombinableDomainsList().isEmpty() ) { - throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); - } - final Set all_binary_combinations = new HashSet(); - for( final GenomeWideCombinableDomains gwcd : getGenomeWideCombinableDomainsList() ) { - for( final BinaryDomainCombination bc : gwcd.toBinaryDomainCombinations() ) { - all_binary_combinations.add( bc ); - } - } - return all_binary_combinations.size(); - } - - CharacterStateMatrix createMatrixOfBinaryDomainCombinationPresenceOrAbsence() { - return createMatrixOfBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); - } - - CharacterStateMatrix createMatrixOfDomainPresenceOrAbsence() { - return createMatrixOfDomainPresenceOrAbsence( getGenomeWideCombinableDomainsList(), getPositiveFilter() ); - } - - CharacterStateMatrix createMatrixOfSecondaryFeaturePresenceOrAbsence( final Map mapping_results_map ) { - return createMatrixOfSecondaryFeaturePresenceOrAbsence( getGenomeWideCombinableDomainsList(), - getDomainIdToSecondaryFeaturesMap(), - mapping_results_map ); - } - - Phylogeny decoratePhylogenyWithDomains( final Phylogeny phylogeny ) { - for( final PhylogenyNodeIterator it = phylogeny.iteratorPostorder(); it.hasNext(); ) { - final PhylogenyNode node = it.next(); - final String node_identifier = node.getName(); - final BinaryCharacters bc = new BinaryCharacters( getUnitsOnNode( node_identifier ), - getUnitsGainedOnNode( node_identifier ), - getUnitsLostOnNode( node_identifier ), - TYPE_FORBINARY_CHARACTERS, - getSumOfPresentOnNode( node_identifier ), - getSumOfGainsOnNode( node_identifier ), - getSumOfLossesOnNode( node_identifier ) ); - node.getNodeData().setBinaryCharacters( bc ); - } - return phylogeny; - } - - private void executeDolloParsimony( final boolean on_domain_presence ) { - reset(); - final DolloParsimony dollo = DolloParsimony.createInstance(); - dollo.setReturnGainLossMatrix( true ); - dollo.setReturnInternalStates( true ); - CharacterStateMatrix states = null; - if ( on_domain_presence ) { - states = createMatrixOfDomainPresenceOrAbsence(); - } - else { - states = createMatrixOfBinaryDomainCombinationPresenceOrAbsence(); - } - dollo.execute( getPhylogeny(), states ); - setGainLossMatrix( dollo.getGainLossMatrix() ); - setBinaryInternalStatesMatrix( dollo.getInternalStatesMatrix() ); - setCost( dollo.getCost() ); - setTotalGains( dollo.getTotalGains() ); - setTotalLosses( dollo.getTotalLosses() ); - setTotalUnchanged( dollo.getTotalUnchanged() ); - } - public void executeDolloParsimonyOnBinaryDomainCombintionPresence() { executeDolloParsimony( false ); } @@ -153,7 +95,7 @@ public final class DomainParsimonyCalculator { executeDolloParsimony( true ); } - public void executeDolloParsimonyOnDomainPresence( final SortedSet positive_filter ) { + public void executeDolloParsimonyOnDomainPresence( final SortedSet positive_filter ) { setPositiveFilter( positive_filter ); executeDolloParsimony( true ); setPositiveFilter( null ); @@ -177,38 +119,6 @@ public final class DomainParsimonyCalculator { setTotalUnchanged( dollo.getTotalUnchanged() ); } - private void executeFitchParsimony( final boolean on_domain_presence, - final boolean use_last, - final boolean randomize, - final long random_number_seed ) { - reset(); - if ( use_last ) { - System.out.println( " Fitch parsimony: use_last = true" ); - } - final FitchParsimony fitch = new FitchParsimony(); - fitch.setRandomize( randomize ); - if ( randomize ) { - fitch.setRandomNumberSeed( random_number_seed ); - } - fitch.setUseLast( use_last ); - fitch.setReturnGainLossMatrix( true ); - fitch.setReturnInternalStates( true ); - CharacterStateMatrix states = null; - if ( on_domain_presence ) { - states = createMatrixOfDomainPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); - } - else { - states = createMatrixOfBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); - } - fitch.execute( getPhylogeny(), states ); - setGainLossMatrix( fitch.getGainLossMatrix() ); - setBinaryInternalStatesMatrix( fitch.getInternalStatesMatrix() ); - setCost( fitch.getCost() ); - setTotalGains( fitch.getTotalGains() ); - setTotalLosses( fitch.getTotalLosses() ); - setTotalUnchanged( fitch.getTotalUnchanged() ); - } - public void executeFitchParsimonyOnBinaryDomainCombintion( final boolean use_last ) { executeFitchParsimony( false, use_last, false, 0 ); } @@ -217,6 +127,10 @@ public final class DomainParsimonyCalculator { executeFitchParsimony( false, false, true, random_number_seed ); } + public void executeFitchParsimonyOnBinaryDomainCombintionOnSecondaryFeatures( final boolean use_last ) { + executeFitchParsimonyOnSecondaryFeatures( use_last, false, 0 ); + } + public void executeFitchParsimonyOnDomainPresence( final boolean use_last ) { executeFitchParsimony( true, use_last, false, 0 ); } @@ -302,10 +216,6 @@ public final class DomainParsimonyCalculator { return _cost; } - private Map> getDomainIdToSecondaryFeaturesMap() { - return _domain_id_to_secondary_features_map; - } - public CharacterStateMatrix getGainLossCountsMatrix() { final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( getGainLossMatrix() .getNumberOfIdentifiers(), 3 ); @@ -338,10 +248,6 @@ public final class DomainParsimonyCalculator { return _gain_loss_matrix; } - private List getGenomeWideCombinableDomainsList() { - return _gwcd_list; - } - public CharacterStateMatrix getInternalStatesMatrix() { return _binary_internal_states_matrix; } @@ -363,14 +269,6 @@ public final class DomainParsimonyCalculator { return net; } - private Phylogeny getPhylogeny() { - return _phylogeny; - } - - private SortedSet getPositiveFilter() { - return _positive_filter; - } - public int getSumOfGainsOnNode( final String node_identifier ) { return getStateSumDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.GAIN ); } @@ -383,18 +281,6 @@ public final class DomainParsimonyCalculator { return getSumOfGainsOnNode( node_identifier ) + getSumOfUnchangedPresentOnNode( node_identifier ); } - int getSumOfUnchangedAbsentOnNode( final String node_identifier ) { - return getStateSumDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_ABSENT ); - } - - int getSumOfUnchangedOnNode( final String node_identifier ) { - return getSumOfUnchangedPresentOnNode( node_identifier ) + getSumOfUnchangedAbsentOnNode( node_identifier ); - } - - int getSumOfUnchangedPresentOnNode( final String node_identifier ) { - return getStateSumDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_PRESENT ); - } - public int getTotalGains() { return _total_gains; } @@ -421,6 +307,61 @@ public final class DomainParsimonyCalculator { return present; } + int calculateNumberOfBinaryDomainCombination() { + if ( getGenomeWideCombinableDomainsList().isEmpty() ) { + throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); + } + final Set all_binary_combinations = new HashSet(); + for( final GenomeWideCombinableDomains gwcd : getGenomeWideCombinableDomainsList() ) { + for( final BinaryDomainCombination bc : gwcd.toBinaryDomainCombinations() ) { + all_binary_combinations.add( bc ); + } + } + return all_binary_combinations.size(); + } + + CharacterStateMatrix createMatrixOfBinaryDomainCombinationPresenceOrAbsence() { + return createMatrixOfBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); + } + + CharacterStateMatrix createMatrixOfDomainPresenceOrAbsence() { + return createMatrixOfDomainPresenceOrAbsence( getGenomeWideCombinableDomainsList(), getPositiveFilter() ); + } + + CharacterStateMatrix createMatrixOfSecondaryFeaturePresenceOrAbsence( final Map mapping_results_map ) { + return createMatrixOfSecondaryFeaturePresenceOrAbsence( getGenomeWideCombinableDomainsList(), + getDomainIdToSecondaryFeaturesMap(), + mapping_results_map ); + } + + Phylogeny decoratePhylogenyWithDomains( final Phylogeny phylogeny ) { + for( final PhylogenyNodeIterator it = phylogeny.iteratorPostorder(); it.hasNext(); ) { + final PhylogenyNode node = it.next(); + final String node_identifier = node.getName(); + final BinaryCharacters bc = new BinaryCharacters( getUnitsOnNode( node_identifier ), + getUnitsGainedOnNode( node_identifier ), + getUnitsLostOnNode( node_identifier ), + TYPE_FORBINARY_CHARACTERS, + getSumOfPresentOnNode( node_identifier ), + getSumOfGainsOnNode( node_identifier ), + getSumOfLossesOnNode( node_identifier ) ); + node.getNodeData().setBinaryCharacters( bc ); + } + return phylogeny; + } + + int getSumOfUnchangedAbsentOnNode( final String node_identifier ) { + return getStateSumDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_ABSENT ); + } + + int getSumOfUnchangedOnNode( final String node_identifier ) { + return getSumOfUnchangedPresentOnNode( node_identifier ) + getSumOfUnchangedAbsentOnNode( node_identifier ); + } + + int getSumOfUnchangedPresentOnNode( final String node_identifier ) { + return getStateSumDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_PRESENT ); + } + SortedSet getUnitsUnchangedAbsentOnNode( final String node_identifier ) { return getUnitsDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_ABSENT ); } @@ -429,6 +370,111 @@ public final class DomainParsimonyCalculator { return getUnitsDeltaOnNode( node_identifier, getGainLossMatrix(), GainLossStates.UNCHANGED_PRESENT ); } + private void executeDolloParsimony( final boolean on_domain_presence ) { + reset(); + final DolloParsimony dollo = DolloParsimony.createInstance(); + dollo.setReturnGainLossMatrix( true ); + dollo.setReturnInternalStates( true ); + CharacterStateMatrix states = null; + if ( on_domain_presence ) { + states = createMatrixOfDomainPresenceOrAbsence(); + } + else { + states = createMatrixOfBinaryDomainCombinationPresenceOrAbsence(); + } + dollo.execute( getPhylogeny(), states ); + setGainLossMatrix( dollo.getGainLossMatrix() ); + setBinaryInternalStatesMatrix( dollo.getInternalStatesMatrix() ); + setCost( dollo.getCost() ); + setTotalGains( dollo.getTotalGains() ); + setTotalLosses( dollo.getTotalLosses() ); + setTotalUnchanged( dollo.getTotalUnchanged() ); + } + + private void executeFitchParsimony( final boolean on_domain_presence, + final boolean use_last, + final boolean randomize, + final long random_number_seed ) { + reset(); + if ( use_last ) { + System.out.println( " Fitch parsimony: use_last = true" ); + } + final FitchParsimony fitch = new FitchParsimony(); + fitch.setRandomize( randomize ); + if ( randomize ) { + fitch.setRandomNumberSeed( random_number_seed ); + } + fitch.setUseLast( use_last ); + fitch.setReturnGainLossMatrix( true ); + fitch.setReturnInternalStates( true ); + CharacterStateMatrix states = null; + if ( on_domain_presence ) { + states = createMatrixOfDomainPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); + } + else { + states = createMatrixOfBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList() ); + } + fitch.execute( getPhylogeny(), states, true ); + setGainLossMatrix( fitch.getGainLossMatrix() ); + setBinaryInternalStatesMatrix( fitch.getInternalStatesMatrix() ); + setCost( fitch.getCost() ); + setTotalGains( fitch.getTotalGains() ); + setTotalLosses( fitch.getTotalLosses() ); + setTotalUnchanged( fitch.getTotalUnchanged() ); + } + + private void executeFitchParsimonyOnSecondaryFeatures( final boolean use_last, + final boolean randomize, + final long random_number_seed ) { + reset(); + if ( use_last ) { + System.out.println( " Fitch parsimony: use_last = true" ); + } + final FitchParsimony fitch = new FitchParsimony(); + fitch.setRandomize( randomize ); + if ( randomize ) { + fitch.setRandomNumberSeed( random_number_seed ); + } + fitch.setUseLast( use_last ); + fitch.setReturnGainLossMatrix( true ); + fitch.setReturnInternalStates( true ); + final Map> map = getDomainIdToSecondaryFeaturesMap(); + final Map newmap = new HashMap(); + final Iterator>> it = map.entrySet().iterator(); + while ( it.hasNext() ) { + final Map.Entry> pair = it.next(); + if ( pair.getValue().size() != 1 ) { + throw new IllegalArgumentException( pair.getKey() + " mapps to " + pair.getValue().size() + " items" ); + } + newmap.put( pair.getKey(), ( String ) pair.getValue().toArray()[ 0 ] ); + } + final CharacterStateMatrix states = createMatrixOfSecondaryFeatureBinaryDomainCombinationPresenceOrAbsence( getGenomeWideCombinableDomainsList(), + newmap ); + fitch.execute( getPhylogeny(), states, true ); + setGainLossMatrix( fitch.getGainLossMatrix() ); + setBinaryInternalStatesMatrix( fitch.getInternalStatesMatrix() ); + setCost( fitch.getCost() ); + setTotalGains( fitch.getTotalGains() ); + setTotalLosses( fitch.getTotalLosses() ); + setTotalUnchanged( fitch.getTotalUnchanged() ); + } + + private Map> getDomainIdToSecondaryFeaturesMap() { + return _domain_id_to_secondary_features_map; + } + + private List getGenomeWideCombinableDomainsList() { + return _gwcd_list; + } + + private Phylogeny getPhylogeny() { + return _phylogeny; + } + + private SortedSet getPositiveFilter() { + return _positive_filter; + } + private void init() { setDomainIdToSecondaryFeaturesMap( null ); setPositiveFilter( null ); @@ -452,7 +498,7 @@ public final class DomainParsimonyCalculator { _cost = cost; } - private void setDomainIdToSecondaryFeaturesMap( final Map> domain_id_to_secondary_features_map ) { + private void setDomainIdToSecondaryFeaturesMap( final Map> domain_id_to_secondary_features_map ) { _domain_id_to_secondary_features_map = domain_id_to_secondary_features_map; } @@ -460,7 +506,7 @@ public final class DomainParsimonyCalculator { _gain_loss_matrix = gain_loss_matrix; } - private void setPositiveFilter( final SortedSet positive_filter ) { + private void setPositiveFilter( final SortedSet positive_filter ) { _positive_filter = positive_filter; } @@ -491,7 +537,7 @@ public final class DomainParsimonyCalculator { public static DomainParsimonyCalculator createInstance( final Phylogeny phylogeny, final List gwcd_list, - final Map> domain_id_to_secondary_features_map ) { + final Map> domain_id_to_secondary_features_map ) { if ( phylogeny.getNumberOfExternalNodes() != gwcd_list.size() ) { throw new IllegalArgumentException( "size of external nodes does not equal size of genome wide combinable domains list" ); } @@ -533,13 +579,13 @@ public final class DomainParsimonyCalculator { for( int ci = 0; ci < matrix.getNumberOfCharacters(); ++ci ) { BinaryDomainCombination bc = null; if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED_ADJACTANT ) { - bc = AdjactantDirectedBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + bc = AdjactantDirectedBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); } else if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED ) { - bc = DirectedBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + bc = DirectedBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); } else { - bc = BasicBinaryDomainCombination.createInstance( matrix.getCharacter( ci ) ); + bc = BasicBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); } if ( binary_combinations_per_genome[ identifier_index ].contains( bc ) ) { matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); @@ -553,12 +599,8 @@ public final class DomainParsimonyCalculator { return matrix; } - static CharacterStateMatrix createMatrixOfDomainPresenceOrAbsence( final List gwcd_list ) { - return createMatrixOfDomainPresenceOrAbsence( gwcd_list, null ); - } - public static CharacterStateMatrix createMatrixOfDomainPresenceOrAbsence( final List gwcd_list, - final SortedSet positive_filter ) { + final SortedSet positive_filter ) { if ( gwcd_list.isEmpty() ) { throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); } @@ -566,9 +608,9 @@ public final class DomainParsimonyCalculator { throw new IllegalArgumentException( "positive filter is empty" ); } final int number_of_identifiers = gwcd_list.size(); - final SortedSet all_domain_ids = new TreeSet(); + final SortedSet all_domain_ids = new TreeSet(); for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { - for( final DomainId domain : gwcd.getAllDomainIds() ) { + for( final String domain : gwcd.getAllDomainIds() ) { all_domain_ids.add( domain ); } } @@ -576,7 +618,7 @@ public final class DomainParsimonyCalculator { if ( positive_filter != null ) { //number_of_characters = positive_filter.size(); -- bad if doms in filter but not in genomes number_of_characters = 0; - for( final DomainId id : all_domain_ids ) { + for( final String id : all_domain_ids ) { if ( positive_filter.contains( id ) ) { number_of_characters++; } @@ -585,13 +627,13 @@ public final class DomainParsimonyCalculator { final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( number_of_identifiers, number_of_characters ); int character_index = 0; - for( final DomainId id : all_domain_ids ) { + for( final String id : all_domain_ids ) { if ( positive_filter == null ) { - matrix.setCharacter( character_index++, id.getId() ); + matrix.setCharacter( character_index++, id ); } else { if ( positive_filter.contains( id ) ) { - matrix.setCharacter( character_index++, id.getId() ); + matrix.setCharacter( character_index++, id ); } } } @@ -608,7 +650,7 @@ public final class DomainParsimonyCalculator { if ( ForesterUtil.isEmpty( matrix.getCharacter( ci ) ) ) { throw new RuntimeException( "this should not have happened: problem with character #" + ci ); } - final DomainId id = new DomainId( matrix.getCharacter( ci ) ); + final String id = matrix.getCharacter( ci ); if ( gwcd.contains( id ) ) { matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); } @@ -621,6 +663,80 @@ public final class DomainParsimonyCalculator { return matrix; } + public static CharacterStateMatrix createMatrixOfSecondaryFeatureBinaryDomainCombinationPresenceOrAbsence( final List gwcd_list, + final Map domain_id_to_second_features_map ) { + if ( gwcd_list.isEmpty() ) { + throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); + } + if ( ( domain_id_to_second_features_map == null ) || domain_id_to_second_features_map.isEmpty() ) { + throw new IllegalArgumentException( "domain id to secondary features map is null or empty" ); + } + final int number_of_identifiers = gwcd_list.size(); + final SortedSet all_binary_combinations_mapped = new TreeSet(); + final Set[] binary_combinations_per_genome_mapped = new HashSet[ number_of_identifiers ]; + int identifier_index = 0; + final SortedSet no_mappings = new TreeSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + binary_combinations_per_genome_mapped[ identifier_index ] = new HashSet(); + for( final BinaryDomainCombination bc : gwcd.toBinaryDomainCombinations() ) { + final BinaryDomainCombination mapped_bc = mapBinaryDomainCombination( domain_id_to_second_features_map, + bc, + no_mappings ); + all_binary_combinations_mapped.add( mapped_bc ); + binary_combinations_per_genome_mapped[ identifier_index ].add( mapped_bc ); + } + ++identifier_index; + } + if ( !no_mappings.isEmpty() ) { + ForesterUtil.programMessage( surfacing.PRG_NAME, "No mappings for the following (" + no_mappings.size() + + "):" ); + for( final String id : no_mappings ) { + ForesterUtil.programMessage( surfacing.PRG_NAME, id ); + } + } + final int number_of_characters = all_binary_combinations_mapped.size(); + final CharacterStateMatrix matrix = new BasicCharacterStateMatrix( number_of_identifiers, + number_of_characters ); + int character_index = 0; + for( final BinaryDomainCombination bc : all_binary_combinations_mapped ) { + matrix.setCharacter( character_index++, bc.toString() ); + } + identifier_index = 0; + final Set all_identifiers = new HashSet(); + for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { + final String species_id = gwcd.getSpecies().getSpeciesId(); + if ( all_identifiers.contains( species_id ) ) { + throw new AssertionError( "species [" + species_id + "] is not unique" ); + } + all_identifiers.add( species_id ); + matrix.setIdentifier( identifier_index, species_id ); + for( int ci = 0; ci < matrix.getNumberOfCharacters(); ++ci ) { + BinaryDomainCombination bc = null; + if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED_ADJACTANT ) { + bc = AdjactantDirectedBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); + } + else if ( gwcd.getDomainCombinationType() == DomainCombinationType.DIRECTED ) { + bc = DirectedBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); + } + else { + bc = BasicBinaryDomainCombination.obtainInstance( matrix.getCharacter( ci ) ); + } + if ( binary_combinations_per_genome_mapped[ identifier_index ].contains( bc ) ) { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.PRESENT ); + } + else { + matrix.setState( identifier_index, ci, CharacterStateMatrix.BinaryStates.ABSENT ); + } + } + ++identifier_index; + } + return matrix; + } + + static CharacterStateMatrix createMatrixOfDomainPresenceOrAbsence( final List gwcd_list ) { + return createMatrixOfDomainPresenceOrAbsence( gwcd_list, null ); + } + /** * For folds instead of Pfam-domains, for example * @@ -629,7 +745,7 @@ public final class DomainParsimonyCalculator { * @return */ static CharacterStateMatrix createMatrixOfSecondaryFeaturePresenceOrAbsence( final List gwcd_list, - final Map> domain_id_to_second_features_map, + final Map> domain_id_to_second_features_map, final Map mapping_results_map ) { if ( gwcd_list.isEmpty() ) { throw new IllegalArgumentException( "genome wide combinable domains list is empty" ); @@ -642,7 +758,7 @@ public final class DomainParsimonyCalculator { for( final GenomeWideCombinableDomains gwcd : gwcd_list ) { int mapped = 0; int not_mapped = 0; - for( final DomainId domain : gwcd.getAllDomainIds() ) { + for( final String domain : gwcd.getAllDomainIds() ) { if ( domain_id_to_second_features_map.containsKey( domain ) ) { all_secondary_features.addAll( domain_id_to_second_features_map.get( domain ) ); mapped++; @@ -676,7 +792,7 @@ public final class DomainParsimonyCalculator { all_identifiers.add( species_id ); matrix.setIdentifier( identifier_index, species_id ); final Set all_second_per_gwcd = new HashSet(); - for( final DomainId domain : gwcd.getAllDomainIds() ) { + for( final String domain : gwcd.getAllDomainIds() ) { if ( domain_id_to_second_features_map.containsKey( domain ) ) { all_second_per_gwcd.addAll( domain_id_to_second_features_map.get( domain ) ); } @@ -741,4 +857,27 @@ public final class DomainParsimonyCalculator { } return d; } + + private static BinaryDomainCombination mapBinaryDomainCombination( final Map domain_id_to_second_features_map, + final BinaryDomainCombination bc, + final SortedSet no_mappings ) { + String id0 = ""; + String id1 = ""; + if ( !domain_id_to_second_features_map.containsKey( bc.getId0() ) ) { + no_mappings.add( bc.getId0() ); + id0 = bc.getId0(); + } + else { + id0 = domain_id_to_second_features_map.get( bc.getId0() ); + } + if ( !domain_id_to_second_features_map.containsKey( bc.getId1() ) ) { + no_mappings.add( bc.getId1() ); + id1 = bc.getId1(); + } + else { + id1 = domain_id_to_second_features_map.get( bc.getId1() ); + } + // return new BasicBinaryDomainCombination( id0, id1 ); + return BasicBinaryDomainCombination.obtainInstance( id0, id1 ); + } }