1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
33 // ==========================================================================
34 // Tests for the SeqAn model store, I/O functionality.
35 // ==========================================================================
36 
37 #include <seqan/basic.h>  // For test functionality.
38 #include <seqan/store.h>  // Header under test.
39 
40 using namespace seqan;
41 
SEQAN_DEFINE_TEST(test_store_io_bam_read)42 SEQAN_DEFINE_TEST(test_store_io_bam_read)
43 {
44   /*
45     // Construct name to reference FASTA files.
46     char fastaBuffer[1023];
47     strcpy(fastaBuffer, getAbsolutePath(""));
48     strcat(fastaBuffer, "/projects/tests/store/toy.fa");
49     // Construct file name to SAM file.
50     char samBuffer[1023];
51     strcpy(samBuffer, getAbsolutePath(""));
52     strcat(samBuffer, "/projects/tests/store/toy.sam");
53     // Construct file name to BAM file.
54     char bamBuffer[1023];
55     strcpy(bamBuffer, getAbsolutePath(""));
56     strcat(bamBuffer, "/projects/tests/store/toy.bam");
57 
58     // Load FragmentStore from SAM and BAM file.
59     FragmentStore<> samStore;
60     loadContigs(samStore, fastaBuffer);
61     FILE * samFp = fopen(samBuffer, "rb");
62     read(samFp, samStore, Sam());
63     fclose(samFp);
64 
65     FragmentStore<> bamStore;
66     loadContigs(bamStore, fastaBuffer);
67     samfile_t * bamFp = samopen(bamBuffer, "rb", 0);
68     read(bamFp, bamStore, Bam());
69     samclose(bamFp);
70 
71     // Check that the stores are the same.
72     SEQAN_ASSERT_EQ(length(samStore.alignedReadStore), length(bamStore.alignedReadStore));
73     for (unsigned i = 0; i < length(samStore.alignedReadStore); ++i)
74         SEQAN_ASSERT(samStore.alignedReadStore[i] == bamStore.alignedReadStore[i]);
75     SEQAN_ASSERT_EQ(length(samStore.alignedReadTagStore), length(bamStore.alignedReadTagStore));
76     for (unsigned i = 0; i < length(samStore.alignedReadTagStore); ++i)
77         SEQAN_ASSERT(samStore.alignedReadTagStore[i] == bamStore.alignedReadTagStore[i]);
78     SEQAN_ASSERT_EQ(length(samStore.alignQualityStore), length(bamStore.alignQualityStore));
79     for (unsigned i = 0; i < length(samStore.alignQualityStore); ++i)
80         SEQAN_ASSERT(samStore.alignQualityStore[i] == bamStore.alignQualityStore[i]);
81     SEQAN_ASSERT_EQ(length(samStore.contigFileStore), length(bamStore.contigFileStore));
82     for (unsigned i = 0; i < length(samStore.contigFileStore); ++i)
83         SEQAN_ASSERT(samStore.contigFileStore[i] == bamStore.contigFileStore[i]);
84     SEQAN_ASSERT_EQ(length(samStore.contigNameStore), length(bamStore.contigNameStore));
85     for (unsigned i = 0; i < length(samStore.contigNameStore); ++i)
86         SEQAN_ASSERT(samStore.contigNameStore[i] == bamStore.contigNameStore[i]);
87     SEQAN_ASSERT_EQ(length(samStore.contigStore), length(bamStore.contigStore));
88     for (unsigned i = 0; i < length(samStore.contigStore); ++i)
89         SEQAN_ASSERT(samStore.contigStore[i] == bamStore.contigStore[i]);
90     SEQAN_ASSERT_EQ(length(samStore.libraryNameStore), length(bamStore.libraryNameStore));
91     for (unsigned i = 0; i < length(samStore.libraryNameStore); ++i)
92         SEQAN_ASSERT(samStore.libraryNameStore[i] == bamStore.libraryNameStore[i]);
93     SEQAN_ASSERT_EQ(length(samStore.libraryStore), length(bamStore.libraryStore));
94     for (unsigned i = 0; i < length(samStore.libraryStore); ++i)
95         SEQAN_ASSERT(samStore.libraryStore[i] == bamStore.libraryStore[i]);
96     SEQAN_ASSERT_EQ(length(samStore.matePairNameStore), length(bamStore.matePairNameStore));
97     for (unsigned i = 0; i < length(samStore.matePairNameStore); ++i)
98         SEQAN_ASSERT(samStore.matePairNameStore[i] == bamStore.matePairNameStore[i]);
99     SEQAN_ASSERT_EQ(length(samStore.matePairStore), length(bamStore.matePairStore));
100     for (unsigned i = 0; i < length(samStore.matePairStore); ++i)
101         SEQAN_ASSERT(samStore.matePairStore[i] == bamStore.matePairStore[i]);
102     SEQAN_ASSERT_EQ(length(samStore.readNameStore), length(bamStore.readNameStore));
103     for (unsigned i = 0; i < length(samStore.readNameStore); ++i)
104         SEQAN_ASSERT(samStore.readNameStore[i] == bamStore.readNameStore[i]);
105     SEQAN_ASSERT_EQ(length(samStore.readSeqStore), length(bamStore.readSeqStore));
106     for (unsigned i = 0; i < length(samStore.readSeqStore); ++i)
107         SEQAN_ASSERT(samStore.readSeqStore[i] == bamStore.readSeqStore[i]);
108     SEQAN_ASSERT_EQ(length(samStore.readStore), length(bamStore.readStore));
109     for (unsigned i = 0; i < length(samStore.readStore); ++i)
110         SEQAN_ASSERT(samStore.readStore[i] ==  bamStore.readStore[i]);
111 
112     // TODO(holtgrew): Actually check contents.
113     // */
114 }
115 
SEQAN_DEFINE_TEST(test_store_io_bam_write)116 SEQAN_DEFINE_TEST(test_store_io_bam_write)
117 {
118     // Construct name to reference FASTA files.
119     char fastaBuffer[1023];
120     strcpy(fastaBuffer, getAbsolutePath(""));
121     strcat(fastaBuffer, "/projects/tests/store/toy.fa");
122     // strcat(fastaBuffer, "/projects/tests/store/ex1.fa");
123     // Construct file name to SAM file.
124     char samBuffer[1023];
125     strcpy(samBuffer, getAbsolutePath(""));
126     strcat(samBuffer, "/projects/tests/store/toy.sam");
127     // strcat(samBuffer, "/projects/tests/store/ex1.sam");
128     // Construct path to a temporary output file.
129     char tmpBuffer[1023];
130     strcpy(tmpBuffer, SEQAN_TEMP_FILENAME());
131 
132     // Load FragmentStore from SAM and BAM file.
133     FragmentStore<> samStore;
134     loadContigs(samStore, fastaBuffer);
135     FILE * samFp = fopen(samBuffer, "rb");
136     read(samFp, samStore, Sam());
137     fclose(samFp);
138 
139     // Write out BAM file.
140     // write(tmpBuffer, samStore, Bam());
141     FILE * outFb = fopen(tmpBuffer, "wb");
142     write(outFb, samStore, Sam());
143     fclose(outFb);
144 }
145