001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    package org.biojava3.core.sequence.storage;
006    
007    import java.util.List;
008    import org.biojava3.core.sequence.Strand;
009    import org.biojava3.core.sequence.template.Compound;
010    import org.biojava3.core.sequence.template.CompoundSet;
011    
012    /**
013     * This is a common method that can be used across multiple storage/proxy implementations to
014     * handle Negative strand and other interesting elements of sequence data.
015     * @author Scooter Willis <willishf at gmail dot com>
016     */
017    public class SequenceAsStringHelper<C extends Compound> {
018    
019        /**
020         *
021         * @param parsedCompounds
022         * @param compoundSet
023         * @param bioBegin
024         * @param bioEnd
025         * @param strand
026         * @return
027         */
028        public String getSequenceAsString(List<C> parsedCompounds, CompoundSet<C> compoundSet, Integer bioBegin, Integer bioEnd, Strand strand) {
029            // TODO Optimise/cache.
030            if(parsedCompounds.size() == 0)
031                return "";
032            StringBuilder builder = new StringBuilder();
033            if (strand.equals(Strand.NEGATIVE)) {
034                //we expect bioBegin to be bigger but could have circular case
035                if (bioBegin <= bioEnd) {
036                    for (int index = bioEnd - 1; index >= bioBegin - 1; index--) {
037                        C compound = parsedCompounds.get(index);
038                        builder.append(compoundSet.getStringForCompound(compound));
039                    }
040                }else{
041                    //go to 0 and the up
042                    for (int index = bioBegin - 1; index >= 0; index--) {
043                        C compound = parsedCompounds.get(index);
044                        builder.append(compoundSet.getStringForCompound(compound));
045                    }
046                    
047                    for (int index = parsedCompounds.size() - 1; index >= bioEnd - 1; index--) {
048                        C compound = parsedCompounds.get(index);
049                        builder.append(compoundSet.getStringForCompound(compound));
050                    }
051                }
052            } else {
053                if (bioBegin <= bioEnd) {
054                    for (int index = bioBegin - 1; index <= bioEnd - 1 ; index++) {
055                        C compound = parsedCompounds.get(index);
056                        builder.append(compoundSet.getStringForCompound(compound));
057                    }
058                }else{
059                    //go to 0 and the up
060                    for (int index = bioBegin - 1; index <=  parsedCompounds.size() - 1; index++) {
061                        C compound = parsedCompounds.get(index);
062                        builder.append(compoundSet.getStringForCompound(compound));
063                    }
064    
065                    for (int index = 0; index <= bioEnd - 1; index++) {
066                        C compound = parsedCompounds.get(index);
067                        builder.append(compoundSet.getStringForCompound(compound));
068                    }
069                }
070    
071    
072            }
073    
074            return builder.toString();
075        }
076    }