Skip to content

Commit e7bf919

Browse files
committed
Bugfix LoFreq variant import
1 parent 798f0f0 commit e7bf919

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

SequenceAnalysis/src/org/labkey/sequenceanalysis/run/analysis/NextCladeHandler.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -263,32 +263,57 @@ public static void processAndImportNextCladeAa(PipelineJob job, File jsonFile, i
263263
{
264264
JSONObject aa = aaSubstitutions.getJSONObject(i);
265265
int pos = aa.getInt("codon");
266-
String alt = aa.getString("queryAA");
266+
String aaName = aa.getString("gene");
267267

268-
List<VariantContext> vcList = consensusMap.get(pos);
269-
if (vcList == null)
268+
JSONObject range = aa.getJSONObject("nucRange");
269+
List<Integer> positions = new ArrayList<>();
270+
for (int p = range.getInt("begin");p <= range.getInt("end"); p++)
270271
{
271-
throw new PipelineJobException("Expected variant for position: " + pos);
272+
positions.add(p);
272273
}
273274

274-
VariantContext vc = null;
275-
for (VariantContext v : vcList)
275+
List<VariantContext> vcList = new ArrayList<>();
276+
for (Integer ntPos : positions)
276277
{
277-
if (v.getAlternateAlleles().get(0).getBaseString().equals(alt))
278+
if (consensusMap.containsKey(ntPos))
278279
{
279-
vc = v;
280-
break;
280+
vcList.addAll(consensusMap.get(ntPos));
281281
}
282282
}
283283

284-
if (vc == null)
284+
if (vcList.isEmpty())
285285
{
286-
throw new PipelineJobException("Unable to find matching allele at position: " + pos);
286+
throw new PipelineJobException("Expected variant for AA position: " + aaName + " " + pos);
287287
}
288288

289-
int depth = vc.getAttributeAsInt("GATK_DP", 0);
289+
Double depth;
290+
Double alleleDepth;
291+
Double af;
292+
Double dp;
293+
294+
if (vcList.size() == 1)
295+
{
296+
depth = (double)vcList.get(0).getAttributeAsInt("GATK_DP", 0);
297+
298+
List<Integer> depths = vcList.get(0).getAttributeAsIntList("DP4", 0);
299+
alleleDepth = (double)depths.get(2) + depths.get(3);
300+
dp = (double)vcList.get(0).getAttributeAsInt("DP", 0);
301+
af = vcList.get(0).getAttributeAsDouble("AF", 0.0);
302+
}
303+
else
304+
{
305+
job.getLogger().info("Multiple NT SNPs found at AA pos: " + aaName + " " + pos + ", was: " + vcList.size());
306+
depth = vcList.stream().mapToInt(x -> x.getAttributeAsInt("GATK_DP", 0)).summaryStatistics().getAverage();
307+
308+
alleleDepth = vcList.stream().mapToDouble(x -> {
309+
List<Integer> dps = x.getAttributeAsIntList("DP4", 0);
310+
return dps.get(2) + dps.get(3);
311+
}).summaryStatistics().getAverage();
312+
313+
af = vcList.stream().mapToDouble(x -> x.getAttributeAsDouble("AF", 0)).summaryStatistics().getAverage();
314+
dp = vcList.stream().mapToDouble(x -> x.getAttributeAsDouble("DP", 0)).summaryStatistics().getAverage();
315+
}
290316

291-
String aaName = aa.getString("gene");
292317
int refAaId = ViralSnpUtil.resolveGene(refNtId, aaName);
293318

294319
Map<String, Object> aaRow = new CaseInsensitiveHashMap<>();
@@ -300,22 +325,12 @@ public static void processAndImportNextCladeAa(PipelineJob job, File jsonFile, i
300325
aaRow.put("ref_aa", aa.getString("refAA"));
301326
aaRow.put("q_aa", aa.getString("queryAA"));
302327
aaRow.put("codon", aa.getString("queryCodon"));
303-
304-
JSONObject range = aa.getJSONObject("nucRange");
305-
List<String> positions = new ArrayList<>();
306-
for (int p = range.getInt("begin");p <= range.getInt("end"); p++)
307-
{
308-
positions.add(String.valueOf(p));
309-
}
310328
aaRow.put("ref_nt_positions", StringUtils.join(positions, ","));
311329

312-
List<Integer> depths = vc.getAttributeAsIntList("DP4", 0);
313-
int alleleDepth = depths.get(2) + depths.get(3);
314-
315330
aaRow.put("readcount", alleleDepth);
316331
aaRow.put("depth", depth);
317-
aaRow.put("adj_depth", vc.getAttribute("DP"));
318-
aaRow.put("pct", vc.getAttribute("AF"));
332+
aaRow.put("adj_depth", dp);
333+
aaRow.put("pct", af);
319334

320335
aaRow.put("createdby", job.getUser().getUserId());
321336
aaRow.put("created", job.getUser().getUserId());
@@ -324,19 +339,6 @@ public static void processAndImportNextCladeAa(PipelineJob job, File jsonFile, i
324339
aaRow.put("container", job.getContainer());
325340

326341
Table.insert(job.getUser(), aaTable, aaRow);
327-
328-
consensusMap.get(pos).remove(vc);
329-
330-
if (consensusMap.get(pos).isEmpty())
331-
{
332-
consensusMap.remove(pos);
333-
}
334-
}
335-
336-
if (!consensusMap.isEmpty())
337-
{
338-
consensusMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList()).stream().forEach(x -> job.getLogger().error("Unexpected variant: " + x.toString()));
339-
throw new PipelineJobException("Not all variants translated!");
340342
}
341343
}
342344

0 commit comments

Comments
 (0)