/*
 * The Gemma project
 * 
 * Copyright (c) 2007 University of British Columbia
 * 
 * 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 ubic.gemma.analysis.sequence;

import ubic.gemma.model.genome.sequenceAnalysis.BlatResult;

/**
 * Used to convert BlatResult objects into PSL lines that can be displayed in the UCSC Genome Browser.
 * 
 * @author pavlidis
 * @version $Id$
 */
public class BlatResult2Psl {

    private static final int EXTRA_WINDOW = 1000;

    /**
     * See {@link  http://genome.ucsc.edu/goldenPath/help/customTrack.html}
     * 
     * @param blatResult
     * @return
     */
    public static String blatResult2Psl( BlatResult blatResult ) {
        StringBuilder buf = new StringBuilder();
        buf.append( blatResult.getMatches() + " " );
        buf.append( blatResult.getMismatches() + " " );
        buf.append( blatResult.getRepMatches() + " " );
        buf.append( blatResult.getNs() + " " );
        buf.append( blatResult.getQueryGapCount() + " " );
        buf.append( blatResult.getQueryGapBases() + " " );
        buf.append( blatResult.getTargetGapCount() + " " );
        buf.append( blatResult.getTargetGapBases() + " " );
        buf.append( blatResult.getStrand() + " " );
        buf.append( blatResult.getQuerySequence().getName() + " " );
        buf.append( blatResult.getQuerySequence().getLength() + " " );
        buf.append( blatResult.getQueryStart() + " " );
        buf.append( blatResult.getQueryEnd() + " " );
        buf.append( "chr" + blatResult.getTargetChromosome().getName() + " " );
        if ( blatResult.getTargetChromosome() != null && blatResult.getTargetChromosome().getSequence() != null
                && blatResult.getTargetChromosome().getSequence().getLength() != null ) {
            buf.append( blatResult.getTargetChromosome().getSequence().getLength() + " " );
        } else {
            buf.append( ( blatResult.getTargetEnd() + 1 ) + " " );// seems okay as long as more than the target end.
        }
        buf.append( blatResult.getTargetStart() + " " );
        buf.append( blatResult.getTargetEnd() + " " );
        buf.append( blatResult.getBlockCount() + " " );
        buf.append( blatResult.getBlockSizes() + " " );
        buf.append( blatResult.getQueryStarts() + " " );
        buf.append( blatResult.getTargetStarts() );
        buf.append( "\n" );
        return buf.toString();
    }

    /**
     * Creates text that can be displayed directly as a track in UCSC, using their hgTracks program. See
     * {@link  http://genome.ucsc.edu/goldenPath/help/customTrack.html}
     * 
     * @param blatResult
     * @return
     */
    public static String blatResult2PslTrack( BlatResult blatResult ) {

        StringBuilder buf = new StringBuilder();

        buf.append( "## Generated by Gemma (http://www.bioinformatics.ubc.ca/Gemma)\n" );
        buf.append( "browser position " );
        buf.append( "chr" + blatResult.getTargetChromosome().getName() + ":" );
        buf.append( ( blatResult.getTargetStart() - EXTRA_WINDOW ) + "-" );
        buf.append( ( blatResult.getTargetEnd() + EXTRA_WINDOW ) + "\n" );
        buf.append( "track name=" + blatResult.getQuerySequence().getName() );
        buf.append( " description=\"Gemma BLAT alignment\" visibility=2 useScore=1\n" );

        buf.append( blatResult2Psl( blatResult ) + "\n" );

        return buf.toString();
    }

}

@@ -2351,32 +2351,225 @@
 ngth() + %22 %22 );%0A
+        %7D else if ( blatResult.getTargetSequence() != null && blatResult.getTargetSequence().getLength() != null ) %7B%0A            buf.append( blatResult.getTargetSequence().getLength() + %22 %22 );%0A
         %7D else %7B

check if can use target sequence