def main():
  (spec, odict) = parseOrthoFile( sys.argv[1] ) 
  recDir = sys.argv[2]
  ofname = sys.argv[3]
  
  orthoGroups = odict.keys()
  with open(ofname,'wb') as ofile:
    for og in orthoGroups:
      ifname = "{0}/{1}.nwk.rooting.0".format(recDir,og)
      t = cogent.LoadTree(ifname)
      ofile.write(t.Name+"\n") 

  sys.exit(1)
Example #2
0
def main():
    (spec, odict) = parseOrthoFile(sys.argv[1])
    recDir = sys.argv[2]
    ofname = sys.argv[3]

    orthoGroups = odict.keys()
    with open(ofname, 'wb') as ofile:
        for og in orthoGroups:
            ifname = "{0}/{1}.nwk.rooting.0".format(recDir, og)
            t = cogent.LoadTree(ifname)
            ofile.write(t.Name + "\n")

    sys.exit(1)
Example #3
0
def main(args):
  usingParana = (args["--method"].upper() == "PARANA")
  usingRWS = (args["--method"].upper() == "RWS")
  outputCVFile = (args["--method"].upper() == "CV")
  (spec, odict) = parseOrthoFile( args["--ortho"] ) #sys.argv[1] )
  recDir = args["--recdir"]#sys.argv[2]
  ofname = args["--outdir"]#sys.argv[3]
  beta = float(args["--beta"]) 
  
  if not outputCVFile:
    mkdir_p(ofname)

  getName = lambda x : x.Name

  def getRelevantEdges( adjGraph, t1, t2 ):
    pxml = Phyloxml()
    pxml.build_from_file(t1)
    pxml.build_from_file(t2)
    la = filter( lambda x : x.find('LOST') == -1, pxml.phylogeny[0].get_leaf_names() )
    lb = filter( lambda x : x.find('LOST') == -1, pxml.phylogeny[1].get_leaf_names() )
    #lb = filter( lambda x : x.find('LOST') == -1, map( getName, cogent.LoadTree(t2).tips() ) )
    crossValidationEdges = filter( lambda (x,y) : ((x in la) and (y in lb)) or ((y  in la) and (x in lb))  , adjGraph.edges() )
    relevantEdges = filter( lambda (x,y) : ((x in la) or (x in lb)) and ((y in la) or (y in lb)) , adjGraph.edges() )
    newGraph = nx.Graph()
    newGraph.add_nodes_from( la + lb )
    newGraph.add_edges_from( relevantEdges )
    return newGraph, crossValidationEdges


  if outputCVFile:
    root = ET.Element("cvtest", name="herpes_loocv")
    
  ctr = 0
  orthoGroups = odict.keys()
  progress = ProgressBar(len(orthoGroups)*len(orthoGroups)).start()
  for i, (og1, og2) in enumerate(iterWithSelf(orthoGroups)):
      progress.update(i)
      #print("computing ancestral state between groups {0} and {1}".format(og1, og2))
      #print("i = {0}".format(i))
      alist = nx.read_adjlist(extantName)
      

      t1name = "{0}/{1}.xml.rooting.0.ntg.reconciled.0.ntg.rearrange.0.ntg".format(recDir,og1)
      t2name = "{0}/{1}.xml.rooting.0.ntg.reconciled.0.ntg.rearrange.0.ntg".format(recDir,og2)

      relevantGraph, crossValidationEdges = getRelevantEdges(alist, t1name, t2name)
      if len(crossValidationEdges) > 1:
          for e in crossValidationEdges:
            cGraph = relevantGraph.copy()
            cGraph.remove_edge(e[0], e[1])
            ofileName = "{0}/{1}@{2}@removed@{3}#{4}@txt".format(ofname, og1, og2, e[0], e[1])
            assert(cGraph.size() == relevantGraph.size()-1)
              
            callargs = None
            if usingParana:
              with tempfile.NamedTemporaryFile(mode='wb', suffix='.adj', prefix='tmp', dir="/tmp", delete=True) as gfile:
                nx.write_adjlist(cGraph, gfile.name)
                callargs = constructParanaCall(gfile, ofileName, beta, og1, og2, t1name, t2name)
                os.system(" ".join(callargs)+"> /dev/null 2>&1")
            elif usingRWS:
              copy = alist.copy()
              copy.remove_edge(e[0],e[1])
              #print(cGraph.size())
              runRWS(copy, ofileName)
            elif outputCVFile:
              tset = ET.SubElement(root, "testset", name="fold_{0}".format(ctr))
              u,v = sorted((e[0],e[1]))
              ET.SubElement(tset, "edge", u=u, v=v)      
              ctr += 1
            else:
              raise "Method must be one of {Parana|RWS}"

        #p = subprocess.Popen( callargs , shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE )
        #out, err = p.communicate()
      if outputCVFile:
        with open(ofname,'wb') as ofile:
          ofile.write(ET.tostring(root, pretty_print=True))
          
  progress.finish()
  sys.exit(1)