Repository navigation
Expand file tree
/
Copy pathTestDNAAlignment.java
More file actions
168 lines (144 loc) · 6.41 KB
/
Copy pathTestDNAAlignment.java
File metadata and controls
168 lines (144 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* BioJava development code
*
* This code may be freely distributed and modified under the terms of the GNU
* Lesser General Public Licence. This should be distributed with the code. If
* you do not have a copy, see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on Oct 5, 2011
* Created by Andreas Prlic
*
* @since 3.0.2
*/
package org.biojava.nbio.alignment;
import org.biojava.nbio.core.alignment.matrices.SubstitutionMatrixHelper;
import org.biojava.nbio.alignment.Alignments.PairwiseSequenceAlignerType;
import org.biojava.nbio.alignment.template.PairwiseSequenceAligner;
import org.biojava.nbio.core.alignment.template.Profile;
import org.biojava.nbio.core.alignment.template.SequencePair;
import org.biojava.nbio.core.alignment.template.SubstitutionMatrix;
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
import org.biojava.nbio.core.sequence.DNASequence;
import org.biojava.nbio.core.sequence.compound.AmbiguityDNACompoundSet;
import org.biojava.nbio.core.sequence.compound.DNACompoundSet;
import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
import org.biojava.nbio.core.util.ConcurrencyTools;
import org.junit.Assert;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class TestDNAAlignment {
private static final double PRECISION = 0.00000001;
@Test
public void testDNAAlignment() {
try {
List<DNASequence> lst = getDNAFASTAFile();
Profile<DNASequence, NucleotideCompound> profile = Alignments.getMultipleSequenceAlignment(lst);
Assert.assertTrue(profile.getSize() == 10);
Assert.assertTrue(profile.getAlignedSequence(1).getSequenceAsString().length() > 50);
// here how to print the MSA:
//System.out.printf("MSA:%n%s%n", profile);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
ConcurrencyTools.shutdown();
}
private static List<DNASequence> getDNAFASTAFile() throws Exception {
InputStream inStream = TestDNAAlignment.class.getResourceAsStream(String.format("/dna-fasta.txt"));
LinkedHashMap<String, DNASequence> fastas = FastaReaderHelper.readFastaDNASequence(inStream);
List<DNASequence> sequences = new ArrayList<DNASequence>();
for (String key : fastas.keySet()) {
DNASequence seq = fastas.get(key);
sequences.add(seq);
}
return sequences;
}
/**
* @author brandstaetter
*/
@Test
public void testDNAMultipleAlignmentWithMixedCompoundSets() throws CompoundNotFoundException {
DNASequence target = new DNASequence("ACTGACGTGTAGCTGACTGA", DNACompoundSet.getDNACompoundSet());
DNASequence query = new DNASequence("ACTGACGTGTAGCTGACTGTA", AmbiguityDNACompoundSet.getDNACompoundSet());
List<DNASequence> lst = new ArrayList<DNASequence>();
lst.add(target);
lst.add(query);
try {
@SuppressWarnings("unused")
Profile<DNASequence, NucleotideCompound> profile = Alignments.getMultipleSequenceAlignment(lst);
Assert.fail("Alignments.getMultipleSequenceAlignment(lst) expected exception with differing compound sets");
} catch (IllegalArgumentException ex) {
// expected exception
}
}
/**
* @author brandstaetter
*/
@Test
public void testDNAPairwiseAlignmentWithMixedCompoundSets() throws CompoundNotFoundException {
DNASequence target = new DNASequence("ACTGACGTGTAGCTGACTGA", DNACompoundSet.getDNACompoundSet());
DNASequence query = new DNASequence("ACTGACGTGTAGCTGACTGT", AmbiguityDNACompoundSet.getDNACompoundSet());
SubstitutionMatrix<NucleotideCompound> matrix = SubstitutionMatrixHelper.getNuc4_4();
SimpleGapPenalty gapP = new SimpleGapPenalty();
gapP.setOpenPenalty((short) 5);
gapP.setExtensionPenalty((short) 2);
try {
@SuppressWarnings("unused")
SequencePair<DNASequence, NucleotideCompound> psa = Alignments.getPairwiseAlignment(query, target, PairwiseSequenceAlignerType.LOCAL, gapP, matrix);
Assert.fail("Alignments.getPairwiseAlignment() expected exception with differing compound sets");
} catch (IllegalArgumentException ex) {
// expected exception
}
}
/**
* @author Daniel Cameron
*/
@Test
public void testMixedCaseInputStringsMatchUnderlyingBases() throws CompoundNotFoundException {
DNASequence target = new DNASequence("AAAAAAAAGTC", DNACompoundSet.getDNACompoundSet());
DNASequence query = new DNASequence("aaaaaaaagtc", DNACompoundSet.getDNACompoundSet());
SubstitutionMatrix<NucleotideCompound> matrix = SubstitutionMatrixHelper.getNuc4_4();
SimpleGapPenalty gapP = new SimpleGapPenalty((short)5, (short)2);
// should be a full match with +5 per match
Assert.assertEquals(5.0 * query.getLength(), Alignments.getPairwiseAligner(query, target, PairwiseSequenceAlignerType.LOCAL, gapP, matrix).getScore(), PRECISION);
}
/**
* @author Daniel Cameron
*/
@Test
public void testNoAlignedBases() throws CompoundNotFoundException {
DNASequence target = new DNASequence("A", DNACompoundSet.getDNACompoundSet());
DNASequence query = new DNASequence("T", DNACompoundSet.getDNACompoundSet());
SubstitutionMatrix<NucleotideCompound> matrix = SubstitutionMatrixHelper.getNuc4_4();
SimpleGapPenalty gapP = new SimpleGapPenalty((short)0, (short)1);
PairwiseSequenceAligner<DNASequence, NucleotideCompound> aligner = Alignments.getPairwiseAligner(query, target, PairwiseSequenceAlignerType.GLOBAL, gapP, matrix);
Assert.assertEquals(2, aligner.getPair().getLength());
}
/**
* @author Daniel Cameron
*/
@Test
public void testLinearAlignment() throws CompoundNotFoundException {
DNASequence query = new DNASequence("GTAAAAG", DNACompoundSet.getDNACompoundSet());
DNASequence target = new DNASequence("GAAAACGTTTTTTTTTT", DNACompoundSet.getDNACompoundSet());
SubstitutionMatrix<NucleotideCompound> matrix = SubstitutionMatrixHelper.getNuc4_4();
SimpleGapPenalty gapP = new SimpleGapPenalty((short)0, (short)3);
PairwiseSequenceAligner<DNASequence, NucleotideCompound> aligner = Alignments.getPairwiseAligner(query, target, PairwiseSequenceAlignerType.GLOBAL, gapP, matrix);
Assert.assertEquals(String.format("GTAAAA-G----------%nG-AAAACGTTTTTTTTTT%n"), aligner.getPair().toString());;
}
}