Ejemplo n.º 1
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.º 2
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.º 3
0
 def __call__(self, parser, namespace, values, option_string=None):
     prospective_dir = values
     if not os.path.isdir(prospective_dir):
         log.error(
             "readable_dir:{0} is not a directory".format(prospective_dir))
         sys.exit(1)
     if os.access(prospective_dir, os.R_OK):
         setattr(namespace, self.dest, prospective_dir)
     else:
         log.error(
             "readable_dir:{0} is not a directory".format(prospective_dir))
         sys.exit(1)
Ejemplo n.º 4
0
def fastq_writer(tmp_dir):
    finalfile = os.path.join(work_dir, (prefix + '.fastq.gz'))
    file_list = os.listdir(tmp_dir)
    file_list.sort(key=lambda x: int(ntpath.basename(x)[4:-9]))
    with open(finalfile, 'wb') as outfile:
        for infile in map(lambda x: os.path.join(tmp_dir, x), file_list):
            shutil.copyfileobj(open(infile, 'rb'), outfile, 1024*1024*10)
    for b in map(lambda x: os.path.join(tmp_dir, x), file_list):
        try:
            os.remove(b)
        except Exception:
            log.warning("File %s doesn't exist!" % b)
    if not os.path.exists(os.path.join(work_dir, out_dir)):
        os.makedirs(os.path.join(work_dir, out_dir))            
    try:
        shutil.move(finalfile, os.path.join(work_dir, out_dir))
    except shutil.Error:
        log.error("Unable to move %s" % finalfile)
    shutil.rmtree(tmp_dir, ignore_errors=True)
Ejemplo n.º 5
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.º 6
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.º 7
0
def run(argsin):
    ldir = tempfile.mkdtemp()
    atexit.register(lambda dir=ldir: shutil.rmtree(ldir))
    parser = argparse.ArgumentParser(
        add_help=False,
        prog='fastqgen',
        usage='''\r      \n
                                  ________________
                                 |                |
                                 |    #####       |
                                 |    ##  ##      |
                                 |    #####       |
                                 |    ## #####    |
                                 |    ## ##  ##   |
                                 |       #####    |
                                 |       ##       |
                                 |       ##       |
                                 |________________|

                                       PyPore    
                                      FastQGen                                 
                                                
usage: %(prog)s [-i <input_directory>] [-l <my_label>] [options]''',
        epilog='''
                                     
PyPore. Written by Roberto Semeraro, Department of Clinical and Sperimental Medicine,
University of Florence. For bug report or suggestion write to [email protected]''',
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    g = parser.add_argument_group(
        title='mandatory arguments',
        description=
        '''-i, --input_directory                                       path to the file folder
-l, --label                                                    label for fastq file
    ''')
    g.add_argument('-i',
                   '--input_directory',
                   action=readable_dir,
                   default=ldir,
                   help=argparse.SUPPRESS)
    g.add_argument('-l',
                   '--label',
                   action='store',
                   metavar='',
                   help=argparse.SUPPRESS)
    f = parser.add_argument_group(
        title='options',
        description=
        '''-f, --fail_reads                                  avoid skipping of fail reads [no]
-o, --output_dir                                    if omitted, generates a results 
                                                  directory in the current position
-n, --threads_number                               number of processing threads [1]
-v, --verbose                      increase verbosity: 0 = warnings only, 1 = info, 
                                    2 = debug, 3 = debug on terminal. Default is no
                                                    verbosity. No number means info
-h, --help                                          show this help message and exit
    ''')
    f.add_argument('-f',
                   '--fail_reads',
                   action="store",
                   nargs='*',
                   default=['n'],
                   metavar='',
                   help=argparse.SUPPRESS,
                   choices=['y', 'n', 'yes', 'no'])
    f.add_argument('-o',
                   '--output_dir',
                   action="store",
                   nargs=1,
                   metavar='',
                   help=argparse.SUPPRESS)
    f.add_argument('-n',
                   '--threads_number',
                   action="store",
                   type=int,
                   default=1,
                   metavar='',
                   help=argparse.SUPPRESS)
    f.add_argument('-h', '--help', action="help", help=argparse.SUPPRESS)
    f.add_argument('-v',
                   '--verbose',
                   const=1,
                   default=1,
                   type=int,
                   nargs="?",
                   help=argparse.SUPPRESS,
                   choices=range(0, 4))

    try:
        args = parser.parse_args(argsin)
        if not args.input_directory or not args.label:
            parser.print_help()
            log.error('Missing mandatory arguments!')
            sys.exit(1)
        else:
            ArgsReader(args)
    except IOError as msg:
        parser.error(str(msg))
Ejemplo n.º 8
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.º 9
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.º 10
0
def run(argsin):
    parser = argparse.ArgumentParser(
        add_help=False,
        prog='alignment',
        usage='''\r      \n
                                  ________________
                                 |                |
                                 |    #####       |
                                 |    ##  ##      |
                                 |    #####       |
                                 |    ## #####    |
                                 |    ## ##  ##   |
                                 |       #####    |
                                 |       ##       |
                                 |       ##       |
                                 |________________|
                                       PyPore    
                                      Alignment
                                                
usage: %(prog)s [-i <input.fq>] [-r <reference.fa>] [-l <my_label>] [options]''',
        epilog='''
                                     
PyPore. Written by Roberto Semeraro, Department of Clinical and Sperimental Medicine,
University of Florence. For bug report or suggestion write to [email protected]''',
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    g = parser.add_argument_group(
        title='mandatory arguments',
        description=
        '''-i, --inputs                                       path to the read file (query.fq)
-r, --reference                                     indexed reference sequence file
-l, --prefix                                              prefix for alignment file
    ''')
    g.add_argument('-i',
                   '--inputs',
                   action='store',
                   metavar='',
                   nargs='*',
                   help=argparse.SUPPRESS)
    g.add_argument('-r',
                   '--reference',
                   action='store',
                   metavar='',
                   help=argparse.SUPPRESS)
    g.add_argument('-l',
                   '--prefix',
                   action='store',
                   metavar='',
                   help=argparse.SUPPRESS)
    f = parser.add_argument_group(
        title='options',
        description=
        '''-a, --aligner           aligment module (b = bwa, m = minimap2, n = ngmlr)  [m b n]
-s, --alignment_stats                                 generate alignment stats [no]
-o, --output_dir                          if omitted, generates a results directory
                                                            in the current position                         
-n, --threads_number                               number of processing threads [1]
-v, --verbose           increase verbosity: 0 = only warnings, 1 = info, 2 = debug,
                                        3 = debug on terminal. No number means info
                                                            Default is no verbosity
-h, --help                                          show this help message and exit
    ''')
    f.add_argument('-a',
                   '--aligner',
                   action="store",
                   nargs='*',
                   default=['m'],
                   metavar='',
                   help=argparse.SUPPRESS,
                   choices=['m', 'b', 'n'])
    f.add_argument('-s',
                   '--alignment_stats',
                   action="store",
                   nargs='*',
                   default=['n'],
                   metavar='',
                   help=argparse.SUPPRESS,
                   choices=['y', 'n', 'yes', 'no'])
    f.add_argument('-o',
                   '--output_dir',
                   action="store",
                   nargs=1,
                   metavar='',
                   help=argparse.SUPPRESS)
    f.add_argument('-n',
                   '--threads_number',
                   action="store",
                   type=int,
                   default=1,
                   metavar='',
                   help=argparse.SUPPRESS)
    f.add_argument('-h', '--help', action="help", help=argparse.SUPPRESS)
    f.add_argument('-v',
                   '--verbose',
                   const=1,
                   default=1,
                   type=int,
                   nargs="?",
                   help=argparse.SUPPRESS,
                   choices=range(0, 4))

    try:
        args = parser.parse_args(argsin)
        if not args.inputs or not args.reference or not args.prefix:
            parser.print_help()
            log.error('Missing mandatory arguments!')
            sys.exit(1)
        else:
            MainReader(args)
    except IOError as msg:
        parser.error(str(msg))
Ejemplo n.º 11
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)