Skip to content

Instantly share code, notes, and snippets.

@silvtal
Last active March 16, 2022 16:51
Show Gist options
  • Save silvtal/9929b7744e44aaf14a9146833cbcd6c8 to your computer and use it in GitHub Desktop.
Save silvtal/9929b7744e44aaf14a9146833cbcd6c8 to your computer and use it in GitHub Desktop.
C script to convert from fixedStep to variableStep wiggle format
#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;
}
@silvtal
Copy link
Author

silvtal commented Mar 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment