1
$\begingroup$

I have a .wig file in this format:

variableStep chrom=chr1 span=20
59965   7
59985   10
60005   10
60025   10

and I would like to format it to something like this format:

#bedGraph section chr16:0-103584
chr16   0       59901   0
chr16   59901   59927   1
chr16   59927   59931   2

Please assume, I am working with a reference hg19 genome, in case the info isn't deducible from the variableStep .wig file. I couldn't find anything in WiggleTools

$\endgroup$
  • $\begingroup$ To me it is not obvious if these two files are consistent. What is the first position 59965 converted to ? (Perhaps it is because I'm unfamiliar with these type of files..) $\endgroup$ – llrs Apr 8 at 9:10
  • 1
    $\begingroup$ @llrs they aren’t of the same data. It’s just to demonstrate the format $\endgroup$ – 0x90 Apr 8 at 12:47
  • $\begingroup$ Could you please change one or the other so that they represent the same data? $\endgroup$ – gringer Apr 9 at 9:39
1
$\begingroup$

When I had to do something similar previously, I had used the following method using sed and awk. It is not as robust in terms of error detection and correction as the python solution but it is a simple one-liner.

$ cat temp.wig
variableStep chrom=chr1 span=20
59965   7
59985   10
60005   10
60025   10

$ sed -r 's/variableStep chrom=(.*) span=([0-9]*)/\1\t\2/g' temp.wig \
        | awk 'BEGIN{FS="\t";OFS="\t"}{if ($1~/^chr/) {c=$1 ; s=$2} else {print c,$1,$1+s,$2}}' \
    > temp.bedgraph

$ cat temp.bedgraph
chr1    59965   59985   7
chr1    59985   60005   10
chr1    60005   60025   10
chr1    60025   60045   10
$\endgroup$
2
$\begingroup$

This is a temporary solution I came up with that worked for my specific case:

def convert_line(line, chrom, span, start=None, fixedStep=None, count=None):
        new_line=''
        if fixedStep is None or count is None or start is None:
                line_arr  = line.split()
                start = line_arr[0]
                end   = int(line_arr[0]) + int(span)
                val   = line_arr[1]
                new_line = '{0}\t{1}\t{2}\t{3}\n'.format(chrom, start, end, val)
        else:
                line_arr  = line.split()
                val   = np.float(line_arr[0])
                start = int(start) + int(fixedStep)*int(count)
                end   = start + int(fixedStep)
                new_line = '{0}\t{1}\t{2}\t{3}\n'.format(chrom, start, end, val)

        return new_line

def convert_file(input_file, output_file):
        chrom=None
        span=None
        fixedStep=None
        count=None
        start=None
        with open(input_file, 'r') as fin, open(output_file, 'w') as fout:
                for line in fin:
                        if len(line)<1 or line[0] == '#':
                                continue
                        elif 'variableStep' in line or 'fixedStep' in line:
                                for key in line.split():
                                        if 'chrom=' in key:
                                                chrom = key.split('chrom=')[-1]
                                        if 'span=' in key:
                                                span = key.split('span=')[-1]
                                        if 'step=' in key:
                                                fixedStep = key.split('step=')[-1]
                                        if 'start=' in key:
                                                start = key.split('start=')[-1]
                                                count = 0
                        else:
                                line_out = convert_line(line, chrom, span, start, fixedStep, count)
                                fout.write(line_out)
                                if count is not None:
                                        count += 1

input:

variableStep chrom=chr16 span=20
59965   7
59985   10
60005   10

output:

chr16   59965   59985   7
chr16   59985   60005   10
chr16   60005   60025   10
chr16   60025   60045   10
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.