#! /opt/gensoft/exe/taxoptimizer/2.0/venv/bin/python2.7

# Corinne Maufrais
# Institut Pasteur, Centre d'informatique pour les biologistes
# maufrais@pasteur.fr
#

# version 1.2

import os, sys, optparse

try:
    LIB = os.environ['RANKOPTIMIZERLIB']
except:
    LIB = '/usr/local/bin'

if LIB not in sys.path:
    sys.path.append( LIB )

try:
    LIB2 = os.environ['BLAST2TC_LIB']
except:
    LIB2 = '/usr/local/bin'

if LIB2 not in sys.path:
    sys.path.append( LIB2 )



import Golden
#os.environ['GOLDENDATA'] = "/mount/banques/prod/index/golden"

try:
    GOLDENDATA =  os.environ['GOLDENDATA']
except:
    GOLDENDATA = "/mount/banques/prod/index/golden"
    os.environ['GOLDENDATA'] = GOLDENDATA

import genoGd

class RankOptimizerError:
    def __init__( self, err ):
        self.err = err

    def __repr__( self ):
        return "[rankoptimizer] " + self.err

def parseUniprot( flatFile , DE ):
    """
    parse uniprot or embl like flat file
    """
    description = ''
    taxId= ''
    taxoLight = ''
    orgName = ''
    lineFld= flatFile.split('\n')
    vuOS = False
    vuOC = False
    vuOCXX = False
    for line in lineFld:
        if line =='\n':
            continue
        tag = line[0:2].strip()
        value = line[5:]
        value = value.replace('\n','')
        if DE and tag == 'DE':
            description += value
        elif tag == 'OS' and not vuOS:
            fldOS=value.split('(')
            orgName += fldOS[0].strip()
            vuOS = True
        elif tag == 'OC' and not vuOCXX:
            taxoLight += value
            vuOC = True
        elif tag == 'XX' and vuOC:
            vuOCXX = True
        elif tag == 'OX':
            taxId += value
        elif tag in ['RN', 'DR','CC', 'FH', 'SQ']:
            return orgName, taxId, taxoLight, description
    return orgName, taxId, taxoLight, description

def parseUniprotSeq( flatFile, format ):
    """
    parse uniprot or embl like flat file
    """
    ID= ''
    SEQ = ''
    DE = ''
    seqVu = False
    lineFld= flatFile.split('\n')
    for line in lineFld:
        if line =='\n':
            continue
        tag = line[0:2].strip()
        value = line[5:]
        value = value.replace('\n','')
        if tag == 'ID':
            ID += value.split()[0]
        elif tag == 'DE':
            DE += value
        elif tag == 'SQ' and not seqVu:
            seqVu = True
        elif seqVu:
            SEQ += value.replace(' ' , '' )
    if format =='fasta':
        sequence = '>%s %s\n' % (ID, DE)
        i = 0
        while i < len(SEQ):
            sequence += SEQ[i:i+80] +'\n'
            i+=80
        return sequence
    elif format =='raw':
        return SEQ

def parseGenbank( flatFile , DE  ):
    """
    parse genbank like flat file
    """
    description = ''
    taxId= ''
    taxoLight = ''
    orgName = ''
    taxoL = False
    lineFld= flatFile.split('\n')
    for line in lineFld:
        if line =='\n':
            continue
        tag = line[0:12].strip()
        value = line[12:]
        value = value.replace('\n','')
        if DE and tag == 'DEFINITION':
            description += value
        elif tag == 'ORGANISM': # one line ##### pb qqfois plusieurs ligne
            orgName += value.strip()
            taxoL=True
        elif taxoL and not tag: # multiple line without tag
            taxoLight += value
        elif tag == 'TaxID': # gi number
            taxId += value
        elif tag in ['REFERENCE','COMMENT', 'FEATURES', 'ORIGIN']:
            return orgName, taxId, taxoLight, description
        elif not tag: # fin taxonomy
            taxoL = False
    return orgName, taxId, taxoLight, description
    
def parseGenbankSeq( flatFile, format ):
    """
    parse genbank like flat file
    """
    ID= ''
    SEQ = ''
    DE = ''
    seqVu = False
    lineFld= flatFile.split('\n')
    for line in lineFld:
        if line =='\n':
            continue
        tag = line[0:12].strip()
        value = line[12:]
        value = value.replace('\n','')
        if tag == 'LOCUS':
            ID += value.split()[0]
        elif tag == 'DEFINITION':
            DE += value
        elif tag == 'ORIGIN' and not seqVu:
            seqVu = True
        elif seqVu:
            SEQ += line[10:].replace(' ' , '' )
    if format =='fasta':
        sequence = '>%s %s\n' % (ID, DE)
        i = 0
        while i < len(SEQ):
            sequence += SEQ[i:i+80] +'\n'
            i+=80
        return sequence
    elif format =='raw':
        return SEQ
        
def parse( flatFile, format ):
    """
    parse db flat file ( uniprot, embl, genbank) 
    """
    if flatFile[:2] == 'ID':
        return parseUniprotSeq( flatFile, format )
    elif flatFile[:5] == 'LOCUS':
        return parseGenbankSeq( flatFile, format )
    return ''




def doGolden( db,ac,  format='fasta' ):
    ########################### db ref
    if db in[ 'sp', 'sw','swissprot','tr', 'trembl']:
        db = 'uniprot'
    elif db in ['emb', 'dbj']:
        db = 'embl'
    elif db in ['gb']:
        db = 'genbank'
    elif db in ['gp']:
        db = 'genpept'
    elif db in ['ref']:
        db = 'refseq'
    elif db in ['rdpii']:
           db = 'rdpii'
    elif db[0:8] == 'embl_wgs':
        db = 'embl_wgs'
    elif db[0:8] == 'genbank_wgs':
        db = 'genbank_wgs'
    #elif db in ['pir','pdb','tpg', 'tpe', 'tpd', 'prf']:
    #    return '','','',''
    
    try:
        flatFile = Golden.access(db,ac)
    except IOError, err:        
        return ''
        
    if flatFile:
        return parse( flatFile, format ) #orgName, taxId, taxoLight
    else:
        return ''


def _blastScoreVsColor( val ):
    if 0 <= val < 50:
        return 'blue'
    elif  50 <= val < 100:
        return 'grey'
    elif  100 <= val <150:
        return 'orange'
    elif  150 <= val < 200:
        return 'green'
    elif  200 <= val:
        return 'red'
    else:
        return 'black'


def _blastScoreVsColorInv( val ):
    if 0 <= val < 50:
        return 'lightblue'
    elif  50 <= val < 100:
        return 'lightgrey'
    elif  100 <= val <150:
        return 'yellow'
    elif  150 <= val < 200:
        return 'lightgreen'
    elif  200 <= val:
        return 'lightred'
    else:
        return 'black'

#def _blastScoreVsColor( val ):
#    return 'red'

#def _blastScoreVsColorInv( val ):
#    return 'blue'

def plot( pictureFileName, sbjctLen, sbjctAcc, blHits, sizeX = 700, fontsize = 4, typePlot='png'):
    """ picture output """
    #blHits: list of split (blast m8 line)
    #blHits = [qry, sbjct, % identity, aln length, mismatches, gap, q. start, q. end, s. start, s. end, e-value, bit score]
    gdPlot = genoGd.genoGD( fontsize = fontsize, sizeX = sizeX, nbQuery = len( blHits ))
    gdPlot.plotSeq( seqlen = sbjctLen , start=0, stop=sizeX, header = sbjctAcc, color = 'red', up=True )
    gdPlot._nbligneP()
    rpt = float(sizeX) / sbjctLen
    nbhit = 0
    for blh in blHits:
        head = True
        #if len( blLine[0] )<= 10:
        #    header = blLine[0]
        #else:   
        #    header = blLine[0][0:10]+'..'
        header = blh[0][8:18]+'..'
        #print blh[8] , blh[9], float(blh[9])-float(blh[8]), blh[11]
        if int( blh[8] ) < int( blh[9] ):
            gdPlot.plotHit( int (int( blh[8]) * rpt), int(int( blh[9] ) * rpt), header, _blastScoreVsColor( float(blh[11]) ),delta = 2,  head=head)
            nbhit +=1
            gdPlot._nbligneP()
        else:
            gdPlot.plotHit( int( int( blh[9]) * rpt), int(int( blh[8] ) * rpt), header, _blastScoreVsColorInv( float(blh[11]) ), delta = 2,head=head)
            nbhit +=1
            gdPlot._nbligneP()
        head = False
        
        #print 'ii:' , nbhit
    gdPlot._nbligneP()
    gdPlot.plotSeq( seqlen=sbjctLen, start=0, stop=sizeX, header = sbjctAcc, color = 'red', up = False )
    gdPlot.legend()
    gdPlot.legendInv()
    if typePlot == 'png' and nbhit > 0:
        picturefh = open( pictureFileName, "w")
        gdPlot.picture.writePng(picturefh)
        picturefh.close()

def plotMerge( pictureFileName, sbjctLen, sbjctAcc, blHits, sizeX = 700, fontsize = 4, typePlot='png'):
    """ picture output """
    #blHits: list of split (blast m8 line)
    #blHits = [qry, sbjct, % identity, aln length, mismatches, gap, q. start, q. end, s. start, s. end, e-value, bit score]
    gdPlot = genoGd.genoGD( fontsize = fontsize, sizeX = sizeX, nbQuery = 1 )
    gdPlot.plotSeq( seqlen = sbjctLen , start=0, stop=sizeX, header = sbjctAcc, color = 'red', up=True )
    gdPlot._nbligneP()
    gdPlot._nbligneP()
    rpt = float(sizeX) / sbjctLen
    nbhit = 1
    for blh in blHits:
        head = False
        header = ''
        if int( blh[8] ) < int( blh[9] ):
            gdPlot.plotHit( int (int( blh[8]) * rpt), int(int( blh[9] ) * rpt), header, 'lightred',delta = 2,  head=head)
        else:
            gdPlot.plotHit( int( int( blh[9]) * rpt), int(int( blh[8] ) * rpt), header, 'blue', delta = 2,head=head)
        head = False
        
    gdPlot._nbligneP()
    #gdPlot.plotSeq( seqlen=sbjctLen, start=0, stop=sizeX, header = sbjctAcc, color = 'red', up = False )
    #gdPlot._nbligneP()
    gdPlot.legendMerge()
    if typePlot == 'png' and nbhit > 0:
        picturefh = open( pictureFileName, "w")
        gdPlot.picture.writePng(picturefh)
        picturefh.close()

def plotMerge2plus( pictureFileName, sbjctLen, sbjctAcc, blHits, sizeX = 700, fontsize = 4, typePlot='png'):
    """ picture output """
    #blHits: list of split (blast m8 line)
    #blHits = [qry, sbjct, % identity, aln length, mismatches, gap, q. start, q. end, s. start, s. end, e-value, bit score]
    gdPlot = genoGd.genoGD( fontsize = fontsize, sizeX = sizeX, nbQuery = len(blHits)/60 )
    gdPlot.plotSeq( seqlen = sbjctLen , start=0, stop=sizeX, header = sbjctAcc, color = 'red', up=True )
    gdPlot._nbligneP()
    gdPlot._nbligneP()
    rpt = float(sizeX) / sbjctLen
    nbhit = 1
    i = 0
    nmax = 0
    while i < len( blHits):
        head = False
        header = ''
        end = int( blHits[i][9] )
        gdPlot.plotHit( int (int( blHits[i][8]) * rpt), int(int( blHits[i][9] ) * rpt), '', _blastScoreVsColor( float(blHits[i][11]) ),delta = 2,  head=False)
        n =0
        while (i < len( blHits)-1) and (int(blHits[i+1][8]) < end) :
            gdPlot._nbligneP()
            gdPlot.plotHit( int (int( blHits[i+1][8]) * rpt), int(int( blHits[i+1][9] ) * rpt), header, _blastScoreVsColor( float(blHits[i+1][11]) ),delta = 2,  head=head)
            i+=1
            n+=1
            if n > nmax:
                nmax = n
        gdPlot.nbligne -= n
        i+=1
        
    gdPlot.nbligne += nmax
    #gdPlot.plotSeq( seqlen=sbjctLen, start=0, stop=sizeX, header = sbjctAcc, color = 'red', up = False )
    #gdPlot._nbligneP()
    gdPlot.legendMerge()
    if typePlot == 'png' and nbhit > 0:
        picturefh = open( pictureFileName, "w")
        gdPlot.picture.writePng(picturefh)
        picturefh.close()

def plotMerge2minus( pictureFileName, sbjctLen, sbjctAcc, blHits, sizeX = 700, fontsize = 4, typePlot='png'):
    """ picture output """
    #blHits: list of split (blast m8 line)
    #blHits = [qry, sbjct, % identity, aln length, mismatches, gap, q. start, q. end, s. start, s. end, e-value, bit score]
    gdPlot = genoGd.genoGD( fontsize = fontsize, sizeX = sizeX, nbQuery = len(blHits)/60 )
    gdPlot.plotSeq( seqlen = sbjctLen , start=0, stop=sizeX, header = sbjctAcc, color = 'red', up=True )
    gdPlot._nbligneP()
    gdPlot._nbligneP()
    rpt = float(sizeX) / sbjctLen
    nbhit = 1
    i = len( blHits)-1
    nmax = 0
    while i >= 0:
        head = False
        header = ''
        begin = int( blHits[i][8] )
        gdPlot.plotHit( int (int( blHits[i][9]) * rpt), int(int( blHits[i][8] ) * rpt), '', _blastScoreVsColorInv( float(blHits[i][11]) ),delta = 2,  head=False)
        n =0
        while (i > 1 ) and (int(blHits[i-1][9]) < begin) :
            gdPlot._nbligneP()
            gdPlot.plotHit( int (int( blHits[i-1][9]) * rpt), int(int( blHits[i-1][8] ) * rpt), header, _blastScoreVsColorInv( float(blHits[i-1][11]) ),delta = 2,  head=head)
            i-=1
            n+=1
            if n > nmax:
                nmax = n
        gdPlot.nbligne -= n
        i-=1
        
    gdPlot.nbligne += nmax
    #gdPlot.plotSeq( seqlen=sbjctLen, start=0, stop=sizeX, header = sbjctAcc, color = 'red', up = False )
    #gdPlot._nbligneP()
    gdPlot.legendMerge()
    if typePlot == 'png' and nbhit > 0:
        picturefh = open( pictureFileName, "w")
        gdPlot.picture.writePng(picturefh)
        picturefh.close()

def plotMerge2( pictureFileName, sbjctLen, sbjctAcc, blHits, sizeX = 700, fontsize = 4, typePlot='png'):
    """ picture output """
    #blHits: list of split (blast m8 line)
    #blHits = [qry, sbjct, % identity, aln length, mismatches, gap, q. start, q. end, s. start, s. end, e-value, bit score]
    gdPlot = genoGd.genoGD( fontsize = fontsize, sizeX = sizeX, nbQuery = len(blHits)/60 )
    gdPlot.plotSeq( seqlen = sbjctLen , start=0, stop=sizeX, header = sbjctAcc, color = 'red', up=True )
    gdPlot._nbligneP()
    gdPlot._nbligneP()
    rpt = float(sizeX) / sbjctLen
    nbhit = 1
    i = 0
    nmax = 0
    while i < len( blHits):
        head = False
        header = ''
        if int(  blHits[i][8] ) < int(  blHits[i][9] ):
            end = int( blHits[i][9] )
            gdPlot.plotHit( int (int( blHits[i][8]) * rpt), int(int( blHits[i][9] ) * rpt), '', _blastScoreVsColor( float(blHits[i][11]) ),delta = 2,  head=False)
        else:
            end = int( blHits[i][8] )
            gdPlot.plotHit( int (int( blHits[i][9]) * rpt), int(int( blHits[i][8] ) * rpt), '', _blastScoreVsColorInv( float(blHits[i][11]) ),delta = 2,  head=False)
        n =0
        while (i < len( blHits)-1) and (int(blHits[i+1][8]) < end or int(blHits[i+1][9]) <end) :
            gdPlot._nbligneP()
            if int(  blHits[i+1][8] ) < int(  blHits[i+1][9] ):
                gdPlot.plotHit( int (int( blHits[i+1][8]) * rpt), int(int( blHits[i+1][9] ) * rpt), header, _blastScoreVsColor( float(blHits[i+1][11]) ),delta = 2,  head=head)
            else:
                gdPlot.plotHit( int (int( blHits[i+1][9]) * rpt), int(int( blHits[i+1][8] ) * rpt), header, _blastScoreVsColorInv( float(blHits[i+1][11]) ),delta = 1,  head=head)
            i+=1
            n+=1
            if n > nmax:
                nmax = n
        gdPlot.nbligne -= n
        i+=1
        
    gdPlot.nbligne += nmax
    #gdPlot.plotSeq( seqlen=sbjctLen, start=0, stop=sizeX, header = sbjctAcc, color = 'red', up = False )
    #gdPlot._nbligneP()
    gdPlot.legendMerge()
    if typePlot == 'png' and nbhit > 0:
        picturefh = open( pictureFileName, "w")
        gdPlot.picture.writePng(picturefh)
        picturefh.close()        




if __name__=='__main__':
    usage = "mappinghsp4taxo [options] -i <file>/-j <file> -b <file>"
    
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-i", "--one_column_file", dest="offset_file",
                  help="Offset's list of line to parse [p]taxoptomizer output file (cf option -b). File contains ptaxoptimizer's line offset (integer).", metavar="FILE")
    parser.add_option("-j", "--two_column_file", dest="krona_outfile",
                  help="kronaextract output file. FIle contains two column; first: reads name and second:  ptaxoptimizer's line offset (integer).", metavar="FILE")
    parser.add_option("-b", "--taxofile", dest="taxofile",
                  help="[p]taxoptimizer output file (format: blast m8 + OC + taxonomy +/-  DE ).", metavar="FILE")
    
    parser.add_option("-e", "--extract_file", dest="extract_file",
                  help="Extract lines of interest from [p]taxoptimizer file and write them in <file>", metavar="FILE")
    
    parser.add_option("-c", "--blast_column",
                  dest="blast_column", metavar = "int",
                  help="Column number to parse in ptaxoptimizer file (default: 2)", default= 1)
    parser.add_option("-s", "--separator",
                  dest="separator", metavar = "STRING",
                  help="Separator character (default '|')",
                  default = '|')
    
    parser.add_option("-p", "--picture_file", dest="picture_prefix",
                  help="Draw picture(s) with prefix <str>. png format", metavar="STRING", default='')
    
    
    parser.add_option("-x", "--strand_orientation", dest='strand_orientation',
                help="Split mapping acccording to strand's orientation (minus and plus orientations)",
                metavar = "BOOLEAN", action="store_true", default = False)
    
    parser.add_option("-m", "--merge", dest='merge',
                help="Draw all hsp in one line",
                metavar = "BOOLEAN", action="store_true", default = False)
    
    parser.add_option("-M", "--merge2", dest='merge2',
                help="Draw non overlapping hsp in few lines   --> in developpment",
                metavar = "BOOLEAN", action="store_true", default = False)
    
    parser.add_option("-g", "--galaxy_restrict", dest='galaxy_restrict',
                help="Restrict output(s) to one reference sequence",
                metavar = "BOOLEAN", action="store_true", default = False)
    
    
    (opts, args) = parser.parse_args()
    
    mandatories = ['taxofile']
    
    for option in parser.option_list:
        #option.help, option.dest, option._long_opts, option._short_opts
        if option.dest in mandatories and not opts.__dict__[option.dest]:
            print >>sys.stderr, "REQUIRED option is missing: %s/%s\n" % (option._short_opts[0], option._long_opts[0])
            parser.print_help()
            exit(-1)


    if not opts.offset_file and not opts.krona_outfile:
        print >>sys.stderr, "REQUIRED inputfile is missing: -i/--one_column_file or -j/two_column_file \n" 
        sys.exit( 0 )
   
    
    
    if opts.offset_file:
        try:
            offlinefhin = open( opts.offset_file )
            offset_column = 0
        except IOError, err:
            print >>sys.stderr, err
            sys.exit()
    elif opts.krona_outfile:
        try:
            offlinefhin = open( opts.krona_outfile )
            offset_column = 1
        except IOError, err:
            print >>sys.stderr, err
            sys.exit()
    
    
    try:
        blastfhin = open( opts.taxofile )
    except IOError, err:
        print >>sys.stderr, err
        sys.exit()
        

    if opts.extract_file:
        extractfhin = open( opts.extract_file, 'w' )

    
    DBInfo = {}

    max = 0
    sbjctAccMax = []
    offline = offlinefhin.readline()
    while offline:
        off_field = offline.split('\t')
        if len(off_field) != offset_column +1:
            print >>sys.stderr, "format error: cf option error: -i/--one_column_file or -j/two_column_file \n" 
            sys.exit(1)
        position = int(off_field[offset_column])
        blastfhin.seek( position )
        blLine = blastfhin.readline()
        if opts.extract_file:
            print >>extractfhin, blLine[:-1]
        blfld = blLine.split('\t')
        query = blfld[0]
        try:
            sbjctInfo = blfld[opts.blast_column].split(opts.separator)
        except:
            if opts.blast_column < len(blfld):
                print >>sys.stderr, RankOptimizerError("Parsing error: %s" % blfld[opts.blast_column])
            else:
                print >>sys.stderr, RankOptimizerError("Parsing error: column %s doesn't exist in line nb %s"  % (opts.blast_column+1, blLine) )
            continue
        sbjctAcc = ''
        sbjctDB = ''
        if len(sbjctInfo) == 5:
            # PF8/Pathoquest
            sbjctDB = sbjctInfo[2]
            sbjctAcc = sbjctInfo[3].split('.')[0]
        elif len(sbjctInfo) == 2 or len(sbjctInfo) == 3:
            # Lionel extraction
            sbjctDB = sbjctInfo[0]
            sbjctAcc = sbjctInfo[1]
        
        if not sbjctAcc or not sbjctDB:
            print >>sys.stderr, RankOptimizerError ( "Parsing error: %s" % blfld[opts.blast_column] )
            continue        
        
        if DBInfo.has_key(sbjctAcc ):
            DBInfo[ sbjctAcc ]['nb'] +=1
            DBInfo[ sbjctAcc ]['blast'].append(blfld[:12])
        else:
            DBInfo[ sbjctAcc ]={'nb' :1, 'db': sbjctDB , 'blast' : [blfld[:12] ]}
        
        if DBInfo[ sbjctAcc ]['nb'] > max:
            max = DBInfo[ sbjctAcc ]['nb']
            sbjctAccMax = [sbjctAcc]
        elif DBInfo[ sbjctAcc ]['nb'] == max:
            sbjctAccMax.append(sbjctAcc)
            
        offline = offlinefhin.readline()
    
    if opts.galaxy_restrict:
        # fichiers multiples mal geres par galaxy
        sbjctAccMax = sbjctAccMax[0:1]
        
    for elemMax in sbjctAccMax:
        if not opts.galaxy_restrict:
            plot_name_prefix = opts.picture_prefix + '_' + elemMax
        else:
            plot_name_prefix = opts.picture_prefix
        plus = []
        minus = []
        DBInfo[ elemMax ]['blast'].sort(lambda x, y: cmp(int(x[8]), int(y[8])))
        if opts.strand_orientation or (opts.merge or opts.merge2):
            
            for bl in DBInfo[ elemMax ]['blast']:
                if int(bl[8]) <= int(bl[9]):
                    plus.append( bl )
                else:
                    minus.append( bl ) 
            if (opts.merge or opts.merge2):
                new=[]
                i = 0
                j = 0
                minus.sort(lambda x, y: cmp(int(x[9]), int(y[9])))
                while j < len( minus):
                    while i < len( plus )-1 and j < len( minus):
                        if int( minus[j][9]) > int(plus[i][8]):
                            if int( minus[j][9]) > int(plus[i+1][8]):
                                new.append( plus[i])
                                new.append( minus[j])
                                i+=1
                                j+=1
                            else:
                                new.append( plus[i])
                                i+=1
                        else:
                            new.append( minus[j])
                            j+=1
                            
            else:           
                minus.reverse()
                        
                
        sbjctSeq = doGolden( DBInfo[ elemMax ]['db'], elemMax,  format='raw' )
        if not sbjctSeq:
            print >>sys.stderr, ('Golden error: %s %s' % (DBInfo[ elemMax ]['db'],elemMax) )
            continue
        sbjctLen = len( sbjctSeq )
        #sbjctLen = 2900
        if opts.picture_prefix and (not opts.merge  and not opts.merge2):
            if not opts.strand_orientation:
                plot( plot_name_prefix +'.png', sbjctLen, elemMax, DBInfo[elemMax ]['blast'], sizeX = 700, fontsize = 8, typePlot='png')
            else:
                plot( plot_name_prefix +'_plus.png', sbjctLen, elemMax, plus, sizeX = 700, fontsize = 8, typePlot='png')
                plot( plot_name_prefix +'_minus.png', sbjctLen, elemMax, minus, sizeX = 700, fontsize = 8, typePlot='png')
        
        if opts.picture_prefix and opts.merge:
            #if not opts.galaxy_restrict:
            plot_name_prefix += '_merge'
            
            if not opts.strand_orientation:
                plotMerge( plot_name_prefix +'.png', sbjctLen, elemMax, DBInfo[ elemMax ]['blast'], sizeX = 700, fontsize = 8, typePlot='png')
            else:
                plotMerge( plot_name_prefix +'_plus.png', sbjctLen, elemMax, plus, sizeX = 700, fontsize = 8, typePlot='png')
                plotMerge( plot_name_prefix + '_minus.png', sbjctLen, elemMax, minus, sizeX = 700, fontsize = 8, typePlot='png')

        if opts.picture_prefix and opts.merge2:
            #if not opts.galaxy_restrict:
            plot_name_prefix += '_noOverlap'
            if not opts.strand_orientation:
                plotMerge2( plot_name_prefix +'.png', sbjctLen, elemMax, new, sizeX = 700, fontsize = 8, typePlot='png')
            else:
                plotMerge2plus( plot_name_prefix +'_plus.png', sbjctLen, elemMax, plus, sizeX = 700, fontsize = 8, typePlot='png')
                plotMerge2minus( plot_name_prefix + '_minus.png', sbjctLen, elemMax, minus, sizeX = 700, fontsize = 8, typePlot='png')

