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

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

<span class="math-container">$ 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,$</span>2}}' \
    &gt; temp.bedgraph

$ cat temp.bedgraph
chr1    59965   59985   7
chr1    59985   60005   10
chr1    60005   60025   10
chr1    60025   60045   10
</code></pre>

CC BY-SA 4.0
2019-04-10T12:17:24.910Z
2019-04-10T12:17:24.910Z
vkkodali
<p>This is a temporary solution I came up with that worked for my specific case:</p>

<pre><code>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)&lt;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
</code></pre>

<p>input:</p>

<pre><code>variableStep chrom=chr16 span=20
59965   7
59985   10
60005   10
</code></pre>

<p>output:</p>

<pre><code>chr16   59965   59985   7
chr16   59985   60005   10
chr16   60005   60025   10
chr16   60025   60045   10
</code></pre>

CC BY-SA 4.0
2019-04-08T15:27:27.040Z
2019-04-08T17:40:53.393Z
2019-04-08T17:40:53.393Z
0x90
<p>I have a .wig file in this format:</p>

<pre><code>variableStep chrom=chr1 span=20
59965   7
59985   10
60005   10
60025   10
</code></pre>

<p>and I would like to format it to something like this format:</p>

<pre><code>#bedGraph section chr16:0-103584
chr16   0       59901   0
chr16   59901   59927   1
chr16   59927   59931   2
</code></pre>

<p>Please assume, I am working with a reference hg19 genome, in case the info isn't deducible from the <code>variableStep</code> .wig file. I couldn't find anything in <a href="https://github.com/Ensembl/WiggleTools" rel="nofollow noreferrer">WiggleTools</a> </p>

CC BY-SA 4.0
2019-04-08T09:10:45.287Z
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..)
llrs
CC BY-SA 4.0
2019-04-08T12:47:34.207Z
@llrs they aren’t of the same data. It’s just to demonstrate the format
0x90
CC BY-SA 4.0
2019-04-09T09:39:30.113Z
Could you please change one or the other so that they represent the same data?
gringer
<python>
Is there a tool to convert a variableStep .wig to fixedStep one?
# Is there a tool to convert a variableStep .wig to fixedStep one?

(Asked by: username_0 on 2019-04-07)

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

**Tags:** python
> **Comments:**
> **username_1**: 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..)  
> **username_0**: @llrs they aren’t of the same data. It’s just to demonstrate the format  
> **username_2**: Could you please change one or the other so that they represent the same data?  

---

## 2 answers

### Answer by username_3 on 2019-04-10. Score: 1 | Accepted answer

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


---

### Answer by username_0 on 2019-04-08. Score: 2

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


---

