##################################
#                                #
# Last modified 2021/02/02       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import os

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s config STAR_index_path chrom.sizes gtf' % sys.argv[0]
        print '\tconfig format:'
        print '\tlabel\tfastq file(or end1+end2, separate by space)'
        print '\tuse NaN if there is no GTF'
        sys.exit(1)

    config = sys.argv[1]
    STAR_index = sys.argv[2]
    CSfile = sys.argv[3]
    GTF = sys.argv[4]

    print STAR_index     
    print CSfile
    print GTF

    DataDict = {}

    linelist = open(config)
    for line in linelist:
        if line.startswith('#'):
            continue
        fields = line.strip().split('\t')
        label = fields[0]
        fastq = fields[1]
        DataDict[label] = {}
        DataDict[label]['input'] = fastq
    
    labels = DataDict.keys()
    labels.sort()

    print 'generating countlines.sh'

    outfile = open('countlines.sh', 'w')
    for label in labels:
        end1 = DataDict[label]['input'].split(' ')[0]
        outline = 'zcat ' + end1 + ' | wc -l > ' + label + '.fastq.lines'
        outfile.write(outline + '\n')
    outfile.close()

    print 'generating STAR.sh'
    outfile = open('STAR.sh', 'w')
    for label in labels:
        outline = 'mkdir ' + label + '-STAR-2.5.3a; '
        outline = outline + '/oak/stanford/groups/akundaje/marinovg/programs/STAR-2.5.3a/bin/Linux_x86_64_static/STAR --limitSjdbInsertNsj 10000000 --genomeDir ' + STAR_index 
        outline = outline + ' --outFileNamePrefix ' + label + '-STAR-2.5.3a/STAR-2.5.3a- --readFilesIn ' + DataDict[label]['input']
        if GTF != 'NaN':
             outline = outline + ' --sjdbGTFfile ' + GTF
        outline = outline + ' --runThreadN 20 --outSAMunmapped Within --outFilterType BySJout --outSAMattributes NH HI AS NM MD --outFilterMultimapNmax 50 --outSAMstrandField intronMotif --outFilterMismatchNmax 999 --outFilterMismatchNoverReadLmax 0.04 --alignIntronMin 10 --alignIntronMax 1000000 --alignMatesGapMax 1000000 --alignSJoverhangMin 8 --alignSJDBoverhangMin 1 --sjdbScore 1 --readFilesCommand zcat --outSAMtype BAM SortedByCoordinate --outWigStrand Stranded  --twopassMode Basic --twopass1readsN -1 --limitBAMsortRAM 500000000000 '
        outfile.write(outline + '\n')
    outfile.close()

    print 'generating samtools-index.sh'
    outfile = open('samtools-index.sh', 'w')
    for label in labels:
        outline = 'samtools index ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam'
        outfile.write(outline + '\n')
    outfile.close()

    print 'generating SAMstats.sh'
    outfile = open('SAMstats.sh', 'w')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/SAMstats.py ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam '
        outline = outline + ' SAMstats-' + label + ' -bam ' + CSfile + ' ' + ' samtools -paired '
        outfile.write(outline + '\n')
    outfile.close()

    print 'generating SAMstats.files'
    outfile = open('SAMstats.files', 'w')
    for label in labels:
        outline = label + '\tSAMstats-' + label
        outfile.write(outline + '\n')
    outfile.close()

    if GTF != 'NaN':
        print 'generating sam_reads_in_genes.sh'
        outfile = open('sam_reads_in_genes.sh', 'w')
        for label in labels:
            outline = 'python /oak/stanford/groups/akundaje/marinovg/code/transcriptome/SAM_reads_in_genes3_BAM.py ' + GTF + ' ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
            outline = outline + ' sam_reads_in_genes-' + label + ' -nomulti'
            outfile.write(outline + '\n')
        outfile.close()

        print 'generating sam_reads_in_genes.files'
        outfile = open('sam_reads_in_genes.files', 'w')
        for label in labels:
            outline = label + '\tsam_reads_in_genes-' + label + '\t0\t1'
            outfile.write(outline + '\n')
        outfile.close()

    print 'generating makewig.sh'
    outfile = open('makewig.sh', 'w')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.wig -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.wig -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.wig -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.wig -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.wig -stranded - -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.wig -stranded - -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.wig -stranded - -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.wig -stranded - -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.wig -stranded + -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.wig -stranded + -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.wig -stranded + -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/makewigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.wig -stranded + -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.5p.wig -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.5p.wig -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.5p.wig -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.5p.wig -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.5p.wig -stranded - -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.5p.wig -stranded - -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.5p.wig -stranded - -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.5p.wig -stranded - -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.5p.wig -stranded + -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.5p.wig -stranded + -RPM -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.5p.wig -stranded + -RPM -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    for label in labels:
        outline = 'python /oak/stanford/groups/akundaje/marinovg/code/make5primeWigglefromBAM-NH.py --- ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.bam ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.5p.wig -stranded + -nomulti -notitle -end1only'
        outfile.write(outline + '\n')
    outfile.close()

    if GTF != 'NaN':
        print 'generating coverage.sh'
        outfile = open('coverage.sh', 'w')
        for label in labels:
            outline = 'python /oak/stanford/groups/akundaje/marinovg/code/transcriptome/gene_coverage_wig_gtf.py ' + GTF + ' ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.5p.wig 1000 coverage-' + label + '.unique.5p -normalize -singlemodelgenes'
            outfile.write(outline + '\n')
        outfile.close()

        print 'generating coverage.files'
        outfile = open('coverage.files', 'w')
        for label in labels:
            outline = label + '\tcoverage-' + label + '.unique.5p\t0\t1'
            outfile.write(outline + '\n')
        outfile.close()

    print 'generating wigToBigWig.sh'
    outfile = open('wigToBigWig.sh', 'w')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.minus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.minus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.5p.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.minus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.5p.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.minus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.counts.plus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.5p.wig ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.RPM.plus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.5p.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.RPM.plus.5p.bigWig'
        outfile.write(outline + '\n')
    for label in labels:
        outline = '~/programs/UCSC-utils-2017-07-13/wigToBigWig ' + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.5p.wig  ' + CSfile + ' '
        outline = outline + label + '-STAR-2.5.3a/STAR-2.5.3a-Aligned.sortedByCoord.out.unique.counts.plus.5p.bigWig'
        outfile.write(outline + '\n')
    outfile.close()


run()
