PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/phyutility/mainrunner/Main.java

https://code.google.com/
Java | 1533 lines | 1379 code | 46 blank | 108 comment | 336 complexity | c4c14bb1fc5933186a8688724a5f0bfd MD5 | raw file
  1. package phyutility.mainrunner;
  2. import jade.tree.TreeReader;
  3. import java.util.*;
  4. import java.io.*;
  5. import jebl.evolution.io.ImportException;
  6. import phyutility.concat.Concat;
  7. import phyutility.drb.WwdEmbedded;
  8. public class Main {
  9. private boolean treesupp = false; //-ts
  10. private boolean linmovement = false; //-lm requires lmt
  11. private boolean prune = false; //use -names for which to prune
  12. private String tree = null; //-tree
  13. private boolean leafstab = false; //-ls
  14. private boolean convert = false; //-vert for newick to nex or nex to nex no trans
  15. private boolean consensus = false; //-con requires threshold
  16. private boolean out_oth = false; //-othout
  17. private double threshold = 0.0; //-t
  18. private boolean reroot = false; //-rr
  19. private boolean thinner = false;//-tt # every
  20. private double thin = 10;
  21. private boolean ltt = false;
  22. private boolean derb = false;
  23. //sequence functions
  24. private boolean concat = false;
  25. private boolean clean = false;
  26. private double cleannum = 0.5;
  27. private boolean parse = false;
  28. private int parsenum = 1;
  29. private boolean ncbisearch = false;
  30. private String sterm = "";
  31. private int sdb = 1;//nuc = 1, prot = 2, genome = 3, tax = 4
  32. private boolean ncbiget = false;
  33. private String outfor = "31";
  34. private String sep = "_";
  35. private int lengthlim;
  36. private boolean blast = false;//not implemented yet
  37. //db
  38. private WwdEmbedded db;
  39. private ArrayList<String> mrcanames; //-names
  40. private String logfile = null; //-log
  41. private String outfile = null; //-out
  42. private ArrayList<String> infiles; //-in
  43. private FileWriter fw;
  44. public Main(String [] args){
  45. if(args.length == 0){
  46. printUsage();
  47. }else if(args.length == 1 ){
  48. if(args[0].toUpperCase().compareTo("-I")== 0){
  49. goInteractive();
  50. runArgs();
  51. }
  52. else{
  53. processArgs(args);
  54. runArgs();
  55. }
  56. }else{
  57. processArgs(args);
  58. runArgs();
  59. }
  60. }
  61. private void processArgs(String [] args){
  62. /*
  63. * catch the secret functions
  64. */
  65. if(args[0].toLowerCase().compareTo("-sec")==0){
  66. TestFuncs tf = new TestFuncs(args);
  67. System.exit(0);
  68. }
  69. /*
  70. * end catch the secrets
  71. */
  72. //get help
  73. for(int i=0;i<args.length;i++){
  74. if(args[i].toLowerCase().compareTo("-h")==0){
  75. i++;
  76. if(args.length == i){
  77. System.err.println("you have to enter a command name after -h");
  78. printUsage();
  79. System.exit(0);
  80. }
  81. if(args[i].charAt(0)=='-'){
  82. System.err.println("you have to enter a command name after -h");
  83. printUsage();
  84. System.exit(0);
  85. }else{
  86. printHelp( args[i]);
  87. }
  88. System.exit(0);
  89. }
  90. }
  91. //get log
  92. for(int i=0;i<args.length;i++){
  93. if(args[i].toLowerCase().compareTo("-log")==0){
  94. i++;
  95. if(args[i].charAt(0)=='-'){
  96. System.err.println("you have to enter a log filename after -log");
  97. System.exit(0);
  98. }else{
  99. logfile = args[i];
  100. }
  101. }
  102. }
  103. startLogging();
  104. for(int i=0;i<args.length;i++){
  105. if(args[i].toLowerCase().compareTo("-lm")==0){
  106. linmovement = true;
  107. log("lineage movement\n");
  108. }else if(args[i].toLowerCase().compareTo("-derb")==0){
  109. //derb = true;
  110. derb = false;
  111. log("trying to use derby database for trees\n");
  112. log("DERBY NO LONGER SUPPORTED [not free]\n");
  113. System.exit(0);
  114. }else if(args[i].toLowerCase().compareTo("-ts")==0){
  115. treesupp = true;
  116. log("tree support\n");
  117. }else if(args[i].toLowerCase().compareTo("-ls")==0){
  118. leafstab = true;
  119. log("leaf stability\n");
  120. }else if(args[i].toLowerCase().compareTo("-rr")==0){
  121. reroot = true;
  122. log("reroot\n");
  123. }else if(args[i].toLowerCase().compareTo("-vert")==0){
  124. convert = true;
  125. log("tree convert\n");
  126. }else if(args[i].toLowerCase().compareTo("-con")==0){
  127. consensus = true;
  128. log("consensus\n");
  129. }else if(args[i].toLowerCase().compareTo("-lmt")==0){
  130. i++;
  131. if(args[i].charAt(0)=='-'){
  132. System.err.println("you have to enter a consensus filename after -lmt");
  133. System.exit(0);
  134. }else{
  135. tree = args[i];
  136. log("tree filename "+args[i]+"\n");
  137. }
  138. }else if(args[i].toLowerCase().compareTo("-tree")==0){
  139. i++;
  140. if(args[i].charAt(0)=='-'){
  141. System.err.println("you have to enter a tree filename after -tree");
  142. System.exit(0);
  143. }else{
  144. tree = args[i];
  145. log("tree filename "+args[i]+"\n");
  146. }
  147. }else if(args[i].toLowerCase().compareTo("-pr")==0){
  148. prune = true;
  149. }else if(args[i].toLowerCase().compareTo("-t")==0){
  150. i++;
  151. if(args[i].charAt(0)=='-'){
  152. System.err.println("you have to enter a threshhold after -t");
  153. System.exit(0);
  154. }else{
  155. threshold = Double.valueOf(args[i]);
  156. }
  157. }else if(args[i].toLowerCase().compareTo("-tt")==0){
  158. i++;
  159. if(args[i].charAt(0)=='-'){
  160. System.err.println("you have to enter a thinning number after -tt");
  161. System.exit(0);
  162. }else{
  163. thin = Double.valueOf(args[i]);
  164. thinner = true;
  165. }
  166. }else if(args[i].toLowerCase().compareTo("-names")==0){
  167. mrcanames = new ArrayList<String>();
  168. i++;
  169. if(args[i].charAt(0)=='-'){
  170. System.err.println("you have to enter taxon names after -names");
  171. System.exit(0);
  172. }else{
  173. while(args[i].trim().startsWith("-")==false){
  174. mrcanames.add(args[i]);
  175. log("mrca: "+args[i]+"\n");
  176. i++;
  177. if(i >= args.length){
  178. break;
  179. }
  180. }
  181. i--;
  182. }
  183. }
  184. //seqence
  185. else if(args[i].toLowerCase().compareTo("-concat")==0){
  186. concat = true;
  187. log("concat\n");
  188. }else if(args[i].toLowerCase().compareTo("-clean")==0){
  189. clean = true;
  190. log("clean\n");
  191. i++;
  192. if(args[i].charAt(0)=='-'){
  193. System.err.println("you have to enter a cleannum after -clean");
  194. System.exit(0);
  195. }else{
  196. cleannum = Double.valueOf(args[i]);
  197. log("cleannum: "+cleannum+"\n");
  198. }
  199. }else if(args[i].toLowerCase().compareTo("-parse")==0){
  200. parse = true;
  201. log("parse\n");
  202. i++;
  203. if(args[i].charAt(0)=='-'){
  204. System.err.println("you have to enter a parsenum after -parse");
  205. System.exit(0);
  206. }else{
  207. parsenum = Integer.valueOf(args[i]);
  208. log("parsenum: "+parsenum+"\n");
  209. }
  210. }else if(args[i].toLowerCase().compareTo("-es")==0){
  211. ncbisearch = true;
  212. log("ncbisearch\n");
  213. }else if(args[i].toLowerCase().compareTo("-term")==0){
  214. i++;
  215. if(args[i].charAt(0)=='-'){
  216. System.err.println("you have to enter a search term after -term");
  217. System.exit(0);
  218. }else{
  219. sterm = args[i];
  220. log("term: "+sterm+"\n");
  221. }
  222. }else if(args[i].toLowerCase().compareTo("-db")==0){
  223. i++;
  224. if(args[i].charAt(0)=='-'){
  225. System.err.println("you have to enter a database number after -db");
  226. System.exit(0);
  227. }else{
  228. sdb = Integer.valueOf(args[i]);
  229. log("database: "+sdb+"\n");
  230. }
  231. }else if(args[i].toLowerCase().compareTo("-ef")==0){
  232. ncbiget = true;
  233. log("ncbiget\n");
  234. }else if(args[i].toLowerCase().compareTo("-ll")==0){
  235. i++;
  236. if(args[i].charAt(0)=='-'){
  237. System.err.println("you have to enter a length limit after -ll");
  238. System.exit(0);
  239. }else{
  240. this.lengthlim = Integer.valueOf(args[i]);
  241. log("length lim: "+this.lengthlim+"\n");
  242. }
  243. }else if(args[i].toLowerCase().compareTo("-outfor")==0){
  244. i++;
  245. if(args[i].charAt(0)=='-'){
  246. System.err.println("you have to enter numbers after -outfor");
  247. System.exit(0);
  248. }else{
  249. outfor = args[i];
  250. log("outfor: "+outfor+"\n");
  251. }
  252. }else if(args[i].toLowerCase().compareTo("-sep")==0){
  253. i++;
  254. if(args[i].charAt(0)=='-'){
  255. System.err.println("you have to enter a seperator after -sep");
  256. System.exit(0);
  257. }else{
  258. sep = args[i];
  259. log("sep: "+sep+"\n");
  260. }
  261. }
  262. //other
  263. else if(args[i].toLowerCase().compareTo("-othout")==0){
  264. out_oth = true;
  265. }else if(args[i].toLowerCase().compareTo("-out")==0){
  266. i++;
  267. if(args[i].charAt(0)=='-'){
  268. System.err.println("you have to enter a out filename after -out");
  269. System.exit(0);
  270. }else{
  271. outfile = args[i];
  272. log("outfile: "+outfile+"\n");
  273. }
  274. }else if(args[i].toLowerCase().compareTo("-in")==0){
  275. infiles = new ArrayList<String>();
  276. i++;
  277. if(args[i].charAt(0)=='-'){
  278. System.err.println("you have to enter filenames after -in");
  279. System.exit(0);
  280. }else{
  281. while(args[i].trim().startsWith("-")==false){
  282. infiles.add(args[i]);
  283. log("in filename: "+args[i]+"\n");
  284. i++;
  285. if(i >= args.length){
  286. break;
  287. }
  288. }
  289. i--;
  290. }
  291. }else if(args[i].toLowerCase().compareTo("-log")==0){
  292. i++;
  293. if(args[i].charAt(0)=='-'){
  294. System.err.println("you have to enter a out filename after -log");
  295. System.exit(0);
  296. }
  297. }else{
  298. System.out.println("don't recognize argument "+args[i]);
  299. }
  300. }
  301. }
  302. private void goInteractive(){
  303. }
  304. private void runArgs(){
  305. int x = 0;
  306. if(linmovement == true){
  307. x++;
  308. }if(treesupp == true){
  309. x++;
  310. }if(leafstab == true){
  311. x++;
  312. }if(convert == true){
  313. x++;
  314. }if(consensus == true){
  315. x++;
  316. }if(reroot == true){
  317. x++;
  318. }if(thinner == true){
  319. x++;
  320. }if(prune == true){
  321. x++;
  322. }
  323. //sequence
  324. if(concat == true){
  325. x++;
  326. }if(parse == true){
  327. x++;
  328. }if(clean == true){
  329. x++;
  330. }if(ncbisearch == true){
  331. x++;
  332. }if(ncbiget == true){
  333. x++;
  334. }if(blast == true){
  335. x++;
  336. }
  337. if(x == 0){
  338. System.out.println("you have to enter some sort of analysis");
  339. printUsage();
  340. System.exit(0);
  341. }if(x > 1){
  342. System.out.println("you can only do one thing at a time");
  343. printUsage();
  344. System.exit(0);
  345. }
  346. if(infiles == null && (ncbisearch == false && ncbiget == false)){
  347. System.out.println("you have to enter some infiles (-in)");
  348. printUsage();
  349. System.exit(0);
  350. }
  351. if(linmovement == true){
  352. lineageMove();
  353. stopLogging();
  354. System.exit(0);
  355. }if(treesupp == true){
  356. treeSupp();
  357. stopLogging();
  358. System.exit(0);
  359. }if(leafstab == true){
  360. leafStab();
  361. System.exit(0);
  362. }if(convert == true){
  363. convert();
  364. System.exit(0);
  365. }if(consensus == true){
  366. consensus();
  367. System.exit(0);
  368. }if(reroot == true){
  369. reroot();
  370. System.exit(0);
  371. }if(thinner == true){
  372. thin();
  373. System.exit(0);
  374. }if(prune == true){
  375. prune();
  376. System.exit(0);
  377. }
  378. //sequence
  379. if(concat == true){
  380. concat();
  381. System.exit(0);
  382. }if(parse == true){
  383. parse();
  384. System.exit(0);
  385. }if(clean == true){
  386. clean();
  387. System.exit(0);
  388. }if(ncbisearch == true){
  389. ncbisearch();
  390. System.exit(0);
  391. }if(ncbiget == true){
  392. ncbiget();
  393. System.exit(0);
  394. }if(blast == true){
  395. blast();
  396. System.exit(0);
  397. }
  398. }
  399. /*
  400. * tree tools
  401. */
  402. private void lineageMove(){
  403. if(tree == null){
  404. System.out.println("you have to enter a consensus tree when performing a lineage movement analysis (-tree)");
  405. System.exit(0);
  406. }
  407. if(mrcanames == null){
  408. System.out.println("you have to enter MRCA or tip names when performing a lineage movement analysis (-names)");
  409. System.exit(0);
  410. }
  411. ArrayList<jade.tree.Tree> intrees = new ArrayList<jade.tree.Tree>();
  412. ArrayList<jade.tree.Tree> alltrees = new ArrayList<jade.tree.Tree>();
  413. boolean testnex = false;
  414. for(int i=0;i<infiles.size();i++){
  415. testnex = testForNexus(infiles.get(i));
  416. if(testnex){
  417. alltrees = getNewickFromNexus(infiles.get(i));
  418. for(int j=0;j<alltrees.size();j++){
  419. intrees.add(alltrees.get(j));
  420. }
  421. }else{
  422. try{
  423. BufferedReader br = new BufferedReader(new FileReader(infiles.get(i)));
  424. String str = "";
  425. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  426. while((str = br.readLine())!=null){
  427. tr.setTree(str);
  428. intrees.add(tr.readTree());
  429. }
  430. br.close();
  431. }catch(IOException ioe){};
  432. }
  433. }
  434. log("reading in consensus tree\n");
  435. boolean nex = false;
  436. jade.tree.Tree contr = null;
  437. String str = "";
  438. try{
  439. BufferedReader br = new BufferedReader(new FileReader(tree));
  440. str = br.readLine();
  441. if(str.toUpperCase().trim().compareTo("#NEXUS")==0)
  442. nex = true;
  443. br.close();
  444. }catch(IOException ioe){}
  445. if(nex == true){
  446. contr = getNewickFromNexus(tree).get(0);
  447. }else{
  448. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  449. tr.setTree(str);
  450. contr = tr.readTree();
  451. }
  452. phyutility.lineagemovement.Main lm = new phyutility.lineagemovement.Main(intrees,contr,mrcanames);
  453. if(outfile == null){
  454. System.out.println(lm.run().getRoot().getNewick(true));
  455. }else{
  456. try{
  457. //nexus out
  458. if((testnex == true && nex == true && out_oth != true)||
  459. (testnex == false || nex == false && out_oth == true)){
  460. String nw = lm.run().getRoot().getNewick(true)+";";
  461. ArrayList<String > nwa = new ArrayList<String>();
  462. nwa.add(nw);
  463. Utils.newickToNexus(outfile, nwa);
  464. }else{
  465. FileWriter outw = new FileWriter(outfile);
  466. outw.write(lm.run().getRoot().getNewick(true)+";");
  467. outw.close();
  468. }
  469. }catch(IOException ioe){};
  470. }
  471. }
  472. private void treeSupp(){
  473. ArrayList<jade.tree.Tree> intrees = new ArrayList<jade.tree.Tree>();
  474. ArrayList<jade.tree.Tree> alltrees = new ArrayList<jade.tree.Tree>();
  475. boolean testnex = false;
  476. for(int i=0;i<infiles.size();i++){
  477. testnex = testForNexus(infiles.get(i));
  478. if(testnex){
  479. alltrees = getNewickFromNexus(infiles.get(i));
  480. for(int j=0;j<alltrees.size();j++){
  481. intrees.add(alltrees.get(j));
  482. }
  483. }else{
  484. try{
  485. BufferedReader br = new BufferedReader(new FileReader(infiles.get(i)));
  486. String str = "";
  487. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  488. while((str = br.readLine())!=null){
  489. tr.setTree(str);
  490. intrees.add(tr.readTree());
  491. }
  492. br.close();
  493. }catch(IOException ioe){};
  494. }
  495. }
  496. log("reading in tree\n");
  497. boolean nex = false;
  498. jade.tree.Tree contr = null;
  499. String str = "";
  500. try{
  501. BufferedReader br = new BufferedReader(new FileReader(tree));
  502. str = br.readLine();
  503. if(str.toUpperCase().trim().compareTo("#NEXUS")==0)
  504. nex = true;
  505. br.close();
  506. }catch(IOException ioe){}
  507. if(nex == true){
  508. contr = getNewickFromNexus(tree).get(0);
  509. }else{
  510. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  511. tr.setTree(str);
  512. contr = tr.readTree();
  513. }
  514. phyutility.treesupport.Runner ls = new phyutility.treesupport.Runner(intrees, contr);
  515. if(outfile == null){
  516. System.out.println(ls.run().getRoot().getNewick(true)+";");
  517. log("finished tree support\n");
  518. }else{
  519. try{
  520. //nexus out
  521. if((testnex == true && nex == true && out_oth != true)||
  522. (testnex == false || nex == false && out_oth == true)){
  523. String nw = ls.run().getRoot().getNewick(true)+";";
  524. ArrayList<String > nwa = new ArrayList<String>();
  525. nwa.add(nw);
  526. Utils.newickToNexus(outfile, nwa);
  527. }else{
  528. FileWriter outw = new FileWriter(outfile);
  529. outw.write(ls.run().getRoot().getNewick(true)+";");
  530. outw.close();
  531. }
  532. }catch(IOException ioe){};
  533. }
  534. }
  535. private void leafStab(){
  536. ArrayList<jade.tree.Tree> intrees = new ArrayList<jade.tree.Tree>();
  537. ArrayList<jade.tree.Tree> alltrees = new ArrayList<jade.tree.Tree>();
  538. for(int i=0;i<infiles.size();i++){
  539. if(testForNexus(infiles.get(i))){
  540. alltrees = getNewickFromNexus(infiles.get(i));
  541. for(int j=0;j<alltrees.size();j++){
  542. intrees.add(alltrees.get(j));
  543. }
  544. }else{
  545. try{
  546. BufferedReader br = new BufferedReader(new FileReader(infiles.get(i)));
  547. String str = "";
  548. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  549. while((str = br.readLine())!=null){
  550. tr.setTree(str);
  551. intrees.add(tr.readTree());
  552. }
  553. br.close();
  554. }catch(IOException ioe){};
  555. }
  556. }
  557. phyutility.leafstability.Runner ls = new phyutility.leafstability.Runner(intrees);
  558. ls.run();
  559. ls.printResults();
  560. }
  561. //nexus to newick
  562. //nexus to nexus tr with othout
  563. //newick to nexus
  564. //newick to nexus tr with othout
  565. private void convert(){
  566. ArrayList<jebl.evolution.trees.Tree> alltrees = new ArrayList<jebl.evolution.trees.Tree>();
  567. if(infiles.size() > 1){
  568. System.err.println("Converting trees is done one file at a time, only the first file will be used.");
  569. }
  570. if(outfile == null){
  571. System.err.println("You must enter an outfile (-outfile).");
  572. System.exit(0);
  573. }
  574. if(testForNexus(infiles.get(0))){
  575. jebl.evolution.io.NexusImporter ni;
  576. try {
  577. ni = new jebl.evolution.io.NexusImporter(new FileReader(infiles.get(0)));
  578. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  579. } catch (FileNotFoundException e) {
  580. // TODO Auto-generated catch block
  581. e.printStackTrace();
  582. } catch (IOException e) {
  583. // TODO Auto-generated catch block
  584. e.printStackTrace();
  585. } catch (ImportException e) {
  586. // TODO Auto-generated catch block
  587. e.printStackTrace();
  588. }
  589. if(out_oth == true){
  590. FileWriter fw;
  591. try {
  592. fw = new FileWriter(outfile);
  593. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  594. HashMap <String, String> map = new HashMap<String, String>();
  595. Set<jebl.evolution.taxa.Taxon> tax = alltrees.get(0).getTaxa();
  596. Iterator<jebl.evolution.taxa.Taxon> it = tax.iterator();
  597. int i=1;
  598. while(it.hasNext()){
  599. map.put(String.valueOf(i),it.next().getName());
  600. }
  601. ne.exportTreesWithTranslation(alltrees,map);
  602. fw.close();
  603. } catch (IOException e) {
  604. // TODO Auto-generated catch block
  605. e.printStackTrace();
  606. }
  607. }else{
  608. try {
  609. BufferedWriter fw = new BufferedWriter (new FileWriter(outfile));
  610. jebl.evolution.io.NewickExporter ne = new jebl.evolution.io.NewickExporter(fw);
  611. ne.exportTrees(alltrees);
  612. fw.close();
  613. } catch (IOException e) {
  614. // TODO Auto-generated catch block
  615. e.printStackTrace();
  616. }
  617. }
  618. }else{
  619. jebl.evolution.io.NewickImporter ni;
  620. try {
  621. ni = new jebl.evolution.io.NewickImporter(new FileReader(infiles.get(0)), true);
  622. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  623. } catch (FileNotFoundException e) {
  624. // TODO Auto-generated catch block
  625. e.printStackTrace();
  626. } catch (IOException e) {
  627. // TODO Auto-generated catch block
  628. e.printStackTrace();
  629. } catch (ImportException e) {
  630. // TODO Auto-generated catch block
  631. e.printStackTrace();
  632. }
  633. if(out_oth == true){
  634. FileWriter fw;
  635. try {
  636. fw = new FileWriter(outfile);
  637. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  638. HashMap <String, String> map = new HashMap<String, String>();
  639. Set<jebl.evolution.taxa.Taxon> tax = alltrees.get(0).getTaxa();
  640. Iterator<jebl.evolution.taxa.Taxon> it = tax.iterator();
  641. int i=1;
  642. while(it.hasNext()){
  643. map.put(String.valueOf(i),it.next().getName());
  644. }
  645. ne.exportTreesWithTranslation(alltrees,map);
  646. fw.close();
  647. } catch (IOException e) {
  648. // TODO Auto-generated catch block
  649. e.printStackTrace();
  650. }
  651. }else{
  652. try {
  653. BufferedWriter fw = new BufferedWriter(new FileWriter(outfile));
  654. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  655. ne.exportTrees(alltrees);
  656. fw.close();
  657. } catch (IOException e) {
  658. // TODO Auto-generated catch block
  659. e.printStackTrace();
  660. }
  661. }
  662. }
  663. }
  664. private void consensus(){
  665. ArrayList<jebl.evolution.trees.Tree> intrees = new ArrayList<jebl.evolution.trees.Tree>();
  666. ArrayList<jebl.evolution.trees.Tree> alltrees = new ArrayList<jebl.evolution.trees.Tree>();
  667. boolean rooted = false;
  668. for(int i=0;i<infiles.size();i++){
  669. if(testForNexus(infiles.get(0))){
  670. jebl.evolution.io.NexusImporter ni;
  671. try {
  672. ni = new jebl.evolution.io.NexusImporter(new FileReader(infiles.get(0)));
  673. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  674. if(getJadeTreeFromJeblTree(alltrees.get(0)).getRoot().getChildCount()>2){
  675. for(int j=0;j<alltrees.size();j++){
  676. intrees.add(alltrees.get(j));
  677. }
  678. }else{
  679. rooted = true;
  680. for(int j=0;j<alltrees.size();j++){
  681. intrees.add(alltrees.get(j));
  682. }
  683. }
  684. alltrees = null;
  685. } catch (FileNotFoundException e) {
  686. // TODO Auto-generated catch block
  687. e.printStackTrace();
  688. } catch (IOException e) {
  689. // TODO Auto-generated catch block
  690. e.printStackTrace();
  691. } catch (ImportException e) {
  692. // TODO Auto-generated catch block
  693. e.printStackTrace();
  694. }
  695. }else{
  696. jebl.evolution.io.NewickImporter ni;
  697. try {
  698. ni = new jebl.evolution.io.NewickImporter(new FileReader(infiles.get(0)), true);
  699. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  700. if(getJadeTreeFromJeblTree(alltrees.get(0)).getRoot().getChildCount()>2){
  701. for(int j=0;j<alltrees.size();j++){
  702. intrees.add(alltrees.get(j));
  703. }
  704. }else{
  705. rooted = true;
  706. for(int j=0;j<alltrees.size();j++){
  707. intrees.add(alltrees.get(j));
  708. }
  709. }
  710. alltrees = null;
  711. } catch (FileNotFoundException e) {
  712. // TODO Auto-generated catch block
  713. e.printStackTrace();
  714. } catch (IOException e) {
  715. // TODO Auto-generated catch block
  716. e.printStackTrace();
  717. } catch (ImportException e) {
  718. // TODO Auto-generated catch block
  719. e.printStackTrace();
  720. }
  721. }
  722. }
  723. if(rooted == true){
  724. System.out.println("rooted");
  725. jebl.evolution.trees.RootedTree [] tr = new jebl.evolution.trees.RootedTree[intrees.size()];
  726. for(int i=0;i<tr.length;i++){
  727. tr[i] = (jebl.evolution.trees.RootedTree)intrees.get(i);
  728. }
  729. intrees = null;
  730. jebl.evolution.trees.GreedyRootedConsensusTreeBuilder con = new jebl.evolution.trees.GreedyRootedConsensusTreeBuilder(tr, threshold);
  731. if(outfile == null){
  732. log("place an outfile to get internal node values (-out)\n");
  733. System.out.println(jebl.evolution.trees.Utils.toNewick(con.build()));
  734. }else{
  735. try {
  736. if(out_oth == true){
  737. File toutf = new File(outfile+"temp");
  738. BufferedWriter fw = new BufferedWriter(new FileWriter(toutf));
  739. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  740. ne.exportTree(con.build());
  741. fw.close();
  742. ArrayList<jade.tree.Tree> ttrees = getNewickFromNexus(outfile+"temp");
  743. FileWriter outw = new FileWriter(outfile);
  744. for(int i=0;i<ttrees.size();i++){
  745. outw.write(ttrees.get(i).getRoot().getNewick(true)+";\n");
  746. }
  747. outw.close();
  748. toutf.delete();
  749. }else{
  750. BufferedWriter fw = new BufferedWriter(new FileWriter(outfile));
  751. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  752. ne.exportTree(con.build());
  753. fw.close();
  754. }
  755. } catch (IOException e) {
  756. // TODO Auto-generated catch block
  757. e.printStackTrace();
  758. }
  759. }
  760. }else{
  761. jebl.evolution.trees.Tree [] tr = new jebl.evolution.trees.Tree[intrees.size()];
  762. for(int i=0;i<tr.length;i++){
  763. tr[i] = intrees.get(i);
  764. }
  765. intrees = null;
  766. jebl.evolution.trees.GreedyUnrootedConsensusTreeBuilder con = new jebl.evolution.trees.GreedyUnrootedConsensusTreeBuilder(tr, null, threshold);
  767. if(outfile == null){
  768. log("place an outfile to get internal node values (-out)\n");
  769. System.out.println(jebl.evolution.trees.Utils.toNewick((jebl.evolution.trees.RootedTree)con.build()));
  770. }else{
  771. try {
  772. if(out_oth == true){
  773. File toutf = new File(outfile+"temp");
  774. BufferedWriter fw = new BufferedWriter(new FileWriter(toutf));
  775. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  776. ne.exportTree(con.build());
  777. fw.close();
  778. ArrayList<jade.tree.Tree> ttrees = getNewickFromNexus(outfile+"temp");
  779. FileWriter outw = new FileWriter(outfile);
  780. for(int i=0;i<ttrees.size();i++){
  781. outw.write(ttrees.get(i).getRoot().getNewick(true)+";\n");
  782. }
  783. outw.close();
  784. toutf.delete();
  785. }else{
  786. BufferedWriter fw = new BufferedWriter(new FileWriter(outfile));
  787. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  788. ne.exportTree(con.build());
  789. fw.close();
  790. }
  791. } catch (IOException e) {
  792. // TODO Auto-generated catch block
  793. e.printStackTrace();
  794. }
  795. }
  796. }
  797. }
  798. private void reroot(){
  799. if(mrcanames == null){
  800. System.out.println("you not entered MRCA or tip names when performing a reroot analysis (-names), so phyutility will attempt an unroot");
  801. //System.exit(0);
  802. }
  803. ArrayList<jade.tree.Tree> intrees = new ArrayList<jade.tree.Tree>();
  804. ArrayList<jade.tree.Tree> alltrees = new ArrayList<jade.tree.Tree>();
  805. boolean testnex = false;
  806. for(int i=0;i<infiles.size();i++){
  807. testnex = testForNexus(infiles.get(i));
  808. if(testnex){
  809. if(derb == true){
  810. db = new WwdEmbedded("readtree");
  811. db.connectToDB();
  812. System.out.println("table made = "+db.makeTable(true));
  813. WwdEmbedded dbtemp = getNewickFromNexusDerby(infiles.get(i));
  814. for(int j=0;j<dbtemp.getTableTreeSize();j++){
  815. db.addTree(dbtemp.getTree(j)+";");
  816. }
  817. }else{
  818. alltrees = getNewickFromNexus(infiles.get(i));
  819. for(int j=0;j<alltrees.size();j++){
  820. intrees.add(alltrees.get(j));
  821. }
  822. }
  823. }else{
  824. try{
  825. BufferedReader br = new BufferedReader(new FileReader(infiles.get(i)));
  826. String str = "";
  827. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  828. if(derb == true){
  829. db = new WwdEmbedded("readtree");
  830. db.connectToDB();
  831. System.out.println("table made = "+db.makeTable(true));
  832. }
  833. while((str = br.readLine())!=null){
  834. tr.setTree(str);
  835. if(derb == true){
  836. if(str.length()>1)
  837. db.addTree(tr.readTree().getRoot().getNewick(true)+";");
  838. }
  839. else
  840. intrees.add(tr.readTree());
  841. }
  842. if(derb ==true){
  843. System.out.println("read "+db.getTableTreeSize());
  844. }
  845. br.close();
  846. }catch(IOException ioe){}
  847. }
  848. }
  849. if(outfile == null){
  850. for(int i=0;i<intrees.size();i++){
  851. intrees.get(i).reRoot(intrees.get(i).getMRCA(mrcanames));
  852. System.out.println(intrees.get(i).getRoot().getNewick(true)+";");
  853. }
  854. log("finished reroot\n");
  855. }else{
  856. try{
  857. //nexus out
  858. if((testnex == true && out_oth != true)||
  859. (testnex == false && out_oth == true)){
  860. if(derb == true){
  861. if(mrcanames != null){
  862. Utils.newickToNexusRerootDerby(outfile, db, mrcanames);
  863. }else{
  864. Utils.newickToNexusUnrootDerby(outfile, db);
  865. }
  866. this.deleteDatabaseFolder(new File("null"));
  867. }else{
  868. ArrayList<String > nwa = new ArrayList<String>();
  869. for(int i=0;i<intrees.size();i++){
  870. if(mrcanames != null){
  871. intrees.get(i).reRoot(intrees.get(i).getMRCA(mrcanames));
  872. }else{
  873. intrees.get(i).unRoot(intrees.get(i).getRoot());
  874. }
  875. nwa.add(intrees.get(i).getRoot().getNewick(true)+";");
  876. }
  877. Utils.newickToNexus(outfile, nwa);
  878. }
  879. }else{
  880. if(derb == true){
  881. FileWriter outw = new FileWriter(outfile);
  882. for(int i=0;i<db.getTableTreeSize();i++){
  883. jade.tree.Tree x = db.getJadeTree(i);
  884. if(mrcanames != null){
  885. x.reRoot(x.getMRCA(mrcanames));
  886. }else{
  887. x.reRoot(x.getRoot());
  888. }
  889. outw.write(x.getRoot().getNewick(true)+";\n");
  890. }
  891. outw.close();
  892. this.deleteDatabaseFolder(new File("null"));
  893. }else{
  894. FileWriter outw = new FileWriter(outfile);
  895. for(int i=0;i<intrees.size();i++){
  896. if(mrcanames != null){
  897. intrees.get(i).reRoot(intrees.get(i).getMRCA(mrcanames));
  898. }else{
  899. intrees.get(i).unRoot(intrees.get(i).getRoot());
  900. }
  901. outw.write(intrees.get(i).getRoot().getNewick(true)+";\n");
  902. }
  903. outw.close();
  904. }
  905. }
  906. }catch(IOException ioe){};
  907. }
  908. }
  909. private void thin(){
  910. log("thinning trees\n");
  911. ArrayList<jebl.evolution.trees.Tree> alltrees = new ArrayList<jebl.evolution.trees.Tree>();
  912. ArrayList<jebl.evolution.trees.Tree> thtrees = new ArrayList<jebl.evolution.trees.Tree>();
  913. if(infiles.size() > 1){
  914. System.err.println("Converting trees is done one file at a time, only the first file will be used.");
  915. }
  916. if(outfile == null){
  917. System.err.println("You must enter an outfile (-outfile).");
  918. System.exit(0);
  919. }
  920. log("thinning every "+thin+"th tree\n");
  921. if(testForNexus(infiles.get(0))){
  922. jebl.evolution.io.NexusImporter ni;
  923. try {
  924. if(derb == true){
  925. db = new WwdEmbedded("readtree");
  926. db.connectToDB();
  927. System.out.println("table made = "+db.makeTable(true));
  928. WwdEmbedded dbtemp = getNewickFromNexusDerby(infiles.get(0));
  929. for(int i=0;i<dbtemp.getTableTreeSize();i++){
  930. if(i%thin == 0){
  931. db.addTree(dbtemp.getTree(i)+";");
  932. }
  933. }
  934. }else{
  935. ni = new jebl.evolution.io.NexusImporter(new FileReader(infiles.get(0)));
  936. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  937. for(int i=0;i<alltrees.size();i++){
  938. if(i%thin == 0){
  939. thtrees.add(alltrees.get(i));
  940. }
  941. }
  942. alltrees = null;
  943. }
  944. } catch (FileNotFoundException e) {
  945. // TODO Auto-generated catch block
  946. e.printStackTrace();
  947. } catch (IOException e) {
  948. // TODO Auto-generated catch block
  949. e.printStackTrace();
  950. } catch (ImportException e) {
  951. // TODO Auto-generated catch block
  952. e.printStackTrace();
  953. }
  954. try {
  955. if(derb == true){
  956. Utils.newickToNexusDerby(outfile, db);
  957. }else{
  958. BufferedWriter fw = new BufferedWriter (new FileWriter(outfile));
  959. jebl.evolution.io.NexusExporter ne = new jebl.evolution.io.NexusExporter(fw);
  960. ne.exportTrees(thtrees);
  961. fw.close();
  962. }
  963. } catch (IOException e) {
  964. // TODO Auto-generated catch block
  965. e.printStackTrace();
  966. }
  967. }else{
  968. jebl.evolution.io.NewickImporter ni;
  969. try {
  970. if(derb == true){
  971. try{
  972. BufferedReader br = new BufferedReader(new FileReader(infiles.get(0)));
  973. String str = "";
  974. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  975. db = new WwdEmbedded("readtree");
  976. db.connectToDB();
  977. System.out.println("table made = "+db.makeTable(true));
  978. int i=0;
  979. while((str = br.readLine())!=null){
  980. tr.setTree(str);
  981. if(str.length()>1 && i%thin == 0)
  982. db.addTree(tr.readTree().getRoot().getNewick(true)+";");
  983. i++;
  984. }
  985. br.close();
  986. }catch(IOException ioe){}
  987. }else{
  988. ni = new jebl.evolution.io.NewickImporter(new FileReader(infiles.get(0)), true);
  989. alltrees = (ArrayList<jebl.evolution.trees.Tree>)ni.importTrees();
  990. for(int i=0;i<alltrees.size();i++){
  991. if(i%thin == 0){
  992. thtrees.add(alltrees.get(i));
  993. }
  994. }
  995. alltrees = null;
  996. }
  997. } catch (FileNotFoundException e) {
  998. // TODO Auto-generated catch block
  999. e.printStackTrace();
  1000. } catch (IOException e) {
  1001. // TODO Auto-generated catch block
  1002. e.printStackTrace();
  1003. } catch (ImportException e) {
  1004. // TODO Auto-generated catch block
  1005. e.printStackTrace();
  1006. }
  1007. try {
  1008. if(derb == true){
  1009. FileWriter fw = new FileWriter(outfile);
  1010. for(int i=0;i<db.getTableTreeSize();i++){
  1011. jade.tree.Tree x = db.getJadeTree(i);
  1012. fw.write(x.getRoot().getNewick(true)+";\n");
  1013. }
  1014. fw.close();
  1015. this.deleteDatabaseFolder(new File("null"));
  1016. }else{
  1017. BufferedWriter fw = new BufferedWriter (new FileWriter(outfile));
  1018. jebl.evolution.io.NewickExporter ne = new jebl.evolution.io.NewickExporter(fw);
  1019. ne.exportTrees(thtrees);
  1020. fw.close();
  1021. }
  1022. } catch (IOException e) {
  1023. // TODO Auto-generated catch block
  1024. e.printStackTrace();
  1025. }
  1026. }
  1027. }
  1028. private void prune(){
  1029. if(mrcanames == null){
  1030. System.out.println("you have to enter tip names when performing a pruning analysis (-names)");
  1031. System.exit(0);
  1032. }
  1033. ArrayList<jade.tree.Tree> intrees = new ArrayList<jade.tree.Tree>();
  1034. ArrayList<jade.tree.Tree> alltrees = new ArrayList<jade.tree.Tree>();
  1035. boolean testnex = false;
  1036. for(int i=0;i<infiles.size();i++){
  1037. testnex = testForNexus(infiles.get(i));
  1038. if(testnex){
  1039. alltrees = getNewickFromNexus(infiles.get(i));
  1040. for(int j=0;j<alltrees.size();j++){
  1041. intrees.add(alltrees.get(j));
  1042. }
  1043. }else{
  1044. try{
  1045. BufferedReader br = new BufferedReader(new FileReader(infiles.get(i)));
  1046. String str = "";
  1047. jade.tree.TreeReader tr = new jade.tree.TreeReader();
  1048. while((str = br.readLine())!=null){
  1049. tr.setTree(str);
  1050. intrees.add(tr.readTree());
  1051. }
  1052. br.close();
  1053. }catch(IOException ioe){};
  1054. }
  1055. }
  1056. if(outfile == null){
  1057. phyutility.pruner.Pruner pr = new phyutility.pruner.Pruner(intrees, mrcanames);
  1058. pr.go();
  1059. for(int i=0;i<intrees.size();i++){
  1060. System.out.println(intrees.get(i).getRoot().getNewick(true)+";");
  1061. }
  1062. log("finished pruning\n");
  1063. }else{
  1064. try{
  1065. //nexus out
  1066. if((testnex == true && out_oth != true)||
  1067. (testnex == false && out_oth == true)){
  1068. phyutility.pruner.Pruner pr = new phyutility.pruner.Pruner(intrees, mrcanames);
  1069. intrees = pr.go();
  1070. ArrayList<String > nwa = new ArrayList<String>();
  1071. for(int i=0;i<intrees.size();i++){
  1072. nwa.add(intrees.get(i).getRoot().getNewick(true)+";");
  1073. }
  1074. Utils.newickToNexus(outfile, nwa);
  1075. }else{
  1076. FileWriter outw = new FileWriter(outfile);
  1077. phyutility.pruner.Pruner pr = new phyutility.pruner.Pruner(intrees, mrcanames);
  1078. intrees = pr.go();
  1079. for(int i=0;i<intrees.size();i++){
  1080. outw.write(intrees.get(i).getRoot().getNewick(true)+";\n");
  1081. }
  1082. outw.close();
  1083. }
  1084. }catch(IOException ioe){};
  1085. }
  1086. }
  1087. private ArrayList<jade.tree.Tree> getNewickFromNexus(String filename){
  1088. log("reading in nexus trees\n");
  1089. return phyutility.jebl2jade.NexusToJade.getJadeFromJeblNexus(filename);
  1090. }
  1091. private WwdEmbedded getNewickFromNexusDerby(String filename){
  1092. log("reading in nexus trees to derby\n");
  1093. return phyutility.jebl2jade.NexusToJade.getJadeFromJeblNexusDerby(filename);
  1094. }
  1095. private jade.tree.Tree getJadeTreeFromJeblTree(jebl.evolution.trees.Tree intr){
  1096. jade.tree.Tree rettr = null;
  1097. String st = jebl.evolution.trees.Utils.toNewick((jebl.evolution.trees.RootedTree)intr);
  1098. TreeReader tr = new TreeReader();
  1099. BufferedReader br;
  1100. br = new BufferedReader (new StringReader(st));
  1101. String str = "";
  1102. try {
  1103. while((str = br.readLine())!=null){
  1104. tr.setTree(str+";");
  1105. rettr = tr.readTree();
  1106. }
  1107. } catch (IOException e) {
  1108. // TODO Auto-generated catch block
  1109. e.printStackTrace();
  1110. }
  1111. return rettr;
  1112. }
  1113. /*
  1114. * sequence tools
  1115. */
  1116. /*
  1117. * takes fasta or nexus input (aligned)
  1118. * will output fasta or nexus (aligned)
  1119. * assumes different files are different genes
  1120. */
  1121. private void concat(){
  1122. Concat cc = new Concat(infiles);
  1123. if(outfile != null){
  1124. if(out_oth == true){
  1125. cc.printtofileFASTA(outfile);
  1126. }else{//nexus
  1127. cc.printtofileNEXUS(outfile);
  1128. }
  1129. }else{
  1130. cc.printtoscreenNEXUS();
  1131. }
  1132. }
  1133. /*
  1134. * takes fasta as input (unaligned)
  1135. * will output fasta (unaligned)
  1136. * System.out.println("\tthis assumes that the file is a genbank fasta file\n" +
  1137. "\tthe numbers correspond to these options for the names\n" +
  1138. "\t1) gi number\n" +
  1139. "\t2) gb number\n" +
  1140. "\t3) taxon name\n" +
  1141. "\t4) taxon_name\n" +
  1142. "\t5) T_name\n" +
  1143. "\t6) taxon_name_ginumber\n" +
  1144. "\t7) taxon_name_gbnumber\n" +
  1145. "\t8) formatting for bioorganizer so type java -jar gbparser.jar 8 file regionname\n"+
  1146. "\n" +
  1147. "NOTE 1: if there are duplicate names, this will add numbers to the end\n" +
  1148. "NOTE 2: some genbank entries are poorly formed and therefore do not get formatted correctly here, so double check!");
  1149. */
  1150. private void parse(){
  1151. if(parsenum != 666){
  1152. if(outfile != null){
  1153. FileWriter fw;
  1154. try {
  1155. fw = new FileWriter(outfile);
  1156. for(int i=0;i<infiles.size();i++){
  1157. jade.data.GBParser ca = new jade.data.GBParser(infiles.get(i), parsenum);
  1158. ArrayList<jade.data.Sequence> seqs = ca.getSeqs();
  1159. for(int j=0;j<seqs.size();j++){
  1160. fw.write(">"+seqs.get(j).getID()+"\n");
  1161. fw.write(seqs.get(j).getSeq()+"\n\n");
  1162. }
  1163. }
  1164. fw.close();
  1165. } catch (IOException e) {
  1166. // TODO Auto-generated catch block
  1167. e.printStackTrace();
  1168. }
  1169. }else{
  1170. for(int i=0;i<infiles.size();i++){
  1171. jade.data.GBParser ca;
  1172. try {
  1173. ca = new jade.data.GBParser(infiles.get(i), parsenum);
  1174. ArrayList<jade.data.Sequence> seqs = ca.getSeqs();
  1175. for(int j=0;j<seqs.size();j++){
  1176. System.out.println(">"+seqs.get(j).getID());
  1177. System.out.println(seqs.get(j).getSeq());
  1178. System.out.println();
  1179. }
  1180. } catch (IOException e) {
  1181. // TODO Auto-generated catch block
  1182. e.printStackTrace();
  1183. }
  1184. }
  1185. }
  1186. }else{
  1187. }
  1188. }
  1189. /*
  1190. * takes fasta or nexus input (aligned)
  1191. * one file at a time
  1192. * will output fasta or nexus (aligned)
  1193. */
  1194. private void clean(){
  1195. if(infiles.size() > 1){
  1196. System.err.println("Trimming is done one file at a time, only the first file will be used.");
  1197. }
  1198. phyutility.trimsites.TrimSites ts = new phyutility.trimsites.TrimSites(infiles.get(0));
  1199. ts.trimAln(cleannum);
  1200. if(testForNexus(infiles.get(0))){
  1201. if(out_oth == true){
  1202. ts.printFastaOutfile(outfile);
  1203. }else{
  1204. ts.printNexusOutfile(outfile);
  1205. }
  1206. }else{
  1207. if(out_oth == false){
  1208. ts.printFastaOutfile(outfile);
  1209. }else{
  1210. ts.printNexusOutfile(outfile);
  1211. }
  1212. }
  1213. }
  1214. /*
  1215. * incomplete
  1216. */
  1217. private void ncbisearch(){
  1218. if(sterm == "" || sterm.length() < 2){
  1219. System.out.println("in order to search ncbi you much enter a search term (-term)");
  1220. }
  1221. phyutility.ncbi.Einfo einfo = new phyutility.ncbi.Einfo();
  1222. if(sdb == 1){
  1223. ArrayList<String> ids = einfo.search("nucleotide", sterm);
  1224. log("Count = "+ids.size()+"\n");
  1225. log("genbankids\n-----------\n");
  1226. for(int i=0;i<ids.size();i++){
  1227. log(ids.get(i)+"\n");
  1228. }
  1229. System.out.println("Count = "+ids.size());
  1230. }else if(sdb == 2){
  1231. ArrayList<String> ids = einfo.search("protein", sterm);
  1232. log("Count = "+ids.size()+"\n");
  1233. log("genbankids\n-----------\n");
  1234. for(int i=0;i<ids.size();i++){
  1235. log(ids.get(i)+"\n");
  1236. }
  1237. System.out.println("Count = "+ids.size());
  1238. }else if(sdb == 3){
  1239. ArrayList<String> ids = einfo.search("genome", sterm);
  1240. log("Count = "+ids.size()+"\n");
  1241. log("genbankids\n-----------\n");
  1242. for(int i=0;i<ids.size();i++){
  1243. log(ids.get(i)+"\n");
  1244. }
  1245. System.out.println("Count = "+ids.size());
  1246. }else if(sdb == 4){
  1247. ArrayList<String> ids = einfo.search("taxonomy", sterm);
  1248. log("Count = "+ids.size()+"\n");
  1249. log("genbankids\n-----------\n");
  1250. for(int i=0;i<ids.size();i++){
  1251. log(ids.get(i)+"\n");
  1252. }
  1253. System.out.println("Count = "+ids.size());
  1254. }else if(sdb == 5){
  1255. ArrayList<String> ids = einfo.search("nuccore", sterm);
  1256. log("Count = "+ids.size()+"\n");
  1257. log("genbankids\n-----------\n");
  1258. for(int i=0;i<ids.size();i++){
  1259. log(ids.get(i)+"\n");
  1260. }
  1261. System.out.println("Count = "+ids.size());
  1262. }
  1263. }
  1264. private void ncbiget(){
  1265. if(sterm == "" || sterm.length() < 2){
  1266. System.out.println("in order to fetch from ncbi you much enter a search term (-term)");
  1267. }
  1268. phyutility.ncbi.Einfo einfo = new phyutility.ncbi.Einfo();
  1269. if(sdb == 1){
  1270. einfo.efetch("nucleotide", sterm, this.lengthlim, outfile, outfor, sep);
  1271. }else if(sdb == 2){
  1272. einfo.efetch("protein", sterm, this.lengthlim, outfile, outfor, sep);
  1273. }else if(sdb == 3){
  1274. einfo.efetch("genome", sterm, this.lengthlim, outfile, outfor, sep);
  1275. }else if(sdb == 4){
  1276. System.out.println("sorry, no taxonomy downloads");
  1277. }
  1278. }
  1279. /*
  1280. * incomplete
  1281. * going to require blastcl3 or blast2, can i bundle this?
  1282. */
  1283. private void blast(){
  1284. }
  1285. /*
  1286. * utils
  1287. */
  1288. private boolean testForNexus(String filename){
  1289. boolean ret = false;
  1290. String str = "";
  1291. try{
  1292. BufferedReader br = new BufferedReader(new FileReader(filename));
  1293. str = br.readLine();
  1294. if(str.toUpperCase().trim().compareTo("#NEXUS")==0)
  1295. ret = true;
  1296. br.close();
  1297. }catch(IOException ioe){}
  1298. return ret;
  1299. }
  1300. private boolean deleteDatabaseFolder(File dir){
  1301. if (dir.isDirectory()) {
  1302. String[] children = dir.list();
  1303. for (int i=0; i<children.length; i++) {
  1304. boolean success = deleteDatabaseFolder(new File(dir, children[i]));
  1305. if (!success) {
  1306. return false;
  1307. }
  1308. }
  1309. }
  1310. // The directory is now empty so delete it
  1311. return dir.delete();
  1312. }
  1313. private void startLogging(){
  1314. try {
  1315. if(logfile == null){
  1316. /*java.util.Date dt = new java.util.Date();
  1317. java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yy.hh.mm");
  1318. String datenewformat = formatter.format(dt);
  1319. logfile = datenewformat+".txt";*/
  1320. logfile = "phyutility.log";
  1321. }
  1322. fw = new FileWriter(logfile,true);
  1323. java.util.Date dt = new java.util.Date();
  1324. java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("dd/MM/yy hh:mm");
  1325. String datenewformat = formatter.format(dt);
  1326. log("starting log: "+datenewformat+"\n");
  1327. } catch (IOException e) {
  1328. // TODO Auto-generated catch block
  1329. e.printStackTrace();
  1330. }
  1331. }
  1332. private void log(String instring){
  1333. try {
  1334. fw.write(instring);
  1335. fw.flush();
  1336. } catch (IOException e) {
  1337. // TODO Auto-generated catch block
  1338. e.printStackTrace();
  1339. }
  1340. }
  1341. private void stopLogging(){
  1342. try {
  1343. fw.flush();
  1344. fw.close();
  1345. } catch (IOException e) {
  1346. // TODO Auto-generated catch block
  1347. e.printStackTrace();
  1348. }
  1349. }
  1350. private void printUsage(){
  1351. System.out.println("Phyutility (fyoo-til-i-te) v.2.2.4");
  1352. System.out.println("Stephen A. Smith http://www.blackrim.org eebsmith@umich.edu");
  1353. System.out.println("help on a specific command use option -h <command>");
  1354. System.out.println("commands:");
  1355. System.out.println("trees:");
  1356. System.out.println(" consensus");
  1357. System.out.println(" convert");
  1358. System.out.println(" leafstab");
  1359. System.out.println(" linmove");
  1360. System.out.println(" prune");
  1361. System.out.println(" reroot");
  1362. System.out.println(" thin");
  1363. System.out.println(" treesupp");
  1364. System.out.println("seqs:");
  1365. System.out.println(" clean");
  1366. System.out.println(" concat");
  1367. System.out.println(" ncbiget");
  1368. System.out.println(" ncbisearch");
  1369. System.out.println(" parse");
  1370. System.out.println("see documentation for more information");
  1371. }
  1372. /*
  1373. * prints a helpful comment with instructions and examples for
  1374. * all the commands (usage)
  1375. */
  1376. private void printHelp(String cmd){
  1377. if(cmd.compareTo("consensus") == 0){
  1378. System.out.println("makes consensus trees from single or multiple files");
  1379. System.out.println("options:");
  1380. System.out.println(" -con | designates that you want to make a consensus");
  1381. System.out.println(" -t <number> | threshold for consensus (1.0 = strict, 0.5 = majrule, 0 = allcompat)");
  1382. System.out.println(" -in <file name> ... | input file names");
  1383. System.out.println(" -out <file name> | output file name");
  1384. System.out.println("java -jar phyutility.jar -con -t 0.5 -in testall.tre -out test.con");
  1385. }else if(cmd.compareTo("convert") == 0){
  1386. System.out.println("converts between tree file types");
  1387. System.out.println("options:");
  1388. System.out.println(" -vert | designates that you want to convert");
  1389. System.out.println(" -in <file name> | input file name");
  1390. System.out.println(" -out <file name> | output file name");
  1391. System.out.println("java -jar phyutility.jar -vert -in test.tre -out testvert.nex");
  1392. }else if(cmd.compareTo("leafstab") == 0){
  1393. System.out.println("calculates leaf stability for all tips");
  1394. System.out.println("options:");
  1395. System.out.println(" -ls | designates that you want to do leaf stability index calculations");
  1396. System.out.println(" -in <file name> ... | input file names");
  1397. System.out.println("java -jar phyutility.jar -ls -in testall.tre");
  1398. }else if(cmd.compareTo("linmove") == 0){
  1399. System.out.println("conducts lineage movement procedure on tree");
  1400. System.out.println("options:");
  1401. System.out.println(" -lm | designates that you want to run lineage movement analysis");
  1402. System.out.println(" -names <tip name> ... | names to check -- multiple form clade");
  1403. System.out.println(" -tree <file name> | consensus file to map movement to");
  1404. System.out.println(" -in <file name> ... | input tree file names");
  1405. System.out.println(" -out <file name> | output file name");
  1406. System.out.println("java -jar phyutility.jar -lm -in testall.tre -tree test.con -out testlm.tre -names three");
  1407. }else if(cmd.compareTo("prune") == 0){
  1408. System.out.println("prunes tips or clades -- has problems if node to prune is root");
  1409. System.out.println("options:");
  1410. System.out.println(" -pr | designates that you want to prune");
  1411. System.out.println(" -names <tip name> ... | names to check -- multiple form clade");
  1412. System.out.println(" -in <file name> ... | input file names");
  1413. System.out.println(" -out <file name> | output file name");
  1414. System.out.println("java -jar phyutility.jar -pr -in test.tre -out testpr.tre -names one");
  1415. }else if(cmd.compareTo("reroot") == 0){
  1416. System.out.println("reroots one or multiple trees");
  1417. System.out.println("options:");
  1418. System.out.println(" -rr | designates that you want to reroot");
  1419. System.out.println(" -names <tip name> ... | names to check -- multiple form clade");
  1420. System.out.println(" -in <file name> ... | input file names");
  1421. System.out.println(" -out <file name> | output file name");
  1422. System.out.println("java -jar phyutility.jar -rr -in test.tre -out testrr.tre -names one two");
  1423. }else if(cmd.compareTo("thin") == 0){
  1424. System.out.println("thins tree files");
  1425. System.out.println("options:");
  1426. System.out.println(" -tt # | designates that you want to thin followed by the number of thinning (sample every #)");
  1427. System.out.println(" -in <file name> ... | input file names");
  1428. System.out.println(" -out <file name> | output file name");
  1429. System.out.println("java -jar phyutility.jar -tt 100 -in testall.tre -out testts.tre");
  1430. }else if(cmd.compareTo("treesupp") == 0){
  1431. System.out.println("allows to calculate support for a tree given a set of trees");
  1432. System.out.println("options:");
  1433. System.out.println(" -ts | designates that you want to calculate support for a tree given a set of trees");
  1434. System.out.println(" -tree <file name> | tree to get support for");
  1435. System.out.println(" -in <file name> ... | input file names");
  1436. System.out.println(" -out <file name> | output file name");
  1437. System.out.println("java -jar phyutility.jar -ts -in testall.tre -tree test.con -out testts.tre");
  1438. }else if(cmd.compareTo("clean") == 0){
  1439. System.out.println("trims sequences based on a threshhold");
  1440. System.out.println("options:");
  1441. System.out.println(" -clean # | designates that you want to trim and the threshold must follow");
  1442. System.out.println(" -in <file name> ... | input file names");
  1443. System.out.println(" -out <file name> | output file name");
  1444. System.out.println("java -jar phyutility.jar -clean 0.5 -in test.nex -out test50.nex");
  1445. }else if(cmd.compareTo("concat") == 0){
  1446. System.out.println("concatenate alignments together");
  1447. System.out.println("options:");
  1448. System.out.println(" -concat | designates that you want to concatenate");
  1449. System.out.println(" -in <file name> ... | input file names");
  1450. System.out.println(" -out <file name> | output file name");
  1451. System.out.println("java -jar phyutility.jar -concat -in test.aln test2.aln -out testall.aln");
  1452. }else if(cmd.compareTo("ncbiget") == 0){
  1453. System.out.println("fetch sequences from ncbi");
  1454. System.out.println("options:");
  1455. System.out.println(" -ef | designates that you want to perform a fetch");
  1456. System.out.println(" -term <term> | the search term(s)");
  1457. System.out.println(" -out <file name> | output file name");
  1458. System.out.println(" -ll <length> | the length limit to return, meant to eliminate genomic sequences or sequences that are too long in the retrieval (should almost always use, especially use if you get a memory error) (optional,default = 10000)");
  1459. System.out.println(" -outfor <format> | a way to customize the fasta line (1 = ginumber, 2 = taxid, 3 = orgname (with spaces replaced with whatever is sep), 4 = defline (with spaces replaced with sep), 5 = seqlength)(optional, default = 31)");
  1460. System.out.println(" -sep <seperator> | seperator for between outfor and between orgname and deine (optional, default= _)");
  1461. System.out.println(" -db # | corresponds to the database you want to search (nucleotide (1), protein (2)) (optional, nucleotide is default)");
  1462. System.out.println(" java -jar phyutility.jar -ef -term lonicera+OR+viburnum+AND+rbcl -ll 3000 -log log.txt -out out.fasta -sep -outfor 13");
  1463. }else if(cmd.compareTo("ncbisearch") == 0){
  1464. System.out.println("found out how many sequences match a query -- probably before ncbifetch -- log file records the GI numbers");
  1465. System.out.println("options:");
  1466. System.out.println(" -es | designates that you want to perform a search");
  1467. System.out.println(" -term <term> | the search term(s)");
  1468. System.out.println(" -db # | corresponds to the database you want to search (right now nucleotide (1), protein (2), genome (3), taxonomy (4)) (optional, default = nucleotide)");
  1469. System.out.println(" -log <file name> | log file name");
  1470. System.out.println("java -jar phyutility.jar -es -term lonicera+OR+viburnum+AND+rbcl -log log.txt");
  1471. }else if(cmd.compareTo("parse") == 0){
  1472. System.out.println("parses fasta files downloaded from genbank");
  1473. System.out.println("options:");
  1474. System.out.println(" -parse # | designates that you want to parse and the option number must follow -- see documentation");
  1475. System.out.println(" -in <file name> ... | input fasta files");
  1476. System.out.println(" -out <file name> | output file name");
  1477. System.out.println("java -jar phyutility.jar -parse 1 -in test.gb -out testgb1.fasta");
  1478. }else{
  1479. System.out.println("don't recognize command: "+cmd);
  1480. printUsage();
  1481. System.exit(0);
  1482. }
  1483. }
  1484. public static void main(String [] args){
  1485. Main m = new Main(args);
  1486. }
  1487. }