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 */
021package org.biojava.nbio.core.sequence.storage;
022
023import org.biojava.nbio.core.sequence.Strand;
024import org.biojava.nbio.core.sequence.template.Compound;
025import org.biojava.nbio.core.sequence.template.CompoundSet;
026
027import java.util.List;
028
029/**
030 * This is a common method that can be used across multiple storage/proxy implementations to
031 * handle Negative strand and other interesting elements of sequence data.
032 * @author Scooter Willis <willishf at gmail dot com>
033 */
034public class SequenceAsStringHelper<C extends Compound> {
035
036        /**
037         *
038         * @param parsedCompounds
039         * @param compoundSet
040         * @param bioBegin
041         * @param bioEnd
042         * @param strand
043         * @return
044         */
045        public String getSequenceAsString(List<C> parsedCompounds, CompoundSet<C> compoundSet, Integer bioBegin, Integer bioEnd, Strand strand) {
046                // TODO Optimise/cache.
047                if(parsedCompounds.size() == 0)
048                        return "";
049                StringBuilder builder = new StringBuilder();
050                if (strand.equals(Strand.NEGATIVE)) {
051                        //we expect bioBegin to be bigger but could have circular case
052                        if (bioBegin <= bioEnd) {
053                                for (int index = bioEnd - 1; index >= bioBegin - 1; index--) {
054                                        C compound = parsedCompounds.get(index);
055                                        builder.append(compoundSet.getStringForCompound(compound));
056                                }
057                        }else{
058                                //go to 0 and the up
059                                for (int index = bioBegin - 1; index >= 0; index--) {
060                                        C compound = parsedCompounds.get(index);
061                                        builder.append(compoundSet.getStringForCompound(compound));
062                                }
063
064                                for (int index = parsedCompounds.size() - 1; index >= bioEnd - 1; index--) {
065                                        C compound = parsedCompounds.get(index);
066                                        builder.append(compoundSet.getStringForCompound(compound));
067                                }
068                        }
069                } else {
070                        if (bioBegin <= bioEnd) {
071                                for (int index = bioBegin - 1; index <= bioEnd - 1 ; index++) {
072                                        C compound = parsedCompounds.get(index);
073                                        builder.append(compoundSet.getStringForCompound(compound));
074                                }
075                        }else{
076                                //go to 0 and the up
077                                for (int index = bioBegin - 1; index <=  parsedCompounds.size() - 1; index++) {
078                                        C compound = parsedCompounds.get(index);
079                                        builder.append(compoundSet.getStringForCompound(compound));
080                                }
081
082                                for (int index = 0; index <= bioEnd - 1; index++) {
083                                        C compound = parsedCompounds.get(index);
084                                        builder.append(compoundSet.getStringForCompound(compound));
085                                }
086                        }
087
088
089                }
090
091                return builder.toString();
092        }
093}