1 /*
2  * This file is part of Artemis
3  *
4  * Copyright (C) 2014  Genome Research Limited
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  */
21 package uk.ac.sanger.artemis.components.alignment;
22 
23 import static org.junit.Assert.assertTrue;
24 
25 import java.awt.GraphicsEnvironment;
26 import java.net.URL;
27 import java.util.Hashtable;
28 import java.util.List;
29 
30 import uk.ac.sanger.artemis.io.Utils;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 
34 import uk.ac.sanger.artemis.EntryGroup;
35 import uk.ac.sanger.artemis.Feature;
36 import uk.ac.sanger.artemis.FeatureVector;
37 import uk.ac.sanger.artemis.components.EntryEdit;
38 import uk.ac.sanger.artemis.components.alignment.BamUtils;
39 import uk.ac.sanger.artemis.components.alignment.BamView;
40 import uk.ac.sanger.artemis.components.alignment.ReadCount;
41 
42 public class MappedReadsTest
43 {
44   private static BamView bv;
45   private static FeatureVector fv;
46 
47   @BeforeClass
setUp()48   public static void setUp()
49   {
50     // ignore if in headless mode with no x11
51     if(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless())
52       return;
53     URL entryFile = MappedReadsTest.class.getResource("/data/MAL_8h.bam");
54     System.setProperty("bam", entryFile.getFile());
55     final EntryGroup egrp = Utils.getEntryGroup("/data/MAL1.embl.gz");
56     final EntryEdit ee = new EntryEdit(egrp);
57     ee.setVisible(true);
58 
59     while( (bv = ee.getJamView()) == null)
60     {
61       // wait for BamView to be constructed
62       try {
63         Thread.sleep(100);
64       } catch(Exception e){};
65     }
66 
67     // get a gene feature
68     fv = new FeatureVector();
69     final FeatureVector features = egrp.getAllFeatures();
70     for(int i=0; i<features.size(); i++)
71     {
72       Feature f = features.elementAt(i);
73       if(f.getSystematicName().equals("PFA0110w"))
74         fv.add(f);
75     }
76   }
77 
78   @Test
79   /**
80    * Test the read count for a gene
81    */
readCounts()82   public void readCounts()
83   {
84     // ignore if in headless mode with no x11
85     if(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless())
86       return;
87 
88     final Hashtable<String, List<ReadCount>> featureReadCount =
89         BamUtils.calculateMappedReads(bv, fv, false, true, false, null, null);
90     final List<ReadCount> cnts = featureReadCount.get("PFA0110w");
91 
92     ReadCount c = cnts.get(0);
93     assertTrue(1495.f == c.senseCnt);
94     assertTrue(998.f == c.antiCnt);
95   }
96 
97   @Test
98   /**
99    * Read count for a gene excluding the intron
100    */
readCountsExcludeIntron()101   public void readCountsExcludeIntron()
102   {
103     // ignore if in headless mode with no x11
104     if(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless())
105       return;
106 
107     final Hashtable<String, List<ReadCount>> featureReadCount =
108         BamUtils.calculateMappedReads(bv, fv, false, false, false, null, null);
109     final List<ReadCount> cnts = featureReadCount.get("PFA0110w");
110 
111     ReadCount c = cnts.get(0);
112     assertTrue(1494.f == c.senseCnt);
113     assertTrue(997.f == c.antiCnt);
114   }
115 
116   @Test
117   /**
118    * Tes the read count for a gene not including those in the intron
119    */
rpkm()120   public void rpkm()
121   {
122     // ignore if in headless mode with no x11
123     if(GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless())
124       return;
125 
126     String refName = (String) bv.getCombo().getSelectedItem();
127     int thisLength = bv.getSeqLengths().get(refName);
128     int mappedReads[] = BamUtils.calc(bv, refName, thisLength,
129         false, null);
130 
131     Hashtable<String, List<ReadCount>> featureReadCount =
132         BamUtils.calculateMappedReads(bv, fv, false, true, false, mappedReads, null);
133     final List<ReadCount> cnts = featureReadCount.get("PFA0110w");
134 
135     ReadCount c = cnts.get(0);
136     assertTrue(183768.719f == c.senseCnt);
137     assertTrue(122676.375f == c.antiCnt);
138   }
139 }
140