Question: Is There A Tool For Converting A Fixed Step Wig To A Variable Step Wig?
3
gravatar for KCC
8.1 years ago by
KCC4.0k
Cambridge, MA
KCC4.0k wrote:

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

wiggle • 3.2k views
ADD COMMENTlink modified 5.9 years ago by choza780 • written 8.1 years ago by KCC4.0k
4
gravatar for Pierre Lindenbaum
8.1 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum124k wrote:

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 COMMENTlink written 8.1 years ago by Pierre Lindenbaum124k
4
gravatar for Nick
7.8 years ago by
Nick40
Nick40 wrote:

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 COMMENTlink written 7.8 years ago by Nick40
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 879 users visited in the last hour