JAL-2909 Bam file parsing with insertions, first pass
[jalview.git] / src / jalview / io / BamFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.datamodel.CigarParser;
24 import jalview.datamodel.Sequence;
25 import jalview.datamodel.SequenceI;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.SortedMap;
30
31 import htsjdk.samtools.SAMRecord;
32 import htsjdk.samtools.SAMRecordIterator;
33 import htsjdk.samtools.SamReader;
34 import htsjdk.samtools.SamReaderFactory;
35 import htsjdk.samtools.ValidationStringency;
36
37 public class BamFile extends AlignFile
38 {
39
40   SamReader fileReader;
41
42   /**
43    * Creates a new BamFile object.
44    */
45   public BamFile()
46   {
47   }
48
49   /**
50    * Creates a new BamFile object.
51    * 
52    * @param inFile
53    *          DOCUMENT ME!
54    * @param sourceType
55    *          DOCUMENT ME!
56    * 
57    * @throws IOException
58    *           DOCUMENT ME!
59    */
60   public BamFile(String inFile, DataSourceType sourceType)
61           throws IOException
62   {
63     super(inFile, sourceType);
64     final SamReaderFactory factory = SamReaderFactory.makeDefault()
65             .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,
66                     SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
67             .validationStringency(ValidationStringency.SILENT);
68     fileReader = factory.open(new File(inFile));
69   }
70
71   public BamFile(FileParse source) throws IOException
72   {
73     final SamReaderFactory factory = SamReaderFactory.makeDefault()
74             .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,
75                     SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
76             .validationStringency(ValidationStringency.SILENT);
77
78     // File-based bam
79     fileReader = factory.open(source.inFile);
80     parse();
81   }
82   
83   @Override
84   public String print(SequenceI[] seqs, boolean jvsuffix)
85   {
86     // TODO Auto-generated method stub
87     return null;
88   }
89
90   @Override
91   public void parse() throws IOException
92   {
93     CigarParser parser = new CigarParser('-');
94     SAMRecordIterator it = fileReader.iterator();
95     SortedMap<Integer, Integer> insertions = parser.getInsertions(it);
96     it.close();
97
98     it = fileReader.iterator();
99     while (it.hasNext())
100     {
101       SAMRecord rec = it.next();
102       int start = rec.getStart();
103       int end = rec.getEnd();
104
105       SequenceI seq = new Sequence(rec.getReadName(), rec.getReadString());
106
107       String cigarredRead = parser.parseCigarToSequence(rec, insertions);
108
109       SequenceI alsq = seq.deriveSequence();
110       alsq.setSequence(cigarredRead);
111       alsq.setStart(start);
112       alsq.setEnd(end);
113       seqs.add(alsq);
114     }
115   }
116
117
118   
119
120 }