-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new interface TextFileFormatted #437
- Loading branch information
Showing
9 changed files
with
196 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
data { | ||
L = 200; | ||
taxa = taxa(names=1:10); | ||
} | ||
model { | ||
Θ ~ LogNormal(meanlog=3.0, sdlog=1.0); | ||
ψ ~ Coalescent(theta=Θ, taxa=taxa); | ||
D ~ PhyloCTMC(tree=ψ, L=L, Q=jukesCantor()); | ||
fastaD = fasta(D); | ||
} |
51 changes: 51 additions & 0 deletions
51
lphy-base/src/main/java/lphy/base/evolution/alignment/FastaAlignment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package lphy.base.evolution.alignment; | ||
|
||
import jebl.evolution.sequences.SequenceType; | ||
import lphy.base.evolution.Taxa; | ||
import lphy.core.logger.LoggerUtils; | ||
import lphy.core.logger.TextFileFormatted; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class FastaAlignment extends SimpleAlignment implements TextFileFormatted { | ||
|
||
public FastaAlignment(Map<String, Integer> idMap, int nchar, SequenceType sequenceType) { | ||
super(idMap, nchar, sequenceType); | ||
} | ||
|
||
public FastaAlignment(Taxa taxa, int nchar, SequenceType sequenceType) { | ||
super(taxa, nchar, sequenceType); | ||
} | ||
|
||
public FastaAlignment(int nchar, Alignment source) { | ||
super(nchar, source); | ||
} | ||
|
||
@Override | ||
public List<String> getTextForFile() { | ||
List<String> lines = new ArrayList<>(); | ||
|
||
for (int i=0; i < ntaxa(); i++) { | ||
try { | ||
String taxonName = getTaxaNames()[i]; | ||
lines.add(taxonName); | ||
String sequence = getSequence(i); | ||
lines.add(sequence); | ||
} catch (Exception ex) { | ||
LoggerUtils.log.severe("Error at " + i + " taxa (" + getTaxaNames()[i] + ") in " + | ||
this.getClass().getName()); | ||
} | ||
} | ||
|
||
return lines; | ||
} | ||
|
||
@Override | ||
public String getFileType() { | ||
return ".fasta"; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
lphy-base/src/main/java/lphy/base/function/io/WriteFasta.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lphy.base.function.io; | ||
|
||
import lphy.base.evolution.alignment.Alignment; | ||
import lphy.base.evolution.alignment.FastaAlignment; | ||
import lphy.core.model.DeterministicFunction; | ||
import lphy.core.model.Value; | ||
import lphy.core.model.annotation.GeneratorCategory; | ||
import lphy.core.model.annotation.GeneratorInfo; | ||
import lphy.core.model.annotation.ParameterInfo; | ||
|
||
public class WriteFasta extends DeterministicFunction<FastaAlignment> { | ||
|
||
|
||
public WriteFasta(@ParameterInfo(name = ReaderConst.ALIGNMENT, | ||
description = "the lphy alignment that is written into the fasta file.") | ||
Value<Alignment> alignmentValue ) { | ||
|
||
|
||
if (alignmentValue == null) throw new IllegalArgumentException("The alignment can't be null!"); | ||
setParam(ReaderConst.ALIGNMENT, alignmentValue); | ||
} | ||
|
||
|
||
@GeneratorInfo(name="fasta", verbClause = "is read from", narrativeName = "fasta file", | ||
category = GeneratorCategory.TAXA_ALIGNMENT, examples = {"covidDPG.lphy"}, | ||
description = "A function that parses an alignment from a fasta file.") | ||
public Value<FastaAlignment> apply() { | ||
|
||
Alignment alignment = ((Value<Alignment>) getParams().get(ReaderConst.ALIGNMENT)).value(); | ||
|
||
// this only creates taxa and nchar | ||
FastaAlignment faData = new FastaAlignment(alignment.nchar(), alignment); | ||
|
||
// fill in states | ||
for (int i=0; i < alignment.ntaxa(); i++) { | ||
for (int j = 0; j < alignment.nchar(); j++) { | ||
faData.setState(i, j, alignment.getState(i, j)); | ||
} | ||
} | ||
|
||
return new Value<>(null, faData, this); | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
lphy/src/main/java/lphy/core/logger/TextFileFormatted.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lphy.core.logger; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Implement this to make the value to be loggable into a file. | ||
*/ | ||
public interface TextFileFormatted { | ||
|
||
List<String> getTextForFile(); | ||
|
||
String getFileType(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters