#!/usr/bin/python3
# A program for finding fasta files that contain single-copy orthologs
# USAGE: python3 Parsing_orthogroups_TA.py --readFile SpeciesIDs.txt --input Alignments/ --output parsed_orthogroups/ --num integer
# Author: <NAME>
# Email: <EMAIL>
# Affiliation: University of New Hampshire, Durham, NH, USA
# Date: 11/22/2015

import os
import shutil
import argparse

parser = argparse.ArgumentParser(description="This script parses single copy orthoglogs among with alignment files produced by Orthofinder.")
parser.add_argument('--readFile', help="PATH to 'SpeciesIDs.txt' generated by Orthofinder", required=True)
parser.add_argument('--input', help="PATH to Alignment files generated by Orthofinder", required=True)
parser.add_argument('--output', help="PATH to the output directory", required=True)
parser.add_argument('--num', help="Min number of species represented in each orthofile", required=True)
args = parser.parse_args()

good_files = []

species_file = open(args.readFile, "r")
lookup_species = []
for line in species_file:
  line = line.rstrip()
  ssp = line.split(":")[1][1:]
  name = ssp.split(".pep")[0]
  lookup_species.append(name)
lookup_species = lookup_species
# print(lookup_species)

for currentFile in os.listdir(args.input):
  if currentFile.endswith(".fa"):
      keep_file = True # assume file is good at the start
      species_list = []
      working_file = open(args.input + "/" + currentFile, "r")
      for currentLine in working_file:
          currentLine = currentLine.rstrip()
          if currentLine.startswith(">"):
              for species in lookup_species:
                  if species in currentLine:
                      if species in species_list:
                          keep_file = False # the file bad
                      else:
                          species_list.append(species)
      if len(species_list) < int(args.num):
          keep_file = False # the file bad
      if keep_file:
          good_files.append(currentFile)

Good_file_dir = args.output + "/"
Orig_file_dir = args.input + "/"

try:
    os.makedirs(Good_file_dir)
except FileExistsError as e:
    pass

for file in good_files:
    shutil.copyfile(Orig_file_dir+file, Good_file_dir+file)


#!/usr/bin/python3
# A program for finding fasta files that contain single-copy orthologs
# USAGE: python3 Parsing_orthogroups_TA.py --readFile SpeciesIDs.txt --input Alignments/ --output parsed_orthogroups/ --num integer
# Author: <NAME>
# Email: <EMAIL>
# Affiliation: University of New Hampshire, Durham, NH, USA
# Date: 11/22/2015

import os
import shutil
import argparse

parser = argparse.ArgumentParser(description="This script parses single copy orthoglogs among with alignment files produced by Orthofinder.")
parser.add_argument('--readFile', help="PATH to 'SpeciesIDs.txt' generated by Orthofinder", required=True)
parser.add_argument('--input', help="PATH to Alignment files generated by Orthofinder", required=True)
parser.add_argument('--output', help="PATH to the output directory", required=True)
parser.add_argument('--num', help="Min number of species represented in each orthofile", required=True)
args = parser.parse_args()

good_files = []

species_file = open(args.readFile, "r")
lookup_species = []
for line in species_file:
  line = line.rstrip()
  ssp = line.split(":")[1][1:]
  name = ssp.split(".pep")[0]
  lookup_species.append(name)
lookup_species = lookup_species
# print(lookup_species)

for currentFile in os.listdir(args.input):
  if currentFile.endswith(".fa"):
      keep_file = True # assume file is good at the start
      species_list = []
      working_file = open(args.input + "/" + currentFile, "r")
      for currentLine in working_file:
          currentLine = currentLine.rstrip()
          if currentLine.startswith(">"):
              for species in lookup_species:
                  if species in currentLine:
                      if species in species_list:
                          keep_file = False # the file bad
                      else:
                          species_list.append(species)
      if len(species_list) < int(args.num):
          keep_file = False # the file bad
      if keep_file:
          good_files.append(currentFile)

Good_file_dir = args.output + "/"
Orig_file_dir = args.input + "/"

try:
    os.makedirs(Good_file_dir)
except FileExistsError as e:
    pass

for file in good_files:
    shutil.copyfile(Orig_file_dir+file, Good_file_dir+file)


#!/usr/bin/python3 # A program for finding fasta files that contain single-copy orthologs # USAGE: python3 Parsing_orthogroups_TA.py --readFile SpeciesIDs.txt --input Alignments/ --output parsed_orthogroups/ --num integer # Author: <NAME> # Email: <EMAIL> # Affiliation: University of New Hampshire, Durham, NH, USA # Date: 11/22/2015 # print(lookup_species) # assume file is good at the start # the file bad # the file bad