+ minor changes in the paths
[qpalma.git] / tools / data_tools / datastructures.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "datastructures.h"
5
6 Read* read_alloc(int _size) {
7 Read* newRead = (Read*) malloc(sizeof(struct read));
8 newRead->chr = 0;
9 newRead->pos = 0;
10 newRead->seq = 0;
11 newRead->id = 0;
12 newRead->strand = ' ';
13 newRead->mismatch = 0;
14 newRead->occurrence = 0;
15 newRead->size = _size;
16 newRead->cut = 0;
17 newRead->prb = 0;
18 newRead->cal_prb = 0;
19 newRead->chastity = 0;
20
21 return newRead;
22 }
23
24 void free_read(Read* oldRead) {
25 if(oldRead->seq != 0)
26 free(oldRead->seq);
27 if(oldRead->prb != 0)
28 free(oldRead->prb);
29 if(oldRead->cal_prb != 0)
30 free(oldRead->cal_prb);
31 if(oldRead->chastity != 0)
32 free(oldRead->chastity);
33 }
34
35 Read* create_read(int chr, int pos, char* seq, unsigned long id, char strand, int mismatch,
36 int occurrence, int size, int cut, char* prb, char* cal_prb, char* chastity) {
37
38 Read* newRead = read_alloc(size);
39
40 newRead->chr = chr;
41 newRead->pos = pos;
42 int _size = strlen(seq);
43 newRead->seq = malloc(sizeof(char)*(_size+1));
44 newRead->seq = strncpy(newRead->seq,seq,_size);
45 newRead->seq[_size] = '\0';
46 newRead->id = id;
47 newRead->strand = strand;
48 newRead->mismatch = mismatch;
49 newRead->occurrence = occurrence;
50 newRead->size = size;
51 newRead->cut = cut;
52 newRead->prb = malloc(sizeof(char)*(size+1));
53 newRead->prb = strncpy(newRead->prb,prb,size);
54 newRead->prb[size] = '\0';
55 newRead->cal_prb = malloc(sizeof(char)*(size+1));
56 newRead->cal_prb = strncpy(newRead->cal_prb,cal_prb,size);
57 newRead->cal_prb[size] = '\0';
58 newRead->chastity = malloc(sizeof(char)*(size+1));
59 newRead->chastity = strncpy(newRead->chastity,chastity,size);
60 newRead->chastity[size] = '\0';
61
62 return newRead;
63 }
64
65 struct gene* gene_alloc(void) {
66 struct gene* newGene = (struct gene*) malloc(sizeof(struct gene));
67 newGene->exon_starts = malloc(2*sizeof(int));
68 newGene->exon_stops = malloc(2*sizeof(int));
69 newGene->num_exons = 0;
70 newGene->max_exons = 2;
71 newGene->id = 0;
72 return newGene;
73 }
74
75 void free_gene(struct gene* oldGene) {
76 free(oldGene->exon_starts);
77 free(oldGene->exon_stops);
78 if( oldGene->id != 0) {
79 free(oldGene->id);
80 }
81 }
82
83 void add_exon(struct gene* currentGene,int start, int stop) {
84 int idx = currentGene->num_exons;
85
86 if ( idx >= currentGene->max_exons) {
87 currentGene->exon_starts = realloc(currentGene->exon_starts,sizeof(int)*2*currentGene->max_exons);
88 currentGene->exon_stops = realloc(currentGene->exon_stops,sizeof(int)*2*currentGene->max_exons);
89 currentGene->max_exons *= 2;
90 }
91
92 currentGene->exon_starts[idx] = start;
93 currentGene->exon_stops[idx] = stop;
94 currentGene->num_exons++;
95 }