Is There A Tool For Converting A Fixed Step Wig To A Variable Step Wig?
3
3
Entering edit mode
13.2 years ago
KCC ★ 4.1k

What is the easiest way of converting a fixed step wig file to a variable step wig file?

wiggle • 4.8k views
ADD COMMENT
6
Entering edit mode
13.2 years ago

The following C program seems to work:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc,char** argv)
{
int span=1;
int start=0;
int step=0;
char line[BUFSIZ];
while(fgets(line,BUFSIZ,stdin)!=NULL)
 {
 if(strncmp(line,"fixedStep",9)==0)
    {
    span=1;
    fputs("variableStep",stdout);
    char* saveptr=NULL;
    char* token=line;
    char* ptr;
    while((ptr=strtok_r(token," \n\t", &saveptr))!=NULL)
        {
        char* key=ptr;
        char* value=strchr(ptr,'=');
        if(value==NULL)
            {
            token=saveptr;
            continue;
            }
        *value=0;
        value++;
        if(strcmp(key,"chrom")==0)
            {
            printf(" %s=%s",key,value);
            }
        else if(strcmp(key,"span")==0)
            {
            span=atoi(value);
            }       
        else if(strcmp(key,"start")==0)
            {
            start=atoi(value);
            }
        else if(strcmp(key,"step")==0)
            {
            step=atoi(value);
            }
        token=saveptr;
        }
    printf(" span=%d\n",span);
    continue;
    }
 printf("%d\t%s",start,line);
 start+=step;
 }
return 0;
}

Compile:

gcc -O3 -Wall -ofix2var source.c

Example:

$ curl -s "http://hgdownload.cse.ucsc.edu/goldenPath/hg18/phastCons28way/vertebrate/chr9_random.pp.gz" | gunzip  -c | ./fix2var  | head

variableStep chrom=chr9_random span=1
3   0.000
4   0.000
5   0.000
6   0.000
7   0.000
8   0.000
9   0.000
10  0.000
11  0.000
ADD COMMENT
5
Entering edit mode
12.9 years ago
Nick ▴ 50

This was placing a 0/t in front of the track line. I also edited it to not print lines with density 0, since being able to omit these are the one advantage of variable step.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc,char** argv)
{
int span=1;
int start=0;
int step=0;
char line[BUFSIZ];
while(fgets(line,BUFSIZ,stdin)!=NULL)
 {
 if(strncmp(line,"track",5)==0)
    {
     printf("%s",line);
     continue;
    }
 if(strncmp(line,"fixedStep",9)==0)
    {
    span=1;
    fputs("variableStep",stdout);
    char* saveptr=NULL;
    char* token=line;
    char* ptr;
    while((ptr=strtok_r(token," \n\t", &saveptr))!=NULL)
        {
        char* key=ptr;
        char* value=strchr(ptr,'=');
        if(value==NULL)
            {
            token=saveptr;
            continue;
            }
        *value=0;
        value++;
        if(strcmp(key,"chrom")==0)
            {
            printf(" %s=%s",key,value);
            }
        else if(strcmp(key,"span")==0)
            {
            span=atoi(value);
            }       
        else if(strcmp(key,"start")==0)
            {
            start=atoi(value);
            }
        else if(strcmp(key,"step")==0)
            {
            step=atoi(value);
            }
        token=saveptr;
        }
    printf(" span=%d\n",span);
    continue;
    }
 if(strncmp(line,"0",1)!=0)
   {  
   printf("%d\t%s",start,line);
   }
 start+=step;
 }
return 0;
}
ADD COMMENT

Login before adding your answer.

Traffic: 944 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6