Repository navigation
Expand file tree
/
Copy pathSequenceTools.java
More file actions
137 lines (121 loc) · 3.97 KB
/
Copy pathSequenceTools.java
File metadata and controls
137 lines (121 loc) · 3.97 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
/*
* 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/
*
*/
package org.biojava.nbio.core.util;
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
import org.biojava.nbio.core.sequence.DNASequence;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.template.Sequence;
public class SequenceTools {
protected static final String NUCLEOTIDE_LETTERS = "GCTAUXN";
/**
* Cyclically permute the characters in {@code string} <em>forward</em> by {@code n} elements.
* @param string The string to permute
* @param n The number of characters to permute by; can be positive or negative; values greater than the length of the array are acceptable
*/
public static String permuteCyclic(String string, int n) {
// single letters are char[]; full names are Character[]
Character[] permuted = new Character[string.length()];
char[] c = string.toCharArray();
Character[] charArray = new Character[c.length];
for (int i = 0; i < c.length; i++) {
charArray[i] = c[i];
}
permuteCyclic(charArray, permuted, n);
char[] p = new char[permuted.length];
for (int i = 0; i < p.length; i++) {
p[i] = permuted[i];
}
return String.valueOf(p);
}
/**
* Cyclically permute {@code array} <em>forward</em> by {@code n} elements.
* @param array The original result; will not be changed
* @param fill The permuted result will be filled into this array
* @param n The number of elements to permute by; can be positive or negative; values greater than the length of the array are acceptable
*/
public static <T> void permuteCyclic(T[] array, T[] fill, int n) {
if (array.length != fill.length) throw new IllegalArgumentException("Lengths do not match");
if (n < 0) n = array.length + n;
while (n > array.length) {
n -= array.length;
}
for (int i = 0; i < array.length; i++) {
if (i + n < array.length) {
fill[i] = array[i + n];
} else {
fill[i] = array[i - array.length + n];
}
}
}
public static int percentNucleotideSequence(String sequence)
{
if (sequence == null || sequence.length() == 0) return 0;
int l = sequence.length();
int n =0;
for (int i = 0; i < l; i++)
{
if (NUCLEOTIDE_LETTERS.indexOf(sequence.charAt(i)) < 0)
{
continue;
}
n++;
}
return (100 * n) / l;
}
public static boolean isNucleotideSequence(String sequence)
{
if (sequence == null || sequence.length() == 0) return false;
int l = sequence.length();
for (int i = 0; i < l; i++)
{
if (NUCLEOTIDE_LETTERS.indexOf(sequence.charAt(i)) < 0)
{
return false;
}
}
return true;
}
public Sequence<?> getSequenceFromString(String sequence) throws CompoundNotFoundException {
if( isNucleotideSequence(sequence)) {
return new DNASequence(sequence);
} else {
return new ProteinSequence(sequence);
}
}
/** A method to check whether an array of sequences contains at least two sequences having an equal length.
*
* @param sequences the array of {@link org.biojava.nbio.core.sequence.ProteinSequence} sequences
* @return true if any two sequences are of an equal length
*/
public static boolean equalLengthSequences(ProteinSequence[] sequences) {
for (int i=0; i<sequences.length-1; i++) {
if (sequences[i]==null)
continue;
for (int j=i+1; j<sequences.length; j++) {
if (sequences[j]==null)
continue;
if (sequences[i].getLength() == sequences[j].getLength())
return true;
}
}
return false;
}
}