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

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

There is a newer version: 0.6.0
Show newest version
/**
 *
 * 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.function.Consumer;
import java.util.function.Function;

import org.github.evenjn.align.TupleAligner;
import org.github.evenjn.align.graph.NotAlignableException;
import org.github.evenjn.knit.BasicAutoHook;
import org.github.evenjn.knit.KnittingCursable;
import org.github.evenjn.knit.ProgressManager;
import org.github.evenjn.yarn.AutoHook;
import org.github.evenjn.yarn.Cursable;
import org.github.evenjn.yarn.Di;
import org.github.evenjn.yarn.Hook;
import org.github.evenjn.yarn.Progress;
import org.github.evenjn.yarn.ProgressSpawner;
import org.github.evenjn.yarn.Tuple;

public class TupleAlignmentAlphabetWithAligner
implements
TupleAlignmentAlphabetBuilder {

	private int min_below;

	private int max_below;

	private TupleAligner aligner;

	private Function> logger;

	private Function a_printer;

	private Function b_printer;

	public TupleAlignmentAlphabetWithAligner(
			TupleAligner aligner) {
		this.aligner = aligner;
	}

	@Override
	public void setMinMax( int min, int max ) {
		this.min_below = min;
		this.max_below = max;
	}
	
	public void setPrinters(
			Function> logger,
			Function a_printer,
			Function b_printer ) {
				this.logger = logger;
				this.a_printer = a_printer;
				this.b_printer = b_printer;
	}

	public TupleAlignmentAlphabet build(
			Cursable, Tuple>> data,
			ProgressSpawner progress_spawner ) {
		KnittingCursable, Tuple>> kd =
				KnittingCursable.wrap( data );
		try ( AutoHook hook = new BasicAutoHook( ) ) {
			Consumer open_logger = null;
			if (logger != null) {
				open_logger = logger.apply( hook );
			}
			TupleAlignmentAlphabet result =
					new TupleAlignmentAlphabet( );
			Progress spawn = ProgressManager.safeSpawn( hook, progress_spawner,
					"TupleAlignmentAlphabetSimpleBuilder::build" );

			spawn.info( "Computing dataset size." );
		  spawn.target( kd.size( ) );
			spawn.info( "Collecting alphabet elements." );
			for ( Di, Tuple> datum : kd.pull( hook )
					.once( ) ) {

				spawn.step( 1 );

				try {
					Iterable> localAlphabet =
							TupleAlignmentAlphabetBuilderTools.localAlphabetWithAligner(
									min_below, max_below, datum.front( ), datum.back( ), aligner );
					for ( TupleAlignmentAlphabetPair pp : localAlphabet ) {
						result.add( pp );
					}
				}
				catch ( NotAlignableException e ) {
					if ( open_logger != null ) {
						open_logger.accept( "Not aligneable: " +
								TupleAlignmentAlphabetBuilderTools.tuple_printer( b_printer,
										datum.back( ) )
								+ " "
								+ TupleAlignmentAlphabetBuilderTools.tuple_printer( a_printer,
										datum.front( ) ) );
					}
					// simply ignore them.
				}
			}
			return result;
		}
		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy