All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.github.evenjn.align.alphabet.TupleAlignmentAlphabet Maven / Gradle / Ivy

/**
 *
 * Copyright 2016 Marco Trevisan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.github.evenjn.align.alphabet;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;
import java.util.function.BiFunction;

import org.github.evenjn.knit.KnittingTuple;
import org.github.evenjn.yarn.Tuple;

public class TupleAlignmentAlphabet {

	private Vector> alphabet =
			new Vector<>( );

	private Vector alphabet_above = new Vector<>( );

	private Vector> sequences_below = new Vector<>( );

	private HashMap>> map =
			new HashMap<>( );

	private HashSet above_set = new HashSet<>( );

	private HashMap, Integer> encode_map =
			new HashMap<>( );

	int add( TupleAlignmentAlphabetPair pair ) {
		Integer integer = encode_map.get( pair );
		if (integer != null) {
			return integer;
		}
		alphabet_above.add( pair.above );
		sequences_below.add( pair.below );
		encode_map.put( pair, alphabet.size( ) );
		alphabet.add( pair );

		HashSet> m = map.get( pair.above );
		if ( m == null ) {
			m = new HashSet<>( );
			map.put( pair.above, m );
			above_set.add( pair.above );
		}
		m.add( pair.below );
		return alphabet.size( );
	}

	public Set above( ) {
		return above_set;
	}

	public Set> correspondingBelow( SymbolAbove above ) {
		return map.get( above );
	}

	public TupleAlignmentAlphabetPair get( int t ) {
		return alphabet.get( t );
	}

	public int size( ) {
		return alphabet.size( );
	}
	
	public BiFunction, Integer> asEncoder(){
		return (a, b) -> encode(a, b);
	}

	public Integer encode( SymbolAbove above, Tuple below ) {
		TupleAlignmentAlphabetPair pair =
				new TupleAlignmentAlphabetPair<>( );
		pair.above = above;
		pair.below = KnittingTuple.wrap( below );
		return encode_map.get( pair );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy