Farmers have a crucial role in cleaning up one of the UK’s most polluted river systems, a study shows.