| 1 |
package org.broadinstitute.hellbender.utils.fermi; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.List; |
| 8 |
|
| 9 |
|
| 10 |
public final class FermiLiteAssembly { |
| 11 |
private final List<Contig> contigs; |
| 12 |
|
| 13 |
public FermiLiteAssembly( final List<Contig> contigs ) { |
| 14 |
this.contigs = Collections.unmodifiableList(contigs); |
| 15 |
} |
| 16 |
|
| 17 |
public int getNContigs() { return contigs.size(); } |
| 18 |
public Contig getContig( final int idx ) { return contigs.get(idx); } |
| 19 |
public List<Contig> getContigs() { return contigs; } |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public int computeN50() { |
| 24 |
final int nContigs = contigs.size(); |
| 25 |
if ( nContigs < 1 ) return 0; |
| 26 |
if ( nContigs == 1 ) return contigs.get(0).getSequence().length; |
| 27 |
|
| 28 |
|
| 29 |
final int[] lengths = new int[nContigs]; |
| 30 |
int totalLength = 0; |
| 31 |
for ( int idx = 0; idx != nContigs; ++idx ) { |
| 32 |
final int length = contigs.get(idx).getSequence().length; |
| 33 |
lengths[idx] = length; |
| 34 |
totalLength += length; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
Arrays.sort(lengths); |
| 39 |
|
| 40 |
int lengthSumDoubled = 0; |
| 41 |
|
| 42 |
for ( int idx = nContigs - 1; idx >= 0; --idx ) { |
| 43 |
lengthSumDoubled += 2 * lengths[idx]; |
| 44 |
if ( lengthSumDoubled >= totalLength ) return lengths[idx]; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
throw new ArithmeticException("impossible situation -- sum of array greater than twice the sum of each element"); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
public static final class Contig { |
| 53 |
private final byte[] sequence; |
| 54 |
private final byte[] perBaseCoverage; |
| 55 |
private final int nSupportingReads; |
| 56 |
private List<Connection> connections; |
| 57 |
|
| 58 |
public Contig( final byte[] sequence, final byte[] perBaseCoverage, final int nSupportingReads ) { |
| 59 |
this.sequence = sequence; |
| 60 |
this.perBaseCoverage = perBaseCoverage; |
| 61 |
this.nSupportingReads = nSupportingReads; |
| 62 |
this.connections = Collections.emptyList(); |
| 63 |
} |
| 64 |
|
| 65 |
public byte[] getSequence() { return sequence; } |
| 66 |
public byte[] getPerBaseCoverage() { return perBaseCoverage; } |
| 67 |
public int getNSupportingReads() { return nSupportingReads; } |
| 68 |
public List<Connection> getConnections() { return connections; } |
| 69 |
|
| 70 |
public void setConnections( final List<Connection> connections ) { |
| 71 |
this.connections = Collections.unmodifiableList(connections); |
| 72 |
} |
| 73 |
|
| 74 |
public Connection getSolePredecessor() { |
| 75 |
return getSingletonConnection(true); |
| 76 |
} |
| 77 |
|
| 78 |
public Connection getSoleSuccessor() { |
| 79 |
return getSingletonConnection(false); |
| 80 |
} |
| 81 |
|
| 82 |
public Connection getSingletonConnection( final boolean isRC ) { |
| 83 |
Connection singleton = null; |
| 84 |
for ( Connection conn : connections ) { |
| 85 |
if ( conn.isRC() == isRC ) { |
| 86 |
if ( singleton != null ) return null; |
| 87 |
singleton = conn; |
| 88 |
} |
| 89 |
} |
| 90 |
return singleton; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public static final class Connection { |
| 96 |
private final Contig target; |
| 97 |
private final int overlapLen; |
| 98 |
private final boolean isRC; |
| 99 |
private final boolean isTargetRC; |
| 100 |
|
| 101 |
public Connection( final Contig target, final int overlapLen, final boolean isRC, final boolean isTargetRC ) { |
| 102 |
this.target = target; |
| 103 |
this.overlapLen = overlapLen; |
| 104 |
this.isRC = isRC; |
| 105 |
this.isTargetRC = isTargetRC; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public Contig getTarget() { return target; } |
| 110 |
|
| 111 |
public int getOverlapLen() { return overlapLen; } |
| 112 |
|
| 113 |
public boolean isRC() { return isRC; } |
| 114 |
|
| 115 |
public boolean isTargetRC() { return isTargetRC; } |
| 116 |
|
| 117 |
|
| 118 |
public Connection rcConnection( final Contig contig ) { |
| 119 |
return new Connection(contig, overlapLen, !isTargetRC, !isRC); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public void writeGFA( final Writer writer ) throws IOException { |
| 125 |
final HashMap<Contig, Integer> idMap = new HashMap<>((int)((contigs.size()*4L)/3) + 1); |
| 126 |
int id = 0; |
| 127 |
for (final Contig contig : contigs) { |
| 128 |
idMap.put(contig, id++); |
| 129 |
} |
| 130 |
writer.write("H\tVN:Z:1.0\n"); |
| 131 |
for ( final Contig contig : contigs ) { |
| 132 |
final int contigId = idMap.get(contig); |
| 133 |
writer.write("S\ttig" + contigId + "\t" + new String(contig.getSequence()) + |
| 134 |
"\tLN:i:" + contig.getSequence().length + "\tRC:i:" + contig.getNSupportingReads() + "\n"); |
| 135 |
for ( final Connection connection : contig.getConnections() ) { |
| 136 |
final int targetId = idMap.get(connection.getTarget()); |
| 137 |
if ( contigId <= targetId ) { |
| 138 |
final int overlapLen = connection.getOverlapLen(); |
| 139 |
writer.write("L\ttig" + contigId + "\t" + (connection.isRC() ? "-" : "+") + |
| 140 |
"\ttig" + targetId + "\t" + (connection.isTargetRC() ? "-" : "+") + "\t" + |
| 141 |
(overlapLen < 0 ? -overlapLen + "H" : overlapLen + "M") + "\n"); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |