Ejemplo n.º 1
0
def HeatTrigger2(html_file_2):
    log.debug("Triggering stats report...")
    try:
        fh = file(html_file_2, 'r')
    except IOError:
        e = sys.exc_info()
        tb = traceback.format_exception(*e)
        log.error(
            "No such file or directory!\n\033[36m##################################\n\033[93m%s\033[32m%s\033[93m%s\033[36m##################################\n\033[0m" % (
                tb[0], tb[1], tb[2]))
        sys.exit(1)
    subject = fh.read()
    fh.close()
    Idpattern = '(?<=\<div id=")(.*?)"'
    try:
        FileId = re.search(Idpattern, subject).group(1)
    except:
        e = sys.exc_info()
        tb = traceback.format_exception(*e)
        log.error(
            "Can't find ID in html file!\n\033[36m##################################\n\033[93m%s\033[32m%s\033[93m%s\033[36m##################################\n\033[0m" % (
                tb[0], tb[1], tb[2]))
        sys.exit(1)
    FixLine = "<script type = 'text/javascript'>var graph = document.getElementById('%(FileId)s');graph.on('plotly_hover', function(data){var divname = data.points.map(function(d){return (d.data.name)});if(divname == 'ReadsPerHour' || divname == 'BasesPerHour' || divname == 'MeanReadLengthPerHour'){if(data.xvals){Plotly.Fx.hover(graph, {xval:data.xvals[0] }, ['x5y', 'x5y3','x5y5']);}}});</script>" % locals()
    SubLine = FixLine + '</body></html>'
    pattern = re.compile('</body></html>')
    result = pattern.sub(SubLine, subject)
    f_out = file(html_file_2, 'w')
    f_out.write(result)
    f_out.close()
    try:
        shutil.move(html_file_2, os.path.join(work_dir, out_dir))
    except shutil.Error:
        log.error("Unable to move %s" % html_file_2)
Ejemplo n.º 2
0
 def bwaal():
     log.warning(
         '[Alignment][bwaal] - Currently not available for Windows, sorry.')
     sys.exit(1)
     bwa_ext_dir = os.path.join(work_dir, out_dir, 'bwa_alignments')
     if not os.path.exists(bwa_ext_dir):
         os.makedirs(bwa_ext_dir)
     bam_bwa_file = os.path.join(bwa_ext_dir, (prefix + '.bam'))
     if not os.path.exists(bam_bwa_file):
         if stats_trigger in ['y', 'yes']:
             log.debug('[Alignment][bwaal] - bwa mem -x ont2d -t %s %s %s' %
                       (th, ref, fast_Q_file))
             bwaline = subprocess.Popen([
                 'lib\BWA\bwa.exe', 'mem', '-x', 'ont2d', '-t',
                 str(th),
                 str(ref),
                 str(fast_Q_file)
             ],
                                        stdout=subprocess.PIPE)
             PairDict = sam_parser(bwaline, bwa_ext_dir)
         else:
             sam_bwa_file = os.path.join(bwa_ext_dir, (prefix + '.sam'))
             log.debug(
                 '[Alignment][bwaal] - bwa mem -x ont2d -t %s %s %s > %s' %
                 (th, ref, fast_Q_file, sam_bwa_file))
             bwaline = subprocess.Popen([
                 'bwa', 'mem', '-x', 'ont2d', '-t',
                 str(th),
                 str(ref),
                 str(fast_Q_file), '>',
                 str(sam_bwa_file)
             ],
                                        stdout=subprocess.PIPE)
             outputfilebam = os.path.join(bwa_ext_dir,
                                          (prefix + '.tmp.bam'))
             log.debug(
                 '[Alignment][bwaal] - samtools view -Sb -@ %s %s -o %s' %
                 (th, sam_bwa_file, outputfilebam))
             pysam.view("-Sb",
                        "-@%s" % str(th),
                        sam_bwa_file,
                        "-o%s" % outputfilebam,
                        catch_stdout=False)
             os.remove(sam_bwa_file)
             pysam.sort(outputfilebam,
                        "-o%s" % bam_bwa_file,
                        catch_stdout=False)
             log.debug('[Alignment][bwaal] - samtools index %s -@%s' %
                       (bam_bwa_file, str(th)))
             pysam.index(bam_bwa_file, "-@%s" % str(th), catch_stdout=False)
     else:
         log.warning('[Alignment][bwaal] - file %s already exists!' %
                     bam_bwa_file)
     try:
         shutil.move(bwa_ext_dir, os.path.join(work_dir, out_dir))
     except shutil.Error:
         log.error("Unable to move %s" % bwa_ext_dir)
Ejemplo n.º 3
0
def ngmlral():
    ngmlr_ext_dir = os.path.join(work_dir, 'ngmlr_alignments')
    if not os.path.exists(ngmlr_ext_dir):
        os.makedirs(ngmlr_ext_dir)
    bam_ngmlr_file = os.path.join(ngmlr_ext_dir, (prefix + '.bam'))
    if not os.path.exists(bam_ngmlr_file):
        if stats_trigger in ['y', 'yes']:
            log.debug('[Alignment][ngmlral] - ngmlr -t %s -r %s -q %s -x ont' %
                      (th, ref, fast_Q_file))
            ngmlrline = subprocess.Popen([
                'ngmlr', '-t',
                str(th), '-r',
                str(ref), '-q',
                str(fast_Q_file), '-x ont'
            ],
                                         stdout=subprocess.PIPE)
            PairDict = sam_parser(ngmlrline, ngmlr_ext_dir)
        else:
            sam_nglmr_file = os.path.join(ngmlr_ext_dir, (prefix + '.sam'))
            log.debug(
                '[Alignment][ngmlral] - ngmlr -t %s -r %s -q %s -o %s -x ont' %
                (th, ref, fast_Q_file, sam_ngmlr_file))
            ngmlrline = subprocess.Popen([
                'ngmlr', '-t',
                str(th), '-r',
                str(ref), '-q',
                str(fast_Q_file), '-o',
                str(sam_ngmlr_file), '-x ont'
            ],
                                         stdout=subprocess.PIPE).wait()
            outputfilebam = os.path.join(ngmlr_ext_dir, (prefix + '.tmp.bam'))
            log.debug(
                '[Alignment][ngmlral] - samtools view -Sb -@ %s %s -o %s' %
                (th, sam_nglmr_file, outputfilebam))
            pysam.view("-Sb",
                       "-@%s" % str(th),
                       sam_nglmr_file,
                       "-o%s" % outputfilebam,
                       catch_stdout=False)
            os.remove(sam_nglmr_file)
            pysam.sort(outputfilebam,
                       "-o%s" % bam_ngmlr_file,
                       catch_stdout=False)
            log.debug('[Alignment][ngmlral] - samtools index %s -@%s' %
                      (bam_ngmlr_file, str(th)))
            pysam.index(bam_ngmlr_file, "-@%s" % str(th), catch_stdout=False)
            os.remove(outputfilebam)
    else:
        log.warning('[Alignment][ngmlral] - file %s already exists!' %
                    bam_ngmlr_file)
    try:
        shutil.move(ngmlr_ext_dir, os.path.join(work_dir, out_dir))
    except shutil.Error:
        log.error("Unable to move %s" % ngmlr_ext_dir)
Ejemplo n.º 4
0
def HeatTrigger(html_file):
    log.debug("Triggering heatmaps...")
    try:
        fh = file(html_file, 'r')
    except IOError:
        e = sys.exc_info()
        tb = traceback.format_exception(*e)
        log.error(
            "No such file or directory!\n\033[36m##################################\n\033[93m%s\033[32m%s\033[93m%s\033[36m##################################\n\033[0m" % (
                tb[0], tb[1], tb[2]))
        sys.exit(1)
    subject = fh.read()
    fh.close()
    Idpattern = '(?<=\<div id=")(.*?)"'
    try:
        FileId = re.search(Idpattern, subject).group(1)
    except:
        e = sys.exc_info()
        tb = traceback.format_exception(*e)
        log.error(
            "Can't find ID in html file!\n\033[36m##################################\n\033[93m%s\033[32m%s\033[93m%s\033[36m##################################\n\033[0m" % (
                tb[0], tb[1], tb[2]))
        sys.exit(1)
    if summary_flag == False:
        FixLine = "<script type='text/javascript'>var gd = document.getElementById('%(FileId)s');gd.on('plotly_click', function(data){var infotext = data.points.map(function(d){return (d.text)});var divname = data.points.map(function(d){return (d.data.name)}); var chan = data.points.map(function(d){return (d.text.split(' ')[1].replace('<br', ''))}); if(divname == 'ReadNHeat' || divname == 'BaseNHeat'){function range(start, stop, step){var a=[start], b=start;while(b<stop){b+=step;a.push(b)}return a;};var editindeces=range(10*(chan-1), 10*chan, 1); editindeces.pop(); var numeroPerguntas = 5122; var anyBoxesChecked = new Array(numeroPerguntas).fill(false); for (i in editindeces) { idx = parseInt(editindeces[i]); anyBoxesChecked[idx]=true}; var Newtitle = {title: infotext.toString()}; if (gd.data[0].visible === true) {var hv=[true, false]} else {var hv=[false, true]}; var exit = hv.concat(anyBoxesChecked); var update = {'visible': exit}; Plotly.update(gd ,update, Newtitle);};});gd.on('plotly_hover', function(data){var divname = data.points.map(function(d){return (d.data.name)});if(divname == 'Pore1' || divname == 'Pore2' || divname == 'Pore3' || divname == 'Pore4'){if(data.xvals){Plotly.Fx.hover(gd, {xval:data.xvals[0] }, ['x3y2', 'x3y3']);}}});</script>" % locals()  
    else:
        FixLine = "<script type='text/javascript'>var gd = document.getElementById('%(FileId)s');gd.on('plotly_click', function(data){var infotext = data.points.map(function(d){return (d.text)});var divname = data.points.map(function(d){return (d.data.name)}); var chan = data.points.map(function(d){return (d.text.split(' ')[1].replace('<br', ''))}); if(divname == 'ReadNHeat' || divname == 'BaseNHeat'){function range(start, stop, step){var a=[start], b=start;while(b<stop){b+=step;a.push(b)}return a;};var editindeces=range(4*(chan-1), 4*chan, 1); editindeces.pop(); var numeroPerguntas = 2050; var anyBoxesChecked = new Array(numeroPerguntas).fill(false); for (i in editindeces) { idx = parseInt(editindeces[i]); anyBoxesChecked[idx]=true}; var Newtitle = {title: infotext.toString()}; if (gd.data[0].visible === true) {var hv=[true, false]} else {var hv=[false, true]}; var exit = hv.concat(anyBoxesChecked); var update = {'visible': exit}; Plotly.update(gd ,update, Newtitle);};});gd.on('plotly_hover', function(data){var divname = data.points.map(function(d){return (d.data.name)});if(divname == 'Channel'){if(data.xvals){Plotly.Fx.hover(gd, {xval:data.xvals[0] }, ['x3y2', 'x3y3']);}}});</script>" % locals()
    SubLine = FixLine + '</body></html>'
    pattern = re.compile('</body></html>')
    result = pattern.sub(SubLine, subject)
    f_out = file(html_file, 'w')
    f_out.write(result)
    f_out.close()            
    try:
        shutil.move(html_file, os.path.join(work_dir, out_dir))
    except shutil.Error:
        log.error("Unable to move %s" % html_file)
Ejemplo n.º 5
0
def minimap2al():
    mm_ext_dir = os.path.join(work_dir, 'minimap2_alignments')
    if not os.path.exists(mm_ext_dir):
        os.makedirs(mm_ext_dir)
    mmi_ref = os.path.splitext(ref)[0] + '.mmi'
    if not os.path.exists(mmi_ref):
        log.debug(
            '[Alignment][minimap2al] - minimap2 -x map-ont -t %s -d %s %s' %
            (th, mmi_ref, ref))
        miniline = ('minimap2 -x map-ont -t', str(th), '-d', str(mmi_ref),
                    str(ref))
        minrun = ' '.join(miniline)
        subprocess.Popen(minrun, stdout=subprocess.PIPE, shell=True).wait()
    bam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.bam'))
    if not os.path.exists(bam_mm2_file):
        if stats_trigger in ['y', 'yes']:
            log.debug(
                '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s'
                %
                (th,
                 str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' +
                     str(prefix)), str(mmi_ref), str(fast_Q_file)))
            minimap2line = subprocess.Popen([
                'minimap2', '-ax', 'map-ont', '--MD', '-L', '-t',
                str(th), '-R',
                str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' +
                    str(prefix)),
                str(mmi_ref),
                str(fast_Q_file)
            ],
                                            stdout=subprocess.PIPE)
            PairDict = sam_parser(minimap2line, mm_ext_dir)
        else:
            sam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.sam'))
            log.debug(
                '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s > %s'
                %
                (th,
                 str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' +
                     str(prefix)), str(mmi_ref), str(fast_Q_file),
                 str(sam_mm2_file)))
            with open(sam_mm2_file, "w") as outfile:
                minimap2line = subprocess.Popen([
                    'minimap2', '-ax', 'map-ont', '--MD', '-L', '-t',
                    str(th), '-R',
                    str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:'
                        + str(prefix)),
                    str(mmi_ref),
                    str(fast_Q_file)
                ],
                                                stdout=outfile).wait()
            outputfilebam = os.path.join(mm_ext_dir, (prefix + '.tmp.bam'))
            log.debug(
                '[Alignment][minimap2al] - samtools view -Sb -@ %s %s -o %s' %
                (th, sam_mm2_file, outputfilebam))
            pysam.view("-Sb",
                       "-@%s" % str(th),
                       sam_mm2_file,
                       "-o%s" % outputfilebam,
                       catch_stdout=False)
            os.remove(sam_mm2_file)
            pysam.sort(outputfilebam,
                       "-o%s" % bam_mm2_file,
                       catch_stdout=False)
            log.debug('[Alignment][minimap2al] - samtools index %s -@%s' %
                      (bam_mm2_file, str(th)))
            pysam.index(bam_mm2_file, "-@%s" % str(th), catch_stdout=False)
            os.remove(outputfilebam)
    else:
        log.warning('[Alignment][minimap2al] - file %s already exists!' %
                    bam_mm2_file)
    try:
        shutil.move(mm_ext_dir, os.path.join(work_dir, out_dir))
    except shutil.Error:
        log.error("Unable to move %s" % mm_ext_dir)
Ejemplo n.º 6
0
 def __call__(self):
     variants_dict = {k: [] for k in ['M', 'I', 'D']}
     RCounter = []
     SeenNames = set()
     pos_dict = {k: {} for k in arange(0, 250000000, 1000000)}
     unmapped = []
     file_out = 'tmp.' + str(self.file_number) + '.sam'
     file = open(file_out, mode='wb')
     for hl in self.header_lines:
         if hl != '':
             file.write(hl)
     for ll in self.sampled_data:
         if ll.strip() is not '':
             file.write(ll)
             pair = ll.strip().split()
             flag = pair[1]
             if int(flag) not in [272, 256]:
                 a_cigar = pair[5]
                 map_quality = int(pair[4])
                 chrome = str(pair[2])
                 read_name = str(pair[0])
                 read_len = len(pair[9])
                 most_left = int(pair[3])
                 if read_name not in SeenNames:
                     SeenNames.add(read_name)
                     if chrome in map(str, range(1, 23)) + map(
                             lambda x: 'chr' + str(x), range(1, 23)):
                         kkk = pos_dict.get(
                             most_left, pos_dict[min(
                                 pos_dict.keys(),
                                 key=lambda k: abs(k - most_left))])
                         kkk[chrome] = kkk.get(chrome, []) + [read_len]
                     SClip = 0
                     if a_cigar != '*':
                         try:
                             SClip = int(
                                 re.findall("[0-9]*S", a_cigar)[0][:-1])
                         except:
                             log.debug(
                                 '[Alignment][error_calc] - No soft clipping for this read...'
                             )
                             continue
                         a_tags = dict([(x[:2], x.split(':')[-1])
                                        for x in pair[11:]])
                         mdgsub = re.sub("([\\^]*[ACGT]+)[0]*", " \\1 ",
                                         a_tags['MD']).split()
                         MissMatchesDelsPos = miss_match_founder(
                             mdgsub, SClip)
                         Insertions = parseCIGAR(a_cigar, SClip)
                         for p, e, s in MissMatchesDelsPos:
                             variants_dict[str(e)].append((p, s))
                         variants_dict['I'].extend(list(Insertions))
                         RCounter.append([read_name, read_len, map_quality])
                     else:
                         unmapped.append([read_name, read_len, map_quality])
     file.close()
     outputfilebam = 'tmp.' + str(self.file_number) + '.bam'
     log.debug('[Alignment][error_calc] - samtools view -Sb %s -o %s' %
               (file_out, outputfilebam))
     pysam.view("-Sb", file_out, "-o%s" % outputfilebam, catch_stdout=False)
     os.remove(file_out)
     outputfilebamsorted = os.path.join(out_dir,
                                        str(self.file_number) + '.bam')
     log.debug('[Alignment][error_calc] - samtools sort %s -o %s' %
               (outputfilebam, outputfilebamsorted))
     pysam.sort(outputfilebam,
                "-o%s" % outputfilebamsorted,
                catch_stdout=False)
     os.remove(outputfilebam)
     bins = [
         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
         200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000,
         5000, 6000, 7000, 8000, 9000, 10000, 15000, 20000, 25000, 30000,
         35000, 40000, 45000, 50000, 100000, 200000, 300000, 400000, 500000,
         750000, 1000000000
     ]
     d_lengths = {}
     d_unm = {}
     r_lengths = map(lambda x: int(x[1]), RCounter)
     if len(unmapped) > 0:
         unm_lengths = map(int, zip(*unmapped)[1])
         binned_unm_data = digitize(unm_lengths, bins)
         for l in binned_unm_data:
             d_unm[bins[l]] = d_unm.get(bins[l], 0) + 1
     else:
         d_unm = {k: 0 for k in bins}
     binned_lengths_data = digitize(r_lengths, bins)
     mapped_frac_size = []
     for l in binned_lengths_data:
         d_lengths[bins[l]] = d_lengths.get(
             bins[l], 0
         ) + 1  ## Conto le read di lunghezza bins[l] e le storo in un dizionario con la lunghezza come chiave
     for le in sorted(set(d_lengths.keys() + d_unm.keys())):
         mapped_frac_size.append([
             le, d_lengths.get(le, 0),
             d_unm.get(le, 0)
         ])  # creo righe con lunghezza e conte per mapped e unmapped
     d_lengths = dict(
         zip(sorted(d_lengths.keys()), [
             sum(zip(*sorted(d_lengths.iteritems(), reverse=True))[1][:i])
             for i in range(
                 1,
                 len(zip(*sorted(d_lengths.iteritems(), reverse=True))[1]) +
                 1)
         ][::-1]))
     OutDict = {k: {} for k in ['M', 'I', 'D']}
     for ev in ['I', 'M', 'D']:
         d_somme = {}
         binned_data = digitize(map(int, (zip(*variants_dict[str(ev)])[0])),
                                bins)
         for index, vi in enumerate(binned_data):
             d_somme[bins[vi]] = d_somme.get(bins[vi], 0) + int(
                 variants_dict[str(ev)][index][1])
         bin_sizes = [sort(d_somme.keys())[0]] + list(
             diff__(sort(d_somme.keys())))
         for i, kv in enumerate(sorted(d_somme.iteritems())):
             kkk = OutDict.get(ev)
             kkk[str(kv[0])] = kkk.get(str(kv[0]), []) + [
                 int(bin_sizes[i]),
                 int(
                     d_lengths.get(
                         kv[0], d_lengths[min(
                             d_lengths.keys(),
                             key=lambda k: abs(k - int(kv[0])))])),
                 int(kv[1])
             ]
     out_data = [pos_dict, OutDict, mapped_frac_size, outputfilebamsorted]
     return (out_data)
Ejemplo n.º 7
0
 def minimap2al():
     mm_ext_dir = os.path.join(work_dir, out_dir, 'minimap2_alignments')
     if not os.path.exists(mm_ext_dir):
         os.makedirs(mm_ext_dir)
     mmi_ref = os.path.splitext(ref)[0] + '.mmi'
     if not os.path.exists(mmi_ref):
         log.debug(
             '[Alignment][minimap2al] - minimap2 -x map-ont -t %s -d %s %s'
             % (th, mmi_ref, ref))
         miniline = (os.path.abspath(os.path.dirname(__file__)) +
                     '\minimap2\minimap2.exe -x map-ont -t', str(th), '-d',
                     str(mmi_ref), str(ref))
         minrun = ' '.join(miniline)
         subprocess.Popen(minrun, stdout=subprocess.PIPE, shell=True).wait()
     bam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.bam'))
     if not os.path.exists(bam_mm2_file):
         if stats_trigger in ['y', 'yes']:
             log.debug(
                 '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s'
                 %
                 (th,
                  str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:'
                      + str(prefix)), str(mmi_ref), str(fast_Q_file)))
             minimap2line = subprocess.Popen([
                 os.path.abspath(os.path.dirname(__file__)) +
                 '\minimap2\minimap2.exe', '-ax', 'map-ont', '--MD', '-L',
                 '-t',
                 str(th), '-R',
                 str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:'
                     + str(prefix)),
                 str(mmi_ref),
                 str(fast_Q_file)
             ],
                                             stdout=subprocess.PIPE)
             PairDict = sam_parser(minimap2line, mm_ext_dir)
         else:
             sam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.sam'))
             log.debug(
                 '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s > %s'
                 %
                 (th,
                  str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:'
                      + str(prefix)), str(mmi_ref), str(fast_Q_file),
                  str(sam_mm2_file)))
             with open(sam_mm2_file, "w") as out:
                 minimap2line = subprocess.Popen([
                     os.path.abspath(os.path.dirname(__file__)) +
                     '\minimap2\minimap2.exe', '-ax', 'map-ont', '--MD',
                     '-L', '-t',
                     str(th), '-R',
                     str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:'
                         + str(prefix)),
                     str(mmi_ref),
                     str(fast_Q_file)
                 ],
                                                 stdout=out).communicate()
             outputfilebam = os.path.join(mm_ext_dir, (prefix + '.tmp.bam'))
             log.debug(
                 '[Alignment][minimap2al] - samtools view -Sb -@ %s %s -o %s'
                 % (th, sam_mm2_file, outputfilebam))
             samviewline = subprocess.Popen(
                 [
                     os.path.abspath(os.path.dirname(__file__)) +
                     '\SAMtools\samtools.exe', 'view', '-Sb', '-@',
                     str(th), sam_mm2_file, '-o', outputfilebam
                 ],
                 stdout=subprocess.PIPE).communicate()
             os.remove(sam_mm2_file)
             samsortline = subprocess.Popen(
                 [
                     os.path.abspath(os.path.dirname(__file__)) +
                     '\SAMtools\samtools.exe', 'sort', '-@',
                     str(th), outputfilebam, '-o', bam_mm2_file
                 ],
                 stdout=subprocess.PIPE).communicate()
             log.debug('[Alignment][minimap2al] - samtools index %s -@%s' %
                       (bam_mm2_file, str(th)))
             samindexline = subprocess.Popen(
                 [
                     os.path.abspath(os.path.dirname(__file__)) +
                     '\SAMtools\samtools.exe', 'index', bam_mm2_file
                 ],
                 stdout=subprocess.PIPE).communicate()
             os.remove(outputfilebam)
     else:
         log.warning('[Alignment][minimap2al] - file %s already exists!' %
                     bam_mm2_file)
Ejemplo n.º 8
0
 def sam_parser(bwastdout, out_dir):
     log.info('Error estimation...')
     bins = [
         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
         200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000,
         5000, 6000, 7000, 8000, 9000, 10000, 15000, 20000, 25000, 30000,
         35000, 40000, 45000, 50000, 100000, 200000, 300000, 400000, 500000,
         750000, 1000000000
     ]
     mapped_frac_size = {k: [0, 0] for k in bins}
     pos_dict = {k: {} for k in arange(0, 250000000, 1000000)}
     OutDict = {k: {} for k in ['M', 'I', 'D']}
     header_list = []
     proc_lists = {k: [] for k in range(th)}
     chr_cov_dict = {}
     file_list = []
     fill_val = 0
     Flag = True
     while Flag:
         line = bwastdout.stdout.readline()
         Threshold = 50000
         while len(proc_lists[th - 1]) <= Threshold:
             proc_check_first = map(lambda x: len(proc_lists[x]),
                                    range(th - 1))
             line_counter = 0
             if line.strip() == '':
                 Flag = False
                 break
             if line[0] == '@':
                 header_list.append(line)
                 line = bwastdout.stdout.readline()
             else:
                 while line_counter < len(proc_lists):
                     proc_lists[line_counter].append(line)
                     line_counter += 1
                     line = bwastdout.stdout.readline()
                 line_counter = 0
                 line = bwastdout.stdout.readline()
             proc_check_second = map(lambda x: len(proc_lists[x]),
                                     range(th - 1))
             if all(v == 0 for v in proc_check_second) == False:
                 if proc_check_second == proc_check_first:
                     time.sleep(5)
                     proc_check_second = map(lambda x: len(proc_lists[x]),
                                             range(th - 1))
                     if proc_check_second == proc_check_first:
                         break
         fill_list = (
             fill_val, fill_val + (th - 1) -
             map(lambda x: len(proc_lists[x]), range(th - 1)).count(0))
         fill_val = fill_val + (th - 1) - map(lambda x: len(proc_lists[x]),
                                              range(th - 1)).count(0)
         res_obj = error_wrap(proc_lists, header_list, fill_list)
         for ro in res_obj:
             p_d, OD, m_f_s, fname = ro
             file_list.append(fname)
             for k, v in p_d.iteritems():
                 if v is not {}:
                     for ch, l in v.iteritems():
                         pos_dict[k][ch] = pos_dict[k].get(ch, []) + l
             for var_k, cnt in OD.iteritems():
                 if cnt is not {}:
                     for k, v in cnt.iteritems():
                         outdictget = OutDict.get(var_k)
                         if outdictget.get(int(k)) == None:
                             outdictget[int(k)] = v
                         else:
                             outdictget[int(k)] = [
                                 outdictget.get(int(k))[0],
                                 int(outdictget.get(int(k))[1]) + int(v[1]),
                                 int(outdictget.get(int(k))[2]) + int(v[2])
                             ]
             for mfs in m_f_s:
                 mapped_frac_size[mfs[0]] = map(
                     sum, zip(mapped_frac_size[mfs[0]], mfs[1:]))
             proc_lists = {k: [] for k in range(th)}
     for ev in ['I', 'M', 'D']:
         OutDict[ev] = dict(
             zip(
                 sorted(OutDict[ev].keys()),
                 zip([sorted(OutDict[ev].keys())[0]] +
                     list(diff(sorted(OutDict[ev].keys()))), [
                         max(
                             zip(*map(lambda x: OutDict[ev].get(x),
                                      sorted(OutDict[ev].keys())))[1])
                         if x[0] < zip(*map(lambda x: OutDict[ev].get(
                             x), sorted(OutDict[ev].keys())))[1].index(
                                 max(
                                     zip(*map(lambda x: OutDict[ev].get(x),
                                              sorted(OutDict[ev].keys())))
                                     [1])) else x[1]
                         for x in enumerate(
                             zip(*map(lambda x: OutDict[ev].get(x),
                                      sorted(OutDict[ev].keys())))[1])
                     ],
                     zip(*map(lambda x: OutDict[ev].get(x),
                              sorted(OutDict[ev].keys())))[2])))
     for k, v in pos_dict.iteritems():
         for k1, v1 in v.iteritems():
             chr_cov_dict[str(k1)] = chr_cov_dict.get(
                 str(k1), []) + [(k, sum(v1) / 1000000.)]
     mapped_frac_size = [[
         k[0], k[1][0], k[1][1],
         round(1 - float(k[1][1]) / (float(k[1][0]) + float(k[1][1])), 3)
     ] for k in sorted(mapped_frac_size.iteritems())
                         if k[1][0] != 0 or k[1][1] != 0]
     sorted_unmapfraqseq = zip(*mapped_frac_size)[3]
     for ev in OutDict.keys():
         for k, v in sorted(OutDict[ev].iteritems()):
             OutDict[ev][k] = round(
                 float(v[2]) / (int(v[0]) * float(v[1])), 4)
     plot_stats(OutDict, sorted_unmapfraqseq, mapped_frac_size,
                chr_cov_dict, out_dir)
     finalfile = os.path.join(tmpdir, (prefix + '.bam'))
     bamsfile = os.path.join(out_dir, 'to_merge.txt')
     file = open(bamsfile, 'w')
     for line in file_list:
         file.write(os.path.join(work_dir, line) + '\n')
     file.close()
     bammergeline = subprocess.Popen([
         os.path.abspath(os.path.dirname(__file__)) +
         '\SAMtools\samtools.exe', 'merge', '-cp', '-@',
         str(th), '-b', bamsfile, finalfile
     ],
                                     stdout=subprocess.PIPE).communicate()
     for b in file_list:
         os.remove(b)
     os.remove(bamsfile)
     log.debug('[Alignment][minimap2al] - samtools index %s' % (finalfile))
     samindexline = subprocess.Popen([
         os.path.abspath(os.path.dirname(__file__)) +
         '\SAMtools\samtools.exe', 'index', finalfile
     ],
                                     stdout=subprocess.PIPE).communicate()
     for f in os.listdir(tmpdir):
         try:
             shutil.move(os.path.join(tmpdir, f),
                         os.path.join(work_dir, out_dir))
         except shutil.Error:
             log.error("Unable to move %s" % tmpdir, f)
     shutil.rmtree(tmpdir, ignore_errors=True)
Ejemplo n.º 9
0
def FastStats(ChannelOut, GCTable = None):
    log.debug('Preparing data for summary...')
    HoursDict = {str(il): [0, 0, 0] for il in range(0, 48)}
    SmallDict = dict([(k, v) for k, v in ChannelOut.items() if len(v) > 0])
    FailSum = sum(map(lambda x: int(SmallDict[str(x)][2]), SmallDict.keys()))
    if GCTable == None:
        for c in SmallDict:
            Sd = dict([(k, v) for k, v in ChannelOut[str(c)][4].items() if len(v) > 0])
            for k, v in Sd.items(): #### itera sui pori
                for o, r in v[0].items(): #### itera sulle ore
                    HoursDict[o][0] += r[0]
                    HoursDict[o][1] += r[1]
    else:            
        for c in SmallDict:
            Sd = dict([(k, v) for k, v in ChannelOut[str(c)][4].items() if len(v) > 0])
            for o, r in Sd.items(): #### itera sulle ore
                HoursDict[o][0] += r[0]
                HoursDict[o][1] += r[1]
    PassSum = sum(map(lambda x: int(HoursDict[x][0]), HoursDict.keys()))
    for x in sort([int(k) for k, v in HoursDict.items() if v != [0, 0, 0]]):
        HoursDict[str(x)][2] = str(round(float(HoursDict[str(x)][1]) / float(HoursDict[str(x)][0])))
    MaxMinvec = []
    LengthsVec = [0]
    QualsVec = [0]
    if GCTable == None:
        GCVec = [0]
    else:
        GCVec = GCTable
    for c in ChannelOut:
        try:
            Bait = map(int, zip(*ChannelOut[c][3])[1])
            LengthsVec.extend(Bait)
            QualsVec.extend(map(float, zip(*ChannelOut[c][3])[2]))
            if GCTable == None:
                GCVec.extend(map(float, zip(*ChannelOut[c][3])[4]))
            MaxMinvec.append(max(Bait))
            MaxMinvec.append(min(Bait))            
        except: pass    
    SmallestRead = min(filter(lambda a: a > 0, MaxMinvec))
    GCVec = filter(lambda a: a > 0, GCVec)  
    BiggestRead = max(MaxMinvec)
    MeanReadLength = round(mean(map(float, map(lambda x: HoursDict[str(x)][2],
                                               sort([int(k) for k, v in HoursDict.items() if v != [0, 0]])))), 1)
    TotalThroughput = sum(map(float, map(lambda x: HoursDict[str(x)][1],
                                                   sort([int(k) for k, v in HoursDict.items() if v != [0, 0]]))))                                               
    sortingindex = [y[0] for y in sorted(enumerate(map(int, HoursDict.keys())), key=lambda z: z[1])]
    SortKeys = [HoursDict.keys()[y] for y in sortingindex]
    HoursDict = [HoursDict[y] for y in SortKeys]
    log.debug('Plotting stats summary...')
    fig = plotly.tools.make_subplots(rows=16, cols=2, shared_xaxes=False,
                                     specs=[[{'rowspan': 3}, {}], [None, None], [None, None], [{'rowspan': 3}, {}],
                                            [None, None], [None, None], [{'rowspan': 3}, {}],
                                            [None, None], [None, None], [None, None], [None, None],
                                            [{'colspan': 2}, {}], [None, None], [None, None], [{'colspan': 2}, {}],
                                            [None, None]],
                                     vertical_spacing=0.001, print_grid=False)
    log.debug('Plotting pie chart...')
    PieChartReads = go.Pie(values=[round(((float(PassSum) / (FailSum + PassSum))) * 100, 2),
                                   round(((float(FailSum) / (FailSum + PassSum))) * 100, 2)],
                           labels=[
                               'Pass',
                               'Fail',
                           ], marker={'colors': ['rgba(25, 133, 37, 1.0)',
                                                 'rgba(133, 37, 25, 1.0)']},
                           domain={'x': [.70, .82], 'y': [.82, 1]},
                           name='Reads',
                           hoverinfo='labels+percent+name',
                           hole=.4,
                           textfont={'color': 'rgb(255,255,255)', 'size': 12},
                           type='pie',
                           visible=True,
                           showlegend=True)

    trace = go.Scatter(
        x=map(int, SortKeys),
        y=zip(*HoursDict)[0],
        name='ReadsPerHour',
        line=dict(
            color=('rgb(50,136,189)')),
        showlegend=False,
        hoverinfo='y',
        visible=True,
        xaxis='x5',
        yaxis='y1'
    )
    trace1 = go.Scatter(
        x=map(int, SortKeys),
        y=zip(*HoursDict)[1],
        name='BasesPerHour',
        line=dict(
            color=('rgb(253,174,97)')),
        showlegend=False,
        hoverinfo='y',
        visible=True,
        xaxis='x5',
        yaxis='y3'
    )
    trace2 = go.Scatter(
        x=map(int, SortKeys),
        y=map(float, zip(*HoursDict)[2]),
        name='MeanReadLengthPerHour',
        line=dict(
            color=('rgb(21,112,76)')),
        showlegend=False,
        visible=True,
        hoverinfo='y',
        xaxis='x5',
        yaxis='y5'
    )

    val = [[FailSum + PassSum], [PassSum], [FailSum], [TotalThroughput], [MeanReadLength], [SmallestRead], [BiggestRead]]
    log.debug('Plotting table...')
    table = go.Table(
        type='table',
        columnorder=[1, 2, 3, 4, 5, 6, 7],
        columnwidth=[5] * 7,
        visible=True,
        domain={'x': [0, 1], 'y': [0, .12]},
        header=dict(values=[['<b>READS TOTAL <br> NUMBER</b>'], ['<b>PASS</b>'], ['<b>FAIL</b>'], ['<b>TOTAL BP</b>'],
                            ['<b>MEAN READ <br>LENGTH (bp)</b>'], ['<b>SHORTEST READ (bp)</b>'],
                            ['<b>LONGEST READ (bp)</b>']],
                    line=dict(color='#7D7F80'),
                    align=['center'] * 7,
                    fill=dict(color='#00083E'),  # 00083e
                    font=dict(color='white', size=12), height=30),
        cells=dict(values=val, line=dict(color='#7D7F80'),
                   align=['center'] * 7,
                   fill=dict(color='#EDEDEE'),  # EDFAFF
                   font=dict(color='#506784', size=12), height=25))

    log.debug('Plotting length histogram...')
    LengthHist = go.Histogram(x=LengthsVec, nbinsx=100, nbinsy=10, opacity=0.75, showlegend=False, histnorm='percent',
                              name='% Reads', marker=dict(color='rgb(0, 200, 200)',
                                                          line=dict(width=0.5, color='rgba(255, 255, 255, 0.3)')),
                              xaxis='x4', yaxis='y4', hoverinfo='y+x+name')

    log.debug('Plotting quality histogram...')
    QualHist = go.Histogram(x=QualsVec, nbinsx=9, xbins=dict(start=6, end=14, size=1), nbinsy=10, opacity=0.75,
                            showlegend=False, histnorm='percent', name='% Reads',
                            error_y={'visible': True, 'thickness': 2, 'color': 'rgb(133, 37, 25)'},
                            marker=dict(color='rgb(0, 0, 255)', line=dict(width=0.5, color='rgba(255, 255, 255, 0.3)')),
                            xaxis='x6', yaxis='y6', hoverinfo='y+x+name')

    log.debug('Plotting GC histogram...')
    GCHist = go.Histogram(x=GCVec, nbinsx=14, xbins=dict(start=0, end=100, size=5), nbinsy=10, opacity=0.75,
                          showlegend=False, histnorm='percent', name='% Reads',
                          marker=dict(color='rgba(154, 0, 0, 1.0)',
                                      line=dict(width=0.5, color='rgba(255, 255, 255, 0.3)')),
                          xaxis='x7', yaxis='y7', hoverinfo='y+x+name')

    data = [trace, trace1, trace2, PieChartReads, LengthHist, QualHist, GCHist, table]

    layout = fig.layout
    fig = go.Figure(data=data, layout=layout)
    fig['layout'].update(title='Sperimental Summary', titlefont=dict(size=20), margin=dict(r=80, t=80, b=80, l=80),
                         hovermode='compare',
                         spikedistance=-1,
                         xaxis1=dict(visible=False, showspikes=False),
                         xaxis2=dict(visible=False, showspikes=False),
                         xaxis3=dict(visible=False, showspikes=False),
                         xaxis4=dict(title='Length', hoverformat='.2s', fixedrange=True, domain=[.55, 1]),
                         xaxis5=dict(visible=True, autotick=False, title='hours', showspikes=True, spikethickness=1.5,
                                     spikemode='across', spikecolor='grey', spikedash='solid'),
                         xaxis6=dict(visible=True, title='Quality', hoverformat='.2s', range=[5, 15], fixedrange=True,
                                     domain=[.55, 1]),
                         xaxis7=dict(visible=True, title='GC-Content', hoverformat='.2s', fixedrange=True,
                                     domain=[.3, .7]),
                         yaxis1=dict(fixedrange=True, title='Reads', anchor='x5', hoverformat='.2s'),
                         yaxis2=dict(visible=False),
                         yaxis3=dict(title='Bases', fixedrange=True, anchor='x5', hoverformat='.2s'),
                         yaxis4=dict(title='%', hoverformat='.2f', anchor='x4', fixedrange=False, domain=[.632, .732]),
                         yaxis5=dict(title='Mean Length', fixedrange=True, hoverformat='.2s'),
                         yaxis6=dict(title='%', hoverformat='.2f', anchor='x6', fixedrange=True, domain=[.445, .545]),
                         yaxis7=dict(title='%', hoverformat='.2f', anchor='x7', fixedrange=True),
                         legend=dict(x=1, y=1, xanchor='right'),
                         )
    config = {'displaylogo': False,
              'modeBarButtonsToRemove': ['toImage', 'sendDataToCloud', 'zoom2d', 'pan2d', 'select2d', 'lasso2d',
                                         'hoverClosestCartesian', 'toggleHover', 'autoScale2d']}
    out_html_2 = os.path.join(work_dir, prefix + '_sequencing_summary.html')
    plotly.offline.plot(fig, filename=out_html_2, auto_open=False, show_link=False, config=config)
    HeatTrigger2(out_html_2)
    log.info('Finish')
    sys.exit()
Ejemplo n.º 10
0
def result_plotting(ch_par):
    def rngrabber(t):
        try:
            Value = int(Nanoline[t][0])
        except:
            Value = 0
        return Value

    def bgrabber(t):
        try:
            Value = int(Nanoline[t][1])
        except:
            Value = 0
        return Value

    log.info('Plotting results...')
    log.debug("Preparing heatmaps...")
    NanoLayout = [['1', '2', '3', '4', '5', '6', '7', '8', '40', '39', '38', '37', '36', '35', '34', '33'],
                  ['9', '10', '11', '12', '13', '14', '15', '16', '48', '47', '46', '45', '44', '43', '42', '41'],
                  ['17', '18', '19', '20', '21', '22', '23', '24', '56', '55', '54', '53', '52', '51', '50', '49'],
                  ['25', '26', '27', '28', '29', '30', '31', '32', '64', '63', '62', '61', '60', '59', '58', '57'],
                  ['449', '450', '451', '452', '453', '454', '455', '456', '488', '487', '486', '485', '484', '483',
                   '482',
                   '481'],
                  ['457', '458', '459', '460', '461', '462', '463', '464', '496', '495', '494', '493', '492', '491',
                   '490',
                   '489'],
                  ['465', '466', '467', '468', '469', '470', '471', '472', '504', '503', '502', '501', '500', '499',
                   '498',
                   '497'],
                  ['473', '474', '475', '476', '477', '478', '479', '480', '512', '511', '510', '509', '508', '507',
                   '506',
                   '505'],
                  ['385', '386', '387', '388', '389', '390', '391', '392', '424', '423', '422', '421', '420', '419',
                   '418',
                   '417'],
                  ['393', '394', '395', '396', '397', '398', '399', '400', '432', '431', '430', '429', '428', '427',
                   '426',
                   '425'],
                  ['401', '402', '403', '404', '405', '406', '407', '408', '440', '439', '438', '437', '436', '435',
                   '434',
                   '433'],
                  ['409', '410', '411', '412', '413', '414', '415', '416', '448', '447', '446', '445', '444', '443',
                   '442',
                   '441'],
                  ['321', '322', '323', '324', '325', '326', '327', '328', '360', '359', '358', '357', '356', '355',
                   '354',
                   '353'],
                  ['329', '330', '331', '332', '333', '334', '335', '336', '368', '367', '366', '365', '364', '363',
                   '362',
                   '361'],
                  ['337', '338', '339', '340', '341', '342', '343', '344', '376', '375', '374', '373', '372', '371',
                   '370',
                   '369'],
                  ['345', '346', '347', '348', '349', '350', '351', '352', '384', '383', '382', '381', '380', '379',
                   '378',
                   '377'],
                  ['257', '258', '259', '260', '261', '262', '263', '264', '296', '295', '294', '293', '292', '291',
                   '290',
                   '289'],
                  ['265', '266', '267', '268', '269', '270', '271', '272', '304', '303', '302', '301', '300', '299',
                   '298',
                   '297'],
                  ['273', '274', '275', '276', '277', '278', '279', '280', '312', '311', '310', '309', '308', '307',
                   '306',
                   '305'],
                  ['281', '282', '283', '284', '285', '286', '287', '288', '320', '319', '318', '317', '316', '315',
                   '314',
                   '313'],
                  ['193', '194', '195', '196', '197', '198', '199', '200', '232', '231', '230', '229', '228', '227',
                   '226',
                   '225'],
                  ['201', '202', '203', '204', '205', '206', '207', '208', '240', '239', '238', '237', '236', '235',
                   '234',
                   '233'],
                  ['209', '210', '211', '212', '213', '214', '215', '216', '248', '247', '246', '245', '244', '243',
                   '242',
                   '241'],
                  ['217', '218', '219', '220', '221', '222', '223', '224', '256', '255', '254', '253', '252', '251',
                   '250',
                   '249'],
                  ['129', '130', '131', '132', '133', '134', '135', '136', '168', '167', '166', '165', '164', '163',
                   '162',
                   '161'],
                  ['137', '138', '139', '140', '141', '142', '143', '144', '176', '175', '174', '173', '172', '171',
                   '170',
                   '169'],
                  ['145', '146', '147', '148', '149', '150', '151', '152', '184', '183', '182', '181', '180', '179',
                   '178',
                   '177'],
                  ['153', '154', '155', '156', '157', '158', '159', '160', '192', '191', '190', '189', '188', '187',
                   '186',
                   '185'],
                  ['65', '66', '67', '68', '69', '70', '71', '72', '104', '103', '102', '101', '100', '99', '98', '97'],
                  ['73', '74', '75', '76', '77', '78', '79', '80', '112', '111', '110', '109', '108', '107', '106',
                   '105'],
                  ['81', '82', '83', '84', '85', '86', '87', '88', '120', '119', '118', '117', '116', '115', '114',
                   '113'],
                  ['89', '90', '91', '92', '93', '94', '95', '96', '128', '127', '126', '125', '124', '123', '122',
                   '121']]

    NanoBasePlate = []
    NanoPlate = []
    for i in NanoLayout:
        Nanoline = [ch_par[_] for _ in i]
        Exlin = map(lambda x: rngrabber(x), range(0, len(Nanoline)))
        NanoPlate.append(Exlin)
        Exlin = map(lambda x: bgrabber(x), range(0, len(Nanoline)))
        NanoBasePlate.append(Exlin)

    owncolorscale = [[0.0, 'rgb(0,0,0)'], [0.001, 'rgb(140,81,10)'], [0.1111111111111111, 'rgb(140,81,10)'],
                     [0.2222222222222222, 'rgb(191,129,45)'], [0.33333333333, 'rgb(223,194,125)'],
                     [0.44444444444, 'rgb(254,224,139)'], [0.5555555555555555, 'rgb(246,232,195)'],
                     [0.6666666666666666, 'rgb(199,234,229)'], [0.7777777777777778, 'rgb(128,205,193)'],
                     [0.8888888888888888, 'rgb(53,151,143)'], [1.0, 'rgb(1,102,94)']]

    xh = map(list, zip(*NanoLayout))
    yh = map(list, zip(*NanoPlate))
    zh = map(list, zip(*NanoBasePlate))
    hovertext=[[] for i in range(len(xh))]

    for x1, y1 in enumerate(xh):
        for x2, y2 in enumerate(y1):
            hovertext[x1].append('<b>CHANNEL:</b> {}<br /><b>RN:</b> {}<br /><b>BN:</b> {}'.format(y2, yh[x1][x2], zh[x1][x2]))

    trace = go.Heatmap(name="ReadNHeat", z=map(list, zip(*NanoPlate)), x=range(1, 33), y=range(1, 17),
                       text=hovertext, hoverinfo="text", xgap=23, ygap=8, colorscale=owncolorscale,
                       colorbar=dict(y=0.77, len=0.470, exponentformat="SI"))  # y=0.77,
    trace1 = go.Heatmap(name="BaseNHeat", z=map(list, zip(*NanoBasePlate)), x=range(1, 33), y=range(1, 17),
                        text=hovertext, hoverinfo="text", xgap=23, ygap=8, colorscale=owncolorscale,
                        colorbar=dict(y=0.77, len=0.470, exponentformat="SI"), visible=False)
    fig = plotly.tools.make_subplots(rows=13, cols=1, shared_xaxes=False,
                                     specs=[[{'rowspan': 6}], [None], [None], [None], [None], [None], [None],
                                            [{'rowspan': 2}], [None], [{'rowspan': 2}], [None], [None],
                                            [{'rowspan': 1}]],
                                     vertical_spacing=0.001, print_grid=False)

    log.debug("Heatmaps ready!")

    VisibleData = [False] * (513 * 10)

    updatemenus = list([
        dict(type="buttons",
             active=-1,
             buttons=list([
                 dict(label='Read Number',
                      method='update',
                      args=[{'visible': [True, False] + VisibleData},
                            {'annotations': trace}]),
                 dict(label='Base Pair Number',
                      method='update',
                      args=[{'visible': [False, True] + VisibleData},
                            {'annotations': trace1}]),
             ]),
             x=-0.03,
             y=0.99
             )
    ])

    fig.append_trace(trace, 1, 1)
    fig.append_trace(trace1, 1, 1)

    log.debug("Computing pore performance...")

    ChannelsInfoList = []
    X3Index = []
    X4index = []
    global hour_vec
    hour_vec = range(48)
    for c in range(1, 513):
        Res, x3i, x4i = ChannelPlotter_f(c, ch_par)
        ChannelsInfoList += Res
        X3Index += x3i
        X4index += x4i

    fig.data.extend(ChannelsInfoList)

    for fi in X3Index:
        fig.data[fi].update({'xaxis': 'x3'})

    for fi in X4index:
        fig.data[fi].update({'xaxis': 'x4'})

    log.debug("Plotting pore performance...")

    fig['layout'].update(title='Channel Productivity', spikedistance=-1, yaxis1=dict(range=[16.5, 0.5], visible=False, fixedrange=True),
                         xaxis1=dict(visible=False, fixedrange=True, range=[0.5, 32.5], hoverformat='.2s'),
                         yaxis2=dict(fixedrange=False, title='Reads', hoverformat='.2s', anchor='x3'),
                         xaxis2=dict(visible=False, fixedrange=True),
                         xaxis3=dict(autotick=False, title='hours', range=[0, 48], showspikes=True, spikethickness=1.5,
                                     spikemode='across', spikecolor='grey', spikedash='solid'),
                         yaxis3=dict(fixedrange=False, title='Base Pairs', hoverformat='.2s'),      
                         xaxis4=dict(visible=False, fixedrange=True, range=[0, 100]),
                         yaxis4=dict(fixedrange=True, visible=False),
                         legend=dict(x=-0.062, y=0.439, yanchor='yaxis2', xanchor='right'), updatemenus=updatemenus,
                         barmode='stack')  # y=0.450   range=[0, 1500000] range=[0, 600]
    fig.layout.update({'hovermode': 'compare'})
    config = {'displaylogo': False,
              'modeBarButtonsToRemove': ['toImage', 'sendDataToCloud', 'zoom2d', 'pan2d', 'select2d', 'lasso2d',
                                         'hoverClosestCartesian', 'toggleHover', 'autoScale2d']}
    try:
        out_html = os.path.join(work_dir, prefix + '_pore_activity_map.html')
        plotly.offline.plot(fig, filename=out_html, show_link=False, auto_open=False, config=config)
    except:
        e = sys.exc_info()
        tb = traceback.format_exception(*e)
        log.error(
            "Something went wrong during plotting...\n\033[36m##################################\n\033[93m%s\033[32m%s\033[93m%s\033[36m##################################\n\033[0m" % (
                tb[0], tb[1], tb[2]))
        sys.exit(1)
    HeatTrigger(out_html)
Ejemplo n.º 11
0
def ChannelPlotter_f(c, c_plo):
    if summary_flag == False:
        MuxList = [[0] * 48] * 8
        try:
            log.debug("Plotting channel %s stats..." % c)
            Bait = c_plo[str(c)][4]
            for kk, vv in Bait.items():
                if vv != []:
                    MuxLine1 = [0] * 48
                    MuxLine2 = [0] * 48
                    for u, z in vv[0].items():
                        MuxLine1[int(u)] = int(z[0])
                        MuxLine2[int(u)] = int(z[1])
                    MuxList[int(kk) - 1] = MuxLine1
                    MuxList[(int(kk) - 1) + 4] = MuxLine2
        except:
            pass 
        trace2Mux1 = go.Scatter(
            y=mux_fixer(MuxList[0]),
            name='Pore1',
            line=dict(
                color=('rgb(50,136,189)')),
            legendgroup='Pore1Chr' + str(c),
            showlegend=False,
            text=map(lambda x: str(x[0]) + '<br>' + str(x[1]) + 'h', zip(mux_fixer(MuxList[0]), hour_vec)),
            hoverinfo="text+name",
            fill='tozeroy',
            visible=False,
            xaxis='x3',
            yaxis='y2'
        )
        trace2Mux2 = go.Scatter(
            y=mux_fixer(MuxList[1]),
            name='Pore2',
            line=dict(
                color=('rgb(253,174,97)')),
            legendgroup='Pore2Chr' + str(c),
            showlegend=False,
            text=map(lambda x: str(x[0]) + '<br>' + str(x[1]) + 'h', zip(mux_fixer(MuxList[1]), hour_vec)),
            hoverinfo="text+name",
            fill='tozeroy',
            visible=False,
            xaxis='x3',
            yaxis='y2'
        )
        trace2Mux3 = go.Scatter(
            y=mux_fixer(MuxList[2]),
            name='Pore3',
            line=dict(
                color=('rgb(21,112,76)')),
            legendgroup='Pore3Chr' + str(c),
            showlegend=False,
            text=map(lambda x: str(x[0]) + '<br>' + str(x[1]) + 'h', zip(mux_fixer(MuxList[2]), hour_vec)),
            hoverinfo="text+name",
            fill='tozeroy',
            visible=False,
            xaxis='x3',
            yaxis='y2'
        )
        trace2Mux4 = go.Scatter(
            y=mux_fixer(MuxList[3]),
            name='Pore4',
            line=dict(
                color=('rgb(165,15,21)')),
            legendgroup='Pore4Chr' + str(c),
            showlegend=False,
            text=map(lambda x: str(x[0]) + '<br>' + str(x[1]) + 'h', zip(mux_fixer(MuxList[3]), hour_vec)),
            hoverinfo="text+name",
            fill='tozeroy',
            visible=False,
            xaxis='x3',
            yaxis='y2'
        )
        trace3Mux1 = go.Scatter(
            x=hour_vec,
            y=mux_fixer(MuxList[4]),
            name = 'Pore1',
            line=dict(
                color=('rgb(50,136,189)')),
            legendgroup='Pore1Chr' + str(c),
            visible=False,
            text=map(lambda x: str(x) + 'h', hour_vec),
            hoverinfo="y+text+name",
            fill='tozeroy',
            xaxis='x3',
            yaxis='y3'
        )
        trace3Mux2 = go.Scatter(
            x=hour_vec,
            y=mux_fixer(MuxList[5]),
            name='Pore2',
            line=dict(
                color=('rgb(253,174,97)')),
            legendgroup='Pore2Chr' + str(c),
            visible=False,
            text=map(lambda x: str(x) + 'h', hour_vec),
            hoverinfo="y+text+name",
            fill='tozeroy',
            xaxis='x3',
            yaxis='y3'
        )
        trace3Mux3 = go.Scatter(
            x=hour_vec,
            y=mux_fixer(MuxList[6]),
            name='Pore3',
            line=dict(
                color=('rgb(21,112,76)')),
            legendgroup='Pore3Chr' + str(c),
            visible=False,
            text=map(lambda x: str(x) + 'h', hour_vec),
            hoverinfo="y+text+name",
            fill='tozeroy',
            xaxis='x3',
            yaxis='y3'
        )
        trace3Mux4 = go.Scatter(
            x=hour_vec,
            y=mux_fixer(MuxList[7]),
            name='Pore4',
            line=dict(
                color=('rgb(165,15,21)')),
            legendgroup='Pore4Chr' + str(c),
            visible=False,
            text=map(lambda x: str(x) + 'h', hour_vec),
            hoverinfo="y+text+name",
            fill='tozeroy',
            xaxis='x3',
            yaxis='y3'
        )
        VisibleIndexes = range(10 * (c - 1), 10 * c)
        BarIdx = map(lambda x: x + 2, VisibleIndexes[-2:])
        fixIdx = map(lambda x: x + 2, VisibleIndexes[0:4])
        FailReads = 100
        if len(c_plo[str(c)]) > 0:
            FailReads = (int(c_plo[str(c)][2]) * 100) / int(c_plo[str(c)][0])
        PassReads = 100 - FailReads
        bardata = Bargen(PassReads, FailReads)
        tracedata = [trace2Mux1, trace2Mux2, trace2Mux3, trace2Mux4, trace3Mux1, trace3Mux2, trace3Mux3, trace3Mux4]
        outdata = tracedata + bardata
        Indexeddata = (outdata, fixIdx, BarIdx)
    else:
        try:
            MuxList1 = [0] * 48
            MuxList2 = [0] * 48
            Bait = c_plo[str(c)][4]
            for kk, vv in Bait.items():
                if vv != []:
                    nr, nb = vv
                    MuxList1[int(kk)] = int(nr)
                    MuxList2[int(kk)] = int(nb)
        except:
            pass
        trace2Mux1 = go.Scatter(
            y=mux_fixer(MuxList1),
            name='Channel',
            line=dict(
                color=('rgb(50,136,189)')),
            legendgroup='Pore1Chr' + str(c),
            showlegend=False,
            text=map(lambda x: str(x[0]) + '<br>' + str(x[1]) + 'h', zip(mux_fixer(MuxList1), hour_vec)),
            hoverinfo="text+name",
            fill='tozeroy',
            visible=False,
            xaxis='x3',
            yaxis='y2'
        )
        trace3Mux1 = go.Scatter(
            x=hour_vec,
            y=mux_fixer(MuxList2),
            name = 'Channel',
            line=dict(
                color=('rgb(50,136,189)')),
            legendgroup='Pore1Chr' + str(c),
            visible=False,
            text=map(lambda x: str(x) + 'h', hour_vec),
            hoverinfo="y+text+name",
            fill='tozeroy',
            xaxis='x3',
            yaxis='y3'
        )
        VisibleIndexes = range(4 * (c - 1), 4 * c)
        BarIdx = map(lambda x: x + 2, VisibleIndexes[-2:])
        fixIdx = map(lambda x: x + 2, VisibleIndexes[0:4])
        FailReads = 100
        if len(c_plo[str(c)]) > 0:
            FailReads = (int(c_plo[str(c)][2]) * 100) / int(c_plo[str(c)][0])
        PassReads = 100 - FailReads
        bardata = Bargen(PassReads, FailReads)
        tracedata = [trace2Mux1, trace3Mux1]
        outdata = tracedata + bardata
        Indexeddata = (outdata, fixIdx, BarIdx)          
    return Indexeddata