Skip to content

Commit

Permalink
fixing bugs in tree coloring code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachlan Coin committed Feb 17, 2019
1 parent 50bef46 commit 80e8488
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 229 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ LIB_DIR=libs

#List of external libraries
##EXT_LIBS=colloquial.jar commons-io-2.5.jar commons-math3-3.0.jar jhdf5.jar jhdfobj.jar htsjdk-2.10.1.jar guava-18.0.jar jcommon-1.0.23.jar jfreechart-1.0.19.jar JRIEngine.jar JRI.jar gs-core-1.3.jar gs-ui-1.3.jar gs-algo-1.3.jar javax.json-api-1.0.jar javax.json-1.0.4.jar slf4j-api-1.7.25.jar slf4j-simple-1.7.25.jar jfxrt.jar gson-2.3.jar
EXT_LIBS=colloquial.jar commons-io-2.5.jar commons-math3-3.0.jar jhdf5.jar jhdfobj.jar htsjdk-2.10.1.jar guava-18.0.jar jcommon-1.0.23.jar jfreechart-1.0.19.jar gs-core-1.3.jar gs-ui-1.3.jar gs-algo-1.3.jar javax.json-api-1.0.jar javax.json-1.0.4.jar slf4j-api-1.7.25.jar slf4j-simple-1.7.25.jar jfxrt.jar gson-2.3.jar
EXT_LIBS=pal1.5.1.1.jar mdsj.jar colloquial.jar commons-io-2.5.jar commons-math3-3.0.jar jhdf5.jar jhdfobj.jar htsjdk-2.10.1.jar guava-18.0.jar jcommon-1.0.23.jar jfreechart-1.0.19.jar gs-core-1.3.jar gs-ui-1.3.jar gs-algo-1.3.jar javax.json-api-1.0.jar javax.json-1.0.4.jar slf4j-api-1.7.25.jar slf4j-simple-1.7.25.jar jfxrt.jar gson-2.3.jar

###########################################################################
##What this scripts does:
Expand Down
87 changes: 58 additions & 29 deletions src/main/java/japsa/bio/phylo/CSSProcessCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ public class CSSProcessCommand {
private static final Logger LOG = LoggerFactory.getLogger(CSSProcessCommand.class);

public static void main(String[] args){
//input files


File resistant_gene_list = new File("resistant_genes_list.txt");
File taxdir = new File("taxdump");

if(!taxdir.exists()) taxdir = new File("../taxdump");
if(!taxdir.exists()){
throw new RuntimeException("cannot find taxdump/ directory. Please make a symbolic link into the "
+ "working directory, or into parent directory");

}
File taxdump = new File("taxdump/names.dmp");
File nodesdmp= new File("taxdump/nodes.dmp");
File speciesIndex = new File("speciesIndex");
Expand All @@ -35,63 +44,78 @@ public static void main(String[] args){
File treeout = new File("commontree.txt.css");
File treeout_mod = new File("commontree.txt.css.mod");

GetTaxonID gid = null;
NCBITree trees_species = null ;
AntibioticTree trees_drugs = null;

//NCBITree trees_species = null ;
// AntibioticTree trees_drugs = null;

/*Step 1 read resistance gene tree and color it */
if(resistant_gene_list.exists() && ! resistance_treeout.exists()){
trees_drugs = makeResistanceTree(resistant_gene_list, resistance_treeout);
makeResistanceTree(resistant_gene_list, resistance_treeout);
}



try{

GetTaxonID gid = new GetTaxonID(taxdump, nodesdmp);

//Step2 get taxon information
if(taxdump.exists() && speciesIndex.exists() && !taxon_file.exists()){
LOG.info("getting taxon information");
gid = getTaxaForSpecies(taxdump, speciesIndex, taxon_file);
getTaxaForSpecies(gid, speciesIndex, taxon_file);
}
// if(true) return;
/*step -3 read tree */
if(taxdump.exists() && taxon_file.exists() && !treein.exists()){
if( taxon_file.exists() && !treein.exists()){
LOG.info("making tree");
trees_species = readTaxaTree(gid, nodesdmp, taxon_file, taxdump, treein);

readTaxaTree(gid,taxon_file, treein);

}

/*Step 4 color species tree */
if(treein.exists() && !treeout.exists()){
LOG.info("adding CSS to tree");
addCSSToTree(trees_species, treein, treeout);
addCSSToTree(treein, treeout);
}

/*Step 5 place the lines from speciesIndex in the tree and add color */
if(treeout.exists() && ! treeout_mod.exists()){
LOG.info("adding extra nodes from speciesIndex");
trees_species = addExtraNodesFromSpeciesIndex(trees_species, treeout, taxon_file, taxdump, speciesIndex, treeout_mod);
addExtraNodesFromSpeciesIndex( gid, treeout, taxon_file, taxdump, speciesIndex, treeout_mod);
}

}catch(Exception exc){
exc.printStackTrace();
}



}

public static void test(String[] args){
File treeout_mod = new File("commontree.txt.css.mod");
File resistance_treeout = new File("resistancetree.txt.css");

/*Step 6 testing */
if(treeout_mod.exists()){
String totest = "NC_004355.1:AJ717516.1:NC_023018.1:Homo sapiens:Capnocytophaga canimorsus:Staphylococcus aureus:NC_023018.1:NC_002645.1";

LOG.info("testing");

test(treeout_mod, totest.split(":"));


}

if(resistance_treeout.exists()){
LOG.info("testing");
String totest = "blaCTX:tetX";
test(resistance_treeout, totest.split(":"));
}

}

public static GetTaxonID getTaxaForSpecies(File taxdump, File speciesIndex, File output){
GetTaxonID gid = null;
public static GetTaxonID getTaxaForSpecies(GetTaxonID gid, File speciesIndex, File output){
//GetTaxonID gid = null;
try{
File taxaToInclude = null;
gid = new GetTaxonID(taxaToInclude, taxdump);

if(true){
// gid.processGenBank(assembly_summary_inputs);
gid.process(speciesIndex);
Expand All @@ -103,13 +127,15 @@ public static GetTaxonID getTaxaForSpecies(File taxdump, File speciesIndex, File
return gid;
}

public static NCBITree readTaxaTree(GetTaxonID gid, File nodesdmp, File taxonid, File taxdump, File treeout){
public static NCBITree readTaxaTree(GetTaxonID gid, File taxonFile ,File treeout){
NCBITree trees = null;
try{
trees=
gid == null ? new NCBITree(nodesdmp, taxonid, taxdump) : new NCBITree(nodesdmp, gid);


gid.read(taxonFile); // this tells which nodes to include in tree
boolean cts = gid.taxon_set.contains("191289");
//System.err.println(gid.taxon_set.size());
//System.err.println(gid.taxa2Sci.get("191289"));
trees = new NCBITree(gid);

trees.print(treeout);

}catch(Exception exc){
Expand All @@ -119,11 +145,11 @@ public static NCBITree readTaxaTree(GetTaxonID gid, File nodesdmp, File taxonid,
return trees;
}

public static CommonTree addCSSToTree(CommonTree trees1, File treein, File treeout){
public static CommonTree addCSSToTree( File treein, File treeout){
CommonTree trees = null;
try{
trees = //trees1==null ?
new NCBITree(treein, null, null); //need to re-read
new NCBITree(treein, false); //need to re-read
//: trees1;
Tree[] tree = trees.getTrees();
for(int i=0; i<tree.length; i++){
Expand Down Expand Up @@ -160,23 +186,26 @@ public static AntibioticTree makeResistanceTree(File resistant_gene_list, File t
return null;
}

public static NCBITree addExtraNodesFromSpeciesIndex(NCBITree trees1, File treein, File taxonid, File taxdump, File speciesIndex, File treeout){
public static void addExtraNodesFromSpeciesIndex(GetTaxonID gid1, File treein, File taxonid, File taxdump, File speciesIndex, File treeout){
NCBITree t = null;
try{
t = trees1 == null ? new NCBITree(treein, taxonid, taxdump) : trees1;//, new File(args[1]));
System.err.println(gid1.getTaxa("Sclerophthora macrospora virus A"));
t = new NCBITree(treein, true) ;
t.gid = gid1;//, new File(args[1]));

t.addSpeciesIndex(speciesIndex);
t.print(treeout);
}catch(Exception exc){
exc.printStackTrace();
}
return t;
//return t;
}


public static void test(File treein, String[] totest){

try{
NCBITree t = new NCBITree(treein);
NCBITree t = new NCBITree(treein, false);
for(int i=0; i<totest.length; i++){
String[][] taxa = t.getTaxonomy(totest[i]);
LOG.info(totest[i]);
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/japsa/bio/phylo/ColorTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,9 @@ public class ColorTree {

double[] startend ; // this partitions the hue space
static double maxlight = 85;
//https://journals.sagepub.com/doi/full/10.4137/EBO.S7565#_i6
public static void main(String[] args){
try{
// File speciesIndex = new File("speciesIndex");

File f = new File(args[1]);
boolean species = args[0].equals("species");

CommonTree trees =species? NCBITree.readTree(f, null): AntibioticTree.readTree(f);
Tree[] tree = trees.getTrees();
for(int i=0; i<tree.length; i++){
System.err.println(i);;
//if(tree[i].getExternalNodeCount()>1000) continue;
ColorTree ct = new ColorTree(tree[i]);
ct.color();
}
trees.print(new File(f.getName()+".css"));

}catch(Exception exc){
exc.printStackTrace();

}
}



public void printSlug(String out) throws Exception{
PrintWriter pw = new PrintWriter(new FileWriter(out));
/*
for(Iterator<String> it = this.colors.keySet().iterator(); it.hasNext();){
String key = it.next();
String value = colors.get(key);
pw.println(key+"\t"+value);
}*/
pw.close();
}

static double rand =0;

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/japsa/bio/phylo/CommonTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ public void print(File out) throws IOException{
// PrintWriter pw = new PrintWriter(new FileWriter(out));

for(int i=0; i<this.roots.size(); i++){
System.err.println(roots.get(i).getIdentifier().getName());
Iterator<Node> n = NodeUtils.preOrderIterator(roots.get(i));

inner: for(int j=0; n.hasNext() ;j++){
// System.err.println(i+" "+j);
inner: while(n.hasNext()){
Node node = n.next();

Identifier id = node.getIdentifier();
String nme =id.getName();

//if(nme.indexOf("unclassified ssRNA")>=0){
// System.err.println("h");
//}
Integer level = ((Integer)id.getAttribute("level")).intValue();
String hex = ((String)id.getAttribute("css"));
// String hex = ((String)id.getAttribute("level"));
String alias = ((String)id.getAttribute("alias"));
String alias1 = ((String)id.getAttribute("alias1"));
String prefix = ((String)id.getAttribute("prefix"));
Integer taxon = ((Integer)id.getAttribute("taxon"));
double height = node.getNodeHeight();
//System.err
pw.print(prefix+nme);
if(hex!=null) pw.print("\tcss="+hex);
if(alias!=null) pw.print("\talias="+alias);
Expand Down
Loading

0 comments on commit 80e8488

Please sign in to comment.