use of com.milaboratory.core.sequence.NucleotideSequence in project mixcr by milaboratory.
the class TargetBuilder method generateSequence.
public static NucleotideSequence generateSequence(VDJCGenes genes, String model, RandomGenerator rg) {
// Pre-processing
model = preProcessModel(model);
// Building sequence
SequenceBuilder<NucleotideSequence> builder = NucleotideSequence.ALPHABET.createBuilder();
// builder.ensureCapacity(model.length());
Matcher matcher = modelParserPattern.matcher(model);
ReferencePoint leftPoint = null;
ReferencePoint rightPoint;
int prevPosition = 0;
while (matcher.find()) {
if (matcher.start() != prevPosition)
throw new IllegalArgumentException("Can't parse " + model.substring(prevPosition, matcher.start()));
prevPosition = matcher.end();
String rpGroup = matcher.group(refPointGroupId);
String gf1Group = matcher.group(geneFeaturePoint1GroupId);
String gf2Group = matcher.group(geneFeaturePoint2GroupId);
String nGroup = matcher.group(nGroupId);
String atgcGroup = matcher.group(atgcGroupId);
if (rpGroup != null)
leftPoint = ReferencePoint.parse(rpGroup);
else if (gf1Group != null) {
leftPoint = ReferencePoint.parse(gf1Group);
rightPoint = ReferencePoint.parse(gf2Group);
GeneFeature geneFeature = new GeneFeature(leftPoint, rightPoint);
VDJCGene gene = genes.geneByGeneType(geneFeature.getGeneType());
NucleotideSequence seqGF = gene.getFeature(geneFeature);
if (seqGF == null)
throw new RuntimeException("No sequence for feature " + geneFeature);
builder.append(seqGF);
leftPoint = rightPoint;
} else if (nGroup != null)
for (int j = 0; j < nGroup.length(); j++) builder.append((byte) rg.nextInt(4));
else if (atgcGroup != null)
builder.append(new NucleotideSequence(atgcGroup));
else {
for (int i = 0; i < 4; i++) {
GeneType geneType = GeneType.VDJC_REFERENCE[i];
String group = matcher.group(i + vGroupId);
if (group != null) {
if (leftPoint == null || leftPoint.getGeneType() != geneType)
throw new IllegalArgumentException("No reference point for " + group);
rightPoint = leftPoint.move(group.length());
GeneFeature geneFeature = new GeneFeature(leftPoint, rightPoint);
VDJCGene gene = genes.geneByGeneType(geneType);
NucleotideSequence seqGF = gene.getFeature(geneFeature);
if (seqGF == null)
throw new RuntimeException("No sequence for feature " + geneFeature);
for (int j = 0; j < seqGF.size(); j++) {
char ch = group.charAt(j);
byte n = seqGF.codeAt(j);
if (Character.isLowerCase(ch))
builder.append((byte) ((n + 1 + rg.nextInt(3)) % 3));
else
builder.append(n);
}
}
}
}
}
if (model.length() != prevPosition)
throw new IllegalArgumentException("Can't parse " + model.substring(prevPosition, model.length()));
return builder.createAndDestroy();
}
Aggregations