Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse lexicographical order #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/fastq-sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,30 @@ int seq_cmp_seq(const void* a, const void* b)
return strcmp(((seq_t*) a)->seq.s, ((seq_t*) b)->seq.s);
}

int seq_cmp_rlo(const void* a, const void* b)
{
//read right-to-left
size_t offset_a=((seq_t*) a)->seq.n - 1;
size_t offset_b=((seq_t*) b)->seq.n - 1;

while( *((((seq_t*) a)->seq.s)+offset_a)==*((((seq_t*) b)->seq.s)+offset_b) && offset_a>=0 && offset_b>=0){
offset_a--;
offset_b--;
}

if(offset_a>=0 && offset_b>=0){
if(*((((seq_t*) a)->seq.s)+offset_a)<*((((seq_t*) b)->seq.s)+offset_b))
return -1;
else
return 1;
}
else if(offset_a<0 && offset_b>=0)
return -1;
else if(offset_a>=0 && offset_b<0)
return 1;
else
return 0;
}

static float seq_gc(const seq_t* s)
{
Expand Down Expand Up @@ -446,6 +470,7 @@ void print_help()
" -i, --id sort alphabetically by read identifier\n"
" -n, --idn sort alphanumerically by read identifier according to \"samtools sort -n\"\n"
" -s, --seq sort alphabetically by sequence\n"
" -c, --rlo sort co-lexicographically (i.e., reverse lexicographical order) by sequence\n"
" -R, --random randomly shuffle the sequences\n"
" --seed[=SEED] seed to use for random shuffle.\n"
" -G, --gc sort by GC content\n"
Expand Down Expand Up @@ -491,6 +516,7 @@ int main(int argc, char* argv[])
{"id", no_argument, NULL, 'i'},
{"idn", no_argument, NULL, 'n'},
{"seq", no_argument, NULL, 's'},
{"rlo", no_argument, NULL, 'c'},
{"random", no_argument, NULL, 'R'},
{"seed", optional_argument, NULL, 0},
{"gc", no_argument, NULL, 'G'},
Expand Down Expand Up @@ -528,6 +554,10 @@ int main(int argc, char* argv[])
case 's':
user_cmp = seq_cmp_seq;
break;

case 'c':
user_cmp = seq_cmp_rlo;
break;

case 'R':
user_cmp = seq_cmp_hash;
Expand Down