Skip to content

Commit

Permalink
massive restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
hasindu2008 committed Feb 7, 2024
1 parent 1dd7a20 commit 6fa302d
Show file tree
Hide file tree
Showing 15 changed files with 1,194 additions and 1,050 deletions.
26 changes: 23 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ OBJ = $(BUILD_DIR)/main.o \
$(BUILD_DIR)/misc.o \
$(BUILD_DIR)/sim.o \
$(BUILD_DIR)/thread.o \
$(BUILD_DIR)/format.o \
$(BUILD_DIR)/gensig.o \
$(BUILD_DIR)/genread.o \
$(BUILD_DIR)/ref.o \

PREFIX = /usr/local
VERSION = `git describe --tags`
Expand All @@ -29,7 +33,10 @@ endif
$(BINARY): $(OBJ) slow5lib/lib/libslow5.a
$(CC) $(CFLAGS) $(OBJ) slow5lib/lib/libslow5.a $(LDFLAGS) -o $@

$(BUILD_DIR)/main.o: src/main.c src/misc.h src/error.h src/sq.h
HEADERS = src/error.h src/format.h src/kseq.h src/misc.h src/model.h \
src/prof.h src/rand.h src/ref.h src/seq.h src/sq.h src/str.h src/version.h

$(BUILD_DIR)/main.o: src/main.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/model.o: src/model.c src/model.h src/misc.h
Expand All @@ -38,12 +45,25 @@ $(BUILD_DIR)/model.o: src/model.c src/model.h src/misc.h
$(BUILD_DIR)/thread.o: src/thread.c
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/misc.o: src/misc.c
$(BUILD_DIR)/misc.o: src/misc.c src/error.h
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/sim.o: src/sim.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/format.o: src/format.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/sim.o: src/sim.c src/ref.h src/misc.h src/str.h
$(BUILD_DIR)/gensig.o: src/gensig.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/genread.o: src/genread.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@

$(BUILD_DIR)/ref.o: src/ref.c $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $@


slow5lib/lib/libslow5.a:
$(MAKE) -C slow5lib zstd=$(zstd) no_simd=$(no_simd) zstd_local=$(zstd_local) lib/libslow5.a

Expand Down
123 changes: 123 additions & 0 deletions src/format.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* @file format.c
** squigulator, a nanopore signal simulator
**
** @@
******************************************************************************/
/*
MIT License
Copyright (c) 2023 Hasindu Gamaarachchi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "format.h"
#include "error.h"
#include "str.h"
#include "seq.h"
#include "version.h"

aln_t *init_aln() {
aln_t *aln = (aln_t*)malloc(sizeof(aln_t));
MALLOC_CHK(aln);
aln->ss_n = 0;
aln->ss_c = 1000; //todo can be the readlen
aln->ss = (int32_t*)malloc(aln->ss_c * sizeof(int32_t));
MALLOC_CHK(aln->ss);

aln->prefix_end = 0;
return aln;
}

void free_aln(aln_t *aln) {
free(aln->ss);
free(aln);
}

char *paf_str(aln_t *aln) {
kstring_t str;
kstring_t *sp = &str;
str_init(sp, aln->tlen*3+1000);

assert(aln->sig_end > aln->sig_start);
sprintf_append(sp, "%s\t%ld\t%ld\t%ld\t%c\t", aln->read_id, (long)aln->len_raw_signal,(long)aln->sig_start, (long)aln->sig_end, aln->strand);
//target: name, start, end
sprintf_append(sp, "%s\t%ld\t%ld\t%ld\t", aln->tid, aln->tlen, (long)aln->t_st, (long)aln->t_end);
//residue matches, block len, mapq
int64_t blocklen = aln->t_end - aln->t_st;
blocklen = blocklen > 0 ? blocklen : -blocklen;
sprintf_append(sp, "%ld\t%ld\t%d\t",blocklen,blocklen,255);
sprintf_append(sp, "sc:f:%f\t",1.0);
sprintf_append(sp, "sh:f:%f\t",0.0);
sprintf_append(sp, "ss:Z:");

int8_t rna = aln->t_st > aln->t_end ? 1 : 0;
for(int i=0; i<aln->ss_n; i++) {
int idx = rna ? aln->ss_n - i - 1 : i;
sprintf_append(sp, "%d,", aln->ss[idx]);
}

sprintf_append(sp, "\n");
str.s[str.l] = '\0';
return sp->s;
}


void sam_hdr_wr(FILE *fp, ref_t *ref) {
for(int i=0; i<ref->num_ref; i++) {
fprintf(fp, "@SQ\tSN:%s\tLN:%ld\n", ref->ref_names[i], (long)ref->ref_lengths[i]);
}
fprintf(fp, "@PG\tID:squigulator\tPN:squigulator\tVN:%s\n", SQ_VERSION);

}

char *sam_str(aln_t *aln, char *seq, char *rname, int32_t ref_pos_st) {
kstring_t str;
kstring_t *sp = &str;
str_init(sp, aln->tlen*3+1000);

assert(aln->sig_end > aln->sig_start);

int flag = aln->strand == '+' ? 0 : 16;
sprintf_append(sp, "%s\t%d\t", aln->read_id, flag); //qname, flag
sprintf_append(sp, "%s\t%ld\t%d\t", rname, (long)ref_pos_st+1, 255); //rname, pos, mapq
sprintf_append(sp, "%ldM\t%c\t%d\t%d\t",strlen(seq), '*', 0, 0); //cigar, rnext, pnext, tlen
if (aln->strand == '+'){
str_cat(sp, seq, strlen(seq));//seq
} else {
char *rc = reverse_complement(seq);
str_cat(sp, rc, strlen(rc));
free(rc);
}
sprintf_append(sp, "\t%c\t",'*'); //seq, qual

sprintf_append(sp, "si:Z:%ld,%ld,%ld,%ld\t",(long)aln->sig_start, (long)aln->sig_end, (long)aln->si_st_ref, (long)aln->si_end_ref);
sprintf_append(sp, "ss:Z:");

int8_t rna = aln->t_st > aln->t_end ? 1 : 0;
for(int i=0; i<aln->ss_n; i++) {
int idx = rna ? aln->ss_n - i - 1 : i;
sprintf_append(sp, "%d,", aln->ss[idx]);
}

sprintf_append(sp, "\n");
str.s[str.l] = '\0';
return sp->s;
}

34 changes: 34 additions & 0 deletions src/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef SQ_FORMAT_H
#define SQ_FORMAT_H

#include <stdint.h>
#include <stdio.h>
#include "ref.h"

typedef struct {
char *read_id;
int64_t len_raw_signal;
int64_t sig_start;
int64_t sig_end;
char strand;
char *tid;
int64_t tlen;
int64_t t_st;
int64_t t_end;

int64_t si_st_ref; //for sam
int64_t si_end_ref;
int32_t *ss;
int64_t ss_n;
int64_t ss_c;

int64_t prefix_end;
} aln_t;

aln_t *init_aln();
void free_aln(aln_t *aln);
char *paf_str(aln_t *aln);
void sam_hdr_wr(FILE *fp, ref_t *ref);
char *sam_str(aln_t *aln, char *seq, char *rname, int32_t ref_pos_st);

#endif
Loading

0 comments on commit 6fa302d

Please sign in to comment.