|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# @Time : 3/2/2021 5:32 PM |
| 4 | +# @Author : Runsheng |
| 5 | +# @File : get_near_ref.py |
| 6 | +""" |
| 7 | +from mutiple references, get the nearest reference for further polish |
| 8 | +mostly used for RNA virus reference choosing |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import print_function |
| 12 | +import os |
| 13 | +import argparse |
| 14 | +import subprocess |
| 15 | +import logging |
| 16 | +import sys |
| 17 | +import signal |
| 18 | +from Bio import SeqIO |
| 19 | +import gzip |
| 20 | +import operator |
| 21 | +from collections import OrderedDict |
| 22 | + |
| 23 | + |
| 24 | +def myexe(cmd, timeout=0): |
| 25 | + """ |
| 26 | + a simple wrap of the shell |
| 27 | + mainly used to run the bwa mem mapping and samtool orders |
| 28 | + """ |
| 29 | + def setupAlarm(): |
| 30 | + signal.signal(signal.SIGALRM, alarmHandler) |
| 31 | + signal.alarm(timeout) |
| 32 | + |
| 33 | + def alarmHandler(signum, frame): |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + proc=subprocess.Popen(cmd, shell=True, preexec_fn=setupAlarm, |
| 37 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE,cwd=os.getcwd()) |
| 38 | + out, err=proc.communicate() |
| 39 | + print(err) |
| 40 | + return out, err, proc.returncode |
| 41 | + |
| 42 | + |
| 43 | +def fastq2dic(fastqfile): |
| 44 | + """ |
| 45 | + Give a fastq file name, return a dict contains the name and seq |
| 46 | + Require Biopython SeqIO medule to parse the sequence into dict, a large readfile may take a lot of RAM |
| 47 | + """ |
| 48 | + if ".gz" in fastqfile: |
| 49 | + handle=gzip.open(fastqfile, "rU") |
| 50 | + else: |
| 51 | + handle=open(fastqfile, "rU") |
| 52 | + record_dict=SeqIO.to_dict(SeqIO.parse(handle, "fastq")) |
| 53 | + handle.close() |
| 54 | + return record_dict |
| 55 | + |
| 56 | + |
| 57 | +def chr_select(record_dict, chro): |
| 58 | + """ |
| 59 | + Note the start and end is 0 based |
| 60 | + give the name of refdic, and the chr, start and end to be used |
| 61 | + return the name and sequence (both as str) |
| 62 | + for example, chrcut(record_dict, "I", 100,109) returns |
| 63 | + ("I:100_109","AAAAAAAAAA") |
| 64 | + """ |
| 65 | + name=record_dict[chro].name |
| 66 | + seq=str(record_dict[chro].seq) |
| 67 | + return name,seq |
| 68 | + |
| 69 | + |
| 70 | +def wrapper_run_get_bedfile(ref, fastq, core=32): |
| 71 | + |
| 72 | + cmd_minimap2=""" |
| 73 | + minimap2 -ax map-ont -t {core} {ref} {fastq} > map.sam |
| 74 | + samtools view -F 4 -b map.sam > map.bam |
| 75 | + samtools sort map.bam > maps.bam |
| 76 | + bedtools genomecov -ibam maps.bam -bga > {ref}.bed |
| 77 | + rm map.sam |
| 78 | + rm map.bam |
| 79 | + """.format(ref=ref, fastq=fastq, core=core) |
| 80 | + print(cmd_minimap2) |
| 81 | + print(myexe(cmd_minimap2)) |
| 82 | + |
| 83 | + return ref+".bed" |
| 84 | + |
| 85 | + |
| 86 | +def bed_parser_get_higest_coverage(bedfile): |
| 87 | + """ |
| 88 | + parser the bed file and get the refname which has higest coverage |
| 89 | + :param bedfile: |
| 90 | + :return: |
| 91 | + """ |
| 92 | + cov_sum={} |
| 93 | + |
| 94 | + f=open(bedfile, "r") |
| 95 | + for line in f.readlines(): |
| 96 | + line_l=line.strip().split("\t") |
| 97 | + name, start, end, coverage=line_l |
| 98 | + try: |
| 99 | + cov_sum[name]+=(int(end)-int(start)) * int(coverage) |
| 100 | + except KeyError: |
| 101 | + cov_sum[name] = (int(end) - int(start)) * int(coverage) |
| 102 | + sorted_d = sorted(cov_sum.items(), key=operator.itemgetter(1), reverse=True) |
| 103 | + print(sorted_d[0]) |
| 104 | + |
| 105 | + f.close() |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +if __name__=="__main__": |
| 111 | + example_text = '''example: |
| 112 | + ### example to run the bedtools intersection for all bed files with the nr3c1exon.gtf file |
| 113 | + get_near_ref.py -r ref.fasta -f read.fastq > near1.fasta |
| 114 | + ''' |
| 115 | + |
| 116 | + parser = argparse.ArgumentParser(prog='runpara', |
| 117 | + description='Run bash cmd lines for files with the same surfix', |
| 118 | + epilog=example_text, |
| 119 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 120 | + |
| 121 | + parser.add_argument("-f", "--file", help="the fastq file") |
| 122 | + parser.add_argument("-r", "--reference", help="the reference file") |
| 123 | + parser.add_argument("-c", "--core", help="the core", default= 32) |
| 124 | + |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + ref_bed=wrapper_run_get_bedfile(args.reference, args.file, args.core) |
| 128 | + bed_parser_get_higest_coverage(ref_bed) |
| 129 | + |
| 130 | + |
0 commit comments