Skip to content

Commit

Permalink
update Biostar175929 pour julien
Browse files Browse the repository at this point in the history
  • Loading branch information
lindenb committed Oct 12, 2021
1 parent 96e4ef0 commit 941843d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 47 deletions.
8 changes: 8 additions & 0 deletions docs/Biostar175929.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Usage: biostar175929 [options] Files
print help and exit
--helpFormat
What kind of help. One of [usage,markdown,xml].
--no-recursion
Disable recursion. Print only one sequence
Default: false
-o, --output
Output file. Optional . Default: stdout
* -R, --reference
Expand Down Expand Up @@ -59,6 +62,11 @@ $ ./gradlew biostar175929

The java jar file will be installed in the `dist` directory.


## Creation Date

20160208

## Source code

[https://github.com/lindenb/jvarkit/tree/master/src/main/java/com/github/lindenb/jvarkit/tools/biostar/Biostar175929.java](https://github.com/lindenb/jvarkit/tree/master/src/main/java/com/github/lindenb/jvarkit/tools/biostar/Biostar175929.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.beust.jcommander.Parameter;
import com.github.lindenb.jvarkit.util.jcommander.Launcher;
Expand Down Expand Up @@ -132,7 +133,8 @@ of this software and associated documentation files (the "Software"), to deal
description="Construct a combination set of fasta sequences from a vcf",
biostars=175929,
keywords={"fasta","vcf"},
modificationDate="20190926"
creationDate="20160208",
modificationDate="20211012"
)
public class Biostar175929 extends Launcher
{
Expand All @@ -150,11 +152,14 @@ public class Biostar175929 extends Launcher

@Parameter(names={"-R","--reference"},description="indexed Fasta reference",required=true)
private Path faidx = null;

@Parameter(names={"--no-recursion"},description="Disable recursion. Print only one sequence")
private boolean disable_recursion = false;


private PrintWriter pw;

private void recursive(
final PrintWriter pw,
final GenomicSequence chromosome,
final List<VariantContext> variants,
int index,
Expand Down Expand Up @@ -183,9 +188,9 @@ private void recursive(
if(index==0)
{
int firstPos0= (variants.get(0).getStart()-1);
int chromStart=Math.max(0, firstPos0-extendBases);
int chromStart=Math.max(0, firstPos0-this.extendBases);
title.append(variants.get(0).getContig()+":"+chromStart);
for(int i=Math.max(0, firstPos0-extendBases);i< firstPos0 ;++i)
for(int i=Math.max(0, firstPos0-this.extendBases);i< firstPos0 ;++i)
{
sequence.append(Character.toLowerCase(chromosome.charAt(i)));
}
Expand All @@ -203,19 +208,31 @@ private void recursive(
final int title_length= title.length();
final int sequence_length= sequence.length();
final VariantContext ctx = variants.get(index);
for(final Allele allele: ctx.getAlleles())
{
if(allele.isNoCall()) continue;
if(allele.isSymbolic()) continue;
title.setLength(title_length);
sequence.setLength(sequence_length);
title.append("|"+ctx.getContig()+":"+ctx.getStart()+"-"+ctx.getEnd()+"("+allele.getBaseString()+")");
if(this.bracket) sequence.append("[");
sequence.append(allele.getBaseString().toUpperCase());
if(this.bracket) sequence.append("]");
recursive(chromosome, variants, index+1, title, sequence);
}

if(disable_recursion) {
title.append("|"+ctx.getContig()+":"+ctx.getStart()+"-"+ctx.getEnd());
sequence.append("[");
sequence.append(ctx.getAlleles().stream().
filter(A->!A.isNoCall()).
map(A->A.getBaseString()).
collect(Collectors.joining("/")));
sequence.append("]");
recursive(pw,chromosome, variants, index+1, title, sequence);
}
else {
for(final Allele allele: ctx.getAlleles())
{
if(allele.isNoCall()) continue;
if(allele.isSymbolic()) continue;
title.setLength(title_length);
sequence.setLength(sequence_length);
title.append("|"+ctx.getContig()+":"+ctx.getStart()+"-"+ctx.getEnd()+"("+allele.getBaseString()+")");
if(this.bracket) sequence.append("[");
sequence.append(allele.getBaseString().toUpperCase());
if(this.bracket) sequence.append("]");
recursive(pw,chromosome, variants, index+1, title, sequence);
}
}

}

Expand All @@ -227,46 +244,42 @@ public int doWork(final List<String> args) {
LOG.error("fasta reference was not defined.");
return -1;
}
ReferenceSequenceFile reference = null;
VCFIterator iter=null;
try {
reference = ReferenceSequenceFileFactory.getReferenceSequenceFile(this.faidx);
iter = super.openVCFIterator(oneFileOrNull(args));
this.pw = openPathOrStdoutAsPrintWriter(this.outputFile);
final List<VariantContext> variants = new ArrayList<>();
for(;;)
{
VariantContext ctx = null;
if(iter.hasNext()) {
ctx = iter.next();
}

if( ctx == null || (!variants.isEmpty() && !ctx.getContig().equals(variants.get(0).getContig()))) {
if(!variants.isEmpty())
try( ReferenceSequenceFile reference = ReferenceSequenceFileFactory.getReferenceSequenceFile(this.faidx)) {
try(VCFIterator iter = super.openVCFIterator(oneFileOrNull(args))) {
try(PrintWriter pw=openPathOrStdoutAsPrintWriter(this.outputFile)) {
final List<VariantContext> variants = new ArrayList<>();
for(;;)
{
LOG.info("chrom:" +variants.get(0).getContig()+ " N="+variants.size());
final GenomicSequence genomic = new GenomicSequence(reference,variants.get(0).getContig());
final StringBuilder title= new StringBuilder();
final StringBuilder sequence= new StringBuilder();
recursive(genomic,variants,0,title,sequence);
variants.clear();
VariantContext ctx = null;
if(iter.hasNext()) {
ctx = iter.next();
}

if( ctx == null || (!variants.isEmpty() && !ctx.contigsMatch(variants.get(0)))) {
if(!variants.isEmpty())
{
LOG.info("chrom:" +variants.get(0).getContig()+ " N="+variants.size());
final GenomicSequence genomic = new GenomicSequence(reference,variants.get(0).getContig());
final StringBuilder title= new StringBuilder();
final StringBuilder sequence= new StringBuilder();
recursive(pw,genomic,variants,0,title,sequence);
variants.clear();
}
if( ctx == null) break;
}
variants.add(ctx);
}
pw.flush();
}
if( ctx == null) break;
}
variants.add(ctx);
}
iter.close();iter=null;
this.pw.flush();
this.pw.close();
return RETURN_OK;
} catch (Exception e) {
return 0;
} catch (Throwable e) {
LOG.error(e);
return -1;
}
finally {
CloserUtil.close(reference);
CloserUtil.close(iter);
CloserUtil.close(pw);
}
}

Expand Down

0 comments on commit 941843d

Please sign in to comment.