Chain c1 = new Chain(); c1.addChain(new Chain()); Chain preNullChain = c1; Chain returnedChained = null; while ((returnedChained = preNullChain.returnChain()) != null) { preNullChain = returnedChained; } //here, preNullChain is the last Chain that is not null
/** * Load UCSC chain file in order to lift over Intervals. */ public LiftOver(File chainFile) { IOUtil.assertFileIsReadable(chainFile); chains = Chain.loadChains(chainFile); }
throwChainFileParseException("chain line has wrong number of fields", chainFile, reader.getLineNumber()); throwChainFileParseException("chain line does not start with 'chain'", chainFile, reader.getLineNumber()); id = Integer.parseInt(chainFields[12]); } catch (NumberFormatException e) { throwChainFileParseException("Invalid field", chainFile, reader.getLineNumber()); final Chain chain = new Chain(score, fromSequenceName, fromSequenceSize, fromChainStart, fromChainEnd, toSequenceName, toSequenceSize, toNegativeStrand, toChainStart, toChainEnd, id); int toBlockStart = chain.toChainStart; if (line == null || line.equals("")) { if (!sawLastLine) { throwChainFileParseException("Reached end of chain without seeing terminal block", chainFile, reader.getLineNumber()); throwChainFileParseException("Terminal block seen before end of chain", chainFile, reader.getLineNumber()); sawLastLine = true; } else if (blockFields.length != 3) { throwChainFileParseException("Block line has unexpected number of fields", chainFile, reader.getLineNumber()); chain.addBlock(fromBlockStart, toBlockStart, size); if (!sawLastLine) { fromBlockStart += Integer.parseInt(blockFields[1]) + size; chain.validate(); return chain;
validatePositive("fromSequenceSize", fromSequenceSize); validateNonNegative("fromChainStart", fromChainStart); validateNonNegative("fromChainEnd", fromChainEnd); validatePositive("toSequenceSize", toSequenceSize); validateNonNegative("toChainStart", toChainStart); validateNonNegative("toChainEnd", toChainEnd); int fromLength = fromChainEnd - fromChainStart; validatePositive("from length", fromLength); int toLength = toChainEnd - toChainStart; validatePositive("to length", toLength); if (fromLength > fromSequenceSize) throw new SAMException("From chain length (" + fromLength + ") < from sequence length (" + fromSequenceSize + ") for chain " + id);
@Test public void testWriteChain() throws Exception { final OverlapDetector<Chain> chains = Chain.loadChains(CHAIN_FILE); File outFile = File.createTempFile("test.", ".chain"); outFile.deleteOnExit(); PrintWriter pw = new PrintWriter(outFile); final Map<Integer, Chain> originalChainMap = new TreeMap<Integer, Chain>(); for (final Chain chain : chains.getAll()) { chain.write(pw); originalChainMap.put(chain.id, chain); } pw.close(); final OverlapDetector<Chain> newChains = Chain.loadChains(outFile); final Map<Integer, Chain> newChainMap = new TreeMap<Integer, Chain>(); for (final Chain chain : newChains.getAll()) { newChainMap.put(chain.id, chain); } Assert.assertEquals(newChainMap, originalChainMap); }
/** * Read all the chains and load into an OverlapDetector. * @param chainFile File in UCSC chain format. * @return OverlapDetector will all Chains from reader loaded into it. */ static OverlapDetector<Chain> loadChains(final File chainFile) { final Set<Integer> ids = new HashSet<Integer>(); BufferedLineReader reader = new BufferedLineReader(IOUtil.openFileForReading(chainFile)); final OverlapDetector<Chain> ret = new OverlapDetector<Chain>(0, 0); Chain chain; while ((chain = Chain.loadChain(reader, chainFile.toString())) != null) { if (ids.contains(chain.id)) { throw new SAMException("Chain id " + chain.id + " appears more than once in chain file."); } ids.add(chain.id); ret.addLhs(chain, chain.interval); } reader.close(); return ret; }
private static Interval createToInterval(final String intervalName, final boolean sourceNegativeStrand, final TargetIntersection targetIntersection) { // Compute the query interval given the offsets of the target interval start and end into the first and // last ContinuousBlocks. int toStart = targetIntersection.chain.getBlock(targetIntersection.firstBlockIndex).toStart + targetIntersection.startOffset; int toEnd = targetIntersection.chain.getBlock(targetIntersection.lastBlockIndex).getToEnd() - targetIntersection.offsetFromEnd; if (toEnd <= toStart || toStart < 0) { throw new SAMException("Something strange lifting over interval " + intervalName); } if (targetIntersection.chain.toOppositeStrand) { // Flip if query is negative. int negativeStart = targetIntersection.chain.toSequenceSize - toEnd; int negativeEnd = targetIntersection.chain.toSequenceSize - toStart; toStart = negativeStart; toEnd = negativeEnd; } // Convert to 1-based, inclusive. final boolean negativeStrand = targetIntersection.chain.toOppositeStrand ? !sourceNegativeStrand : sourceNegativeStrand; return new Interval(targetIntersection.chain.toSequenceName, toStart+1, toEnd, negativeStrand, intervalName); }
int startOffset = -1; int offsetFromEnd = -1; List<Chain.ContinuousBlock> blockList = chain.getBlocks(); for (int i = 0; i < blockList.size(); ++i) { final Chain.ContinuousBlock block = blockList.get(i);
var moo = new Chain();
throwChainFileParseException("chain line has wrong number of fields", chainFile, reader.getLineNumber()); throwChainFileParseException("chain line does not start with 'chain'", chainFile, reader.getLineNumber()); id = Integer.parseInt(chainFields[12]); } catch (NumberFormatException e) { throwChainFileParseException("Invalid field", chainFile, reader.getLineNumber()); final Chain chain = new Chain(score, fromSequenceName, fromSequenceSize, fromChainStart, fromChainEnd, toSequenceName, toSequenceSize, toNegativeStrand, toChainStart, toChainEnd, id); int toBlockStart = chain.toChainStart; if (line == null || line.equals("")) { if (!sawLastLine) { throwChainFileParseException("Reached end of chain without seeing terminal block", chainFile, reader.getLineNumber()); throwChainFileParseException("Terminal block seen before end of chain", chainFile, reader.getLineNumber()); sawLastLine = true; } else if (blockFields.length != 3) { throwChainFileParseException("Block line has unexpected number of fields", chainFile, reader.getLineNumber()); chain.addBlock(fromBlockStart, toBlockStart, size); if (!sawLastLine) { fromBlockStart += Integer.parseInt(blockFields[1]) + size; chain.validate(); return chain;
validatePositive("fromSequenceSize", fromSequenceSize); validateNonNegative("fromChainStart", fromChainStart); validateNonNegative("fromChainEnd", fromChainEnd); validatePositive("toSequenceSize", toSequenceSize); validateNonNegative("toChainStart", toChainStart); validateNonNegative("toChainEnd", toChainEnd); int fromLength = fromChainEnd - fromChainStart; validatePositive("from length", fromLength); int toLength = toChainEnd - toChainStart; validatePositive("to length", toLength); if (fromLength > fromSequenceSize) throw new SAMException("From chain length (" + fromLength + ") < from sequence length (" + fromSequenceSize + ") for chain " + id);
/** * Read all the chains and load into an OverlapDetector. * @param chainFile File in UCSC chain format. * @return OverlapDetector will all Chains from reader loaded into it. */ static OverlapDetector<Chain> loadChains(final File chainFile) { final BufferedLineReader reader = new BufferedLineReader(IOUtil.openFileForReading(chainFile)); final OverlapDetector<Chain> ret = new OverlapDetector<Chain>(0, 0); Chain chain; while ((chain = Chain.loadChain(reader, chainFile.toString())) != null) { ret.addLhs(chain, chain.interval); } reader.close(); return ret; }
private static Interval createToInterval(final String intervalName, final TargetIntersection targetIntersection) { // Compute the query interval given the offsets of the target interval start and end into the first and // last ContinuousBlocks. int toStart = targetIntersection.chain.getBlock(targetIntersection.firstBlockIndex).toStart + targetIntersection.startOffset; int toEnd = targetIntersection.chain.getBlock(targetIntersection.lastBlockIndex).getToEnd() - targetIntersection.offsetFromEnd; if (toEnd <= toStart || toStart < 0) { throw new SAMException("Something strange lifting over interval " + intervalName); } if (targetIntersection.chain.toNegativeStrand) { // Flip if query is negative. int negativeStart = targetIntersection.chain.toSequenceSize - toEnd; int negativeEnd = targetIntersection.chain.toSequenceSize - toStart; toStart = negativeStart; toEnd = negativeEnd; } // Convert to 1-based, inclusive. return new Interval(targetIntersection.chain.toSequenceName, toStart+1, toEnd, targetIntersection.chain.toNegativeStrand, intervalName); }
int startOffset = -1; int offsetFromEnd = -1; List<Chain.ContinuousBlock> blockList = chain.getBlocks(); for (int i = 0; i < blockList.size(); ++i) { final Chain.ContinuousBlock block = blockList.get(i);
throwChainFileParseException("chain line has wrong number of fields", chainFile, reader.getLineNumber()); throwChainFileParseException("chain line does not start with 'chain'", chainFile, reader.getLineNumber()); id = Integer.parseInt(chainFields[12]); } catch (NumberFormatException e) { throwChainFileParseException("Invalid field", chainFile, reader.getLineNumber()); final Chain chain = new Chain(score, fromSequenceName, fromSequenceSize, fromChainStart, fromChainEnd, toSequenceName, toSequenceSize, toNegativeStrand, toChainStart, toChainEnd, id); int toBlockStart = chain.toChainStart; if (line == null || line.equals("")) { if (!sawLastLine) { throwChainFileParseException("Reached end of chain without seeing terminal block", chainFile, reader.getLineNumber()); throwChainFileParseException("Terminal block seen before end of chain", chainFile, reader.getLineNumber()); sawLastLine = true; } else if (blockFields.length != 3) { throwChainFileParseException("Block line has unexpected number of fields", chainFile, reader.getLineNumber()); chain.addBlock(fromBlockStart, toBlockStart, size); if (!sawLastLine) { fromBlockStart += Integer.parseInt(blockFields[1]) + size; chain.validate(); return chain;
validatePositive("fromSequenceSize", fromSequenceSize); validateNonNegative("fromChainStart", fromChainStart); validateNonNegative("fromChainEnd", fromChainEnd); validatePositive("toSequenceSize", toSequenceSize); validateNonNegative("toChainStart", toChainStart); validateNonNegative("toChainEnd", toChainEnd); int fromLength = fromChainEnd - fromChainStart; validatePositive("from length", fromLength); int toLength = toChainEnd - toChainStart; validatePositive("to length", toLength); if (fromLength > fromSequenceSize) throw new SAMException("From chain length (" + fromLength + ") < from sequence length (" + fromSequenceSize + ") for chain " + id);
Chain returnedChain = null; Chain c1 = new Chain(); c1.addChain(new Chain()); try { returnedChain = c1.returnChain().returnChain().returnChain().returnChain(); } catch (NullPointerException e) { } System.out.println(returnedChain); //now here will print null as what you asked for
/** * Load UCSC chain file in order to lift over Intervals. */ public LiftOver(File chainFile) { IOUtil.assertFileIsReadable(chainFile); chains = Chain.loadChains(chainFile); for (final Chain chain : this.chains.getAll()) { final String from = chain.fromSequenceName; final String to = chain.toSequenceName; final Set<String> names; if (contigMap.containsKey(from)) { names = contigMap.get(from); } else { names = new HashSet<>(); contigMap.put(from, names); } names.add(to); } }
/** * Read all the chains and load into an OverlapDetector. * @param chainFile File in UCSC chain format. * @return OverlapDetector will all Chains from reader loaded into it. */ static OverlapDetector<Chain> loadChains(final File chainFile) { final BufferedLineReader reader = new BufferedLineReader(IOUtil.openFileForReading(chainFile)); final OverlapDetector<Chain> ret = new OverlapDetector<Chain>(0, 0); Chain chain; while ((chain = Chain.loadChain(reader, chainFile.toString())) != null) { ret.addLhs(chain, chain.interval); } reader.close(); return ret; }