001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021 022package org.biojava.bio.symbol; 023 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.Iterator; 028import java.util.List; 029import java.util.Map; 030import java.util.NoSuchElementException; 031import java.util.Set; 032 033import org.biojava.bio.alignment.Alignment; 034import org.biojava.utils.ChangeVetoException; 035import org.biojava.utils.Unchangeable; 036 037/** 038 * An alignment that relabels another alignment. 039 * 040 * @author Matthew Pocock 041 * @author Nimesh Singh 042 */ 043public class RelabeledAlignment 044 extends 045 Unchangeable 046 implements 047 Alignment 048{ 049 private Alignment delegate; 050 private Map<String, String> labelMap = new HashMap<String, String>(); 051 052 public RelabeledAlignment(Alignment delegate) { 053 this.delegate = delegate; 054 for(Iterator<String> i = delegate.getLabels().iterator(); i.hasNext(); ) { 055 String label = i.next(); 056 labelMap.put(label, label); 057 } 058 } 059 060 public List<String> getLabels() { 061 return new ArrayList<String>(labelMap.keySet()); 062 } 063 064 public Symbol symbolAt(String label, int column) 065 throws NoSuchElementException { 066 return delegate.symbolAt(map(label), column); 067 } 068 069 public SymbolList symbolListForLabel(String label) 070 throws NoSuchElementException { 071 return delegate.symbolListForLabel(map(label)); 072 } 073 074 public Alignment subAlignment(Set<String> labels, Location loc) 075 throws NoSuchElementException { 076 return new RelabeledAlignment(delegate.subAlignment(map(labels), loc)); 077 } 078 079 public String seqString() { 080 return delegate.seqString(); 081 } 082 083 public String subStr(int min, int max) { 084 return delegate.subStr(min, max); 085 } 086 087 public Alphabet getAlphabet() { 088 return delegate.getAlphabet(); 089 } 090 091 public Iterator iterator() { 092 return delegate.iterator(); 093 } 094 095 public int length() { 096 return delegate.length(); 097 } 098 099 public List toList() { 100 return delegate.toList(); 101 } 102 103 public SymbolList subList(int min, int max) { 104 return delegate.subList(min, max); 105 } 106 107 public Symbol symbolAt(int pos) { 108 return delegate.symbolAt(pos); 109 } 110 111 public void edit(Edit edit) 112 throws IllegalAlphabetException, ChangeVetoException { 113 delegate.edit(edit); 114 } 115 116 public Iterator symbolListIterator() { 117 return new Alignment.SymbolListIterator(this); 118 } 119 120 protected Set<String> map(Set<String> labels) { 121 Set<String> set = new HashSet<String>(); 122 for(Iterator<String> i = labels.iterator(); i.hasNext(); ) { 123 set.add(map(i.next())); 124 } 125 return set; 126 } 127 128 protected String map(Object label) { 129 return labelMap.get(label); 130 } 131} 132